|
| 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 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 com.google.samples.apps.nowinandroid.core.database.model.TopicEntity |
| 24 | +import kotlinx.coroutines.flow.first |
| 25 | +import kotlinx.coroutines.test.runTest |
| 26 | +import org.junit.After |
| 27 | +import org.junit.Before |
| 28 | +import org.junit.Test |
| 29 | +import kotlin.test.assertEquals |
| 30 | + |
| 31 | +class TopicDaoTest { |
| 32 | + private lateinit var topicDao: TopicDao |
| 33 | + private lateinit var db: NiaDatabase |
| 34 | + |
| 35 | + @Before |
| 36 | + fun createDb() { |
| 37 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 38 | + db = Room.inMemoryDatabaseBuilder( |
| 39 | + context, |
| 40 | + NiaDatabase::class.java, |
| 41 | + ).build() |
| 42 | + topicDao = db.topicDao() |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + fun closeDb() = db.close() |
| 47 | + |
| 48 | + @Test |
| 49 | + fun topicDao_fetchTopics() = runTest { |
| 50 | + insertTopics() |
| 51 | + |
| 52 | + val savedTopics = topicDao.getTopicEntities().first() |
| 53 | + |
| 54 | + assertEquals( |
| 55 | + listOf("1", "2", "3"), savedTopics.map { it.id }, |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun topicDao_getSingleTopicEntity() = runTest { |
| 61 | + insertTopics() |
| 62 | + |
| 63 | + val savedTopicEntity = topicDao.getTopicEntity("2").first() |
| 64 | + |
| 65 | + assertEquals("performance", savedTopicEntity.name) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun topicDao_getOneOffTopicEntity() = runTest { |
| 70 | + insertTopics() |
| 71 | + |
| 72 | + val savedTopics = topicDao.getOneOffTopicEntities() |
| 73 | + |
| 74 | + assertEquals( |
| 75 | + listOf("1", "2", "3"), savedTopics.map { it.id }, |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun topicDao_getTopicEntities_ByIds() = runTest { |
| 81 | + insertTopics() |
| 82 | + |
| 83 | + val savedTopics = topicDao.getTopicEntities(setOf("1", "2")) |
| 84 | + .first() |
| 85 | + |
| 86 | + assertEquals(listOf("compose", "performance"), savedTopics.map { it.name }) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun topicDao_IgnoreNewEntry_If_EntityExists() = runTest { |
| 91 | + insertTopics() |
| 92 | + topicDao.insertOrIgnoreTopics( |
| 93 | + listOf(testTopicEntity("1", "compose")), |
| 94 | + ) |
| 95 | + |
| 96 | + val savedTopics = topicDao.getOneOffTopicEntities() |
| 97 | + |
| 98 | + assertEquals(3, savedTopics.size) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun topicDao_Upsert_Entities() = runTest { |
| 103 | + insertTopics() |
| 104 | + topicDao.upsertTopics( |
| 105 | + listOf(testTopicEntity("1", "newName")), |
| 106 | + ) |
| 107 | + |
| 108 | + val savedTopics = topicDao.getOneOffTopicEntities() |
| 109 | + |
| 110 | + assertEquals(3, savedTopics.size) |
| 111 | + assertEquals("newName", savedTopics.first().name) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun topicDao_Delete_Entities() = runTest { |
| 116 | + insertTopics() |
| 117 | + topicDao.deleteTopics(listOf("1", "2")) |
| 118 | + |
| 119 | + val savedTopics = topicDao.getOneOffTopicEntities() |
| 120 | + |
| 121 | + assertEquals(1, savedTopics.size) |
| 122 | + assertEquals("3", savedTopics.first().id) |
| 123 | + } |
| 124 | + |
| 125 | + private suspend fun insertTopics() { |
| 126 | + val topicEntities = listOf( |
| 127 | + testTopicEntity("1", "compose"), |
| 128 | + testTopicEntity("2", "performance"), |
| 129 | + testTopicEntity("3", "headline"), |
| 130 | + ) |
| 131 | + topicDao.insertOrIgnoreTopics(topicEntities) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +private fun testTopicEntity( |
| 136 | + id: String = "0", |
| 137 | + name: String, |
| 138 | +) = TopicEntity( |
| 139 | + id = id, |
| 140 | + name = name, |
| 141 | + shortDescription = "", |
| 142 | + longDescription = "", |
| 143 | + url = "", |
| 144 | + imageUrl = "", |
| 145 | +) |
0 commit comments