Skip to content

Commit

Permalink
Specify behaviour when updating entities with same ID in different lists
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed May 20, 2024
1 parent 3f19a8b commit be3fb7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class JsonFileEntitiesRepository(directory: File) : EntitiesRepository {
val storedEntities = readEntities()

entities.forEach { entity ->
val existing = storedEntities.find { it.id == entity.id && it.list == entity.list }
val existing = storedEntities.find { it.id == entity.id }

if (existing != null) {
val offline = if (existing.offline) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ abstract class EntitiesRepositoryTest {
assertThat(wines, contains(updatedWine))
}

@Test
fun `#save updates existing entity with matching id in different list`() {
val repository = buildSubject()

val wine = Entity("wines", "1", "Léoville Barton 2008", version = 1)
repository.save(wine)

val updatedWine = Entity("whisky", wine.id, "Edradour 10", version = 2)
repository.save(updatedWine)

val wines = repository.getEntities("wines")
assertThat(wines.size, equalTo(0))
val whiskys = repository.getEntities("whisky")
assertThat(whiskys, contains(updatedWine))
}

@Test
fun `#save updates existing entity with matching id and version`() {
val repository = buildSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class InMemEntitiesRepository : EntitiesRepository {
override fun save(vararg entities: Entity) {
entities.forEach { entity ->
lists.add(entity.list)
val existing = this.entities.find { it.id == entity.id && it.list == entity.list }
val existing = this.entities.find { it.id == entity.id }

if (existing != null) {
val offline = if (existing.offline) {
Expand Down

0 comments on commit be3fb7e

Please sign in to comment.