diff --git a/shared/data/src/main/java/com/ivy/data/db/dao/fake/FakeCategoryDao.kt b/shared/data/src/main/java/com/ivy/data/db/dao/fake/FakeCategoryDao.kt index d95e86ded1..0d24cb0ba3 100644 --- a/shared/data/src/main/java/com/ivy/data/db/dao/fake/FakeCategoryDao.kt +++ b/shared/data/src/main/java/com/ivy/data/db/dao/fake/FakeCategoryDao.kt @@ -11,7 +11,7 @@ class FakeCategoryDao : CategoryDao, WriteCategoryDao { private val items = mutableListOf() override suspend fun findAll(deleted: Boolean): List { - return items + return items.filter { it.isDeleted == deleted } } override suspend fun findById(id: UUID): CategoryEntity? { @@ -23,7 +23,12 @@ class FakeCategoryDao : CategoryDao, WriteCategoryDao { } override suspend fun save(value: CategoryEntity) { - items.add(value) + val existingItemIndex = items.indexOfFirst { it.id == value.id } + if (existingItemIndex > -1) { + items[existingItemIndex] = value + } else { + items.add(value) + } } override suspend fun saveMany(values: List) { @@ -31,7 +36,13 @@ class FakeCategoryDao : CategoryDao, WriteCategoryDao { } override suspend fun flagDeleted(id: UUID) { - TODO("Not yet implemented") + items.replaceAll { categoryEntity -> + if (categoryEntity.id == id) { + categoryEntity.copy(isDeleted = true) + } else { + categoryEntity + } + } } override suspend fun deleteById(id: UUID) { diff --git a/shared/data/src/test/java/com/ivy/data/dao/FakeCategoryDaoTest.kt b/shared/data/src/test/java/com/ivy/data/dao/FakeCategoryDaoTest.kt new file mode 100644 index 0000000000..2877cb3941 --- /dev/null +++ b/shared/data/src/test/java/com/ivy/data/dao/FakeCategoryDaoTest.kt @@ -0,0 +1,423 @@ +package com.ivy.data.dao + +import com.ivy.data.db.dao.fake.FakeCategoryDao +import com.ivy.data.db.entity.CategoryEntity +import io.kotest.core.spec.style.FreeSpec +import io.kotest.matchers.shouldBe +import java.util.UUID + +class FakeCategoryDaoTest : FreeSpec({ + fun newCategoryDao() = FakeCategoryDao() + + "find all" - { + "not deleted" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = true, + id = id2 + ) + ) + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findAll(false) + + // then + res shouldBe listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ) + ) + } + + "deleted" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = true, + id = id2 + ) + ) + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findAll(true) + + // then + res shouldBe listOf( + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = true, + id = id2 + ) + ) + } + + "empty list" { + // given + val categoryDao = newCategoryDao() + val categories = emptyList() + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findAll(false) + + // then + res shouldBe emptyList() + } + } + + "find by id" - { + "existing id" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val category2 = CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = true, + id = id2 + ) + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + category2 + ) + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findById(id2) + + // then + res shouldBe category2 + } + + "not existing id" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = true, + id = id2 + ) + ) + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findById(UUID.randomUUID()) + + // then + res shouldBe null + } + } + + "find max order num" - { + "of categories" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = true, + id = id2 + ) + ) + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findMaxOrderNum() + + // then + res shouldBe 2.0 + } + + "of empty list" - { + // given + val categoryDao = newCategoryDao() + val categories = emptyList() + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findMaxOrderNum() + + // then + res shouldBe null + } + } + + "save" - { + "create new" { + // given + val categoryDao = newCategoryDao() + val id = UUID.randomUUID() + val category = CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id + ) + + // when + categoryDao.save(category) + val res = categoryDao.findById(id) + + // then + res shouldBe category + } + + "update existing" { + // given + val categoryDao = newCategoryDao() + val id = UUID.randomUUID() + val category = CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id + ) + val updated = category.copy(name = "My Home") + + // when + categoryDao.save(category) + categoryDao.save(updated) + val res = categoryDao.findAll(deleted = false) + + // then + res shouldBe listOf(updated) + } + } + + "save many" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = false, + id = id2 + ) + ) + + // when + categoryDao.saveMany(categories) + val res = categoryDao.findAll(false) + + // then + res shouldBe categories + } + + "flag deleted" - { + "existing id" { + // given + val categoryDao = newCategoryDao() + val id = UUID.randomUUID() + val category = CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id + ) + + // when + categoryDao.save(category) + categoryDao.flagDeleted(id) + val res = categoryDao.findById(id) + + // then + res shouldBe category.copy(isDeleted = true) + } + + "not existing id" { + // given + val categoryDao = newCategoryDao() + val id = UUID.randomUUID() + val category = CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id + ) + + // when + categoryDao.save(category) + categoryDao.flagDeleted(UUID.randomUUID()) + val res = categoryDao.findById(id) + + // then + res shouldBe category + } + } + + "delete by id" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = false, + id = id2 + ) + ) + + // when + categoryDao.saveMany(categories) + categoryDao.deleteById(id2) + val res = categoryDao.findAll(false) + + // then + res shouldBe listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ) + ) + } + + "delete all" { + // given + val categoryDao = newCategoryDao() + val id1 = UUID.randomUUID() + val id2 = UUID.randomUUID() + val categories = listOf( + CategoryEntity( + name = "Home", + color = 42, + icon = null, + orderNum = 1.0, + isDeleted = false, + id = id1 + ), + CategoryEntity( + name = "Fun", + color = 42, + icon = null, + orderNum = 2.0, + isDeleted = true, + id = id2 + ) + ) + + // when + categoryDao.saveMany(categories) + categoryDao.deleteAll() + val notDeleted = categoryDao.findAll(false) + val deleted = categoryDao.findAll(true) + + // then + notDeleted shouldBe emptyList() + deleted shouldBe emptyList() + } +})