This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78a6d56
commit be76546
Showing
12 changed files
with
348 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
shared/data/core/src/test/java/com/ivy/data/ArbAccountEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.ivy.data | ||
|
||
import com.ivy.data.db.entity.AccountEntity | ||
import com.ivy.data.model.testing.colorInt | ||
import com.ivy.data.model.testing.iconAsset | ||
import com.ivy.data.model.testing.maybe | ||
import com.ivy.data.model.testing.notBlankTrimmedString | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.arbitrary | ||
import io.kotest.property.arbitrary.boolean | ||
import io.kotest.property.arbitrary.double | ||
import io.kotest.property.arbitrary.of | ||
import io.kotest.property.arbitrary.removeEdgecases | ||
import io.kotest.property.arbitrary.string | ||
import io.kotest.property.arbitrary.uuid | ||
|
||
fun Arb.Companion.invalidAccountEntity(): Arb<AccountEntity> = arbitrary { | ||
val validEntity = validAccountEntity().bind() | ||
validEntity.copy( | ||
name = Arb.of("", " ", " ").bind() | ||
) | ||
} | ||
|
||
fun Arb.Companion.validAccountEntity(): Arb<AccountEntity> = arbitrary { | ||
AccountEntity( | ||
name = Arb.notBlankTrimmedString().bind().value, | ||
currency = Arb.maybe(Arb.string()).bind(), | ||
color = Arb.colorInt().bind().value, | ||
icon = Arb.iconAsset().bind().id, | ||
orderNum = Arb.double().removeEdgecases().bind(), | ||
includeInBalance = Arb.boolean().bind(), | ||
isSynced = Arb.boolean().bind(), | ||
isDeleted = Arb.boolean().bind(), | ||
id = Arb.uuid().bind() | ||
) | ||
} |
80 changes: 80 additions & 0 deletions
80
shared/data/core/src/test/java/com/ivy/data/repository/mapper/AccountMapperPropertyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.ivy.data.repository.mapper | ||
|
||
import com.ivy.data.invalidAccountEntity | ||
import com.ivy.data.model.primitive.AssetCode | ||
import com.ivy.data.model.testing.account | ||
import com.ivy.data.repository.CurrencyRepository | ||
import com.ivy.data.validAccountEntity | ||
import io.kotest.assertions.arrow.core.shouldBeLeft | ||
import io.kotest.assertions.arrow.core.shouldBeRight | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.checkAll | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class AccountMapperPropertyTest { | ||
|
||
private val currencyRepository = mockk<CurrencyRepository>() | ||
|
||
private lateinit var mapper: AccountMapper | ||
|
||
@Before | ||
fun mapper() { | ||
mapper = AccountMapper( | ||
currencyRepository = currencyRepository, | ||
) | ||
} | ||
|
||
@Test | ||
fun `property - domain-entity isomorphism`() = runTest { | ||
checkAll(Arb.account()) { accOrig -> | ||
with(mapper) { | ||
// when: domain -> entity -> domain | ||
val entityOne = accOrig.toEntity() | ||
val accTwo = entityOne.toDomain().getOrNull() | ||
|
||
// then: the recovered domain trn must be the same | ||
accTwo.shouldNotBeNull() shouldBe accOrig | ||
|
||
// and when again: domain -> entity | ||
val entityTwo = accTwo.toEntity() | ||
|
||
// then: the recovered entity must be the same | ||
entityTwo shouldBe entityOne | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `maps invalid accounts - always fails`() = runTest { | ||
checkAll(Arb.invalidAccountEntity()) { entity -> | ||
// given | ||
coEvery { currencyRepository.getBaseCurrency() } returns AssetCode.EUR | ||
|
||
// when | ||
val res = with(mapper) { entity.toDomain() } | ||
|
||
// then | ||
res.shouldBeLeft() | ||
} | ||
} | ||
|
||
@Test | ||
fun `maps valid accounts - always succeeds`() = runTest { | ||
checkAll(Arb.validAccountEntity()) { entity -> | ||
// given | ||
coEvery { currencyRepository.getBaseCurrency() } returns AssetCode.EUR | ||
|
||
// when | ||
val res = with(mapper) { entity.toDomain() } | ||
|
||
// then | ||
res.shouldBeRight() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
shared/data/model-testing/src/main/java/com/ivy/data/model/testing/ArbCategory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
package com.ivy.data.model.testing | ||
|
||
import arrow.core.None | ||
import arrow.core.Option | ||
import arrow.core.getOrElse | ||
import com.ivy.data.model.Category | ||
import com.ivy.data.model.CategoryId | ||
import io.kotest.property.Arb | ||
import io.kotest.property.arbitrary.arbitrary | ||
import io.kotest.property.arbitrary.boolean | ||
import io.kotest.property.arbitrary.double | ||
import io.kotest.property.arbitrary.instant | ||
import io.kotest.property.arbitrary.map | ||
import io.kotest.property.arbitrary.removeEdgecases | ||
import io.kotest.property.arbitrary.uuid | ||
|
||
fun Arb.Companion.category( | ||
categoryId: Option<CategoryId> = None, | ||
): Arb<Category> = arbitrary { | ||
Category( | ||
id = categoryId.getOrElse { Arb.categoryId().bind() }, | ||
name = Arb.notBlankTrimmedString().bind(), | ||
color = Arb.colorInt().bind(), | ||
icon = Arb.maybe(Arb.iconAsset()).bind(), | ||
orderNum = Arb.double().removeEdgecases().bind(), | ||
lastUpdated = Arb.instant().bind(), | ||
removed = Arb.boolean().bind() | ||
) | ||
} | ||
|
||
fun Arb.Companion.categoryId(): Arb<CategoryId> = Arb.uuid().map(::CategoryId) |
28 changes: 28 additions & 0 deletions
28
shared/data/model-testing/src/test/java/com/ivy/data/model/testing/ArbCategoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.ivy.data.model.testing | ||
|
||
import arrow.core.Some | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.property.Arb | ||
import io.kotest.property.checkAll | ||
import io.kotest.property.forAll | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
|
||
class ArbCategoryTest { | ||
|
||
@Test | ||
fun `generates arb category`() = runTest { | ||
forAll(Arb.category()) { | ||
true | ||
} | ||
} | ||
|
||
@Test | ||
fun `arb category respects passed param`() = runTest { | ||
val categoryId = ModelFixtures.CategoryId | ||
|
||
checkAll(Arb.category(categoryId = Some(categoryId))) { category -> | ||
category.id shouldBe categoryId | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.