From 96154e6983a1b0fb12bcaaebea02e6bf92698e90 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 19 Nov 2025 14:54:40 +0400 Subject: [PATCH] Updated on 2026-08-14 --- domain/account/build.gradle.kts | 3 + .../domain/account/models/AccountList.kt | 15 +- .../domain/account/models/AccountListTest.kt | 222 ++++++++++++------ .../usecase/AddCryptoPortfolioUseCaseTest.kt | 39 +-- .../ApplyAccountListSortingUseCaseTest.kt | 34 +-- .../ArchiveCryptoPortfolioUseCaseTest.kt | 6 +- .../tangem/domain/account/utils/AccountExt.kt | 38 --- settings.gradle.kts | 1 + test/mock/.gitignore | 1 + test/mock/build.gradle.kts | 8 + .../java/com/tangem/test/mock/MockAccounts.kt | 60 +++++ 11 files changed, 262 insertions(+), 165 deletions(-) delete mode 100644 domain/account/src/test/kotlin/com/tangem/domain/account/utils/AccountExt.kt create mode 100644 test/mock/.gitignore create mode 100644 test/mock/build.gradle.kts create mode 100644 test/mock/src/main/java/com/tangem/test/mock/MockAccounts.kt diff --git a/domain/account/build.gradle.kts b/domain/account/build.gradle.kts index 984f798a2b..da17a216f8 100644 --- a/domain/account/build.gradle.kts +++ b/domain/account/build.gradle.kts @@ -18,6 +18,9 @@ dependencies { implementation(deps.kotlin.coroutines) implementation(deps.kotlin.serialization) + // region Test libraries testImplementation(projects.test.core) + testImplementation(projects.test.mock) testRuntimeOnly(deps.test.junit5.engine) + // endregion } \ No newline at end of file diff --git a/domain/account/src/main/java/com/tangem/domain/account/models/AccountList.kt b/domain/account/src/main/java/com/tangem/domain/account/models/AccountList.kt index 449c719313..b9378ac209 100644 --- a/domain/account/src/main/java/com/tangem/domain/account/models/AccountList.kt +++ b/domain/account/src/main/java/com/tangem/domain/account/models/AccountList.kt @@ -13,11 +13,11 @@ import com.tangem.utils.extensions.addOrReplace import kotlinx.serialization.Serializable /** - * Represents a list of accounts associated with a user wallet + * Represents a list of accounts associated with a user wallet ID. * * @property userWalletId the user wallet id associated with the account list * @property accounts a list of accounts belonging to the user wallet - * @property totalAccounts the total number of accounts + * @property totalAccounts the total number of accounts (including archived ones) * @property sortType the sorting type applied to the accounts * @property groupType the grouping type applied to the accounts * @@ -142,6 +142,11 @@ data class AccountList private constructor( data object DuplicateAccountNames : Error { override fun toString(): String = "$tag: Account list contains duplicate account names" } + + @Serializable + data object TotalAccountsLessThanActive : Error { + override fun toString(): String = "$tag: Total accounts cannot be less than active accounts" + } } companion object { @@ -180,13 +185,17 @@ data class AccountList private constructor( val uniqueAccountIdsCount = accounts.map { it.accountId.value }.distinct().size ensure(accounts.size == uniqueAccountIdsCount) { Error.DuplicateAccountIds } - val customNames = accounts.mapNotNull { (it.accountName as? AccountName.Custom)?.value } + val customNames = accounts.map { (it.accountName as? AccountName.Custom)?.value } val uniqueCustomNameCount = customNames.distinct().size ensure(customNames.size == uniqueCustomNameCount) { Error.DuplicateAccountNames } + ensure(totalAccounts >= accounts.size) { + Error.TotalAccountsLessThanActive + } + AccountList( userWalletId = userWalletId, accounts = accounts, diff --git a/domain/account/src/test/kotlin/com/tangem/domain/account/models/AccountListTest.kt b/domain/account/src/test/kotlin/com/tangem/domain/account/models/AccountListTest.kt index 70de7ff158..34f6603024 100644 --- a/domain/account/src/test/kotlin/com/tangem/domain/account/models/AccountListTest.kt +++ b/domain/account/src/test/kotlin/com/tangem/domain/account/models/AccountListTest.kt @@ -2,14 +2,20 @@ package com.tangem.domain.account.models import arrow.core.Either import arrow.core.left +import arrow.core.right import com.google.common.truth.Truth -import com.tangem.domain.account.utils.createAccount -import com.tangem.domain.account.utils.createAccounts +import com.tangem.domain.models.TokensGroupType +import com.tangem.domain.models.TokensSortType import com.tangem.domain.models.account.Account import com.tangem.domain.models.account.AccountName import com.tangem.domain.models.account.CryptoPortfolioIcon +import com.tangem.domain.models.account.DerivationIndex import com.tangem.domain.models.wallet.UserWalletId import com.tangem.test.core.ProvideTestModels +import com.tangem.test.mock.MockAccounts +import com.tangem.test.mock.MockAccounts.createAccount +import com.tangem.test.mock.MockAccounts.createAccountList +import com.tangem.test.mock.MockAccounts.createAccounts import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance @@ -19,60 +25,49 @@ import org.junit.jupiter.params.ParameterizedTest [REDACTED_AUTHOR] */ @TestInstance(TestInstance.Lifecycle.PER_CLASS) -class AccountListTest { +internal class AccountListTest { @Test fun mainAccount() { - // Arrange - val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId = userWalletId) - - val accountList = AccountList( - userWalletId = userWalletId, - accounts = listOf(mainAccount), - totalAccounts = 1, - ) - .getOrNull()!! - // Act - val actual = accountList.mainAccount + val actual = MockAccounts.onlyMainAccount.mainAccount // Assert - val expected = mainAccount + val expected = Account.CryptoPortfolio.createMainAccount(userWalletId = userWalletId) Truth.assertThat(actual).isEqualTo(expected) } @Test fun canAddMoreAccounts() { // Arrange - val accountList = AccountList( - userWalletId = userWalletId, - accounts = createAccounts(userWalletId = userWalletId, count = 2), - totalAccounts = 2, - ).getOrNull()!! + val accountList = createAccountList(activeAccounts = 2) - val fullAccountList = AccountList( - userWalletId = userWalletId, - accounts = createAccounts(userWalletId = userWalletId, count = 20), - totalAccounts = 20, - ).getOrNull()!! + val fullAccountList = MockAccounts.fullAccountList // Act & Assert Truth.assertThat(accountList.canAddMoreAccounts).isTrue() Truth.assertThat(fullAccountList.canAddMoreAccounts).isFalse() } + @Test + fun activeAccounts() { + // Arrange + val accountList = createAccountList(activeAccounts = 5, totalAccounts = 10) + + // Act & Assert + val expected = 5 + Truth.assertThat(accountList.activeAccounts).isEqualTo(expected) + } + @Test fun empty() { // Act val actual = AccountList.empty(userWalletId) // Assert - val expected = AccountList( - userWalletId = userWalletId, - accounts = listOf(Account.CryptoPortfolio.createMainAccount(userWalletId = userWalletId)), - totalAccounts = 1, - ).getOrNull()!! + val expected = MockAccounts.onlyMainAccount + // Assert Truth.assertThat(actual).isEqualTo(expected) } @@ -80,6 +75,33 @@ class AccountListTest { @TestInstance(TestInstance.Lifecycle.PER_CLASS) inner class Create { + @Test + fun `invoke with non default sort and group types`() { + // Arrange + val accounts = createAccounts(count = 5) + val sortType = TokensSortType.BALANCE + val groupType = TokensGroupType.NETWORK + + // Act + val actual = AccountList( + userWalletId = userWalletId, + accounts = accounts, + totalAccounts = accounts.size, + sortType = sortType, + groupType = groupType, + ) + + // Assert + val expected = AccountList( + userWalletId = userWalletId, + accounts = accounts, + totalAccounts = accounts.size, + sortType = sortType, + groupType = groupType, + ) + Truth.assertThat(actual).isEqualTo(expected) + } + @ParameterizedTest @ProvideTestModels fun invoke(model: CreateTestModel) { @@ -87,7 +109,7 @@ class AccountListTest { val actual = AccountList( userWalletId = userWalletId, accounts = model.accounts, - totalAccounts = model.accounts.size, + totalAccounts = model.totalAccounts, ) // Assert @@ -99,9 +121,13 @@ class AccountListTest { accounts = emptyList(), expected = AccountList.Error.EmptyAccountsList.left(), ), + CreateTestModel( + accounts = createAccounts(count = 21), + expected = AccountList.Error.ExceedsMaxAccountsCount.left(), + ), CreateTestModel( accounts = listOf( - createAccount(userWalletId = userWalletId, derivationIndex = 1), + createAccount(derivationIndex = 1), ), expected = AccountList.Error.MainAccountNotFound.left(), ), @@ -114,42 +140,44 @@ class AccountListTest { ), expected = AccountList.Error.ExceedsMaxMainAccountsCount.left(), ), - createAccounts(userWalletId = userWalletId, count = 1).let { - CreateTestModel( - accounts = it, - expected = AccountList(userWalletId = userWalletId, accounts = it, totalAccounts = 1), - ) - }, - createAccounts(userWalletId = userWalletId, count = 20).let { - CreateTestModel( - accounts = it, - expected = AccountList(userWalletId = userWalletId, accounts = it, totalAccounts = 20), - ) - }, - CreateTestModel( - accounts = createAccounts(userWalletId = userWalletId, count = 21), - expected = AccountList.Error.ExceedsMaxAccountsCount.left(), - ), CreateTestModel( accounts = listOf( - createAccount(userWalletId = userWalletId, derivationIndex = 0), - createAccount(userWalletId = userWalletId, derivationIndex = 1), - createAccount(userWalletId = userWalletId, derivationIndex = 1), + createAccount(derivationIndex = 0), + createAccount(derivationIndex = 1), + createAccount(derivationIndex = 1), ), expected = AccountList.Error.DuplicateAccountIds.left(), ), CreateTestModel( accounts = listOf( - createAccount(userWalletId = userWalletId, name = "Name", derivationIndex = 0), - createAccount(userWalletId = userWalletId, name = "Name", derivationIndex = 1), + createAccount(name = "Name", derivationIndex = 0), + createAccount(name = "Name", derivationIndex = 1), ), expected = AccountList.Error.DuplicateAccountNames.left(), ), + CreateTestModel( + accounts = createAccounts(count = 2), + totalAccounts = 1, + expected = AccountList.Error.TotalAccountsLessThanActive.left(), + ), + createAccountList(activeAccounts = 1).let { + CreateTestModel( + accounts = it.accounts, + expected = it.right(), + ) + }, + createAccountList(activeAccounts = 20).let { + CreateTestModel( + accounts = it.accounts, + expected = it.right(), + ) + }, ) } data class CreateTestModel( val accounts: List, + val totalAccounts: Int = accounts.size, val expected: Either, ) @@ -171,7 +199,7 @@ class AccountListTest { // region Add new account run { val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId) - val newAccount = createAccount(userWalletId = userWalletId, derivationIndex = 1) + val newAccount = createAccount(derivationIndex = 1) PlusTestModel( initial = AccountList( @@ -208,15 +236,71 @@ class AccountListTest { ) }, // endregion + // region MainAccountNotFound + run { + val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId) + val newAccount = Account.CryptoPortfolio( + accountId = mainAccount.accountId, + accountName = mainAccount.accountName, + derivationIndex = DerivationIndex(1).getOrNull()!!, + icon = mainAccount.icon, + cryptoCurrencies = mainAccount.cryptoCurrencies, + ) + + PlusTestModel( + initial = AccountList( + userWalletId = userWalletId, + accounts = listOf(mainAccount), + totalAccounts = 1, + ).getOrNull()!!, + toAdd = newAccount, + expected = AccountList.Error.MainAccountNotFound.left(), + ) + }, + // endregion + // region ExceedsMaxMainAccountsCount + run { + val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId) + val newAccount = Account.CryptoPortfolio.createMainAccount(UserWalletId("012")) + + PlusTestModel( + initial = AccountList( + userWalletId = userWalletId, + accounts = listOf(mainAccount), + totalAccounts = 1, + ).getOrNull()!!, + toAdd = newAccount, + expected = AccountList.Error.ExceedsMaxMainAccountsCount.left(), + ) + }, + // endregion + // region DuplicateAccountNames + run { + val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId) + val newAccount = createAccount(derivationIndex = 1).copy(accountName = mainAccount.accountName) + + PlusTestModel( + initial = AccountList( + userWalletId = userWalletId, + accounts = listOf(mainAccount), + totalAccounts = 1, + ).getOrNull()!!, + toAdd = newAccount, + expected = AccountList.Error.DuplicateAccountNames.left(), + ) + }, + // endregion + // region ExceedsMaxAccountsCount PlusTestModel( initial = AccountList( userWalletId = userWalletId, - accounts = createAccounts(userWalletId = userWalletId, count = 20), + accounts = createAccounts(count = 20), totalAccounts = 20, ).getOrNull()!!, - toAdd = createAccount(userWalletId = userWalletId, derivationIndex = 21), + toAdd = createAccount(derivationIndex = 21), expected = AccountList.Error.ExceedsMaxAccountsCount.left(), ), + // endregion ) } @@ -244,7 +328,7 @@ class AccountListTest { // region Remove existing account run { val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId) - val secondaryAccount = createAccount(userWalletId = userWalletId, derivationIndex = 2) + val secondaryAccount = createAccount(derivationIndex = 2) MinusTestModel( initial = AccountList( @@ -264,20 +348,18 @@ class AccountListTest { // region Remove unexisting account run { val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId) - val notInList = createAccount(userWalletId = userWalletId, derivationIndex = 3) + val notInList = createAccount(derivationIndex = 3) + + val accountList = AccountList( + userWalletId = userWalletId, + accounts = listOf(mainAccount), + totalAccounts = 1, + ) MinusTestModel( - initial = AccountList( - userWalletId = userWalletId, - accounts = listOf(mainAccount), - totalAccounts = 1, - ).getOrNull()!!, + initial = accountList.getOrNull()!!, toRemove = notInList, - expected = AccountList( - userWalletId = userWalletId, - accounts = listOf(mainAccount), - totalAccounts = 1, - ), + expected = accountList, ) }, // endregion @@ -299,7 +381,7 @@ class AccountListTest { // region MainAccountNotFound run { val mainAccount = Account.CryptoPortfolio.createMainAccount(userWalletId) - val secondaryAccount = createAccount(userWalletId = userWalletId, derivationIndex = 2) + val secondaryAccount = createAccount(derivationIndex = 2) MinusTestModel( initial = AccountList( @@ -323,6 +405,6 @@ class AccountListTest { private companion object { - val userWalletId = UserWalletId("011") + val userWalletId = MockAccounts.userWalletId } } \ No newline at end of file diff --git a/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/AddCryptoPortfolioUseCaseTest.kt b/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/AddCryptoPortfolioUseCaseTest.kt index 1ea298d6e1..d9756586c3 100644 --- a/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/AddCryptoPortfolioUseCaseTest.kt +++ b/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/AddCryptoPortfolioUseCaseTest.kt @@ -10,11 +10,8 @@ import com.tangem.domain.account.models.AccountList import com.tangem.domain.account.repository.AccountsCRUDRepository import com.tangem.domain.account.tokens.MainAccountTokensMigration import com.tangem.domain.account.usecase.AddCryptoPortfolioUseCase.Error -import com.tangem.domain.account.utils.createAccount -import com.tangem.domain.account.utils.createAccounts -import com.tangem.domain.models.account.Account -import com.tangem.domain.models.account.CryptoPortfolioIcon -import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.test.mock.MockAccounts +import com.tangem.test.mock.MockAccounts.createAccount import io.mockk.* import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.BeforeEach @@ -42,7 +39,7 @@ class AddCryptoPortfolioUseCaseTest { @Test fun `invoke should add new crypto portfolio account to existing list`() = runTest { // Arrange - val newAccount = createNewAccount() + val newAccount = createAccount(derivationIndex = 1) val accountList = AccountList.empty(userWalletId) val updatedAccountList = (accountList + newAccount).getOrNull()!! @@ -78,7 +75,7 @@ class AddCryptoPortfolioUseCaseTest { @Test fun `invoke should return error if fetch is failed`() = runTest { // Arrange - val newAccount = createNewAccount() + val newAccount = createAccount(derivationIndex = 1) val exception = Exception("Fetch error") coEvery { @@ -113,7 +110,7 @@ class AddCryptoPortfolioUseCaseTest { @Test fun `invoke should return error if account list none exists`() = runTest { // Arrange - val newAccount = createNewAccount() + val newAccount = createAccount(derivationIndex = 1) coEvery { singleAccountListFetcher(SingleAccountListFetcher.Params(userWalletId)) @@ -147,13 +144,8 @@ class AddCryptoPortfolioUseCaseTest { @Test fun `invoke should return error if account list requirements not met`() = runTest { // Arrange - val accountList = AccountList( - userWalletId = userWalletId, - accounts = createAccounts(userWalletId = userWalletId, count = 20), - totalAccounts = 20, - ).getOrNull()!! - - val newAccount = createNewAccount(derivationIndex = 21) + val accountList = MockAccounts.fullAccountList + val newAccount = createAccount(derivationIndex = 21) coEvery { singleAccountListFetcher(SingleAccountListFetcher.Params(userWalletId)) @@ -190,7 +182,7 @@ class AddCryptoPortfolioUseCaseTest { @Test fun `invoke should return error if getAccounts throws exception`() = runTest { // Arrange - val newAccount = createNewAccount() + val newAccount = createAccount(derivationIndex = 1) val exception = IllegalStateException("Test error") coEvery { @@ -224,7 +216,7 @@ class AddCryptoPortfolioUseCaseTest { @Test fun `invoke should return error if saveAccounts throws exception`() = runTest { // Arrange - val newAccount = createNewAccount() + val newAccount = createAccount(derivationIndex = 1) val accountList = AccountList.empty(userWalletId) val updatedAccountList = (accountList + newAccount).getOrNull()!! @@ -263,7 +255,7 @@ class AddCryptoPortfolioUseCaseTest { @Test fun `invoke should return new account if migrate returns error`() = runTest { // Arrange - val newAccount = createNewAccount() + val newAccount = createAccount(derivationIndex = 1) val accountList = AccountList.empty(userWalletId) val updatedAccountList = (accountList + newAccount).getOrNull()!! @@ -299,15 +291,6 @@ class AddCryptoPortfolioUseCaseTest { private companion object { - val userWalletId = UserWalletId("011") - - fun createNewAccount(derivationIndex: Int = 1): Account.CryptoPortfolio { - return createAccount( - userWalletId = userWalletId, - name = "New Account", - icon = CryptoPortfolioIcon.ofDefaultCustomAccount(), - derivationIndex = derivationIndex, - ) - } + val userWalletId = MockAccounts.userWalletId } } \ No newline at end of file diff --git a/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ApplyAccountListSortingUseCaseTest.kt b/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ApplyAccountListSortingUseCaseTest.kt index 8ca6c00f0e..2d19983849 100644 --- a/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ApplyAccountListSortingUseCaseTest.kt +++ b/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ApplyAccountListSortingUseCaseTest.kt @@ -7,11 +7,9 @@ import com.google.common.truth.Truth import com.tangem.domain.account.models.AccountList import com.tangem.domain.account.repository.AccountsCRUDRepository import com.tangem.domain.account.usecase.ApplyAccountListSortingUseCase.Error -import com.tangem.domain.models.account.Account import com.tangem.domain.models.account.AccountId -import com.tangem.domain.models.account.CryptoPortfolioIcon -import com.tangem.domain.models.account.DerivationIndex import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.test.mock.MockAccounts import com.tangem.utils.coroutines.TestingCoroutineDispatcherProvider import io.mockk.* import kotlinx.coroutines.test.runTest @@ -71,8 +69,7 @@ class ApplyAccountListSortingUseCaseTest { @Test fun `invoke returns DataOperationFailed when getAccountList returns error`() = runTest { // Arrange - val accountList = createAccountList() - val accountIds = accountList.toAccountIds() + val accountIds = defaultAccountList.toAccountIds() val exception = Exception("Test error") coEvery { accountsCRUDRepository.getAccountListSync(userWalletId) } throws exception @@ -91,7 +88,7 @@ class ApplyAccountListSortingUseCaseTest { @Test fun `invoke returns UnableToSortSingleAccount when saved accountList contain one account`() = runTest { // Arrange - val accountIds = createAccountList().toAccountIds() + val accountIds = defaultAccountList.toAccountIds() coEvery { accountsCRUDRepository.getAccountListSync(userWalletId) @@ -115,7 +112,7 @@ class ApplyAccountListSortingUseCaseTest { userWalletId = UserWalletId("012"), ) - val accountList = createAccountList() + val accountList = defaultAccountList val accountIds = listOf(accountList.mainAccount.accountId, unknownAccountId) coEvery { accountsCRUDRepository.getAccountListSync(userWalletId) } returns accountList.some() @@ -134,7 +131,7 @@ class ApplyAccountListSortingUseCaseTest { @Test fun `invoke returns Right without accounts saving when nothing to change`() = runTest { // Arrange - val accountList = createAccountList() + val accountList = defaultAccountList val accountIds = accountList.toAccountIds() coEvery { accountsCRUDRepository.getAccountListSync(userWalletId) } returns accountList.some() @@ -153,7 +150,7 @@ class ApplyAccountListSortingUseCaseTest { @Test fun `invoke returns Right`() = runTest { // Arrange - val accountList = createAccountList() + val accountList = defaultAccountList val accountIds = accountList.toAccountIds().reversed() val updatedAccountList = AccountList( @@ -179,21 +176,12 @@ class ApplyAccountListSortingUseCaseTest { } } - private fun createAccountList(): AccountList { - val accountList = AccountList.empty(userWalletId) - - val account1 = Account.CryptoPortfolio( - accountId = AccountId.forCryptoPortfolio(userWalletId, DerivationIndex(1).getOrNull()!!), - name = "Account 1", - icon = CryptoPortfolioIcon.ofDefaultCustomAccount(), - derivationIndex = 1, - ) - .getOrNull()!! - - return (accountList + account1).getOrNull()!! - } - private fun AccountList.toAccountIds(): List { return this.accounts.map { it.accountId } } + + private companion object { + + val defaultAccountList = MockAccounts.createAccountList(activeAccounts = 2) + } } \ No newline at end of file diff --git a/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ArchiveCryptoPortfolioUseCaseTest.kt b/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ArchiveCryptoPortfolioUseCaseTest.kt index b23bd9bfe0..461bd17267 100644 --- a/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ArchiveCryptoPortfolioUseCaseTest.kt +++ b/domain/account/src/test/kotlin/com/tangem/domain/account/usecase/ArchiveCryptoPortfolioUseCaseTest.kt @@ -8,10 +8,10 @@ import com.google.common.truth.Truth import com.tangem.domain.account.models.AccountList import com.tangem.domain.account.repository.AccountsCRUDRepository import com.tangem.domain.account.usecase.ArchiveCryptoPortfolioUseCase.Error -import com.tangem.domain.account.utils.createAccount import com.tangem.domain.models.account.AccountId import com.tangem.domain.models.account.DerivationIndex import com.tangem.domain.models.wallet.UserWalletId +import com.tangem.test.mock.MockAccounts.createAccount import io.mockk.* import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.BeforeEach @@ -32,7 +32,7 @@ class ArchiveCryptoPortfolioUseCaseTest { @Test fun `invoke should archive existing crypto portfolio account`() = runTest { // Arrange - val account = createAccount(userWalletId) + val account = createAccount(derivationIndex = 1) val accountList = (AccountList.empty(userWalletId) + account).getOrNull()!! val accountId = account.accountId @@ -122,7 +122,7 @@ class ArchiveCryptoPortfolioUseCaseTest { @Test fun `invoke should return error if saveAccounts throws exception`() = runTest { // Arrange - val account = createAccount(userWalletId) + val account = createAccount(derivationIndex = 1) val accountList = (AccountList.empty(userWalletId) + account).getOrNull()!! val accountId = account.accountId diff --git a/domain/account/src/test/kotlin/com/tangem/domain/account/utils/AccountExt.kt b/domain/account/src/test/kotlin/com/tangem/domain/account/utils/AccountExt.kt deleted file mode 100644 index dbcbe4ead7..0000000000 --- a/domain/account/src/test/kotlin/com/tangem/domain/account/utils/AccountExt.kt +++ /dev/null @@ -1,38 +0,0 @@ -package com.tangem.domain.account.utils - -import com.tangem.domain.models.account.* -import com.tangem.domain.models.wallet.UserWalletId -import kotlin.random.Random - -fun createAccounts(userWalletId: UserWalletId, count: Int): List { - return buildList { - add(Account.CryptoPortfolio.createMainAccount(userWalletId)) - - repeat(count - 1) { - val account = createAccount( - userWalletId = userWalletId, - name = "Test Account ${it + 1}", - derivationIndex = it + 1, - ) - - add(account) - } - } -} - -fun createAccount( - userWalletId: UserWalletId, - name: String = "Test Account", - icon: CryptoPortfolioIcon = CryptoPortfolioIcon.ofDefaultCustomAccount(), - derivationIndex: Int = Random.nextInt(1, 21), -): Account.CryptoPortfolio { - val derivationIndex = DerivationIndex(derivationIndex).getOrNull()!! - - return Account.CryptoPortfolio( - accountId = AccountId.forCryptoPortfolio(userWalletId = userWalletId, derivationIndex = derivationIndex), - accountName = AccountName(name).getOrNull()!!, - icon = icon, - derivationIndex = derivationIndex, - cryptoCurrencies = emptySet(), - ) -} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 51d4e9f8dc..9d61983167 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -150,6 +150,7 @@ include(":app") include(":plugins:detekt-rules") include(":test:core") +include(":test:mock") // region Core modules include(":core:analytics") diff --git a/test/mock/.gitignore b/test/mock/.gitignore new file mode 100644 index 0000000000..42afabfd2a --- /dev/null +++ b/test/mock/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/test/mock/build.gradle.kts b/test/mock/build.gradle.kts new file mode 100644 index 0000000000..4bd02a3f21 --- /dev/null +++ b/test/mock/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + alias(deps.plugins.kotlin.jvm) + id("configuration") +} + +dependencies { + implementation(projects.domain.account) +} \ No newline at end of file diff --git a/test/mock/src/main/java/com/tangem/test/mock/MockAccounts.kt b/test/mock/src/main/java/com/tangem/test/mock/MockAccounts.kt new file mode 100644 index 0000000000..c8b8f57517 --- /dev/null +++ b/test/mock/src/main/java/com/tangem/test/mock/MockAccounts.kt @@ -0,0 +1,60 @@ +package com.tangem.test.mock + +import com.tangem.domain.account.models.AccountList +import com.tangem.domain.models.account.* +import com.tangem.domain.models.currency.CryptoCurrency +import com.tangem.domain.models.wallet.UserWalletId + +/** +[REDACTED_AUTHOR] + */ +object MockAccounts { + + val userWalletId = UserWalletId("011") + + val onlyMainAccount = createAccountList(activeAccounts = 1) + + val fullAccountList = createAccountList(activeAccounts = 20) + + fun createAccountList( + activeAccounts: Int, + totalAccounts: Int = activeAccounts, + userWalletId: UserWalletId = this.userWalletId, + ): AccountList { + return AccountList( + userWalletId = userWalletId, + accounts = createAccounts(count = activeAccounts, userWalletId = userWalletId), + totalAccounts = totalAccounts, + ).getOrNull()!! + } + + fun createAccounts(count: Int, userWalletId: UserWalletId = this.userWalletId): List { + return buildList { + add(Account.CryptoPortfolio.createMainAccount(userWalletId)) + + repeat(count - 1) { + val account = createAccount(derivationIndex = it + 1, userWalletId = userWalletId) + + add(account) + } + } + } + + fun createAccount( + derivationIndex: Int, + name: String = "Account #$derivationIndex", + icon: CryptoPortfolioIcon = CryptoPortfolioIcon.ofDefaultCustomAccount(), + cryptoCurrencies: Set = emptySet(), + userWalletId: UserWalletId = this.userWalletId, + ): Account.CryptoPortfolio { + val derivationIndex = DerivationIndex(derivationIndex).getOrNull()!! + + return Account.CryptoPortfolio( + accountId = AccountId.forCryptoPortfolio(userWalletId = userWalletId, derivationIndex = derivationIndex), + accountName = AccountName(name).getOrNull()!!, + icon = icon, + derivationIndex = derivationIndex, + cryptoCurrencies = cryptoCurrencies, + ) + } +} \ No newline at end of file