-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from nprzy/days-requested
Allow batch mode to backfill more than 90 days of transaction history
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 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
89 changes: 89 additions & 0 deletions
89
src/test/kotlin/net/djvk/fireflyPlaidConnector2/lib/MockUtil.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,89 @@ | ||
package net.djvk.fireflyPlaidConnector2.lib | ||
|
||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import io.ktor.util.reflect.* | ||
import net.djvk.fireflyPlaidConnector2.api.firefly.apis.AboutApi | ||
import net.djvk.fireflyPlaidConnector2.api.firefly.apis.AccountsApi | ||
import net.djvk.fireflyPlaidConnector2.api.firefly.apis.TransactionsApi | ||
import net.djvk.fireflyPlaidConnector2.api.firefly.models.SystemInfo | ||
import net.djvk.fireflyPlaidConnector2.api.firefly.models.SystemInfoData | ||
import net.djvk.fireflyPlaidConnector2.api.firefly.infrastructure.BodyProvider as FireflyBodyProvider | ||
import net.djvk.fireflyPlaidConnector2.api.firefly.infrastructure.HttpResponse as FireflyHttpResponse | ||
import net.djvk.fireflyPlaidConnector2.api.plaid.PlaidApiWrapper | ||
import net.djvk.fireflyPlaidConnector2.api.plaid.apis.PlaidApi | ||
import net.djvk.fireflyPlaidConnector2.sync.MINIMUM_FIREFLY_VERSION | ||
import net.djvk.fireflyPlaidConnector2.api.plaid.infrastructure.BodyProvider as PlaidBodyProvider | ||
import net.djvk.fireflyPlaidConnector2.api.plaid.infrastructure.HttpResponse as PlaidHttpResponse | ||
import org.mockito.kotlin.* | ||
import org.mockito.stubbing.Answer | ||
|
||
val OK_RESPONSE = mock<HttpResponse> { | ||
on { status } doReturn HttpStatusCode.OK | ||
on { headers } doReturn Headers.Empty | ||
} | ||
|
||
private class PlaidStubbedBodyProvider<T : Any>(val responseObj: T): PlaidBodyProvider<T> { | ||
override suspend fun body(response: HttpResponse): T { | ||
return responseObj | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override suspend fun <V : Any> typedBody(response: HttpResponse, type: TypeInfo): V { | ||
return responseObj as V | ||
} | ||
} | ||
|
||
fun <T : Any> createPlaidResponse( | ||
response: T, | ||
httpResponse: HttpResponse = OK_RESPONSE, | ||
): PlaidHttpResponse<T> { | ||
return PlaidHttpResponse(httpResponse, PlaidStubbedBodyProvider(response)) | ||
} | ||
|
||
class PlaidMock { | ||
val api = mock<PlaidApi>() | ||
val wrapper = mock<PlaidApiWrapper> { | ||
onBlocking { executeRequest(any<suspend (PlaidApi) -> Any>(), any(), any()) } doSuspendableAnswer { | ||
val requestExecutor = it.getArgument(0) as suspend (PlaidApi) -> Any | ||
requestExecutor.invoke(api) | ||
} | ||
} | ||
} | ||
|
||
private class FireflyStubbedBodyProvider<T : Any>(val responseObj: T): FireflyBodyProvider<T> { | ||
override suspend fun body(response: HttpResponse): T { | ||
return responseObj | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override suspend fun <V : Any> typedBody(response: HttpResponse, type: TypeInfo): V { | ||
return responseObj as V | ||
} | ||
} | ||
|
||
fun <T : Any> createFireflyResponse( | ||
response: T, | ||
httpResponse: HttpResponse = OK_RESPONSE, | ||
): FireflyHttpResponse<T> { | ||
return FireflyHttpResponse(httpResponse, FireflyStubbedBodyProvider(response)) | ||
} | ||
|
||
class FireflyMock { | ||
val aboutApi = mock<AboutApi>() | ||
val transactionsApi = mock<TransactionsApi>() | ||
val accountsApi = mock<AccountsApi>() | ||
|
||
init { | ||
val systemInfoData = SystemInfoData( | ||
version = MINIMUM_FIREFLY_VERSION, | ||
apiVersion = "1.0.0", | ||
phpVersion = "1.0.0", | ||
os = "testOs", | ||
driver = "testDriver" | ||
) | ||
aboutApi.stub { | ||
onBlocking { getAbout() } doAnswer { createFireflyResponse(SystemInfo(systemInfoData)) } | ||
} | ||
} | ||
} |
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
95 changes: 95 additions & 0 deletions
95
src/test/kotlin/net/djvk/fireflyPlaidConnector2/sync/BatchSyncRunnerTest.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,95 @@ | ||
package net.djvk.fireflyPlaidConnector2.sync | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import net.djvk.fireflyPlaidConnector2.api.plaid.models.TransactionsGetResponse | ||
import net.djvk.fireflyPlaidConnector2.config.AccountConfig | ||
import net.djvk.fireflyPlaidConnector2.config.properties.AccountConfigs | ||
import net.djvk.fireflyPlaidConnector2.lib.* | ||
import net.djvk.fireflyPlaidConnector2.transactions.TransactionConverter | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import org.mockito.kotlin.* | ||
|
||
internal class BatchSyncRunnerTest { | ||
companion object { | ||
fun createRunner( | ||
plaid: PlaidMock, | ||
firefly: FireflyMock, | ||
syncDays: Int = 200, | ||
setInitialBalance: Boolean = false, | ||
plaidBatchSize: Int = 100, | ||
syncHelper: SyncHelper? = null, | ||
converter: TransactionConverter? = null, | ||
): BatchSyncRunner { | ||
val defaultSyncHelper = SyncHelper( | ||
plaidAccountsConfig = AccountConfigs(listOf(AccountConfig(1, "account1Token", "plaidAccount1"))), | ||
fireflyAccessToken = "testToken", | ||
fireflyAboutApi = firefly.aboutApi, | ||
fireflyTxApi = firefly.transactionsApi, | ||
fireflyAccountsApi = firefly.accountsApi, | ||
) | ||
val defaultTransactionConverter = TransactionConverter( | ||
useNameForDestination = false, | ||
timeZoneString = "America/New_York", | ||
transferMatchWindowDays = 5, | ||
enablePrimaryCategorization = true, | ||
primaryCategoryPrefix = "primary-", | ||
enableDetailedCategorization = true, | ||
detailedCategoryPrefix = "detailed-", | ||
) | ||
|
||
return BatchSyncRunner( | ||
syncDays, | ||
setInitialBalance, | ||
plaidBatchSize, | ||
plaid.wrapper, | ||
syncHelper ?: defaultSyncHelper, | ||
firefly.accountsApi, | ||
converter ?: defaultTransactionConverter, | ||
) | ||
} | ||
|
||
@JvmStatic | ||
fun provideDaysOfHistoryCases(): List<Arguments> { | ||
return listOf( | ||
Arguments.of("minimum of 180 (1 -> 180)", 1, 180), | ||
Arguments.of("maximum of 730 (730 -> 730)", 730, 730), | ||
Arguments.of("maximum of 730 (999 -> 730)", 9999, 730), | ||
Arguments.of("syncDays plus one (180 -> 181)", 180, 181), | ||
Arguments.of("syncDays plus one (500 -> 501)", 500, 501), | ||
) | ||
} | ||
} | ||
|
||
@ParameterizedTest(name = "{index} => {0}") | ||
@MethodSource("provideDaysOfHistoryCases") | ||
fun runRequestsExpectedDaysOfHistory( | ||
testName: String, | ||
configuredSyncDays: Int, | ||
expectedDaysRequested: Int, | ||
) { | ||
val firefly = FireflyMock() | ||
val plaid = PlaidMock() | ||
|
||
val runner = createRunner(plaid, firefly, syncDays = configuredSyncDays) | ||
val response = TransactionsGetResponse(listOf(), listOf(), 0, PlaidFixtures.getItem(), "requestId1") | ||
|
||
plaid.api.stub { | ||
onBlocking { transactionsGet(any()) } doAnswer { createPlaidResponse(response) } | ||
} | ||
|
||
runBlocking { | ||
runner.run() | ||
} | ||
|
||
verifyBlocking(plaid.api) { | ||
transactionsGet(check { actual -> | ||
assertThat(actual).extracting { it.options } | ||
.isNotNull() | ||
assertThat(actual.options!!).extracting { it.daysRequested }.isEqualTo(expectedDaysRequested) | ||
}) | ||
} | ||
} | ||
} |