Skip to content

Commit

Permalink
Revert "[#3569] Temporary Remove DataSource Test cases (#3570)"
Browse files Browse the repository at this point in the history
This reverts commit dfc0a2a.
  • Loading branch information
mustafaozhan committed Jun 30, 2024
1 parent dd9da8d commit f6aceed
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client/datasource/currency/client-datasource-currency.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
libs.plugins.apply {
alias(kotlinMultiplatform)
alias(androidLibrary)
alias(ksp)
}
}

Expand All @@ -28,12 +29,21 @@ kotlin {
commonTest.dependencies {
libs.common.apply {
implementation(test)
implementation(mockative)
implementation(coroutinesTest)
}
}
}
}

dependencies {
configurations
.filter { it.name.startsWith("ksp") && it.name.contains("Test") }
.forEach {
add(it.name, libs.processors.mockative)
}
}

android {
ProjectSettings.apply {
namespace = Modules.Client.DataSource.currency.packageName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.oztechan.ccc.client.datasource.currency

import co.touchlab.kermit.CommonWriter
import co.touchlab.kermit.Logger
import com.oztechan.ccc.common.core.database.mapper.toLong
import com.oztechan.ccc.common.core.database.sql.Currency
import com.oztechan.ccc.common.core.database.sql.CurrencyQueries
import com.squareup.sqldelight.Query
import com.squareup.sqldelight.db.SqlCursor
import com.squareup.sqldelight.db.SqlDriver
import io.mockative.Mock
import io.mockative.classOf
import io.mockative.configure
import io.mockative.every
import io.mockative.mock
import io.mockative.verify
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlin.random.Random
import kotlin.test.BeforeTest
import kotlin.test.Test

internal class CurrencyDataSourceTest {

private val subject: CurrencyDataSource by lazy {
@Suppress("OPT_IN_USAGE")
CurrencyDataSourceImpl(currencyQueries, UnconfinedTestDispatcher())
}

@Mock
private val currencyQueries =
configure(mock(classOf<CurrencyQueries>())) { stubsUnitByDefault = true }

@Mock
private val sqlDriver = mock(classOf<SqlDriver>())

@Mock
private val sqlCursor = configure(mock(classOf<SqlCursor>())) { stubsUnitByDefault = true }

private val currency = Currency("EUR", "", "", 0.0, 0L)
private val query = Query(-1, mutableListOf(), sqlDriver, query = "") {
currency
}

@BeforeTest
fun setup() {
Logger.setLogWriters(CommonWriter())

every { sqlDriver.executeQuery(-1, "", 0, null) }
.returns(sqlCursor)

every { sqlCursor.next() }
.returns(false)
}

@Test
fun getCurrenciesFlow() {
every { currencyQueries.getCurrencies() }
.returns(query)

runTest {
subject.getCurrenciesFlow()
}

verify { currencyQueries.getCurrencies() }
.wasInvoked()
}

@Test
fun getActiveCurrenciesFlow() {
every { currencyQueries.getActiveCurrencies() }
.returns(query)

runTest {
subject.getActiveCurrenciesFlow()
}

verify { currencyQueries.getActiveCurrencies() }
.wasInvoked()
}

@Test
fun getActiveCurrencies() {
every { currencyQueries.getActiveCurrencies() }
.returns(query)

runTest {
subject.getActiveCurrencies()
}

verify { currencyQueries.getActiveCurrencies() }
.wasInvoked()
}

@Test
fun updateCurrencyStateByCode() {
val mockCode = "mock"
val mockState = Random.nextBoolean()

runTest {
subject.updateCurrencyStateByCode(mockCode, mockState)
}

verify { currencyQueries.updateCurrencyStateByCode(mockState.toLong(), mockCode) }
.wasInvoked()
}

@Test
fun updateCurrencyStates() {
val mockState = Random.nextBoolean()

runTest {
subject.updateCurrencyStates(mockState)
}

verify { currencyQueries.updateCurrencyStates(mockState.toLong()) }
.wasInvoked()
}

@Test
fun getCurrencyByCode() {
every { currencyQueries.getCurrencyByCode(currency.code) }
.returns(query)

runTest {
subject.getCurrencyByCode(currency.code)
}

verify { currencyQueries.getCurrencyByCode(currency.code) }
.wasInvoked()
}
}
10 changes: 10 additions & 0 deletions client/datasource/watcher/client-datasource-watcher.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
libs.plugins.apply {
alias(kotlinMultiplatform)
alias(androidLibrary)
alias(ksp)
}
}

Expand All @@ -28,12 +29,21 @@ kotlin {
commonTest.dependencies {
libs.common.apply {
implementation(test)
implementation(mockative)
implementation(coroutinesTest)
}
}
}
}

dependencies {
configurations
.filter { it.name.startsWith("ksp") && it.name.contains("Test") }
.forEach {
add(it.name, libs.processors.mockative)
}
}

android {
ProjectSettings.apply {
namespace = Modules.Client.DataSource.watcher.packageName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.oztechan.ccc.client.datasource.watcher

import co.touchlab.kermit.CommonWriter
import co.touchlab.kermit.Logger
import com.oztechan.ccc.common.core.database.mapper.toLong
import com.oztechan.ccc.common.core.database.sql.Watcher
import com.oztechan.ccc.common.core.database.sql.WatcherQueries
import com.squareup.sqldelight.Query
import com.squareup.sqldelight.db.SqlCursor
import com.squareup.sqldelight.db.SqlDriver
import io.mockative.Mock
import io.mockative.classOf
import io.mockative.configure
import io.mockative.every
import io.mockative.mock
import io.mockative.verify
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlin.random.Random
import kotlin.test.BeforeTest
import kotlin.test.Test

internal class WatcherDataSourceTest {

private val subject: WatcherDataSource by lazy {
@Suppress("OPT_IN_USAGE")
WatcherDataSourceImpl(watcherQueries, UnconfinedTestDispatcher())
}

@Mock
private val watcherQueries =
configure(mock(classOf<WatcherQueries>())) { stubsUnitByDefault = true }

@Mock
private val sqlDriver = mock(classOf<SqlDriver>())

@Mock
private val sqlCursor = configure(mock(classOf<SqlCursor>())) { stubsUnitByDefault = true }

private val base = "EUR"
private val target = "USD"
private val id = 12L

private val query = Query(-1, mutableListOf(), sqlDriver, query = "") {
Watcher(id, base, target, 1L, 0.0)
}

@BeforeTest
fun setup() {
Logger.setLogWriters(CommonWriter())

every { sqlDriver.executeQuery(-1, "", 0, null) }
.returns(sqlCursor)

every { sqlCursor.next() }
.returns(false)
}

@Test
fun getWatchersFlow() = runTest {
every { watcherQueries.getWatchers() }
.returns(query)

subject.getWatchersFlow()

verify { watcherQueries.getWatchers() }
.wasInvoked()
}

@Test
fun addWatcher() = runTest {
subject.addWatcher(base, target)

verify { watcherQueries.addWatcher(base, target) }
.wasInvoked()
}

@Test
fun getWatchers() = runTest {
every { watcherQueries.getWatchers() }
.returns(query)

subject.getWatchers()

verify { watcherQueries.getWatchers() }
.wasInvoked()
}

@Test
fun deleteWatcher() = runTest {
subject.deleteWatcher(id)

verify { watcherQueries.deleteWatcher(id) }
.wasInvoked()
}

@Test
fun updateWatcherBaseById() = runTest {
subject.updateWatcherBaseById(base, id)

verify { watcherQueries.updateWatcherBaseById(base, id) }
.wasInvoked()
}

@Test
fun updateWatcherTargetById() = runTest {
subject.updateWatcherTargetById(target, id)

verify { watcherQueries.updateWatcherTargetById(target, id) }
.wasInvoked()
}

@Test
fun updateWatcherRelationById() = runTest {
val relation = Random.nextBoolean()
subject.updateWatcherRelationById(relation, id)

verify { watcherQueries.updateWatcherRelationById(relation.toLong(), id) }
.wasInvoked()
}

@Test
fun updateWatcherRateById() = runTest {
val rate = 1.2

subject.updateWatcherRateById(rate, id)

verify { watcherQueries.updateWatcherRateById(rate, id) }
.wasInvoked()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
libs.plugins.apply {
alias(kotlinMultiplatform)
alias(androidLibrary)
alias(ksp)
}
}

Expand Down Expand Up @@ -30,12 +31,21 @@ kotlin {
commonTest.dependencies {
libs.common.apply {
implementation(test)
implementation(mockative)
implementation(coroutinesTest)
}
}
}
}

dependencies {
configurations
.filter { it.name.startsWith("ksp") && it.name.contains("Test") }
.forEach {
add(it.name, libs.processors.mockative)
}
}

android {
ProjectSettings.apply {
namespace = Modules.Common.DataSource.conversion.packageName
Expand Down
Loading

0 comments on commit f6aceed

Please sign in to comment.