forked from Kaaveh/ComposeNews
-
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.
Merge pull request Kaaveh#184 from Kaaveh/feature/migrate_data_layer_…
…to_kotest Migrate Junit tests to kotest
- Loading branch information
Showing
25 changed files
with
310 additions
and
374 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
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
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
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
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
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
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
22 changes: 22 additions & 0 deletions
22
core/test/src/main/java/ir/composenews/core_test/MainCoroutineListener.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,22 @@ | ||
@file:Suppress("PackageNaming", "PackageName") | ||
|
||
package ir.composenews.core_test | ||
|
||
import io.kotest.core.listeners.TestListener | ||
import io.kotest.core.spec.Spec | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.test.TestDispatcher | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.setMain | ||
|
||
class MainCoroutineListener( | ||
private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(), | ||
) : TestListener { | ||
override suspend fun beforeSpec(spec: Spec) { | ||
Dispatchers.setMain(testDispatcher) | ||
} | ||
|
||
override suspend fun afterSpec(spec: Spec) { | ||
Dispatchers.setMain(testDispatcher) | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
core/test/src/main/java/ir/composenews/core_test/MainDispatcherRule.kt
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
core/test/src/main/java/ir/composenews/core_test/SuspendSpec.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,31 @@ | ||
@file:Suppress("PackageNaming", "PackageName") | ||
|
||
package ir.composenews.core_test | ||
|
||
import io.kotest.core.spec.style.FreeSpec | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancelAndJoin | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* A Kotest Spec that allows suspension in its initializer. This works great for the | ||
* `ProjectResource`, for initialising dependencies. | ||
*/ | ||
@Suppress("UnnecessaryAbstractClass") | ||
abstract class SuspendSpec(body: suspend FreeSpec.() -> Unit) : FreeSpec() { | ||
init { | ||
val scope = autoClose(CloseableCoroutineScope(Dispatchers.Unconfined)) | ||
scope.launch(Dispatchers.Unconfined) { body() } | ||
} | ||
} | ||
|
||
private class CloseableCoroutineScope(context: CoroutineContext) : CoroutineScope, AutoCloseable { | ||
private val job = Job() | ||
override val coroutineContext: CoroutineContext = context + job | ||
|
||
override fun close() = runBlocking { job.cancelAndJoin() } | ||
} |
49 changes: 0 additions & 49 deletions
49
core/test/src/main/java/ir/composenews/core_test/TestObserver.kt
This file was deleted.
Oops, something went wrong.
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
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
29 changes: 11 additions & 18 deletions
29
data/market-local/src/test/java/ir/composenews/localdatasource/database/MarketDaoTest.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 |
---|---|---|
@@ -1,38 +1,31 @@ | ||
package ir.composenews.localdatasource.database | ||
|
||
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver | ||
import io.kotest.matchers.collections.shouldContain | ||
import io.kotest.matchers.equals.shouldBeEqual | ||
import ir.composenews.core_test.SuspendSpec | ||
import ir.composenews.db.MarketDatabase | ||
import ir.composenews.localdatasource.test.favoriteMarketEntity | ||
import junit.framework.TestCase.assertTrue | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class MarketDaoTest { | ||
class MarketDaoTest : SuspendSpec({ | ||
lateinit var marketDao: MarketDao | ||
|
||
private lateinit var marketDao: MarketDao | ||
|
||
@Before | ||
fun createDb() { | ||
beforeSpec { | ||
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY) | ||
MarketDatabase.Schema.create(driver) | ||
val db = MarketDatabase(driver) | ||
marketDao = MarketDaoImpl(db) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun emptyTableAtDbInitialization() = runTest { | ||
"Empty table at db initialization" { | ||
val marketList = marketDao.getMarketList().first() | ||
assertTrue(marketList.isEmpty()) | ||
marketList.size shouldBeEqual 0 | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun insertNewsToDb() = runTest { | ||
"Insert market to db" { | ||
marketDao.insertMarket(favoriteMarketEntity) | ||
val marketList = marketDao.getMarketList().first() | ||
assertTrue(marketList.contains(favoriteMarketEntity)) | ||
marketList shouldContain favoriteMarketEntity | ||
} | ||
} | ||
}) |
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
68 changes: 0 additions & 68 deletions
68
...-repository/src/androidTest/java/ir/composenews/data/repository/NewsRepositoryImplTest.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.