Skip to content

Commit

Permalink
Integration test wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Marchuk committed Nov 23, 2023
1 parent 4ab9f27 commit fd5e335
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ private val log = KotlinLogging.logger {}

@Single
class PeriodicFetcherFactory(private val scope: CoroutineScope) {
fun <T> create(name: String, fetch: suspend () -> T) = PeriodicFetcher(name, 1.hours, fetch)
fun <T> create(name: String, fetch: suspend () -> T) = PeriodicFetcher(name, 1.hours, scope, fetch)

inner class PeriodicFetcher<T>(
class PeriodicFetcher<T>(
name: String,
interval: Duration,
scope: CoroutineScope,
fetch: suspend () -> T
) {
private val initial = CompletableDeferred<T>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class LunchmoneyCategoryService(
return BudgetedCategory(categoryName, budget)
}

private suspend fun getCategoryBudget(categoryIdLong: Long): BudgetedCategory.CategoryBudget? =
fetchCurrentBudget(categoryIdLong)
?.data?.values?.firstOrNull { it.isAutomated != true }
?.let {
val budgeted = it.budgetToBase ?: return@let null
val spending = it.spendingToBase ?: return@let null
val currency = it.budgetCurrency ?: return@let null
if (budgeted <= 0) return@let null

toCategoryBudget(budgeted, spending, currency)
}

private suspend fun fetchCurrentBudget(categoryId: Long): LunchmoneyBudget? {
val now = LocalDate.now()
val startOfMonth = now.withDayOfMonth(1)
Expand All @@ -54,18 +66,6 @@ class LunchmoneyCategoryService(
return budgets.firstOrNull { it.categoryId != null && it.categoryId == categoryId }
}

private suspend fun getCategoryBudget(categoryIdLong: Long): BudgetedCategory.CategoryBudget? =
fetchCurrentBudget(categoryIdLong)
?.data?.values?.firstOrNull { it.isAutomated != true }
?.let {
val budgeted = it.budgetToBase ?: return@let null
val spending = it.spendingToBase ?: return@let null
val currency = it.budgetCurrency ?: return@let null
if (budgeted <= 0) return@let null

toCategoryBudget(budgeted, spending, currency)
}

private fun toCategoryBudget(budget: Double, spending: Double, currency: Currency) =
BudgetedCategory.CategoryBudget(
Amount.fromLunchmoneyAmount(budget - spending, currency),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ import io.github.smaugfm.monobudget.common.model.settings.OtherAccountSettings
import io.github.smaugfm.monobudget.common.util.injectAll
import io.github.smaugfm.monobudget.common.util.injectAllMap
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.serialization.ExperimentalSerializationApi
import org.koin.core.annotation.Single
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import reactor.core.publisher.Flux

private val log = KotlinLogging.logger { }

@Single(createdAtStart = true)
@OptIn(ExperimentalSerializationApi::class)
class MonoAccountsService(
fetcherFactory: PeriodicFetcherFactory,
private val settings: MultipleAccountSettings,
) : BankAccountService(), KoinComponent {
class MonoAccountsService : BankAccountService(), KoinComponent {

private val fetcherFactory: PeriodicFetcherFactory by inject()
private val settings: MultipleAccountSettings by inject()
private val otherAccounts by injectAllMap<OtherAccountSettings, Account> {
Account(it.accountId, it.alias, it.currency)
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/kotlin/io/github/smaugfm/monobudget/IntegrationTest.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.github.smaugfm.monobudget

import io.github.smaugfm.lunchmoney.api.LunchmoneyApi
import io.github.smaugfm.lunchmoney.model.LunchmoneyCategoryMultiple
import io.github.smaugfm.monobudget.common.misc.PeriodicFetcherFactory
import io.github.smaugfm.monobudget.common.model.BudgetBackend
import io.github.smaugfm.monobudget.common.model.settings.Settings
import io.github.smaugfm.monobudget.common.telegram.TelegramApi
import io.github.smaugfm.monobudget.common.verify.BudgetSettingsVerifier
import io.github.smaugfm.monobudget.mono.MonoApi
import io.github.smaugfm.monobudget.mono.MonoWebhookSettings
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockkClass
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.koin.core.KoinApplication
import org.koin.dsl.module
Expand All @@ -20,6 +24,7 @@ import java.nio.file.Paths
class IntegrationTest : TestBase(), CoroutineScope {
private val lunchmoneyMock = mockkClass(LunchmoneyApi::class)
private val tgMock = mockkClass(TelegramApi::class)
private val periodicFetcherFactory = mockkClass(PeriodicFetcherFactory::class)

override fun KoinApplication.testKoinApplication() {
setupKoinModules(
Expand All @@ -42,6 +47,7 @@ class IntegrationTest : TestBase(), CoroutineScope {
coEvery { it.verify() } returns Unit
}
}
single { periodicFetcherFactory }
}
)
}
Expand All @@ -51,6 +57,12 @@ class IntegrationTest : TestBase(), CoroutineScope {
@Test
fun contextLoads() {
val budgetBackend = getKoin().get<BudgetBackend>()
every {
periodicFetcherFactory.create(
"Lunchmoney categories",
any<suspend () -> List<LunchmoneyCategoryMultiple>>()
)
} returns mockkClass(PeriodicFetcherFactory.PeriodicFetcher::class)

println(budgetBackend)
// runBlocking(coroutineContext) {
Expand Down

0 comments on commit fd5e335

Please sign in to comment.