Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin 1.6.10, coroutines 1.6 #86

Merged
merged 11 commits into from
Jan 18, 2022
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ dependencies {
androidTestImplementation(deps.test.androidXSspresso)

addUnitTest()
testImplementation(testUtils)
testImplementation(deps.koin.testJunit4)
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/hoc/flowmvi/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class App : Application() {
startKoin {
androidContext(this@App)

androidLogger(if (BuildConfig.DEBUG) Level.DEBUG else Level.NONE)
// TODO(koin): https://github.com/InsertKoinIO/koin/issues/1188
androidLogger(if (BuildConfig.DEBUG) Level.ERROR else Level.NONE)

modules(allModules)
}
Expand Down
9 changes: 6 additions & 3 deletions app/src/test/java/com/hoc/flowmvi/CheckModulesTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hoc.flowmvi

import androidx.lifecycle.SavedStateHandle
import com.hoc.flowmvi.test_utils.TestCoroutineDispatcherRule
import io.mockk.every
import io.mockk.mockkClass
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -11,8 +12,6 @@ import org.koin.dsl.koinApplication
import org.koin.test.AutoCloseKoinTest
import org.koin.test.check.checkModules
import org.koin.test.mock.MockProviderRule
import kotlin.test.Test
import kotlin.time.ExperimentalTime

hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
@ExperimentalStdlibApi
@FlowPreview
Expand All @@ -27,12 +26,16 @@ class CheckModulesTest : AutoCloseKoinTest() {
}
}
}
@get:Rule
val coroutineRule = TestCoroutineDispatcherRule()

@Test
fun verifyKoinApp() {
koinApplication {
modules(allModules)
printLogger(Level.DEBUG)

// TODO(koin): https://github.com/InsertKoinIO/koin/issues/1188
printLogger(Level.ERROR)

checkModules {
withInstance<SavedStateHandle>()
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.gradle.plugin.use.PluginDependenciesSpec
import org.gradle.plugin.use.PluginDependencySpec

const val ktlintVersion = "0.43.2"
const val kotlinVersion = "1.5.31"
const val kotlinVersion = "1.6.10"

object appConfig {
const val applicationId = "com.hoc.flowmvi"
Expand Down Expand Up @@ -52,7 +52,7 @@ object deps {
}

object coroutines {
private const val version = "1.5.2"
private const val version = "1.6.0"

const val core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version"
const val android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import java.io.IOException
import kotlin.time.Duration
hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
import kotlin.time.ExperimentalTime
import arrow.core.Either.Companion.catch as catchEither
import kotlin.time.Duration.Companion.milliseconds
hoc081098 marked this conversation as resolved.
Show resolved Hide resolved

@ExperimentalTime
@ExperimentalCoroutinesApi
Expand Down Expand Up @@ -69,7 +70,7 @@ internal class UserRepositoryImpl(
return withContext(dispatchers.io) {
retrySuspend(
times = 3,
initialDelay = Duration.milliseconds(500),
initialDelay = 500.milliseconds,
factor = 2.0,
shouldRetry = { it is IOException }
) { times ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import android.util.Log
import com.hoc.flowmvi.core.dispatchers.CoroutineDispatchers
import com.hoc.flowmvi.domain.repository.UserRepository
import com.hoc.flowmvi.test_utils.getOrThrow
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.test.Test
import kotlin.test.assertTrue
import kotlin.time.ExperimentalTime
hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
Expand All @@ -13,24 +18,21 @@ import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import org.koin.core.logger.Level
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.KoinTestRule
import org.koin.test.inject
import timber.log.Timber
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.test.Test
import kotlin.test.assertTrue
import kotlin.time.ExperimentalTime

hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
@ExperimentalCoroutinesApi
@ExperimentalTime
@ExperimentalStdlibApi
class UserRepositoryImplRealAPITest : KoinTest {
@get:Rule
val koinRuleTest = KoinTestRule.create {
printLogger()
// TODO(koin): https://github.com/InsertKoinIO/koin/issues/1188
printLogger(Level.ERROR)
modules(
dataModule,
module {
Expand Down
37 changes: 18 additions & 19 deletions data/src/test/java/com/hoc/flowmvi/data/UserRepositoryImplTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import io.mockk.verifySequence
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Rule
import java.io.IOException
hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
Expand All @@ -41,6 +34,13 @@ import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.time.ExperimentalTime
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.Rule
hoc081098 marked this conversation as resolved.
Show resolved Hide resolved

private val USER_BODY = UserBody(
email = "[email protected]",
Expand Down Expand Up @@ -103,7 +103,6 @@ private val VALID_NEL_USERS = USERS.map(User::validNel)
class UserRepositoryImplTest {
@get:Rule
val coroutineRule = TestCoroutineDispatcherRule()
private val testDispatcher get() = coroutineRule.testCoroutineDispatcher

private lateinit var repo: UserRepositoryImpl
private lateinit var userApiService: UserApiService
Expand Down Expand Up @@ -139,7 +138,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_refresh_withApiCallSuccess_returnsRight() = testDispatcher.runBlockingTest {
fun test_refresh_withApiCallSuccess_returnsRight() = runTest {
coEvery { userApiService.getUsers() } returns USER_RESPONSES
every { responseToDomain(any()) } returnsMany VALID_NEL_USERS

Expand All @@ -157,7 +156,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_refresh_withApiCallError_returnsLeft() = testDispatcher.runBlockingTest {
fun test_refresh_withApiCallError_returnsLeft() = runTest {
val ioException = IOException()
coEvery { userApiService.getUsers() } throws ioException
every { errorMapper(ofType<IOException>()) } returns UserError.NetworkError
Expand All @@ -171,7 +170,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_remove_withApiCallSuccess_returnsRight() = testDispatcher.runBlockingTest {
fun test_remove_withApiCallSuccess_returnsRight() = runTest {
val user = USERS[0]
val userResponse = USER_RESPONSES[0]

Expand All @@ -188,7 +187,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_remove_withApiCallError_returnsLeft() = testDispatcher.runBlockingTest {
fun test_remove_withApiCallError_returnsLeft() = runTest {
val user = USERS[0]
coEvery { userApiService.remove(user.id) } throws IOException()
every { errorMapper(ofType<IOException>()) } returns UserError.NetworkError
Expand All @@ -202,7 +201,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_add_withApiCallSuccess_returnsRight() = testDispatcher.runBlockingTest {
fun test_add_withApiCallSuccess_returnsRight() = runTest {
val user = USERS[0]
val userResponse = USER_RESPONSES[0]

Expand All @@ -221,7 +220,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_add_withApiCallError_returnsLeft() = testDispatcher.runBlockingTest {
fun test_add_withApiCallError_returnsLeft() = runTest {
val user = USERS[0]
coEvery { userApiService.add(USER_BODY) } throws IOException()
every { domainToBody(user) } returns USER_BODY
Expand All @@ -238,7 +237,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_search_withApiCallSuccess_returnsRight() = testDispatcher.runBlockingTest {
fun test_search_withApiCallSuccess_returnsRight() = runTest {
val q = "hoc081098"
coEvery { userApiService.search(q) } returns USER_RESPONSES
every { responseToDomain(any()) } returnsMany VALID_NEL_USERS
Expand All @@ -258,7 +257,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_search_withApiCallError_returnsLeft() = testDispatcher.runBlockingTest {
fun test_search_withApiCallError_returnsLeft() = runTest {
val q = "hoc081098"
coEvery { userApiService.search(q) } throws IOException()
every { errorMapper(ofType<IOException>()) } returns UserError.NetworkError
Expand All @@ -273,7 +272,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_getUsers_withApiCallSuccess_emitsInitial() = testDispatcher.runBlockingTest {
fun test_getUsers_withApiCallSuccess_emitsInitial() = runTest {
coEvery { userApiService.getUsers() } returns USER_RESPONSES
every { responseToDomain(any()) } returnsMany VALID_NEL_USERS

Expand All @@ -299,7 +298,7 @@ class UserRepositoryImplTest {
}

@Test
fun test_getUsers_withApiCallError_rethrows() = testDispatcher.runBlockingTest {
fun test_getUsers_withApiCallError_rethrows() = runTest {
coEvery { userApiService.getUsers() } throws IOException()
every { errorMapper(ofType<IOException>()) } returns UserError.NetworkError

Expand All @@ -322,7 +321,7 @@ class UserRepositoryImplTest {

@Test
fun test_getUsers_withApiCallSuccess_emitsInitialAndUpdatedUsers() =
testDispatcher.runBlockingTest {
runTest {
val user = USERS.last()
val userResponse = USER_RESPONSES.last()
coEvery { userApiService.getUsers() } returns USER_RESPONSES.dropLast(1)
Expand Down
31 changes: 15 additions & 16 deletions domain/src/test/java/com/hoc/flowmvi/domain/UseCaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import io.mockk.confirmVerified
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Rule
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Rule

hoc081098 marked this conversation as resolved.
Show resolved Hide resolved
private val USERS = listOf(
User.create(
Expand Down Expand Up @@ -57,7 +57,6 @@ private val USERS = listOf(
class UseCaseTest {
@get:Rule
val coroutineRule = TestCoroutineDispatcherRule()
private val testDispatcher get() = coroutineRule.testCoroutineDispatcher

private lateinit var userRepository: UserRepository
private lateinit var getUsersUseCase: GetUsersUseCase
Expand Down Expand Up @@ -86,7 +85,7 @@ class UseCaseTest {
}

@Test
fun test_getUsersUseCase_whenSuccess_emitsUsers() = testDispatcher.runBlockingTest {
fun test_getUsersUseCase_whenSuccess_emitsUsers() = runTest {
val usersRight = USERS.right()
every { userRepository.getUsers() } returns flowOf(usersRight)

Expand All @@ -97,7 +96,7 @@ class UseCaseTest {
}

@Test
fun test_getUsersUseCase_whenError_throwsError() = testDispatcher.runBlockingTest {
fun test_getUsersUseCase_whenError_throwsError() = runTest {
every { userRepository.getUsers() } returns flowOf(errorLeft)

val result = getUsersUseCase()
Expand All @@ -107,7 +106,7 @@ class UseCaseTest {
}

@Test
fun test_refreshUseCase_whenSuccess_returnsUnit() = testDispatcher.runBlockingTest {
fun test_refreshUseCase_whenSuccess_returnsUnit() = runTest {
coEvery { userRepository.refresh() } returns Unit.right()

val result = refreshUseCase()
Expand All @@ -117,7 +116,7 @@ class UseCaseTest {
}

@Test
fun test_refreshUseCase_whenError_throwsError() = testDispatcher.runBlockingTest {
fun test_refreshUseCase_whenError_throwsError() = runTest {
coEvery { userRepository.refresh() } returns errorLeft

val result = refreshUseCase()
Expand All @@ -127,7 +126,7 @@ class UseCaseTest {
}

@Test
fun test_removeUserUseCase_whenSuccess_returnsUnit() = testDispatcher.runBlockingTest {
fun test_removeUserUseCase_whenSuccess_returnsUnit() = runTest {
coEvery { userRepository.remove(any()) } returns Unit.right()

val result = removeUserUseCase(USERS[0])
Expand All @@ -137,7 +136,7 @@ class UseCaseTest {
}

@Test
fun test_removeUserUseCase_whenError_throwsError() = testDispatcher.runBlockingTest {
fun test_removeUserUseCase_whenError_throwsError() = runTest {
coEvery { userRepository.remove(any()) } returns errorLeft

val result = removeUserUseCase(USERS[0])
Expand All @@ -147,7 +146,7 @@ class UseCaseTest {
}

@Test
fun test_addUserUseCase_whenSuccess_returnsUnit() = testDispatcher.runBlockingTest {
fun test_addUserUseCase_whenSuccess_returnsUnit() = runTest {
coEvery { userRepository.add(any()) } returns Unit.right()

val result = addUserUseCase(USERS[0])
Expand All @@ -157,7 +156,7 @@ class UseCaseTest {
}

@Test
fun test_addUserUseCase_whenError_throwsError() = testDispatcher.runBlockingTest {
fun test_addUserUseCase_whenError_throwsError() = runTest {
coEvery { userRepository.add(any()) } returns errorLeft

val result = addUserUseCase(USERS[0])
Expand All @@ -167,7 +166,7 @@ class UseCaseTest {
}

@Test
fun test_searchUsersUseCase_whenSuccess_returnsUsers() = testDispatcher.runBlockingTest {
fun test_searchUsersUseCase_whenSuccess_returnsUsers() = runTest {
coEvery { userRepository.search(any()) } returns USERS.right()

val query = "hoc081098"
Expand All @@ -178,7 +177,7 @@ class UseCaseTest {
}

@Test
fun test_searchUsersUseCase_whenError_throwsError() = testDispatcher.runBlockingTest {
fun test_searchUsersUseCase_whenError_throwsError() = runTest {
coEvery { userRepository.search(any()) } returns errorLeft

val query = "hoc081098"
Expand Down
Loading