Merge db62cf6da6
into 30a5af5b1f
commit
a693a53406
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
/**
|
||||
* Instrumentation tests for [SearchContentsRepository].
|
||||
*/
|
||||
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<Context>()
|
||||
|
||||
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 ftsEntities_insertAllTwice_countMatches() = runTest {
|
||||
allDataPreSetting()
|
||||
repeat(2) {
|
||||
searchContentsRepository.populateFtsData()
|
||||
}
|
||||
advanceUntilIdle()
|
||||
assertEquals(7, searchContentsRepository.getSearchContentsCount().first())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun searchQuery_searchAndroid_findResult() = 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)
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 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
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/**
|
||||
* Instrumentation tests for [NewsResourceFtsDao].
|
||||
*/
|
||||
class NewsResourceFtsDaoTest {
|
||||
|
||||
private lateinit var newsResourceFtsDao: NewsResourceFtsDao
|
||||
private lateinit var db: NiaDatabase
|
||||
|
||||
private lateinit var newsResourceFtsEntities: List<NewsResourceFtsEntity>
|
||||
|
||||
@Before
|
||||
fun createDb() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
db = Room.inMemoryDatabaseBuilder(
|
||||
context,
|
||||
NiaDatabase::class.java,
|
||||
).build()
|
||||
newsResourceFtsDao = db.newsResourceFtsDao()
|
||||
|
||||
newsResourceFtsEntities = newsResourceEntitiesTestData.map {
|
||||
it.asFtsEntity()
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
fun closeDb() = db.close()
|
||||
|
||||
@Test
|
||||
fun newsResourceFts_insertAllOnce_countMatches() = runTest {
|
||||
insertAllNewsResourceFtsEntities()
|
||||
assertEquals(newsResourceFtsEntities.size, newsResourceFtsDao.getCount().first())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun newsResourceFts_insertAllTwice_countMatches() = runTest {
|
||||
repeat(2) {
|
||||
newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities)
|
||||
}
|
||||
assertEquals(newsResourceFtsEntities.size * 2, newsResourceFtsDao.getCount().first())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun newsResourceFts_insertAllThreeTimes_countMatches() = runTest {
|
||||
repeat(3) {
|
||||
newsResourceFtsDao.deleteAllAndInsertAll(newsResources = newsResourceFtsEntities)
|
||||
}
|
||||
assertEquals(newsResourceFtsEntities.size, newsResourceFtsDao.getCount().first())
|
||||
}
|
||||
|
||||
private suspend fun insertAllNewsResourceFtsEntities() =
|
||||
newsResourceFtsDao.insertAll(newsResources = newsResourceFtsEntities)
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 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
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/**
|
||||
* Instrumentation tests for [TopicFtsDao].
|
||||
*/
|
||||
class TopicFtsDaoTest {
|
||||
|
||||
private lateinit var topicFtsDao: TopicFtsDao
|
||||
private lateinit var db: NiaDatabase
|
||||
|
||||
private lateinit var topicFtsEntities: List<TopicFtsEntity>
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
db = Room.inMemoryDatabaseBuilder(
|
||||
context,
|
||||
NiaDatabase::class.java,
|
||||
).build()
|
||||
topicFtsDao = db.topicFtsDao()
|
||||
|
||||
topicFtsEntities = topicEntitiesTestData.map {
|
||||
it.asFtsEntity()
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
fun closeDb() = db.close()
|
||||
|
||||
@Test
|
||||
fun topicFts_insertAllOnce_countMatches() = runTest {
|
||||
insertAllNewsResourceFtsEntities()
|
||||
assertEquals(topicFtsEntities.size, topicFtsDao.getCount().first())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun topicFts_insertAllTwice_countMatches() = runTest {
|
||||
repeat(2) {
|
||||
topicFtsDao.insertAll(topics = topicFtsEntities)
|
||||
}
|
||||
assertEquals(topicFtsEntities.size * 2, topicFtsDao.getCount().first())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun topicFts_insertAllThreeTimes_countMatches() = runTest {
|
||||
repeat(3) {
|
||||
topicFtsDao.deleteAllAndInsertAll(topics = topicFtsEntities)
|
||||
}
|
||||
assertEquals(topicFtsEntities.size, topicFtsDao.getCount().first())
|
||||
}
|
||||
|
||||
private suspend fun insertAllNewsResourceFtsEntities() =
|
||||
topicFtsDao.insertAll(topics = topicFtsEntities)
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
/**
|
||||
* Test data list of [TopicEntity].
|
||||
*/
|
||||
val topicEntitiesTestData: List<TopicEntity> = 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 = "",
|
||||
),
|
||||
)
|
Loading…
Reference in new issue