Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add topicdao test #1786

Merged
merged 6 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase
import org.junit.After
import org.junit.Before

internal abstract class DatabaseTest {

private lateinit var db: NiaDatabase
protected lateinit var newsResourceDao: NewsResourceDao
protected lateinit var topicDao: TopicDao

@Before
fun setup() {
db = run {
val context = ApplicationProvider.getApplicationContext<Context>()
Room.inMemoryDatabaseBuilder(
context,
NiaDatabase::class.java,
).build()
}
newsResourceDao = db.newsResourceDao()
topicDao = db.topicDao()
}

@After
fun teardown() = db.close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,20 @@

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

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import com.google.samples.apps.nowinandroid.core.database.NiaDatabase
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopicCrossRef
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
import com.google.samples.apps.nowinandroid.core.database.model.asExternalModel
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
import org.junit.After
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals

class NewsResourceDaoTest {

private lateinit var newsResourceDao: NewsResourceDao
private lateinit var topicDao: TopicDao
private lateinit var db: NiaDatabase

@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(
context,
NiaDatabase::class.java,
).build()
newsResourceDao = db.newsResourceDao()
topicDao = db.topicDao()
}

@After
fun closeDb() = db.close()
internal class NewsResourceDaoTest : DatabaseTest() {

@Test
fun newsResourceDao_fetches_items_by_descending_publish_date() = runTest {
fun getNewsResources_allEntries_areOrderedByPublishDateDesc() = runTest {
val newsResourceEntities = listOf(
testNewsResource(
id = "0",
Expand Down Expand Up @@ -88,7 +64,7 @@ class NewsResourceDaoTest {
}

@Test
fun newsResourceDao_filters_items_by_news_ids_by_descending_publish_date() = runTest {
fun getNewsResources_filteredById_areOrderedByDescendingPublishDate() = runTest {
val newsResourceEntities = listOf(
testNewsResource(
id = "0",
Expand Down Expand Up @@ -126,7 +102,7 @@ class NewsResourceDaoTest {
}

@Test
fun newsResourceDao_filters_items_by_topic_ids_by_descending_publish_date() = runTest {
fun getNewsResources_filteredByTopicId_areOrderedByDescendingPublishDate() = runTest {
val topicEntities = listOf(
testTopicEntity(
id = "1",
Expand Down Expand Up @@ -186,7 +162,7 @@ class NewsResourceDaoTest {
}

@Test
fun newsResourceDao_filters_items_by_news_and_topic_ids_by_descending_publish_date() = runTest {
fun getNewsResources_filteredByIdAndTopicId_areOrderedByDescendingPublishDate() = runTest {
val topicEntities = listOf(
testTopicEntity(
id = "1",
Expand Down Expand Up @@ -248,7 +224,7 @@ class NewsResourceDaoTest {
}

@Test
fun newsResourceDao_deletes_items_by_ids() =
fun deleteNewsResources_byId() =
runTest {
val newsResourceEntities = listOf(
testNewsResource(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Test
import kotlin.test.assertEquals

internal class TopicDaoTest : DatabaseTest() {

@Test
fun getTopics() = runTest {
insertTopics()

val savedTopics = topicDao.getTopicEntities().first()

assertEquals(
listOf("1", "2", "3"),
savedTopics.map { it.id },
)
}

@Test
fun getTopic() = runTest {
insertTopics()

val savedTopicEntity = topicDao.getTopicEntity("2").first()

assertEquals("performance", savedTopicEntity.name)
}

@Test
fun getTopics_oneOff() = runTest {
insertTopics()

val savedTopics = topicDao.getOneOffTopicEntities()

assertEquals(
listOf("1", "2", "3"),
savedTopics.map { it.id },
)
}

@Test
fun getTopics_byId() = runTest {
insertTopics()

val savedTopics = topicDao.getTopicEntities(setOf("1", "2"))
.first()

assertEquals(listOf("compose", "performance"), savedTopics.map { it.name })
}

@Test
fun insertTopic_newEntryIsIgnoredIfAlreadyExists() = runTest {
insertTopics()
topicDao.insertOrIgnoreTopics(
listOf(testTopicEntity("1", "compose")),
)

val savedTopics = topicDao.getOneOffTopicEntities()

assertEquals(3, savedTopics.size)
}

@Test
fun upsertTopic_existingEntryIsUpdated() = runTest {
insertTopics()
topicDao.upsertTopics(
listOf(testTopicEntity("1", "newName")),
)

val savedTopics = topicDao.getOneOffTopicEntities()

assertEquals(3, savedTopics.size)
assertEquals("newName", savedTopics.first().name)
}

@Test
fun deleteTopics_byId_existingEntriesAreDeleted() = runTest {
insertTopics()
topicDao.deleteTopics(listOf("1", "2"))

val savedTopics = topicDao.getOneOffTopicEntities()

assertEquals(1, savedTopics.size)
assertEquals("3", savedTopics.first().id)
}

private suspend fun insertTopics() {
val topicEntities = listOf(
testTopicEntity("1", "compose"),
testTopicEntity("2", "performance"),
testTopicEntity("3", "headline"),
)
topicDao.insertOrIgnoreTopics(topicEntities)
}
}

private fun testTopicEntity(
id: String = "0",
name: String,
) = TopicEntity(
id = id,
name = name,
shortDescription = "",
longDescription = "",
url = "",
imageUrl = "",
)
Loading