forked from philipplackner/IvyWallet-PL_Version
-
Notifications
You must be signed in to change notification settings - Fork 0
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
31d7c32
commit 847c3af
Showing
4 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
core/domain/src/test/java/com/ivy/core/domain/action/transaction/AccountCacheDaoFake.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,38 @@ | ||
package com.ivy.core.domain.action.transaction | ||
|
||
import com.ivy.core.persistence.algorithm.accountcache.AccountCacheDao | ||
import com.ivy.core.persistence.algorithm.accountcache.AccountCacheEntity | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.map | ||
import java.time.Instant | ||
|
||
class AccountCacheDaoFake: AccountCacheDao { | ||
|
||
private val accounts = MutableStateFlow<List<AccountCacheEntity>>(emptyList()) | ||
|
||
override fun findAccountCache(accountId: String): Flow<AccountCacheEntity?> { | ||
return accounts | ||
.map { entities -> | ||
entities.find { it.accountId == accountId } | ||
} | ||
} | ||
|
||
override suspend fun findTimestampById(accountId: String): Instant? { | ||
return accounts.value.find { it.accountId == accountId }?.timestamp | ||
} | ||
|
||
override suspend fun save(cache: AccountCacheEntity) { | ||
accounts.value += cache | ||
} | ||
|
||
override suspend fun delete(accountId: String) { | ||
val account = accounts.value.find { it.accountId == accountId } ?: return | ||
accounts.value -= account | ||
} | ||
|
||
override suspend fun deleteAll() { | ||
accounts.value = emptyList() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core/domain/src/test/java/com/ivy/core/domain/action/transaction/TimeProviderFake.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,21 @@ | ||
package com.ivy.core.domain.action.transaction | ||
|
||
import com.ivy.common.time.provider.TimeProvider | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
|
||
class TimeProviderFake: TimeProvider { | ||
|
||
override fun timeNow(): LocalDateTime { | ||
return LocalDateTime.now() | ||
} | ||
|
||
override fun dateNow(): LocalDate { | ||
return LocalDate.now() | ||
} | ||
|
||
override fun zoneId(): ZoneId { | ||
return ZoneId.of("UTC") | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
core/domain/src/test/java/com/ivy/core/domain/action/transaction/TransactionDaoFake.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,82 @@ | ||
package com.ivy.core.domain.action.transaction | ||
|
||
import androidx.sqlite.db.SupportSQLiteQuery | ||
import com.ivy.core.persistence.dao.trn.AccountIdAndTrnTime | ||
import com.ivy.core.persistence.dao.trn.TransactionDao | ||
import com.ivy.core.persistence.entity.attachment.AttachmentEntity | ||
import com.ivy.core.persistence.entity.trn.TransactionEntity | ||
import com.ivy.core.persistence.entity.trn.TrnMetadataEntity | ||
import com.ivy.core.persistence.entity.trn.TrnTagEntity | ||
import com.ivy.data.SyncState | ||
|
||
class TransactionDaoFake: TransactionDao() { | ||
|
||
val transactions = mutableListOf<TransactionEntity>() | ||
val tags = mutableListOf<TrnTagEntity>() | ||
val attachments = mutableListOf<AttachmentEntity>() | ||
val metadatas = mutableListOf<TrnMetadataEntity>() | ||
|
||
override suspend fun saveTrnEntity(entity: TransactionEntity) { | ||
transactions.add(entity) | ||
} | ||
|
||
override suspend fun updateTrnTagsSyncByTrnId(trnId: String, sync: SyncState) { | ||
val transaction = transactions.find { it.id == trnId } ?: return | ||
val index = transactions.indexOf(transaction) | ||
transactions[index] = transaction.copy(sync = sync) | ||
} | ||
|
||
override suspend fun saveTags(entity: List<TrnTagEntity>) { | ||
tags.addAll(entity) | ||
} | ||
|
||
override suspend fun updateAttachmentsSyncByAssociatedId( | ||
associatedId: String, | ||
sync: SyncState | ||
) { | ||
val attachment = attachments.find { it.associatedId == associatedId } ?: return | ||
val index = attachments.indexOf(attachment) | ||
attachments[index] = attachment.copy(sync = sync) | ||
} | ||
|
||
override suspend fun saveAttachments(entity: List<AttachmentEntity>) { | ||
attachments.addAll(entity) | ||
} | ||
|
||
override suspend fun updateMetadataSyncByTrnId(trnId: String, sync: SyncState) { | ||
val metadata = metadatas.find { it.trnId == trnId } ?: return | ||
val index = metadatas.indexOf(metadata) | ||
metadatas[index] = metadata.copy(sync = sync) | ||
} | ||
|
||
override suspend fun saveMetadata(entity: List<TrnMetadataEntity>) { | ||
metadatas.addAll(entity) | ||
} | ||
|
||
override suspend fun findAllBlocking(): List<TransactionEntity> { | ||
return transactions | ||
} | ||
|
||
// Not supported for fake | ||
override suspend fun findBySQL(query: SupportSQLiteQuery): List<TransactionEntity> { | ||
return transactions | ||
} | ||
|
||
override suspend fun findAccountIdAndTimeById(trnId: String): AccountIdAndTrnTime? { | ||
val transaction = transactions.find { | ||
it.id == trnId && it.sync == SyncState.Deleting | ||
} ?: return null | ||
|
||
return AccountIdAndTrnTime( | ||
accountId = transaction.accountId, | ||
time = transaction.time, | ||
timeType = transaction.timeType | ||
) | ||
} | ||
|
||
override suspend fun updateTrnEntitySyncById(trnId: String, sync: SyncState) { | ||
val transaction = transactions.find { it.id == trnId } ?: return | ||
val index = transactions.indexOf(transaction) | ||
transactions[index] = transaction.copy(sync = sync) | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
core/domain/src/test/java/com/ivy/core/domain/action/transaction/WriteTrnsActTest.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,130 @@ | ||
package com.ivy.core.domain.action.transaction | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isNotNull | ||
import com.ivy.core.domain.action.Action | ||
import com.ivy.core.domain.algorithm.accountcache.InvalidateAccCacheAct | ||
import com.ivy.data.Sync | ||
import com.ivy.data.SyncState | ||
import com.ivy.data.Value | ||
import com.ivy.data.account.Account | ||
import com.ivy.data.account.AccountState | ||
import com.ivy.data.attachment.Attachment | ||
import com.ivy.data.attachment.AttachmentSource | ||
import com.ivy.data.attachment.AttachmentType | ||
import com.ivy.data.tag.Tag | ||
import com.ivy.data.tag.TagState | ||
import com.ivy.data.transaction.Transaction | ||
import com.ivy.data.transaction.TransactionType | ||
import com.ivy.data.transaction.TrnMetadata | ||
import com.ivy.data.transaction.TrnPurpose | ||
import com.ivy.data.transaction.TrnState | ||
import com.ivy.data.transaction.TrnTime | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import java.time.LocalDateTime | ||
import java.util.UUID | ||
|
||
internal class WriteTrnsActTest { | ||
|
||
private lateinit var writeTrnsAct: WriteTrnsAct | ||
private lateinit var transactionDaoFake: TransactionDaoFake | ||
private lateinit var timeProviderFake: TimeProviderFake | ||
private lateinit var accountCacheDaoFake: AccountCacheDaoFake | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
transactionDaoFake = TransactionDaoFake() | ||
timeProviderFake = TimeProviderFake() | ||
accountCacheDaoFake = AccountCacheDaoFake() | ||
writeTrnsAct = WriteTrnsAct( | ||
transactionDao = transactionDaoFake, | ||
trnsSignal = TrnsSignal(), | ||
timeProvider = timeProviderFake, | ||
invalidateAccCacheAct = InvalidateAccCacheAct( | ||
accountCacheDao = accountCacheDaoFake, | ||
timeProvider = timeProviderFake | ||
), | ||
accountCacheDao = accountCacheDaoFake | ||
) | ||
} | ||
|
||
@Test | ||
fun `Test create new transaction with expense`() = runBlocking<Unit> { | ||
val account = Account( | ||
id = UUID.randomUUID(), | ||
name = "Test account", | ||
currency = "EUR", | ||
color = 0x00f15e, | ||
icon = null, | ||
excluded = false, | ||
folderId = null, | ||
orderNum = 1.0, | ||
state = AccountState.Default, | ||
sync = Sync( | ||
state = SyncState.Syncing, | ||
lastUpdated = LocalDateTime.now() | ||
) | ||
) | ||
val transactionId = UUID.randomUUID() | ||
val tag = Tag( | ||
id = UUID.randomUUID().toString(), | ||
color = 0x00f15e, | ||
name = "Test tag", | ||
orderNum = 1.0, | ||
state = TagState.Default, | ||
sync = Sync(SyncState.Syncing, LocalDateTime.now()) | ||
) | ||
val attachment = Attachment( | ||
id = UUID.randomUUID().toString(), | ||
associatedId = transactionId.toString(), | ||
uri = "test", | ||
source = AttachmentSource.Local, | ||
filename = null, | ||
type = AttachmentType.Image, | ||
sync = Sync(SyncState.Syncing, LocalDateTime.now()) | ||
) | ||
val transaction = Transaction( | ||
id = transactionId, | ||
account = account, | ||
type = TransactionType.Expense, | ||
value = Value( | ||
amount = 50.0, | ||
currency = "EUR" | ||
), | ||
category = null, | ||
time = TrnTime.Actual(LocalDateTime.now()), | ||
title = "Test transaction", | ||
description = null, | ||
state = TrnState.Default, | ||
purpose = TrnPurpose.Fee, | ||
tags = listOf(tag), | ||
attachments = listOf(attachment), | ||
metadata = TrnMetadata( | ||
recurringRuleId = null, | ||
loanId = null, | ||
loanRecordId = null | ||
), | ||
sync = Sync( | ||
state = SyncState.Syncing, | ||
lastUpdated = LocalDateTime.now() | ||
) | ||
) | ||
|
||
writeTrnsAct(WriteTrnsAct.Input.CreateNew(transaction)) | ||
|
||
val cachedTransaction = transactionDaoFake.transactions.find { | ||
it.id == transactionId.toString() | ||
} | ||
val cachedTag = transactionDaoFake.tags.find { it.tagId == tag.id } | ||
val cachedAttachement = transactionDaoFake.attachments.find { it.id == attachment.id } | ||
|
||
assertThat(cachedTransaction).isNotNull() | ||
assertThat(cachedTransaction?.type).isEqualTo(TransactionType.Expense) | ||
|
||
assertThat(cachedTag).isNotNull() | ||
assertThat(cachedAttachement).isNotNull() | ||
} | ||
} |