Skip to content

Commit

Permalink
[#13] 모듈별로 테스트 트리거되도록 분리(common,data), 테스트 코드 추가 (#14)
Browse files Browse the repository at this point in the history
* [#13] 모듈별로 테스트 트리거되도록 분리(common,data), 테스트 코드 추가

* [#13] workflow 이름 수정

* [#13] data, common test 트리거 검증

* [#13] test 트리거 조건 조정

* [#13] job 이름 조정
  • Loading branch information
cgpathos authored May 8, 2024
1 parent 3a40c24 commit 4adf6ae
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 27 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/module_common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: module_common

on:
push:
branches:
- '**'
paths:
- "common/**"

concurrency:
group: ${{ github.workflow }}-${{ github.pull_request.number }}-${{ github.ref }}
cancel-in-progress: true

jobs:
common:
uses: ./.github/workflows/test.yml
with:
module: 'common'
secrets: inherit
19 changes: 19 additions & 0 deletions .github/workflows/module_data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: module_data

on:
push:
branches:
- '**'
paths:
- "data/**"

concurrency:
group: ${{ github.workflow }}-${{ github.pull_request.number }}-${{ github.ref }}
cancel-in-progress: true

jobs:
data:
uses: ./.github/workflows/test.yml
with:
module: 'data'
secrets: inherit
22 changes: 10 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
name: test

on:
push:
branches:
- '**'

concurrency:
group: ${{ github.workflow }}-${{ github.pull_request.number }}-${{ github.ref }}
cancel-in-progress: true
workflow_call:
inputs:
module:
required: false
type: string

jobs:
test:
unit_test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
- name: set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'adopt'
Expand All @@ -31,4 +29,4 @@ jobs:

- name: Run all tests
id: test
run: bundle exec fastlane test
run: bundle exec fastlane test module:"${{ inputs.module }}"
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ dependencies {

// kotlin
implementation(libs.bundles.kotlin)
testImplementation(libs.bundles.kotlin.test)

implementation(libs.core.ktx)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ sealed interface Result<out T> {

fun <T> Flow<T>.asResult(errorCallback: ((e: Throwable) -> Unit)? = null): Flow<Result<T>> =
map<T, Result<T>> { Result.Success(it) }
.onStart { Result.Loading }
.catch { Result.Error(it, errorCallback) }
.onStart { emit(Result.Loading) }
.catch { emit(Result.Error(it, errorCallback)) }

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package today.pathos.android.portfolio.common.result

import app.cash.turbine.test
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test

class ResultKtTest {

@Test
fun asResultError() = runTest {
flow {
emit(1)
throw Throwable("Some Error")
}.asResult()
.test {
assertEquals(Result.Loading, awaitItem())
assertEquals(Result.Success(1), awaitItem())

when (val error = awaitItem()) {
is Result.Error -> assertEquals(
"Some Error",
error.e.message
)

Result.Loading,
is Result.Success,
-> {
throw IllegalStateException("Flow should have emitted an Result.Error")
}
}

awaitComplete()
}
}
}
5 changes: 1 addition & 4 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ dependencies {
// hilt
implementation(libs.dagger.hilt.android)
ksp(libs.dagger.hilt.compiler)
// androidTestImplementation(libs.dagger.hilt.android.testing)
// kspAndroidTest(libs.dagger.hilt.android.compiler)
// testImplementation(libs.dagger.hilt.android.testing)
// kspTest(libs.dagger.hilt.android.compiler)

// kotlin
implementation(libs.bundles.kotlin)
testImplementation(libs.bundles.kotlin.test)
androidTestImplementation(libs.bundles.kotlin.test)

// api
implementation(libs.bundles.retrofit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package today.pathos.android.portfolio.data.datasource.local.db.dao

import app.cash.turbine.test
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import today.pathos.android.portfolio.data.datasource.local.db.rule.DatabaseRule
import today.pathos.android.portfolio.data.datasource.local.db.table.FameTbl

class FameDaoTest {
@get:Rule
val rule = DatabaseRule()

private val fameDao = rule.database.fameDao()

@Before
fun setUp() {
createTestData()
}

private fun createTestData() = runTest {
val testData = listOf(
FameTbl(
serverId = "SERVER_ID1",
characterId = "CHARACTER_ID",
characterName = "CHARACTER_NAME_1",
level = 99,
jobId = "JOB_ID",
jobGrowId = "JOB_GROW_ID",
jobName = "JOB_NAME",
jobGrowName = "JOB_GROW_NAME",
fame = 999,
),
FameTbl(
serverId = "SERVER_ID2",
characterId = "CHARACTER_ID",
characterName = "CHARACTER_NAME",
level = 99,
jobId = "JOB_ID",
jobGrowId = "JOB_GROW_ID",
jobName = "JOB_NAME",
jobGrowName = "JOB_GROW_NAME",
fame = 999,
),
)
fameDao.createFame(testData)
}

@Test
fun fameListCount() = runTest {
val fameListCount = fameDao.fameListCount()
val expectedCount = 2
assertEquals(expectedCount, fameListCount)
}

@Test
fun firstFameInFameList() = runTest {
val expectedFirstCharacterName = "CHARACTER_NAME_1"

fameDao.getFameList()
.test {
assertEquals(expectedFirstCharacterName, awaitItem().first().characterName)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package today.pathos.android.portfolio.data.datasource.local.db.rule

import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import today.pathos.android.portfolio.data.datasource.local.db.CacheDatabase

class DatabaseRule : TestWatcher() {
val database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
CacheDatabase::class.java
).allowMainThreadQueries()
.build()

override fun finished(description: Description?) {
super.finished(description)
database.close()
}
}
4 changes: 0 additions & 4 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ dependencies {
// hilt
implementation(libs.dagger.hilt.android)
ksp(libs.dagger.hilt.compiler)
androidTestImplementation(libs.dagger.hilt.android.testing)
kspAndroidTest(libs.dagger.hilt.android.compiler)
testImplementation(libs.dagger.hilt.android.testing)
kspTest(libs.dagger.hilt.android.compiler)

// kotlin
implementation(libs.bundles.kotlin)
Expand Down
5 changes: 3 additions & 2 deletions fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ default_platform(:android)

platform :android do
desc "Runs all the tests"
lane :test do
gradle(task: "test")
lane :test do |options|
MODULE = ":#{options[:module]}:" || ""
gradle(task: "#{MODULE}test")
end

desc "Submit a new Beta Build to Crashlytics Beta"
Expand Down
4 changes: 2 additions & 2 deletions fastlane/report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@



<testcase classname="fastlane.lanes" name="0: default_platform" time="0.000209">
<testcase classname="fastlane.lanes" name="0: default_platform" time="0.000196">

</testcase>


<testcase classname="fastlane.lanes" name="1: test" time="10.247009">
<testcase classname="fastlane.lanes" name="1: :data:test" time="14.400112">

</testcase>

Expand Down

0 comments on commit 4adf6ae

Please sign in to comment.