Skip to content

Commit

Permalink
Merge pull request Kaaveh#184 from Kaaveh/feature/migrate_data_layer_…
Browse files Browse the repository at this point in the history
…to_kotest

Migrate Junit tests to kotest
  • Loading branch information
Kaaveh authored Nov 15, 2023
2 parents e18d16a + 1a592a9 commit 75dc73f
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 374 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ For the detail of handling preview of composable functions in this code-base, pl
- [x] GSON → Kotlinx Serialization
- [x] ROOM → SQLDelight
- [ ] Retrofit → Ktor
- [ ] JUnit → Kotest
- [x] JUnit → Kotest
- [ ] Dagger-Hilt → Koin
- [ ] Jetpack Compose → Compose Multiplatform

Expand Down
2 changes: 0 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ dependencies {
implementation(androidx.ktx)
implementation(hilt.work)
implementation(lifecycle.runtime.ktx)
testImplementation(junit)
androidTestImplementation(junit.ext)
implementation(work.runtime.ktx)
implementation(libs.hilt.navigation.compose)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ internal fun Project.configureKotlinAndroid(
resources {
resources.excludes.add("META-INF/*")
resources.merges.add("META-INF/*.version")

// https://github.com/Kotlin/kotlinx.coroutines/issues/2023#issuecomment-858644393
resources.pickFirsts.add("win32-x86-64/attach_hotspot_windows.dll")
resources.pickFirsts.add("win32-x86/attach_hotspot_windows.dll")
resources.excludes.add("META-INF/licenses/ASM")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/extensions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
}

dependencies {
libs.apply {
implementation(junit)
projects.apply {
testImplementation(core.test)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package ir.composenews.extensions

import org.junit.Assert
import org.junit.Test
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.equals.shouldBeEqual
import java.math.BigDecimal

class RoundToTwoDecimalPlacesTest {
@Test
fun testRoundToTwoDecimalPlaces() {
// Define a map of test cases with input numbers and their expected rounded results
class RoundToTwoDecimalPlacesTest : StringSpec({
"Round to two decimal places" {
val testCases = mapOf(
0.12345 to "0.12",
1.0 to "1.00",
Expand All @@ -16,10 +14,9 @@ class RoundToTwoDecimalPlacesTest {
BigDecimal("1234.56789") to "1234.57",
)

// Iterate through the test cases and assert the expected results
testCases.forEach { (input, expected) ->
val result = input.roundToTwoDecimalPlaces()
Assert.assertEquals(expected, result)
result shouldBeEqual expected
}
}
}
})
3 changes: 2 additions & 1 deletion core/sync/src/main/java/ir/composenews/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ private fun Context.syncWorkNotification(): Notification {
notificationManager?.createNotificationChannel(channel)
}

// TODO
return NotificationCompat
.Builder(this, SyncNotificationChannelID)
.setSmallIcon(androidx.hilt.work.R.drawable.notification_action_background)
// .setSmallIcon(androidx.hilt.work.R.drawable.notification_action_background)
.setContentTitle("Background tasks for Compose News")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
Expand Down
3 changes: 1 addition & 2 deletions core/test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ android {

dependencies {
libs.apply {
api(junit)
api(junit.ext)
api(bundles.kotest)
api(coroutines.test)
api(mockk)
}
Expand Down
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)
}
}

This file was deleted.

31 changes: 31 additions & 0 deletions core/test/src/main/java/ir/composenews/core_test/SuspendSpec.kt
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 core/test/src/main/java/ir/composenews/core_test/TestObserver.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
package ir.composenews.core_test.dispatcher

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.UnconfinedTestDispatcher

class TestDispatcherProvider(testScheduler: TestCoroutineScheduler) : DispatcherProvider {
override val ui: CoroutineDispatcher = StandardTestDispatcher(testScheduler)
override val io: CoroutineDispatcher = StandardTestDispatcher(testScheduler)
override val bg: CoroutineDispatcher = StandardTestDispatcher(testScheduler)
override val ui: CoroutineDispatcher = UnconfinedTestDispatcher(testScheduler)
override val io: CoroutineDispatcher = UnconfinedTestDispatcher(testScheduler)
override val bg: CoroutineDispatcher = UnconfinedTestDispatcher(testScheduler)
}
6 changes: 3 additions & 3 deletions data/market-local/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ dependencies {
implementation(sqldelight.android)
implementation(sqldelight.coroutines)
testImplementation(sqldelight.test)
testImplementation(junit)
testImplementation(junit.ext)
testImplementation(coroutines.test)
testImplementation(runner)
}
projects.apply {
testImplementation(core.test)
}
}
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
}
}
})
5 changes: 1 addition & 4 deletions data/market-repository/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ dependencies {
implementation(data.marketLocal)
}
libs.apply {
testImplementation(junit)
testImplementation(bundles.kotest)
androidTestImplementation(junit.ext)
androidTestImplementation(coroutines.test)
androidTestImplementation(runner)
testImplementation(sqldelight.test)
}
}

This file was deleted.

Loading

0 comments on commit 75dc73f

Please sign in to comment.