-
Notifications
You must be signed in to change notification settings - Fork 0
UseCase/Repository/Testの見直し #301
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b47e1ae
AppRepositoryImplのコンストラクタでPackageManagerをDIするように変更
ptkNktq 1019a48
import/exportのUseCase#invokeをcontext不要に修正
ptkNktq 13e963f
テスト修正
ptkNktq c9c4f79
data層にもテスト用ライブラリ追加
ptkNktq 73ab137
AppRepositoryのテスト追加
ptkNktq 6accb47
UserSettingsRepositoryのテスト追加
ptkNktq 5bff627
mockk導入
ptkNktq 248d75b
usecaseのテスト時はmockkのrepositoryを使うように変更
ptkNktq 0fc7b13
repのテスト更新
ptkNktq b150cdb
mockk-android追加
ptkNktq 2a18193
androidTestとtest分離
ptkNktq 4413015
androidTestの修正
ptkNktq aac6226
不要な依存関係削除
ptkNktq b99d43b
viewmodelの更新
ptkNktq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...data/repository/src/androidTest/kotlin/me/nya_n/notificationnotifier/AppRepositoryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package me.nya_n.notificationnotifier | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import com.google.common.truth.Truth.assertThat | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
| import kotlinx.coroutines.test.runTest | ||
| import me.nya_n.notificationnotifier.data.repository.AppRepository | ||
| import me.nya_n.notificationnotifier.data.repository.impl.AppRepositoryImpl | ||
| import me.nya_n.notificationnotifier.data.repository.source.DB | ||
| import me.nya_n.notificationnotifier.model.FilterCondition | ||
| import me.nya_n.notificationnotifier.model.InstalledApp | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| @Suppress("NonAsciiCharacters") | ||
| @RunWith(AndroidJUnit4::class) | ||
| class AppRepositoryTest { | ||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| private val testDispatcher = UnconfinedTestDispatcher() | ||
| private lateinit var appRepository: AppRepository | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
| val db = DB.get(appContext, isInMemory = true).apply { | ||
| clearAllTables() | ||
| } | ||
| appRepository = AppRepositoryImpl( | ||
| appContext.packageManager, | ||
| db.filterConditionDao(), | ||
| db.targetAppDao(), | ||
| testDispatcher | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `通知対象アプリの追加、取得、削除`() { | ||
| runTest(testDispatcher) { | ||
| val app = InstalledApp("sample", "com.sample.www") | ||
| appRepository.addTargetApp(app) | ||
|
|
||
| val added = appRepository.getTargetAppList() | ||
| assertThat(added).hasSize(1) | ||
| assertThat(added.first()).isEqualTo(app) | ||
|
|
||
| appRepository.deleteTargetApp(app) | ||
| val deleted = appRepository.getTargetAppList() | ||
| assertThat(deleted).isEmpty() | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| fun `通知条件の追加、取得、更新`() { | ||
| runTest(testDispatcher) { | ||
| val packageName = "com.sample.www" | ||
|
|
||
| // データなし | ||
| assertThat(appRepository.getFilterCondition(packageName)).isNull() | ||
| assertThat(appRepository.getFilterConditionOrDefault(packageName)) | ||
| .isEqualTo(FilterCondition.default(packageName)) | ||
|
|
||
| // 追加 | ||
| val added = FilterCondition(packageName, false, "test") | ||
| appRepository.saveFilterCondition(added) | ||
| assertThat(appRepository.getFilterCondition(packageName)).isEqualTo(added) | ||
|
|
||
| // メッセージ条件の更新 | ||
| val updatedCondition = added.copy(condition = "updated") | ||
| appRepository.saveFilterCondition(updatedCondition) | ||
| assertThat(appRepository.getFilterCondition(packageName)).isEqualTo(updatedCondition) | ||
|
|
||
| // サマリー条件の更新 | ||
| val updatedSummary = updatedCondition.copy(isIgnoreSummary = true) | ||
| appRepository.saveFilterCondition(updatedSummary) | ||
| assertThat(appRepository.getFilterCondition(packageName)).isEqualTo(updatedSummary) | ||
| } | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
...sitory/src/androidTest/kotlin/me/nya_n/notificationnotifier/UserSettingsRepositoryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package me.nya_n.notificationnotifier | ||
|
|
||
| import androidx.core.content.edit | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import com.google.common.truth.Truth.assertThat | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
| import kotlinx.coroutines.test.runTest | ||
| import me.nya_n.notificationnotifier.data.repository.UserSettingsRepository | ||
| import me.nya_n.notificationnotifier.data.repository.impl.UserSettingsRepositoryImpl | ||
| import me.nya_n.notificationnotifier.data.repository.source.UserSettingsDataStore | ||
| import me.nya_n.notificationnotifier.data.repository.util.SharedPreferenceProvider | ||
| import me.nya_n.notificationnotifier.model.UserSettings | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| @Suppress("NonAsciiCharacters") | ||
| @RunWith(AndroidJUnit4::class) | ||
| class UserSettingsRepositoryTest { | ||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| private val testDispatcher = UnconfinedTestDispatcher() | ||
| private lateinit var userSettingsRepository: UserSettingsRepository | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
| userSettingsRepository = UserSettingsRepositoryImpl( | ||
| UserSettingsDataStore( | ||
| SharedPreferenceProvider.create( | ||
| appContext, | ||
| UserSettingsDataStore.DATA_STORE_NAME | ||
| ).apply { | ||
| edit { | ||
| clear() | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ユーザー設定の保存、取得、更新`() { | ||
| runTest(testDispatcher) { | ||
| val data = UserSettings("192.168.10.18", 8484, false) | ||
| userSettingsRepository.saveUserSettings(data) | ||
| assertThat(userSettingsRepository.getUserSettings()).isEqualTo(data) | ||
|
|
||
| val updated = data.copy(port = 2525, isPackageVisibilityGranted = true) | ||
| userSettingsRepository.saveUserSettings(updated) | ||
| assertThat(userSettingsRepository.getUserSettings()).isEqualTo(updated) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...ository/src/main/kotlin/me/nya_n/notificationnotifier/data/repository/BackupRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package me.nya_n.notificationnotifier.data.repository | ||
|
|
||
| import android.net.Uri | ||
|
|
||
| interface BackupRepository { | ||
| /** [uri]に通知対象や条件、設定を保存 | ||
| * @param uri 保存先 | ||
| * @param data 保存するデータ | ||
| */ | ||
| suspend fun exportToUri(uri: Uri, data: String) | ||
|
|
||
| /** [uri]から通知対象や条件、設定を復元 | ||
| * @param uri 読み込み元 | ||
| * @return 復元したデータ | ||
| */ | ||
| suspend fun importFromUri(uri: Uri): String | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...rc/main/kotlin/me/nya_n/notificationnotifier/data/repository/impl/BackupRepositoryImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package me.nya_n.notificationnotifier.data.repository.impl | ||
|
|
||
| import android.content.Context | ||
| import android.net.Uri | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import me.nya_n.notificationnotifier.data.repository.BackupRepository | ||
| import java.io.BufferedReader | ||
| import java.io.InputStreamReader | ||
|
|
||
| class BackupRepositoryImpl( | ||
| private val context: Context, | ||
| private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO | ||
| ) : BackupRepository { | ||
| override suspend fun exportToUri(uri: Uri, data: String) { | ||
| withContext(coroutineDispatcher) { | ||
| context.contentResolver.openOutputStream(uri)?.use { | ||
| it.write(data.toByteArray()) | ||
| } ?: throw RuntimeException("Failed to open output stream.") | ||
| } | ||
| } | ||
|
|
||
| override suspend fun importFromUri(uri: Uri): String { | ||
| val sb = StringBuilder() | ||
| withContext(coroutineDispatcher) { | ||
| context.contentResolver.openInputStream(uri)?.use { input -> | ||
| BufferedReader(InputStreamReader(input)).use { reader -> | ||
| sb.append(reader.readLine()) | ||
| } ?: throw RuntimeException("Failed to read input stream.") | ||
| } ?: throw RuntimeException("Failed to open input stream.") | ||
| } | ||
| return sb.toString() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
importFromUriの実装に問題があります2つの問題があります:
Line 30のnullチェックが機能しない:
BufferedReader.use { }は常にラムダの結果(sb.append()の戻り値であるStringBuilder)を返すため、?: throw RuntimeException("Failed to read input stream.")は決して実行されません。readLine()がnullを返す場合の処理: ストリームが空の場合、reader.readLine()はnullを返し、sb.append(null)は文字列"null"を追加してしまいます。🐛 修正案
override suspend fun importFromUri(uri: Uri): String { - val sb = StringBuilder() withContext(coroutineDispatcher) { context.contentResolver.openInputStream(uri)?.use { input -> BufferedReader(InputStreamReader(input)).use { reader -> - sb.append(reader.readLine()) - } ?: throw RuntimeException("Failed to read input stream.") + return@withContext reader.readLine() + ?: throw RuntimeException("Failed to read input stream.") + } } ?: throw RuntimeException("Failed to open input stream.") } - return sb.toString() }🤖 Prompt for AI Agents