-
Notifications
You must be signed in to change notification settings - Fork 0
인물 삭제 기능을 개선하고 테스트 코드를 작성한다 #63
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
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
core/data/impl/src/main/java/com/example/metasearch/core/data/impl/di/CoroutineModule.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,24 @@ | ||
| package com.example.metasearch.core.data.impl.di | ||
|
|
||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.Dispatchers | ||
| import javax.inject.Qualifier | ||
| import javax.inject.Singleton | ||
|
|
||
| @Qualifier | ||
| @Retention(AnnotationRetention.BINARY) | ||
| annotation class IoDispatcher | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object CoroutineModule { | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| @IoDispatcher | ||
| fun provideIoDispatcher(): CoroutineDispatcher = Dispatchers.IO | ||
| } |
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
87 changes: 87 additions & 0 deletions
87
...rc/test/java/com/example/metasearch/core/data/impl/repository/PersonRepositoryImplTest.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,87 @@ | ||
| package com.example.metasearch.core.data.impl.repository | ||
|
|
||
| import android.content.Context | ||
| import com.example.metasearch.core.data.api.repository.DatabaseNameRepository | ||
| import com.example.metasearch.core.model.PersonModel | ||
| import com.example.metasearch.core.network.service.AIService | ||
| import com.example.metasearch.core.network.service.WebService | ||
| import com.example.metasearch.core.room.api.dao.PersonDao | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.StandardTestDispatcher | ||
| import kotlinx.coroutines.test.advanceUntilIdle | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import org.mockito.Mock | ||
| import org.mockito.MockitoAnnotations | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.never | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| class PersonRepositoryImplTest { | ||
|
|
||
| @Mock private lateinit var mockDao: PersonDao | ||
|
|
||
| @Mock private lateinit var mockAiService: AIService | ||
|
|
||
| @Mock private lateinit var mockWebService: WebService | ||
|
|
||
| @Mock private lateinit var mockDbRepo: DatabaseNameRepository | ||
|
|
||
| @Mock private lateinit var mockContext: Context | ||
|
|
||
| private val testDispatcher = StandardTestDispatcher() | ||
| private lateinit var repository: PersonRepositoryImpl | ||
|
|
||
| private val fakePerson = PersonModel(id = 1L, name = "test_uuid", inputName = "홍길동") | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| MockitoAnnotations.openMocks(this) | ||
| // 생성자 주입을 통해 테스트 디스패처 전달 | ||
| repository = PersonRepositoryImpl( | ||
| personDao = mockDao, | ||
| aiService = mockAiService, | ||
| webService = mockWebService, | ||
| databaseNameRepository = mockDbRepo, | ||
| ioDispatcher = testDispatcher, | ||
| context = mockContext, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `모든 서버 삭제가 성공하면 로컬 DB에서도 인물을 삭제한다`() = runTest(testDispatcher) { | ||
| // Given | ||
| whenever(mockDbRepo.getPersistentDeviceDatabaseName()).thenReturn("db_name") | ||
|
|
||
| // When | ||
| val result = repository.deleteAnalyzedPerson(fakePerson) | ||
| advanceUntilIdle() // 모든 비동기 작업(async)이 완료될 때까지 대기 | ||
|
|
||
| // Then | ||
| assertTrue(result.isSuccess) | ||
| verify(mockWebService).deleteEntity(any()) | ||
| verify(mockAiService).deletePerson(any(), any()) | ||
| verify(mockDao).deletePersonById(fakePerson.id) | ||
| } | ||
|
|
||
| @Test | ||
| fun `서버 삭제 중 하나라도 실패하면 로컬 DB 삭제를 호출하지 않는다`() = runTest(testDispatcher) { | ||
| // Given | ||
| whenever(mockDbRepo.getPersistentDeviceDatabaseName()).thenReturn("db_name") | ||
| // AI 서비스에서 예외 발생 시나리오 | ||
| whenever(mockAiService.deletePerson(any(), any())).thenThrow(RuntimeException("AI Server Error")) | ||
|
|
||
| // When | ||
| val result = repository.deleteAnalyzedPerson(fakePerson) | ||
| advanceUntilIdle() | ||
|
|
||
| // Then | ||
| assertTrue(result.isFailure) | ||
| // 서버가 터졌으므로 로컬 DB 삭제는 절대 실행되면 안 됨 | ||
| verify(mockDao, never()).deletePersonById(fakePerson.id) | ||
| } | ||
| } | ||
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
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.
🛠️ Refactor suggestion | 🟠 Major
Close MockitoAnnotations to prevent resource leaks.
MockitoAnnotations.openMocks()returns anAutoCloseablethat should be closed after each test to avoid resource leaks.♻️ Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents