Skip to content

Commit 35b9244

Browse files
committed
feat(network): Implement unit tests for TranslationRepository
1 parent e03cf5f commit 35b9244

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

gradle/libs.versions.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ retrofit = "2.11.0"
1616
okhttp = "4.12.0"
1717
moshi = "1.15.1"
1818
androidx_annotation = "1.8.0"
19+
mockk = "1.13.10"
20+
kotlinx_coroutines_test = "1.8.0"
21+
kotlin_test_junit = "1.9.22"
1922

2023
[libraries]
2124
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -38,6 +41,9 @@ okhttp-logging-interceptor = { group = "com.squareup.okhttp3", name = "logging-i
3841
moshi = { group = "com.squareup.moshi", name = "moshi", version.ref = "moshi" }
3942
moshi-kotlin = { group = "com.squareup.moshi", name = "moshi-kotlin", version.ref = "moshi" }
4043
androidx-annotation = { group = "androidx.annotation", name = "annotation", version.ref = "androidx_annotation" }
44+
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
45+
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "kotlinx_coroutines_test" }
46+
kotlin-test-junit = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit", version.ref = "kotlin_test_junit" }
4147

4248
[plugins]
4349
android-application = { id = "com.android.application", version.ref = "android_gradle_plugin" }

network/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ dependencies {
4242
implementation(libs.moshi.kotlin)
4343
implementation(libs.androidx.annotation)
4444
testImplementation(libs.junit)
45+
testImplementation(libs.mockk)
46+
testImplementation(libs.kotlinx.coroutines.test)
47+
testImplementation(libs.kotlin.test.junit)
4548
androidTestImplementation(libs.androidx.test.ext.junit)
4649
androidTestImplementation(libs.androidx.test.espresso.core)
4750
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.anysoftkeyboard.janus.network
2+
3+
import com.anysoftkeyboard.janus.database.Translation
4+
import com.anysoftkeyboard.janus.database.TranslationDao
5+
import io.mockk.coEvery
6+
import io.mockk.coVerify
7+
import io.mockk.mockk
8+
import kotlinx.coroutines.test.runTest
9+
import org.junit.Before
10+
import org.junit.Test
11+
import org.junit.Assert.assertEquals
12+
import org.junit.Assert.assertNull
13+
14+
class TranslationRepositoryTest {
15+
16+
private lateinit var wikipediaApi: WikipediaApi
17+
private lateinit var translationDao: TranslationDao
18+
private lateinit var repository: TranslationRepository
19+
20+
@Before
21+
fun setup() {
22+
wikipediaApi = mockk()
23+
translationDao = mockk()
24+
repository = TranslationRepository(wikipediaApi, translationDao)
25+
}
26+
27+
@Test
28+
fun `getTranslation_returnsCachedTranslationWhenAvailable`() = runTest {
29+
val sourceWord = "hello"
30+
val sourceLang = "en"
31+
val targetLang = "es"
32+
val cachedTranslation = Translation(
33+
id = 1,
34+
sourceWord = sourceWord,
35+
sourceLangCode = sourceLang,
36+
sourceArticleUrl = "url_cached",
37+
sourceShortDescription = "desc_cached",
38+
sourceSummary = null,
39+
translatedWord = "hola",
40+
targetLangCode = targetLang,
41+
targetArticleUrl = "url_cached",
42+
targetShortDescription = "desc_cached",
43+
targetSummary = null
44+
)
45+
46+
coEvery { translationDao.findTranslation(sourceWord, sourceLang, targetLang) } returns cachedTranslation
47+
48+
val result = repository.getTranslation(sourceWord, sourceLang, targetLang)
49+
50+
assertEquals(cachedTranslation, result)
51+
coVerify(exactly = 0) { wikipediaApi.search(any()) }
52+
coVerify(exactly = 0) { translationDao.insertTranslation(any()) }
53+
}
54+
55+
@Test
56+
fun `getTranslation_fetchesFromNetworkAndCachesWhenNotAvailable`() = runTest {
57+
val sourceWord = "hello"
58+
val sourceLang = "en"
59+
val targetLang = "es"
60+
val networkResponse = SearchResponse(
61+
query = Query(
62+
search = listOf(
63+
SearchResult("Hola", "Spanish for hello")
64+
)
65+
)
66+
)
67+
68+
coEvery { translationDao.findTranslation(sourceWord, sourceLang, targetLang) } returns null
69+
coEvery { wikipediaApi.search(sourceWord) } returns networkResponse
70+
coEvery { translationDao.insertTranslation(any()) } returns Unit
71+
72+
val result = repository.getTranslation(sourceWord, sourceLang, targetLang)
73+
74+
assertEquals("Hola", result?.translatedWord)
75+
coVerify(exactly = 1) { wikipediaApi.search(sourceWord) }
76+
coVerify(exactly = 1) { translationDao.insertTranslation(any()) }
77+
}
78+
79+
@Test
80+
fun `getTranslation_returnsNullWhenNetworkFailsAndNoCache`() = runTest {
81+
val sourceWord = "hello"
82+
val sourceLang = "en"
83+
val targetLang = "es"
84+
85+
coEvery { translationDao.findTranslation(sourceWord, sourceLang, targetLang) } returns null
86+
coEvery { wikipediaApi.search(sourceWord) } throws RuntimeException("Network error")
87+
88+
val result = repository.getTranslation(sourceWord, sourceLang, targetLang)
89+
90+
assertNull(result)
91+
coVerify(exactly = 1) { wikipediaApi.search(sourceWord) }
92+
coVerify(exactly = 0) { translationDao.insertTranslation(any()) }
93+
}
94+
}

0 commit comments

Comments
 (0)