Skip to content

Commit

Permalink
✅ Add tests to UseRepository
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Jan 30, 2025
1 parent 012e147 commit 4c1c473
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<List<Use>> = useQueries.selectAll().asFlow().mapToList(
dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 4c1c473

Please sign in to comment.