pull/1359/merge
sangyoon 3 days ago committed by GitHub
commit 0fc615de95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -42,7 +42,7 @@ class GetFollowableTopicsUseCaseTest {
)
@Test
fun whenNoParams_followableTopicsAreReturnedWithNoSorting() = runTest {
fun whenSortOrderIsByNone_followableTopicsAreReturnedWithNoSorting() = runTest {
// Obtain a stream of followable topics.
val followableTopics = useCase()

@ -0,0 +1,81 @@
/*
* 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.domain
import com.google.samples.apps.nowinandroid.core.testing.repository.TestRecentSearchRepository
import com.google.samples.apps.nowinandroid.core.testing.util.MainDispatcherRule
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertEquals
class GetRecentSearchQueriesUseCaseTest {
@get:Rule
val mainDispatcherRule = MainDispatcherRule()
private val recentSearchRepository = TestRecentSearchRepository()
private val useCase = GetRecentSearchQueriesUseCase(
recentSearchRepository,
)
@Test
fun whenQueryLimitIsBy10_recentSearchQueriesAreReturnedUpTo10() = runTest {
// Obtain a stream of recent search queries with no param.
val recentSearchQueries = useCase()
// insert search queries over 10.
testRecentSearchQueries.forEach { query ->
recentSearchRepository.insertOrReplaceRecentSearch(query)
}
// Check that the number of recent search queries are up to 10.
assertEquals(
expected = 10,
actual = recentSearchQueries.first().size,
)
}
@Test
fun whenReceivingRecentSearchQueries_recentSearchQueriesAreReturnedInRecentOrder() = runTest {
// Obtain a stream of recent search queries with no param.
val recentSearchQueries = useCase()
// insert search queries over 10.
testRecentSearchQueries.forEach { query ->
recentSearchRepository.insertOrReplaceRecentSearch(query)
}
// Check that search queries is ordered in recently up to 10
assertEquals(
expected = testRecentSearchQueries.reversed().take(10),
actual = recentSearchQueries.first().map { it.query },
)
}
}
private val testRecentSearchQueries = listOf(
"Compose", "Wear OS",
"Jetpack", "Headlines",
"Architecture", "UI",
"Testing", "Android Studio",
"Performance", "New API",
"Games", "Android TV",
"Camera", "Media",
)

@ -18,19 +18,26 @@ package com.google.samples.apps.nowinandroid.core.testing.repository
import com.google.samples.apps.nowinandroid.core.data.model.RecentSearchQuery
import com.google.samples.apps.nowinandroid.core.data.repository.RecentSearchRepository
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
class TestRecentSearchRepository : RecentSearchRepository {
private val cachedRecentSearches: MutableList<RecentSearchQuery> = mutableListOf()
private val cachedRecentSearches = MutableSharedFlow<MutableList<RecentSearchQuery>>(
replay = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST,
).apply { tryEmit(mutableListOf()) }
override fun getRecentSearchQueries(limit: Int): Flow<List<RecentSearchQuery>> =
flowOf(cachedRecentSearches.sortedByDescending { it.queriedDate }.take(limit))
cachedRecentSearches.map { it.sortedByDescending { it.queriedDate }.take(limit) }
override suspend fun insertOrReplaceRecentSearch(searchQuery: String) {
cachedRecentSearches.add(RecentSearchQuery(searchQuery))
val searchQueries = cachedRecentSearches.map { it.apply { add(RecentSearchQuery(searchQuery)) } }.first()
cachedRecentSearches.emit(searchQueries)
}
override suspend fun clearRecentSearches() = cachedRecentSearches.clear()
override suspend fun clearRecentSearches() = cachedRecentSearches.emit(mutableListOf())
}

Loading…
Cancel
Save