-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Write Test Code In 'core.domain' package #1359
base: main
Are you sure you want to change the base?
Changes from all commits
77677e9
e1ee9b4
31a4560
7313f6c
0f711be
17c937f
db16878
b782291
98fd9ee
16d73b3
80c147c
5b1c704
ba04452
bf1b117
2a9fad4
212e957
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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.advanceUntilIdle | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
/** | ||
* Unit test for [GetRecentSearchQueriesUseCase] | ||
*/ | ||
class GetRecentSearchQueriesUseCaseTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check out https://github.com/android/nowinandroid/wiki/Testing-strategy-and-how-to-test and apply conventions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. i will check this convention. |
||
|
||
@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. | ||
squart300kg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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( | ||
10, | ||
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) | ||
advanceUntilIdle() | ||
} | ||
|
||
// Check that search queries is ordered in recently up to 10 | ||
assertEquals( | ||
testRecentSearchQueries.reversed().take(10), | ||
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", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,14 +19,16 @@ 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.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.flow | ||
|
||
class TestRecentSearchRepository : RecentSearchRepository { | ||
|
||
private val cachedRecentSearches: MutableList<RecentSearchQuery> = mutableListOf() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about using MutableSharedFlow like another TestRepository? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yongsuk44 I agree with your opinion. but because i didn't fully understand using 'MutableList' instead of "MutableSahredFlow' like other unit test code, i did not change this code. and the primary purpose of this 'PR' is writing the test code of 'UseCase' class in domain layer. :) i have thought that your opinion is reasonable so far 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @squart300kg |
||
|
||
override fun getRecentSearchQueries(limit: Int): Flow<List<RecentSearchQuery>> = | ||
flowOf(cachedRecentSearches.sortedByDescending { it.queriedDate }.take(limit)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this change necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, i think that this code need to be changed. because the flow producers such as 'flowOf' and 'flow { emit() }' have different in emitting updated list. and although one depth inner code is same when emitting data by 'flow { emit() }, two depth inner code is different. because of this, i realized that the way of data emitting can not be same. [two depth inner code] because of this change, i had tested 'SearchViewModelTest' and passed. so, i think that this change is reasonable. how about your opinion? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't understand this response. |
||
flow { | ||
emit(cachedRecentSearches.sortedByDescending { it.queriedDate }.take(limit)) | ||
} | ||
|
||
override suspend fun insertOrReplaceRecentSearch(searchQuery: String) { | ||
cachedRecentSearches.add(RecentSearchQuery(searchQuery)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is redundant, please remove.