From 4c1c473744713a7a686af73087871f00cc80fcd3 Mon Sep 17 00:00:00 2001 From: Leonardo Colman Lopes Date: Thu, 30 Jan 2025 08:48:18 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20to=20UseRepository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Leonardo Colman Lopes --- .../petals/use/repository/UseRepository.kt | 4 +++- .../petals/use/repository/UseRepositoryTest.kt | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/br/com/colman/petals/use/repository/UseRepository.kt b/app/src/main/kotlin/br/com/colman/petals/use/repository/UseRepository.kt index 8cb3fcda..e15fe66a 100644 --- a/app/src/main/kotlin/br/com/colman/petals/use/repository/UseRepository.kt +++ b/app/src/main/kotlin/br/com/colman/petals/use/repository/UseRepository.kt @@ -11,6 +11,8 @@ import kotlinx.coroutines.flow.map import timber.log.Timber import java.time.LocalDateTime.parse import java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME +import kotlinx.coroutines.flow.filterNotNull +import kotlinx.coroutines.flow.mapNotNull import br.com.colman.petals.Use as UseEntity class UseRepository( @@ -33,7 +35,7 @@ class UseRepository( fun getLastUseDate() = getLastUse().map { it?.date } fun countAll(dispatcher: CoroutineDispatcher = IO) = - useQueries.countAll().asFlow().mapToOneOrNull(dispatcher).map { it?.toInt() ?: 0 } + useQueries.countAll().asFlow().mapToOneOrNull(dispatcher).filterNotNull().map { it.toInt() } fun all(dispatchers: CoroutineDispatcher = IO): Flow> = useQueries.selectAll().asFlow().mapToList( dispatchers diff --git a/app/src/test/kotlin/br/com/colman/petals/use/repository/UseRepositoryTest.kt b/app/src/test/kotlin/br/com/colman/petals/use/repository/UseRepositoryTest.kt index 933cabdb..bc311178 100644 --- a/app/src/test/kotlin/br/com/colman/petals/use/repository/UseRepositoryTest.kt +++ b/app/src/test/kotlin/br/com/colman/petals/use/repository/UseRepositoryTest.kt @@ -30,6 +30,7 @@ import io.kotest.matchers.shouldBe import io.kotest.property.arbitrary.take import kotlinx.coroutines.flow.first import java.math.BigDecimal +import java.util.UUID import kotlin.system.measureTimeMillis class UseRepositoryTest : FunSpec({ @@ -69,6 +70,17 @@ class UseRepositoryTest : FunSpec({ target.all().first().single() shouldBe use } + test("Count All should return 0 when empty") { + target.countAll().first() shouldBe 0 + } + + test("Count All should return number of elements") { + val otherUse = use.copy(amountGrams = 1.0.toBigDecimal(), id = UUID.randomUUID().toString()) + target.upsert(use) + target.upsert(otherUse) + target.countAll().first() shouldBe 2L + } + test("Delete") { database.useQueries.upsert(use.toEntity()) target.delete(use) @@ -93,6 +105,10 @@ class UseRepositoryTest : FunSpec({ target.getLastUseDate().first() shouldBe use.date } + test("Last use date should return null when no nulls are added") { + target.getLastUseDate().first() shouldBe null + } + test("Last use performance") { UseArb.take(10_000).map(Use::toEntity).forEach(database.useQueries::upsert)