|
| 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