From 2bf146738c31f0e2a41197a6e421ba2901a87714 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sat, 9 Mar 2024 20:34:14 +0900 Subject: [PATCH 01/33] Create new function - deleteAll. - deleteAllAndInsertAll. Change-Id: I1ada2b90ca57d7d96e74992840ac4310d8699b97 --- .../core/database/dao/NewsResourceFtsDao.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDao.kt b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDao.kt index 86cc5529e8..942069fdb8 100644 --- a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDao.kt +++ b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDao.kt @@ -20,6 +20,7 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Transaction import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceFtsEntity import kotlinx.coroutines.flow.Flow @@ -36,4 +37,17 @@ interface NewsResourceFtsDao { @Query("SELECT count(*) FROM newsResourcesFts") fun getCount(): Flow + + @Query( + """ + DELETE FROM newsResourcesFts + """, + ) + suspend fun deleteAll() + + @Transaction + suspend fun deleteAllAndInsertAll(newsResources: List) { + deleteAll() + insertAll(newsResources = newsResources) + } } From 0e422bf997973129e32c9ca9e6a9d836ac7306c7 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sat, 9 Mar 2024 20:37:46 +0900 Subject: [PATCH 02/33] Create NewsResourceFtsDaoTest - whenInsertOnce_InsertedNewsResourceEntityOnce. - whenInsertTwice_InsertedNewsResourceEntityTwice. - whenDeleteAndInsertAllThreeTimes_InsertedOnce Change-Id: I9aaab5ca5573a64260917d01283bbed9f4c0c6e6 --- .../database/dao/NewsResourceFtsDaoTest.kt | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt new file mode 100644 index 0000000000..69375bd4d8 --- /dev/null +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -0,0 +1,98 @@ +/* + * 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 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.NewsResourceFtsEntity +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Before +import org.junit.Test +import kotlin.test.assertEquals + +class NewsResourceFtsDaoTest { + + private lateinit var newsResourceFtsDao: NewsResourceFtsDao + private lateinit var db: NiaDatabase + + private val newsResourceFtsEntities = listOf( + NewsResourceFtsEntity( + newsResourceId = "0", + title = "test0", + content = "content0", + ), + NewsResourceFtsEntity( + newsResourceId = "1", + title = "test1", + content = "content1", + ), + NewsResourceFtsEntity( + newsResourceId = "2", + title = "test2", + content = "content2", + ), + NewsResourceFtsEntity( + newsResourceId = "3", + title = "test3", + content = "content3", + ), + ) + + @Before + fun createDb() { + val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder( + context, + NiaDatabase::class.java, + ).build() + newsResourceFtsDao = db.newsResourceFtsDao() + } + + @After + fun closeDb() { + db.close() + } + + @Test + fun whenInsertOnce_InsertedNewsResourceEntityOnce() = runTest { + insertAllNewsResourceFtsEntities() + assertEquals(4, newsResourceFtsDao.getCount().first()) + } + + @Test + fun whenInsertTwice_InsertedNewsResourceEntityTwice() = runTest { + repeat(2) { + newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities) + } + assertEquals(8, newsResourceFtsDao.getCount().first()) + } + + @Test + fun whenDeleteAndInsertAllThreeTimes_InsertedOnce() = runTest { + repeat(3) { + newsResourceFtsDao.deleteAllAndInsertAll(newsResources = newsResourceFtsEntities) + } + assertEquals(4, newsResourceFtsDao.getCount().first()) + } + + private suspend fun insertAllNewsResourceFtsEntities() = + newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities) +} From 3a778a98478dcd53489c2407ae24c8b27461abaf Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sat, 9 Mar 2024 20:50:51 +0900 Subject: [PATCH 03/33] Create testNewsResourceFtsEntity function. Change-Id: Ic7507223d1f4d8888d3809d9cfd7272d84ad0f66 --- .../core/database/dao/NewsResourceFtsDaoTest.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 69375bd4d8..9209e9454b 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -95,4 +95,12 @@ class NewsResourceFtsDaoTest { private suspend fun insertAllNewsResourceFtsEntities() = newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities) + + private fun testNewsResourceFtsEntity( + number: String, + ) = NewsResourceFtsEntity( + newsResourceId = number, + title = "title$number", + content = "content$number", + ) } From 72ad7940a660f1301fc1fd2a5bf2c6cd53cc94f4 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sat, 9 Mar 2024 20:51:19 +0900 Subject: [PATCH 04/33] Apply testNewsResourceFtsEntity. Change-Id: I6ba5157706dee6a8bc47c0792c871c45a36c41cb --- .../database/dao/NewsResourceFtsDaoTest.kt | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 9209e9454b..7676bbc20d 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -34,26 +34,10 @@ class NewsResourceFtsDaoTest { private lateinit var db: NiaDatabase private val newsResourceFtsEntities = listOf( - NewsResourceFtsEntity( - newsResourceId = "0", - title = "test0", - content = "content0", - ), - NewsResourceFtsEntity( - newsResourceId = "1", - title = "test1", - content = "content1", - ), - NewsResourceFtsEntity( - newsResourceId = "2", - title = "test2", - content = "content2", - ), - NewsResourceFtsEntity( - newsResourceId = "3", - title = "test3", - content = "content3", - ), + testNewsResourceFtsEntity(number = "0"), + testNewsResourceFtsEntity(number = "1"), + testNewsResourceFtsEntity(number = "2"), + testNewsResourceFtsEntity(number = "3"), ) @Before From 58248373b6bcec93de8172eb0664b38f623a4203 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sun, 10 Mar 2024 09:58:44 +0900 Subject: [PATCH 05/33] Create deleteAll and deleteAllAndInsertAll function. Change-Id: Ie185aa1fca002a27580036bcaca4d31da0818b92 --- .../nowinandroid/core/database/dao/TopicFtsDao.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDao.kt b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDao.kt index 25dea5b83f..03ee9c72fa 100644 --- a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDao.kt +++ b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDao.kt @@ -20,6 +20,7 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Transaction import com.google.samples.apps.nowinandroid.core.database.model.TopicFtsEntity import kotlinx.coroutines.flow.Flow @@ -36,4 +37,17 @@ interface TopicFtsDao { @Query("SELECT count(*) FROM topicsFts") fun getCount(): Flow + + @Query( + """ + DELETE FROM topicsFts + """, + ) + suspend fun deleteAll() + + @Transaction + suspend fun deleteAllAndInsertAll(topics: List) { + deleteAll() + insertAll(topics = topics) + } } From cc5d2a1b8ec070d42b81e27faa209720a5878c80 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sun, 10 Mar 2024 10:02:38 +0900 Subject: [PATCH 06/33] Create TopicFtsDaoTest - whenInsertOnce_InsertedTopicsFtsEntityOnce - whenInsertTwice_InsertedNewsResourceEntityTwice - whenDeleteAndInsertAllThreeTimes_InsertedOnce Change-Id: I7efbf465939a33b8b1451f42899284225f8647b3 --- .../core/database/dao/TopicFtsDaoTest.kt | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt new file mode 100644 index 0000000000..04bce1879c --- /dev/null +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt @@ -0,0 +1,91 @@ +/* + * 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 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.TopicFtsEntity +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Before +import org.junit.Test +import kotlin.test.assertEquals + +class TopicFtsDaoTest { + + private lateinit var topicFtsDao: TopicFtsDao + private lateinit var db: NiaDatabase + + private val topicFtsEntities = listOf( + testTopicFtsEntity("0"), + testTopicFtsEntity("1"), + testTopicFtsEntity("2"), + testTopicFtsEntity("3"), + ) + + @Before + fun createDb() { + val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder( + context, + NiaDatabase::class.java, + ).build() + topicFtsDao = db.topicFtsDao() + } + + @After + fun closeDb() { + db.close() + } + + @Test + fun whenInsertOnce_InsertedTopicsFtsEntityOnce() = runTest { + insertAllNewsResourceFtsEntities() + assertEquals(4, topicFtsDao.getCount().first()) + } + + @Test + fun whenInsertTwice_InsertedNewsResourceEntityTwice() = runTest { + repeat(2) { + topicFtsDao.insertAll(topics = topicFtsEntities) + } + assertEquals(8, topicFtsDao.getCount().first()) + } + + @Test + fun whenDeleteAndInsertAllThreeTimes_InsertedOnce() = runTest { + repeat(3) { + topicFtsDao.deleteAllAndInsertAll(topics = topicFtsEntities) + } + assertEquals(4, topicFtsDao.getCount().first()) + } + + private suspend fun insertAllNewsResourceFtsEntities() = + topicFtsDao.insertAll(topics = topicFtsEntities) + + private fun testTopicFtsEntity( + number: String, + ) = TopicFtsEntity( + topicId = number, + name = "name$number", + shortDescription = "Short description$number", + longDescription = "Long description$number", + ) +} From 4aee8a270ae0bd299408d8e695e2d162236fb7eb Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sun, 10 Mar 2024 22:56:31 +0900 Subject: [PATCH 07/33] Make NiaDatabase public to use in core:data test. Change-Id: I50a222fdcd267e57cf25065e76aa488cef66c5e1 --- .../samples/apps/nowinandroid/core/database/NiaDatabase.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/NiaDatabase.kt b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/NiaDatabase.kt index 87fd82af11..fd6b75e49e 100644 --- a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/NiaDatabase.kt +++ b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/NiaDatabase.kt @@ -63,7 +63,7 @@ import com.google.samples.apps.nowinandroid.core.database.util.InstantConverter @TypeConverters( InstantConverter::class, ) -internal abstract class NiaDatabase : RoomDatabase() { +abstract class NiaDatabase : RoomDatabase() { abstract fun topicDao(): TopicDao abstract fun newsResourceDao(): NewsResourceDao abstract fun topicFtsDao(): TopicFtsDao From 89e1a9c7fc0bdcf14bc253b6f25b8d286f28befe Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sun, 10 Mar 2024 23:03:06 +0900 Subject: [PATCH 08/33] Create newsResourceEntitiesTestData. Change-Id: Idaf471323937040def00f8664bb8e3fff0067967 --- .../database/NewsResourceEntitiesTestData.kt | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt new file mode 100644 index 0000000000..e9a61c252f --- /dev/null +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt @@ -0,0 +1,66 @@ +/* + * 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.testing.database + +import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity +import kotlinx.datetime.Instant + +val newsResourceEntitiesTestData: List = listOf( + NewsResourceEntity( + id = "1", + title = "Android Basics with Compose", + content = "We released the first two units of Android Basics with Compose, our first free course that teaches Android Development with Jetpack Compose to anyone; you do not need any prior programming experience other than basic computer literacy to get started. You’ll learn the fundamentals of programming in Kotlin while building Android apps using Jetpack Compose, Android’s modern toolkit that simplifies and accelerates native UI development. These two units are just the beginning; more will be coming soon. Check out Android Basics with Compose to get started on your Android development journey", + url = "https://android-developers.googleblog.com/2022/05/new-android-basics-with-compose-course.html", + headerImageUrl = "https://developer.android.com/images/hero-assets/android-basics-compose.svg", + publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), + type = "Codelab", + ), + NewsResourceEntity( + id = "2", + title = "Thanks for helping us reach 1M YouTube Subscribers", + content = "Thank you everyone for following the Now in Android series and everything the " + + "Android Developers YouTube channel has to offer. During the Android Developer " + + "Summit, our YouTube channel reached 1 million subscribers! Here’s a small video to " + + "thank you all.", + url = "https://youtu.be/-fJ6poHQrjM", + headerImageUrl = "https://i.ytimg.com/vi/-fJ6poHQrjM/maxresdefault.jpg", + publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), + type = "Video 📺", + ), + NewsResourceEntity( + id = "3", + title = "Transformations and customisations in the Paging Library", + content = "A demonstration of different operations that can be performed " + + "with Paging. Transformations like inserting separators, when to " + + "create a new pager, and customisation options for consuming " + + "PagingData.", + url = "https://youtu.be/ZARz0pjm5YM", + headerImageUrl = "https://i.ytimg.com/vi/ZARz0pjm5YM/maxresdefault.jpg", + publishDate = Instant.parse("2021-11-01T00:00:00.000Z"), + type = "Video 📺", + ), + NewsResourceEntity( + id = "4", + title = "New Jetpack Release", + content = "New Jetpack release includes updates to libraries such as CameraX, Benchmark, and" + + "more!", + url = "https://developer.android.com/jetpack/androidx/versions/all-channel", + headerImageUrl = "", + publishDate = Instant.parse("2022-10-01T00:00:00.000Z"), + type = "", + ), +) From f16cdca4b343963831942ef128d5496bcdc92cc5 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Sun, 10 Mar 2024 23:03:17 +0900 Subject: [PATCH 09/33] Create topicEntitiesTestData. Change-Id: Ie31a6fbb10ab10f7d7973b79cf6c4711257c0b9a --- .../testing/database/TopicEntitiesTestData.kt | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt new file mode 100644 index 0000000000..0dea17856b --- /dev/null +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt @@ -0,0 +1,46 @@ +/* + * 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.testing.database + +import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity + +val topicEntitiesTestData: List = listOf( + TopicEntity( + id = "2", + name = "Headlines", + shortDescription = "News we want everyone to see", + longDescription = "Stay up to date with the latest events and announcements from Android!", + imageUrl = "https://firebasestorage.googleapis.com/v0/b/now-in-android.appspot.com/o/img%2Fic_topic_Headlines.svg?alt=media&token=506faab0-617a-4668-9e63-4a2fb996603f", + url = "", + ), + TopicEntity( + id = "3", + name = "UI", + shortDescription = "Material Design, Navigation, Text, Paging, Accessibility (a11y), Internationalization (i18n), Localization (l10n), Animations, Large Screens, Widgets", + longDescription = "Learn how to optimize your app's user interface - everything that users can see and interact with. Stay up to date on topics such as Material Design, Navigation, Text, Paging, Compose, Accessibility (a11y), Internationalization (i18n), Localization (l10n), Animations, Large Screens, Widgets, and many more!", + imageUrl = "https://firebasestorage.googleapis.com/v0/b/now-in-android.appspot.com/o/img%2Fic_topic_UI.svg?alt=media&token=0ee1842b-12e8-435f-87ba-a5bb02c47594", + url = "", + ), + TopicEntity( + id = "4", + name = "Testing", + shortDescription = "CI, Espresso, TestLab, etc", + longDescription = "Testing is an integral part of the app development process. By running tests against your app consistently, you can verify your app's correctness, functional behavior, and usability before you release it publicly. Stay up to date on the latest tricks in CI, Espresso, and Firebase TestLab.", + imageUrl = "https://firebasestorage.googleapis.com/v0/b/now-in-android.appspot.com/o/img%2Fic_topic_Testing.svg?alt=media&token=a11533c4-7cc8-4b11-91a3-806158ebf428", + url = "", + ), +) From bd4b431ce397659ef2bdaca3600c079c2c9b9514 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 11 Mar 2024 17:36:50 +0900 Subject: [PATCH 10/33] Change insertAll to deleteAllAndInsertAll. Change-Id: I49e514960b76a324e2a773f29ecd896c35ea22a4 --- .../core/data/repository/DefaultSearchContentsRepository.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt index 3bacb8a147..4cba4809ce 100644 --- a/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt +++ b/core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultSearchContentsRepository.kt @@ -46,7 +46,7 @@ internal class DefaultSearchContentsRepository @Inject constructor( override suspend fun populateFtsData() { withContext(ioDispatcher) { - newsResourceFtsDao.insertAll( + newsResourceFtsDao.deleteAllAndInsertAll( newsResourceDao.getNewsResources( useFilterTopicIds = false, useFilterNewsIds = false, @@ -54,7 +54,7 @@ internal class DefaultSearchContentsRepository @Inject constructor( .first() .map(PopulatedNewsResource::asFtsEntity), ) - topicFtsDao.insertAll(topicDao.getOneOffTopicEntities().map { it.asFtsEntity() }) + topicFtsDao.deleteAllAndInsertAll(topicDao.getOneOffTopicEntities().map { it.asFtsEntity() }) } } From 6a4c6bbc8c164fc59d5b94708e9652e330dddda2 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 11 Mar 2024 19:16:07 +0900 Subject: [PATCH 11/33] Update gradle - alias(libs.plugins.nowinandroid.android.room). - androidTestImplementation(projects.core.testing). - defaultConfig. - testInstrumentationRunner. Change-Id: I5e3db4da5275ef4fa5efbc1ea151dfc64d34a74f --- core/data/build.gradle.kts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index 142637ff9c..4e4cc965ca 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -17,11 +17,18 @@ plugins { alias(libs.plugins.nowinandroid.android.library) alias(libs.plugins.nowinandroid.android.library.jacoco) alias(libs.plugins.nowinandroid.android.hilt) + alias(libs.plugins.nowinandroid.android.room) id("kotlinx-serialization") } android { namespace = "com.google.samples.apps.nowinandroid.core.data" + + defaultConfig { + testInstrumentationRunner = + "com.google.samples.apps.nowinandroid.core.testing.NiaTestRunner" + } + testOptions { unitTests { isIncludeAndroidResources = true @@ -43,4 +50,6 @@ dependencies { testImplementation(libs.kotlinx.serialization.json) testImplementation(projects.core.datastoreTest) testImplementation(projects.core.testing) + + androidTestImplementation(projects.core.testing) } From 7ec025d4f03f14668864d0bb6edd2ef69ffc6576 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 11 Mar 2024 19:17:00 +0900 Subject: [PATCH 12/33] Create SearchContentsRepositoryTest - whenPopulateFtsDataTwice_PopulateFtsOnce - whenSearchAndroid_ReturnResult Change-Id: Iccb3233415b30837868f4166fd843063ed6ad5f4 --- .../core/data/SearchContentsRepositoryTest.kt | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt diff --git a/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt b/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt new file mode 100644 index 0000000000..9cbb229e94 --- /dev/null +++ b/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt @@ -0,0 +1,113 @@ +/* + * 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.data + +import android.content.Context +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import com.google.samples.apps.nowinandroid.core.data.repository.DefaultSearchContentsRepository +import com.google.samples.apps.nowinandroid.core.data.repository.SearchContentsRepository +import com.google.samples.apps.nowinandroid.core.database.NiaDatabase +import com.google.samples.apps.nowinandroid.core.database.dao.NewsResourceDao +import com.google.samples.apps.nowinandroid.core.database.dao.NewsResourceFtsDao +import com.google.samples.apps.nowinandroid.core.database.dao.TopicDao +import com.google.samples.apps.nowinandroid.core.database.dao.TopicFtsDao +import com.google.samples.apps.nowinandroid.core.testing.database.newsResourceEntitiesTestData +import com.google.samples.apps.nowinandroid.core.testing.database.topicEntitiesTestData +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Before +import org.junit.Test +import kotlin.test.assertEquals + +class SearchContentsRepositoryTest { + + private lateinit var newsResourceDao: NewsResourceDao + private lateinit var topicDao: TopicDao + private lateinit var newsResourceFtsDao: NewsResourceFtsDao + private lateinit var topicFtsDao: TopicFtsDao + private lateinit var db: NiaDatabase + private lateinit var searchContentsRepository: SearchContentsRepository + + @Before + fun setUp() { + val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder( + context, + NiaDatabase::class.java, + ).build() + newsResourceDao = db.newsResourceDao() + topicDao = db.topicDao() + newsResourceFtsDao = db.newsResourceFtsDao() + topicFtsDao = db.topicFtsDao() + + searchContentsRepository = DefaultSearchContentsRepository( + newsResourceDao = newsResourceDao, + newsResourceFtsDao = newsResourceFtsDao, + topicDao = topicDao, + topicFtsDao = topicFtsDao, + ioDispatcher = Dispatchers.IO, + ) + } + + @After + fun closeDb() = db.close() + + @Test + fun whenPopulateFtsDataTwice_PopulateFtsOnce() = runTest { + allDataPreSetting() + repeat(2) { + searchContentsRepository.populateFtsData() + } + advanceUntilIdle() + assertEquals(7, searchContentsRepository.getSearchContentsCount().first()) + } + + @Test + fun whenSearchAndroid_ReturnResult() = runTest { + allDataPreSetting() + searchContentsRepository.populateFtsData() + + val searchQuery = "Android" + val topicIds = listOf("2") + val newsResourceIds = listOf("1", "2") + assertEquals( + topicIds, + searchContentsRepository + .searchContents(searchQuery = searchQuery) + .first() + .topics + .map { it.id }, + ) + assertEquals( + newsResourceIds, + searchContentsRepository + .searchContents(searchQuery = searchQuery) + .first() + .newsResources + .map { it.id }, + ) + } + + private suspend fun allDataPreSetting() { + newsResourceDao.upsertNewsResources(newsResourceEntities = newsResourceEntitiesTestData) + topicDao.upsertTopics(entities = topicEntitiesTestData) + } +} From 5bf8c288f05062f440cd4cb5304ea7dd22bfab41 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 11 Mar 2024 19:28:13 +0900 Subject: [PATCH 13/33] Use test module test data. Change-Id: Ie1a0d90b85e03481744dd1cad06d3a0d08eed466 --- .../core/database/dao/TopicFtsDaoTest.kt | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt index 04bce1879c..5359fb05de 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt @@ -21,6 +21,8 @@ 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.TopicFtsEntity +import com.google.samples.apps.nowinandroid.core.database.model.asFtsEntity +import com.google.samples.apps.nowinandroid.core.testing.database.topicEntitiesTestData import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest import org.junit.After @@ -33,21 +35,20 @@ class TopicFtsDaoTest { private lateinit var topicFtsDao: TopicFtsDao private lateinit var db: NiaDatabase - private val topicFtsEntities = listOf( - testTopicFtsEntity("0"), - testTopicFtsEntity("1"), - testTopicFtsEntity("2"), - testTopicFtsEntity("3"), - ) + private lateinit var topicFtsEntities: List @Before - fun createDb() { + fun setUp() { val context = ApplicationProvider.getApplicationContext() db = Room.inMemoryDatabaseBuilder( context, NiaDatabase::class.java, ).build() topicFtsDao = db.topicFtsDao() + + topicFtsEntities = topicEntitiesTestData.map { + it.asFtsEntity() + } } @After @@ -58,7 +59,7 @@ class TopicFtsDaoTest { @Test fun whenInsertOnce_InsertedTopicsFtsEntityOnce() = runTest { insertAllNewsResourceFtsEntities() - assertEquals(4, topicFtsDao.getCount().first()) + assertEquals(topicFtsEntities.size, topicFtsDao.getCount().first()) } @Test @@ -66,7 +67,7 @@ class TopicFtsDaoTest { repeat(2) { topicFtsDao.insertAll(topics = topicFtsEntities) } - assertEquals(8, topicFtsDao.getCount().first()) + assertEquals(topicFtsEntities.size * 2, topicFtsDao.getCount().first()) } @Test @@ -74,18 +75,9 @@ class TopicFtsDaoTest { repeat(3) { topicFtsDao.deleteAllAndInsertAll(topics = topicFtsEntities) } - assertEquals(4, topicFtsDao.getCount().first()) + assertEquals(topicFtsEntities.size, topicFtsDao.getCount().first()) } private suspend fun insertAllNewsResourceFtsEntities() = topicFtsDao.insertAll(topics = topicFtsEntities) - - private fun testTopicFtsEntity( - number: String, - ) = TopicFtsEntity( - topicId = number, - name = "name$number", - shortDescription = "Short description$number", - longDescription = "Long description$number", - ) } From e8fe319fe7e1f7fc03ed58dc88894cc7ac59f612 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 11 Mar 2024 19:28:42 +0900 Subject: [PATCH 14/33] Compare with newsResourceFtsEntities size. Change-Id: Ia6f923d9987e823d3d3742b34eab087e32596a61 --- .../core/database/dao/NewsResourceFtsDaoTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 7676bbc20d..7045db07ab 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -58,7 +58,7 @@ class NewsResourceFtsDaoTest { @Test fun whenInsertOnce_InsertedNewsResourceEntityOnce() = runTest { insertAllNewsResourceFtsEntities() - assertEquals(4, newsResourceFtsDao.getCount().first()) + assertEquals(newsResourceFtsEntities.size, newsResourceFtsDao.getCount().first()) } @Test @@ -66,7 +66,7 @@ class NewsResourceFtsDaoTest { repeat(2) { newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities) } - assertEquals(8, newsResourceFtsDao.getCount().first()) + assertEquals(newsResourceFtsEntities.size * 2, newsResourceFtsDao.getCount().first()) } @Test @@ -74,7 +74,7 @@ class NewsResourceFtsDaoTest { repeat(3) { newsResourceFtsDao.deleteAllAndInsertAll(newsResources = newsResourceFtsEntities) } - assertEquals(4, newsResourceFtsDao.getCount().first()) + assertEquals(newsResourceFtsEntities.size, newsResourceFtsDao.getCount().first()) } private suspend fun insertAllNewsResourceFtsEntities() = From a50f7e8899dae18c7edf4430d5b2c7e35b652de5 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 25 Mar 2024 19:29:36 +0900 Subject: [PATCH 15/33] Fix long line. Change-Id: I5c2a6387651347bce74399d3ec857afe7e56604d --- .../core/testing/database/NewsResourceEntitiesTestData.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt index e9a61c252f..10043d1ad9 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt @@ -23,7 +23,13 @@ val newsResourceEntitiesTestData: List = listOf( NewsResourceEntity( id = "1", title = "Android Basics with Compose", - content = "We released the first two units of Android Basics with Compose, our first free course that teaches Android Development with Jetpack Compose to anyone; you do not need any prior programming experience other than basic computer literacy to get started. You’ll learn the fundamentals of programming in Kotlin while building Android apps using Jetpack Compose, Android’s modern toolkit that simplifies and accelerates native UI development. These two units are just the beginning; more will be coming soon. Check out Android Basics with Compose to get started on your Android development journey", + content = "We released the first two units of Android Basics with Compose, " + + "our first free course that teaches Android Development with Jetpack Compose to anyone; " + + "you do not need any prior programming experience other than basic computer literacy to get started. " + + "You’ll learn the fundamentals of programming in Kotlin while building Android apps using Jetpack Compose, " + + "Android’s modern toolkit that simplifies and accelerates native UI development. " + + "These two units are just the beginning; more will be coming soon. " + + "Check out Android Basics with Compose to get started on your Android development journey", url = "https://android-developers.googleblog.com/2022/05/new-android-basics-with-compose-course.html", headerImageUrl = "https://developer.android.com/images/hero-assets/android-basics-compose.svg", publishDate = Instant.parse("2021-11-09T00:00:00.000Z"), From 63e28a2d0604b480ddd8e00f08a65b5941a03e0f Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 25 Mar 2024 19:33:42 +0900 Subject: [PATCH 16/33] Change test name to clear meaning. Change-Id: I76da5ddf0d7dc5337b6a7b5c0558bad29d9628b8 --- .../apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt index 5359fb05de..a8902d3873 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt @@ -57,13 +57,13 @@ class TopicFtsDaoTest { } @Test - fun whenInsertOnce_InsertedTopicsFtsEntityOnce() = runTest { + fun insertAllOnce_countMatches() = runTest { insertAllNewsResourceFtsEntities() assertEquals(topicFtsEntities.size, topicFtsDao.getCount().first()) } @Test - fun whenInsertTwice_InsertedNewsResourceEntityTwice() = runTest { + fun insertAllTwice_countMatches() = runTest { repeat(2) { topicFtsDao.insertAll(topics = topicFtsEntities) } @@ -71,7 +71,7 @@ class TopicFtsDaoTest { } @Test - fun whenDeleteAndInsertAllThreeTimes_InsertedOnce() = runTest { + fun deleteAndInsertAllThreeTimes_countMatches() = runTest { repeat(3) { topicFtsDao.deleteAllAndInsertAll(topics = topicFtsEntities) } From c7828836ee0ab41804bedb31f69b5ffabfd45a41 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 25 Mar 2024 19:47:38 +0900 Subject: [PATCH 17/33] Add information on test data. Change-Id: I420b7eddbfd7d5b2cd03a6a52293786aacd4922b --- .../core/testing/database/NewsResourceEntitiesTestData.kt | 3 +++ .../core/testing/database/TopicEntitiesTestData.kt | 3 +++ 2 files changed, 6 insertions(+) diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt index 10043d1ad9..9c7b49833b 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/NewsResourceEntitiesTestData.kt @@ -19,6 +19,9 @@ package com.google.samples.apps.nowinandroid.core.testing.database import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity import kotlinx.datetime.Instant +/** + * Test data list of [NewsResourceEntity]. + */ val newsResourceEntitiesTestData: List = listOf( NewsResourceEntity( id = "1", diff --git a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt index 0dea17856b..c4f450d7b7 100644 --- a/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt +++ b/core/testing/src/main/kotlin/com/google/samples/apps/nowinandroid/core/testing/database/TopicEntitiesTestData.kt @@ -18,6 +18,9 @@ package com.google.samples.apps.nowinandroid.core.testing.database import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity +/** + * Test data list of [TopicEntity]. + */ val topicEntitiesTestData: List = listOf( TopicEntity( id = "2", From e8db8362213cd2179a139b61f045b7ecd6f98ec1 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 25 Mar 2024 19:52:31 +0900 Subject: [PATCH 18/33] Add document on test class. Change-Id: Ia0d34c5ed66edfb3e6910e455529f55be3192d9c --- .../apps/nowinandroid/core/database/dao/NewsResourceDaoTest.kt | 3 +++ .../nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt | 3 +++ .../apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt | 3 +++ 3 files changed, 9 insertions(+) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDaoTest.kt index 535ab61a77..378e428779 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDaoTest.kt @@ -32,6 +32,9 @@ import org.junit.Before import org.junit.Test import kotlin.test.assertEquals +/** + * Instrumentation tests for [NewsResourceDao]. + */ class NewsResourceDaoTest { private lateinit var newsResourceDao: NewsResourceDao diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 7045db07ab..51f383ff82 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -28,6 +28,9 @@ import org.junit.Before import org.junit.Test import kotlin.test.assertEquals +/** + * Instrumentation tests for [NewsResourceFtsDao]. + */ class NewsResourceFtsDaoTest { private lateinit var newsResourceFtsDao: NewsResourceFtsDao diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt index a8902d3873..2422cd059c 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt @@ -30,6 +30,9 @@ import org.junit.Before import org.junit.Test import kotlin.test.assertEquals +/** + * Instrumentation tests for [TopicFtsDao]. + */ class TopicFtsDaoTest { private lateinit var topicFtsDao: TopicFtsDao From 7786f523225f38d6f8f29a00e7b971cd50b663e8 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 25 Mar 2024 19:56:45 +0900 Subject: [PATCH 19/33] Add document on test class. Change-Id: I92ec805606e297f56a0b62e9aed9bab27c3850df --- .../nowinandroid/core/data/SearchContentsRepositoryTest.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt b/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt index 9cbb229e94..c8f76aefd5 100644 --- a/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt +++ b/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt @@ -37,6 +37,9 @@ import org.junit.Before import org.junit.Test import kotlin.test.assertEquals +/** + * Instrumentation tests for [SearchContentsRepository]. + */ class SearchContentsRepositoryTest { private lateinit var newsResourceDao: NewsResourceDao From b6a99e3a019164cad209b29afeb7042011a6bf77 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 21:44:16 +0900 Subject: [PATCH 20/33] Remove testNewsResourceFtsEntity. Change-Id: I9263f9b2b5c690a6627a3503c1d02639720c7bab --- .../core/database/dao/NewsResourceFtsDaoTest.kt | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 51f383ff82..1b721d3ac7 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -36,12 +36,7 @@ class NewsResourceFtsDaoTest { private lateinit var newsResourceFtsDao: NewsResourceFtsDao private lateinit var db: NiaDatabase - private val newsResourceFtsEntities = listOf( - testNewsResourceFtsEntity(number = "0"), - testNewsResourceFtsEntity(number = "1"), - testNewsResourceFtsEntity(number = "2"), - testNewsResourceFtsEntity(number = "3"), - ) + private lateinit var newsResourceFtsEntities: List @Before fun createDb() { @@ -82,12 +77,4 @@ class NewsResourceFtsDaoTest { private suspend fun insertAllNewsResourceFtsEntities() = newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities) - - private fun testNewsResourceFtsEntity( - number: String, - ) = NewsResourceFtsEntity( - newsResourceId = number, - title = "title$number", - content = "content$number", - ) } From ab135ecd3459720efa4df567ebc42fa4f1deaeb8 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 21:44:37 +0900 Subject: [PATCH 21/33] Create `NewsResourceEntity.asFtsEntity`. Change-Id: If515255916b76a29f05109a628c5964625d865fa --- .../core/database/model/NewsResourceFtsEntity.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/NewsResourceFtsEntity.kt b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/NewsResourceFtsEntity.kt index 0ba6250240..3f2920ecfc 100644 --- a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/NewsResourceFtsEntity.kt +++ b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/NewsResourceFtsEntity.kt @@ -36,3 +36,12 @@ data class NewsResourceFtsEntity( @ColumnInfo(name = "content") val content: String, ) + +/** + * Transform [NewsResourceEntity] into [NewsResourceFtsEntity]. + */ +fun NewsResourceEntity.asFtsEntity() = NewsResourceFtsEntity( + newsResourceId = id, + title = "title", + content = "content", +) From 7a0395cd9b234be1707de6dea3299e7610595580 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 21:45:33 +0900 Subject: [PATCH 22/33] Add KDoc on `TopicEntity.asFtsEntity`. Change-Id: Ie3a0aebe98264db53ab63a8b9d4d665d92c835cd --- .../apps/nowinandroid/core/database/model/TopicFtsEntity.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/TopicFtsEntity.kt b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/TopicFtsEntity.kt index 23d56f2dfb..d25e00c76c 100644 --- a/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/TopicFtsEntity.kt +++ b/core/database/src/main/kotlin/com/google/samples/apps/nowinandroid/core/database/model/TopicFtsEntity.kt @@ -40,6 +40,9 @@ data class TopicFtsEntity( val longDescription: String, ) +/** + * Transform [TopicEntity] into [TopicFtsEntity]. + */ fun TopicEntity.asFtsEntity() = TopicFtsEntity( topicId = id, name = name, From 64d7e5771ab32cae5a08926cf0f3b123d803e1bb Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 21:46:05 +0900 Subject: [PATCH 23/33] Change to one line. Change-Id: Ic80f9b04b71cd6bf5b8f08efd62870c4f59732a3 --- .../nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt | 4 +--- .../apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 1b721d3ac7..6357e23c0a 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -49,9 +49,7 @@ class NewsResourceFtsDaoTest { } @After - fun closeDb() { - db.close() - } + fun closeDb() = db.close() @Test fun whenInsertOnce_InsertedNewsResourceEntityOnce() = runTest { diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt index 2422cd059c..419a37cb82 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt @@ -55,9 +55,7 @@ class TopicFtsDaoTest { } @After - fun closeDb() { - db.close() - } + fun closeDb() = db.close() @Test fun insertAllOnce_countMatches() = runTest { From 7b8e31a858dd7ceda2a4b522af36a9a6913d7203 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 21:46:42 +0900 Subject: [PATCH 24/33] Get test data from `newsResourceEntitiesTestData`. Change-Id: I95608be389126608c20861c719cbdde9e957c70f --- .../core/database/dao/NewsResourceFtsDaoTest.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 6357e23c0a..4ea3ae87be 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -21,6 +21,8 @@ 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.NewsResourceFtsEntity +import com.google.samples.apps.nowinandroid.core.database.model.asFtsEntity +import com.google.samples.apps.nowinandroid.core.testing.database.newsResourceEntitiesTestData import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest import org.junit.After @@ -46,6 +48,10 @@ class NewsResourceFtsDaoTest { NiaDatabase::class.java, ).build() newsResourceFtsDao = db.newsResourceFtsDao() + + newsResourceFtsEntities = newsResourceEntitiesTestData.map { + it.asFtsEntity() + } } @After From 9ca3a8a36ef0efad3f13000c3e8fbbc5a7eb509f Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 21:47:06 +0900 Subject: [PATCH 25/33] Change test name style to `given_when_then`. Change-Id: I59c5600f5aa79d638ee8983ceb754c98471ceb66 --- .../core/database/dao/NewsResourceFtsDaoTest.kt | 6 +++--- .../apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index 4ea3ae87be..c26c0ef7e3 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -58,13 +58,13 @@ class NewsResourceFtsDaoTest { fun closeDb() = db.close() @Test - fun whenInsertOnce_InsertedNewsResourceEntityOnce() = runTest { + fun newsResourceFts_insertAllOnce_countMatches() = runTest { insertAllNewsResourceFtsEntities() assertEquals(newsResourceFtsEntities.size, newsResourceFtsDao.getCount().first()) } @Test - fun whenInsertTwice_InsertedNewsResourceEntityTwice() = runTest { + fun newsResourceFts_insertAllTwice_countMatches() = runTest { repeat(2) { newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities) } @@ -72,7 +72,7 @@ class NewsResourceFtsDaoTest { } @Test - fun whenDeleteAndInsertAllThreeTimes_InsertedOnce() = runTest { + fun newsResourceFts_deleteAndInsertAllThreeTimes_countMatches() = runTest { repeat(3) { newsResourceFtsDao.deleteAllAndInsertAll(newsResources = newsResourceFtsEntities) } diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt index 419a37cb82..91c6337928 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt @@ -58,13 +58,13 @@ class TopicFtsDaoTest { fun closeDb() = db.close() @Test - fun insertAllOnce_countMatches() = runTest { + fun topicFts_insertAllOnce_countMatches() = runTest { insertAllNewsResourceFtsEntities() assertEquals(topicFtsEntities.size, topicFtsDao.getCount().first()) } @Test - fun insertAllTwice_countMatches() = runTest { + fun topicFts_insertAllTwice_countMatches() = runTest { repeat(2) { topicFtsDao.insertAll(topics = topicFtsEntities) } @@ -72,7 +72,7 @@ class TopicFtsDaoTest { } @Test - fun deleteAndInsertAllThreeTimes_countMatches() = runTest { + fun topicFts_deleteAndInsertAllThreeTimes_countMatches() = runTest { repeat(3) { topicFtsDao.deleteAllAndInsertAll(topics = topicFtsEntities) } From 0798beeb3776790f163a2b7b995b811c1f5062d4 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 22:08:02 +0900 Subject: [PATCH 26/33] Change test name style to `given_when_then`. Change-Id: I06be4b51f177d0084df34f27bb334e108987b462 --- .../nowinandroid/core/data/SearchContentsRepositoryTest.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt b/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt index c8f76aefd5..b303484dfc 100644 --- a/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt +++ b/core/data/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/data/SearchContentsRepositoryTest.kt @@ -52,6 +52,7 @@ class SearchContentsRepositoryTest { @Before fun setUp() { val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder( context, NiaDatabase::class.java, @@ -74,7 +75,7 @@ class SearchContentsRepositoryTest { fun closeDb() = db.close() @Test - fun whenPopulateFtsDataTwice_PopulateFtsOnce() = runTest { + fun ftsEntities_insertAllTwice_countMatches() = runTest { allDataPreSetting() repeat(2) { searchContentsRepository.populateFtsData() @@ -84,7 +85,7 @@ class SearchContentsRepositoryTest { } @Test - fun whenSearchAndroid_ReturnResult() = runTest { + fun searchQuery_searchAndroid_findResult() = runTest { allDataPreSetting() searchContentsRepository.populateFtsData() From 81ad8f413317e2810d39813a4e42424e9943670e Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 26 Apr 2024 22:15:59 +0900 Subject: [PATCH 27/33] Change test name to `insertAllThreeTimes`. Change-Id: I7a15694c4bc12a9002e2a6f336d5d05ef372225b --- .../nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt | 2 +- .../apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt index c26c0ef7e3..5f03ba2813 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceFtsDaoTest.kt @@ -72,7 +72,7 @@ class NewsResourceFtsDaoTest { } @Test - fun newsResourceFts_deleteAndInsertAllThreeTimes_countMatches() = runTest { + fun newsResourceFts_insertAllThreeTimes_countMatches() = runTest { repeat(3) { newsResourceFtsDao.deleteAllAndInsertAll(newsResources = newsResourceFtsEntities) } diff --git a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt index 91c6337928..a640948d58 100644 --- a/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt +++ b/core/database/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/core/database/dao/TopicFtsDaoTest.kt @@ -72,7 +72,7 @@ class TopicFtsDaoTest { } @Test - fun topicFts_deleteAndInsertAllThreeTimes_countMatches() = runTest { + fun topicFts_insertAllThreeTimes_countMatches() = runTest { repeat(3) { topicFtsDao.deleteAllAndInsertAll(topics = topicFtsEntities) } From 81db4fbd1806f938f7aa4fb85af4934785f83851 Mon Sep 17 00:00:00 2001 From: Ben Weiss Date: Mon, 9 Dec 2024 12:32:57 +0100 Subject: [PATCH 28/33] Update core/data/build.gradle.kts --- core/data/build.gradle.kts | 1 - 1 file changed, 1 deletion(-) diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index 4e4cc965ca..02d75201fa 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -17,7 +17,6 @@ plugins { alias(libs.plugins.nowinandroid.android.library) alias(libs.plugins.nowinandroid.android.library.jacoco) alias(libs.plugins.nowinandroid.android.hilt) - alias(libs.plugins.nowinandroid.android.room) id("kotlinx-serialization") } From c5eb7c5cb6f80bb586ee4676bb11173bfc4bd991 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 9 Dec 2024 21:39:47 +0900 Subject: [PATCH 29/33] Change android.hilt to hilt. Change-Id: I3ac7a75070a7efc6a281bedb48c3e11bd257af90 --- core/data/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index 02d75201fa..8ca52fea28 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -16,7 +16,7 @@ plugins { alias(libs.plugins.nowinandroid.android.library) alias(libs.plugins.nowinandroid.android.library.jacoco) - alias(libs.plugins.nowinandroid.android.hilt) + alias(libs.plugins.nowinandroid.hilt) id("kotlinx-serialization") } From a022eb66a20d5ad6a882b8654dd2b38ae933c61e Mon Sep 17 00:00:00 2001 From: Don Turner Date: Thu, 19 Dec 2024 13:45:00 +0000 Subject: [PATCH 30/33] Empty commit to trigger build workflow From 7c2bdac71a38751c330cb5d70c0237ace0273dc5 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 20 Dec 2024 22:52:46 +0900 Subject: [PATCH 31/33] Add missing dependency. - androidTestImplementation(projects.core.testing) Change-Id: Icb626d1f6c84085aa77a0352d89dc910cd27b08e --- core/database/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/core/database/build.gradle.kts b/core/database/build.gradle.kts index 8bab355b4d..792d776aaf 100644 --- a/core/database/build.gradle.kts +++ b/core/database/build.gradle.kts @@ -30,6 +30,7 @@ dependencies { implementation(libs.kotlinx.datetime) + androidTestImplementation(projects.core.testing) androidTestImplementation(libs.androidx.test.core) androidTestImplementation(libs.androidx.test.runner) androidTestImplementation(libs.kotlinx.coroutines.test) From b775df1fa5a0535a2b8094d09c761dce842d26c7 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 20 Dec 2024 23:00:01 +0900 Subject: [PATCH 32/33] Add missing plugin. - alias(libs.plugins.nowinandroid.android.room) Change-Id: Ibafaadbfdf8d8c2f7e6659059a145026c483c7fc --- core/data/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index aef9ca5545..03ee5cd846 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -17,6 +17,7 @@ plugins { alias(libs.plugins.nowinandroid.android.library) alias(libs.plugins.nowinandroid.android.library.jacoco) alias(libs.plugins.nowinandroid.hilt) + alias(libs.plugins.nowinandroid.android.room) id("kotlinx-serialization") } From e9dcf10bcfe7f2a300c74d25ab9133254bc72986 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 20 Dec 2024 23:05:19 +0900 Subject: [PATCH 33/33] Add missing dependency. - androidTestImplementation(libs.androidx.test.core) Change-Id: I60d0f4150a54181f6e9ee3d2cfffc9ea56ae6e62 --- core/data/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index 03ee5cd846..3f72d9a1ae 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -51,4 +51,5 @@ dependencies { testImplementation(projects.core.testing) androidTestImplementation(projects.core.testing) + androidTestImplementation(libs.androidx.test.core) }