Skip to content

Commit 87c1f11

Browse files
authored
Merge pull request #1786 from tobioyelekan/add-topicdao-test
Add topicdao test
2 parents c6588b0 + 249d336 commit 87c1f11

File tree

3 files changed

+179
-30
lines changed

3 files changed

+179
-30
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.nowinandroid.core.database.dao
18+
19+
import android.content.Context
20+
import androidx.room.Room
21+
import androidx.test.core.app.ApplicationProvider
22+
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase
23+
import org.junit.After
24+
import org.junit.Before
25+
26+
internal abstract class DatabaseTest {
27+
28+
private lateinit var db: NiaDatabase
29+
protected lateinit var newsResourceDao: NewsResourceDao
30+
protected lateinit var topicDao: TopicDao
31+
32+
@Before
33+
fun setup() {
34+
db = run {
35+
val context = ApplicationProvider.getApplicationContext<Context>()
36+
Room.inMemoryDatabaseBuilder(
37+
context,
38+
NiaDatabase::class.java,
39+
).build()
40+
}
41+
newsResourceDao = db.newsResourceDao()
42+
topicDao = db.topicDao()
43+
}
44+
45+
@After
46+
fun teardown() = db.close()
47+
}

core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDaoTest.kt

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,20 @@
1616

1717
package com.google.samples.apps.nowinandroid.core.database.dao
1818

19-
import android.content.Context
20-
import androidx.room.Room
21-
import androidx.test.core.app.ApplicationProvider
22-
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase
2319
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity
2420
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopicCrossRef
2521
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
2622
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
2723
import kotlinx.coroutines.flow.first
2824
import kotlinx.coroutines.test.runTest
2925
import kotlinx.datetime.Instant
30-
import org.junit.After
31-
import org.junit.Before
3226
import org.junit.Test
3327
import kotlin.test.assertEquals
3428

35-
class NewsResourceDaoTest {
36-
37-
private lateinit var newsResourceDao: NewsResourceDao
38-
private lateinit var topicDao: TopicDao
39-
private lateinit var db: NiaDatabase
40-
41-
@Before
42-
fun createDb() {
43-
val context = ApplicationProvider.getApplicationContext<Context>()
44-
db = Room.inMemoryDatabaseBuilder(
45-
context,
46-
NiaDatabase::class.java,
47-
).build()
48-
newsResourceDao = db.newsResourceDao()
49-
topicDao = db.topicDao()
50-
}
51-
52-
@After
53-
fun closeDb() = db.close()
29+
internal class NewsResourceDaoTest : DatabaseTest() {
5430

5531
@Test
56-
fun newsResourceDao_fetches_items_by_descending_publish_date() = runTest {
32+
fun getNewsResources_allEntries_areOrderedByPublishDateDesc() = runTest {
5733
val newsResourceEntities = listOf(
5834
testNewsResource(
5935
id = "0",
@@ -88,7 +64,7 @@ class NewsResourceDaoTest {
8864
}
8965

9066
@Test
91-
fun newsResourceDao_filters_items_by_news_ids_by_descending_publish_date() = runTest {
67+
fun getNewsResources_filteredById_areOrderedByDescendingPublishDate() = runTest {
9268
val newsResourceEntities = listOf(
9369
testNewsResource(
9470
id = "0",
@@ -126,7 +102,7 @@ class NewsResourceDaoTest {
126102
}
127103

128104
@Test
129-
fun newsResourceDao_filters_items_by_topic_ids_by_descending_publish_date() = runTest {
105+
fun getNewsResources_filteredByTopicId_areOrderedByDescendingPublishDate() = runTest {
130106
val topicEntities = listOf(
131107
testTopicEntity(
132108
id = "1",
@@ -186,7 +162,7 @@ class NewsResourceDaoTest {
186162
}
187163

188164
@Test
189-
fun newsResourceDao_filters_items_by_news_and_topic_ids_by_descending_publish_date() = runTest {
165+
fun getNewsResources_filteredByIdAndTopicId_areOrderedByDescendingPublishDate() = runTest {
190166
val topicEntities = listOf(
191167
testTopicEntity(
192168
id = "1",
@@ -248,7 +224,7 @@ class NewsResourceDaoTest {
248224
}
249225

250226
@Test
251-
fun newsResourceDao_deletes_items_by_ids() =
227+
fun deleteNewsResources_byId() =
252228
runTest {
253229
val newsResourceEntities = listOf(
254230
testNewsResource(
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.nowinandroid.core.database.dao
18+
19+
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
20+
import kotlinx.coroutines.flow.first
21+
import kotlinx.coroutines.test.runTest
22+
import org.junit.Test
23+
import kotlin.test.assertEquals
24+
25+
internal class TopicDaoTest : DatabaseTest() {
26+
27+
@Test
28+
fun getTopics() = runTest {
29+
insertTopics()
30+
31+
val savedTopics = topicDao.getTopicEntities().first()
32+
33+
assertEquals(
34+
listOf("1", "2", "3"),
35+
savedTopics.map { it.id },
36+
)
37+
}
38+
39+
@Test
40+
fun getTopic() = runTest {
41+
insertTopics()
42+
43+
val savedTopicEntity = topicDao.getTopicEntity("2").first()
44+
45+
assertEquals("performance", savedTopicEntity.name)
46+
}
47+
48+
@Test
49+
fun getTopics_oneOff() = runTest {
50+
insertTopics()
51+
52+
val savedTopics = topicDao.getOneOffTopicEntities()
53+
54+
assertEquals(
55+
listOf("1", "2", "3"),
56+
savedTopics.map { it.id },
57+
)
58+
}
59+
60+
@Test
61+
fun getTopics_byId() = runTest {
62+
insertTopics()
63+
64+
val savedTopics = topicDao.getTopicEntities(setOf("1", "2"))
65+
.first()
66+
67+
assertEquals(listOf("compose", "performance"), savedTopics.map { it.name })
68+
}
69+
70+
@Test
71+
fun insertTopic_newEntryIsIgnoredIfAlreadyExists() = runTest {
72+
insertTopics()
73+
topicDao.insertOrIgnoreTopics(
74+
listOf(testTopicEntity("1", "compose")),
75+
)
76+
77+
val savedTopics = topicDao.getOneOffTopicEntities()
78+
79+
assertEquals(3, savedTopics.size)
80+
}
81+
82+
@Test
83+
fun upsertTopic_existingEntryIsUpdated() = runTest {
84+
insertTopics()
85+
topicDao.upsertTopics(
86+
listOf(testTopicEntity("1", "newName")),
87+
)
88+
89+
val savedTopics = topicDao.getOneOffTopicEntities()
90+
91+
assertEquals(3, savedTopics.size)
92+
assertEquals("newName", savedTopics.first().name)
93+
}
94+
95+
@Test
96+
fun deleteTopics_byId_existingEntriesAreDeleted() = runTest {
97+
insertTopics()
98+
topicDao.deleteTopics(listOf("1", "2"))
99+
100+
val savedTopics = topicDao.getOneOffTopicEntities()
101+
102+
assertEquals(1, savedTopics.size)
103+
assertEquals("3", savedTopics.first().id)
104+
}
105+
106+
private suspend fun insertTopics() {
107+
val topicEntities = listOf(
108+
testTopicEntity("1", "compose"),
109+
testTopicEntity("2", "performance"),
110+
testTopicEntity("3", "headline"),
111+
)
112+
topicDao.insertOrIgnoreTopics(topicEntities)
113+
}
114+
}
115+
116+
private fun testTopicEntity(
117+
id: String = "0",
118+
name: String,
119+
) = TopicEntity(
120+
id = id,
121+
name = name,
122+
shortDescription = "",
123+
longDescription = "",
124+
url = "",
125+
imageUrl = "",
126+
)

0 commit comments

Comments
 (0)