From 7924d4ea98f969dc82be8c56c9c7fbde4c003464 Mon Sep 17 00:00:00 2001 From: John O'Reilly Date: Sat, 1 Jan 2022 10:31:43 +0000 Subject: [PATCH] test updates --- .github/workflows/android.yml | 2 +- buildSrc/src/main/java/Dependencies.kt | 1 + common/build.gradle.kts | 6 ++- .../surrus/peopleinspace/PeopleInSpaceTest.kt | 18 -------- .../repository/PeopleInSpaceRepository.kt | 5 +-- .../surrus/peopleinspace/PeopleInSpaceTest.kt | 43 +++++++++++++++++++ .../peopleinspace/PeopleInSpaceTest.kt | 18 -------- 7 files changed, 51 insertions(+), 42 deletions(-) delete mode 100644 common/src/androidTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt create mode 100644 common/src/commonTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt delete mode 100644 common/src/iOSTest/kotlin/com.surrus/peopleinspace/PeopleInSpaceTest.kt diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index d968e0e..58d0cef 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -59,4 +59,4 @@ jobs: with: api-level: 29 target: google_apis - script: ./gradlew app:connectedAndroidTest common:connectedAndroidTest \ No newline at end of file + script: ./gradlew app:connectedAndroidTest \ No newline at end of file diff --git a/buildSrc/src/main/java/Dependencies.kt b/buildSrc/src/main/java/Dependencies.kt index a6f3df0..1dc81dc 100644 --- a/buildSrc/src/main/java/Dependencies.kt +++ b/buildSrc/src/main/java/Dependencies.kt @@ -59,6 +59,7 @@ object Deps { object Kotlinx { const val serializationCore = "org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.kotlinxSerialization}" const val coroutinesCore = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}" + const val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}" const val htmlJs = "org.jetbrains.kotlinx:kotlinx-html-js:${Versions.kotlinxHtmlJs}" } diff --git a/common/build.gradle.kts b/common/build.gradle.kts index 4efb4e4..5b7468f 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -71,7 +71,7 @@ kotlin { } with(Deps.Kotlinx) { - implementation(Deps.Kotlinx.coroutinesCore) + implementation(coroutinesCore) implementation(serializationCore) } @@ -90,6 +90,10 @@ kotlin { } } sourceSets["commonTest"].dependencies { + implementation(Deps.Koin.test) + implementation(Deps.Kotlinx.coroutinesTest) + implementation(kotlin("test-common")) + implementation(kotlin("test-annotations-common")) } sourceSets["androidMain"].dependencies { diff --git a/common/src/androidTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt b/common/src/androidTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt deleted file mode 100644 index ec938d1..0000000 --- a/common/src/androidTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.surrus.peopleinspace - -import com.surrus.common.di.initKoin -import com.surrus.common.remote.PeopleInSpaceApi -import kotlinx.coroutines.runBlocking -import org.junit.Assert.assertTrue -import org.junit.Test - -class PeopleInSpaceTest { - @Test - fun testGetPeople() = runBlocking { - val koin = initKoin(enableNetworkLogs = true).koin - val peopleInSpaceApi = koin.get() - val result = peopleInSpaceApi.fetchPeople() - println(result) - assertTrue(result.people.isNotEmpty()) - } -} diff --git a/common/src/commonMain/kotlin/com/surrus/common/repository/PeopleInSpaceRepository.kt b/common/src/commonMain/kotlin/com/surrus/common/repository/PeopleInSpaceRepository.kt index 7d3986e..cdec673 100644 --- a/common/src/commonMain/kotlin/com/surrus/common/repository/PeopleInSpaceRepository.kt +++ b/common/src/commonMain/kotlin/com/surrus/common/repository/PeopleInSpaceRepository.kt @@ -75,12 +75,9 @@ class PeopleInSpaceRepository : KoinComponent, PeopleInSpaceRepositoryInterface override suspend fun fetchPeople(): List = peopleInSpaceApi.fetchPeople().people override fun pollISSPosition(): Flow { - // The returned will be frozen in Kotlin Native. We can't freeze the Koin internals - // so we'll use local variables to prevent the Koin internals from freezing. - val api = peopleInSpaceApi return flow { while (true) { - val position = api.fetchISSPosition().iss_position + val position = peopleInSpaceApi.fetchISSPosition().iss_position emit(position) logger.d { position.toString() } delay(POLL_INTERVAL) diff --git a/common/src/commonTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt b/common/src/commonTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt new file mode 100644 index 0000000..2ec91fa --- /dev/null +++ b/common/src/commonTest/kotlin/com/surrus/peopleinspace/PeopleInSpaceTest.kt @@ -0,0 +1,43 @@ +package com.surrus.peopleinspace + +import com.surrus.common.di.PeopleInSpaceDatabaseWrapper +import com.surrus.common.di.commonModule +import com.surrus.common.repository.PeopleInSpaceRepositoryInterface +import com.surrus.common.repository.platformModule +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import org.koin.core.context.startKoin +import org.koin.dsl.module +import org.koin.test.KoinTest +import org.koin.test.inject +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertTrue + +class PeopleInSpaceTest: KoinTest { + private val repo : PeopleInSpaceRepositoryInterface by inject() + + @BeforeTest + fun setUp() { + Dispatchers.setMain(StandardTestDispatcher()) + + startKoin{ + modules( + commonModule(true), + platformModule(), + module { + single { PeopleInSpaceDatabaseWrapper(null) } + } + ) + } + } + + @Test + fun testGetPeople() = runTest { + val result = repo.fetchPeople() + println(result) + assertTrue(result.isNotEmpty()) + } +} diff --git a/common/src/iOSTest/kotlin/com.surrus/peopleinspace/PeopleInSpaceTest.kt b/common/src/iOSTest/kotlin/com.surrus/peopleinspace/PeopleInSpaceTest.kt deleted file mode 100644 index 14d5ca0..0000000 --- a/common/src/iOSTest/kotlin/com.surrus/peopleinspace/PeopleInSpaceTest.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.surrus.peopleinspace - -import com.surrus.common.di.initKoin -import com.surrus.common.repository.PeopleInSpaceRepositoryInterface -import kotlinx.coroutines.runBlocking -import kotlin.test.Test -import kotlin.test.assertTrue - -class PeopleInSpaceTest { - @Test - fun testGetPeople() = runBlocking { - val koin = initKoin(enableNetworkLogs = true).koin - val repo = koin.get() - val result = repo.fetchPeople() - println(result) - assertTrue(result.isNotEmpty()) - } -}