From 78882943617750328cc2e7f5a5830fad81fecceb Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:59:52 +0900 Subject: [PATCH 1/4] Feat: RecentViewModelTest --- .../kr/co/bookmark/BookmarkViewModelTest.kt | 11 --- .../kr/co/recent/RecentViewModelTest.kt | 74 +++++++++++++++++++ 2 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt diff --git a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt index 054fb5e..fb33f5b 100644 --- a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt +++ b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt @@ -77,16 +77,5 @@ internal class BookmarkViewModelTest { createdAt = LocalDateTime.now(), lastModified = LocalDateTime.now() ) - - val FOLDER_DUMMY = FileInfo( - name = "DUMMY", - path = "", - type = PDF, - isDirectory = true, - isHidden = false, - size = 0, - createdAt = LocalDateTime.now(), - lastModified = LocalDateTime.now() - ) } } \ No newline at end of file diff --git a/feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt b/feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt new file mode 100644 index 0000000..ce0ca17 --- /dev/null +++ b/feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt @@ -0,0 +1,74 @@ +package kr.co.recent + +import io.mockk.MockKAnnotations +import io.mockk.coEvery +import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.test.runTest +import kr.co.data.repository.RecentRepository +import kr.co.model.FileInfo +import kr.co.model.FileInfo.Type.PDF +import kr.co.model.RecentSideEffect +import kr.co.model.RecentUiIntent +import kr.co.testing.rule.CoroutineTestRule +import kr.co.testing.util.testWithItem +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import java.time.LocalDateTime +import kotlin.test.assertEquals + +internal class RecentViewModelTest { + + @get: Rule + val coroutineTestRule = CoroutineTestRule() + + private lateinit var viewModel: RecentViewModel + + @MockK + private lateinit var recentRepository: RecentRepository + + @Before + fun setup() { + MockKAnnotations.init(this) + viewModel = RecentViewModel(recentRepository) + } + + @Test + fun `Given a Unit when Init intent is handled then state is updated`() = runTest { + val dummy = listOf(PDF_DUMMY, PDF_DUMMY) + + coEvery { recentRepository.get() } returns flowOf(dummy) + + viewModel.handleIntent(RecentUiIntent.Init) + + viewModel.uiState.testWithItem { + assertEquals(files, dummy) + } + } + + @Test + fun `Given a file when ClickFile intent is handled then navigate to pdf`() = runTest { + coEvery { recentRepository.insert(PDF_DUMMY) } returns Unit + + viewModel.handleIntent(RecentUiIntent.ClickFile(PDF_DUMMY)) + + viewModel.sideEffect.testWithItem { + assert(this is RecentSideEffect.NavigateToPdf) + assertEquals((this as RecentSideEffect.NavigateToPdf).path,PDF_DUMMY.path) + } + } + + companion object { + val PDF_DUMMY = FileInfo( + name = "DUMMY.pdf", + path = "", + type = PDF, + isDirectory = false, + isHidden = false, + size = 0, + createdAt = LocalDateTime.now(), + lastModified = LocalDateTime.now() + ) + } +} \ No newline at end of file From 22debabef43a30fe7ff62a55456abf3b10bc8758 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Sat, 18 Jan 2025 20:19:06 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Refactor:=20assert=20=EC=88=9C=EC=84=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kr/co/bookmark/BookmarkViewModelTest.kt | 17 +++++++---------- .../kr/co/explore/ExploreViewModelTest.kt | 4 ++-- .../kotlin/kr/co/recent/RecentViewModelTest.kt | 4 ++-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt index fb33f5b..03fa142 100644 --- a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt +++ b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt @@ -1,6 +1,5 @@ package kr.co.bookmark -import app.cash.turbine.test import io.mockk.MockKAnnotations import io.mockk.coEvery import io.mockk.impl.annotations.MockK @@ -13,6 +12,7 @@ import kr.co.model.BookmarkUiIntent import kr.co.model.FileInfo import kr.co.model.FileInfo.Type.PDF import kr.co.testing.rule.CoroutineTestRule +import kr.co.testing.util.testWithItem import org.junit.Before import org.junit.Rule import org.junit.Test @@ -35,7 +35,7 @@ internal class BookmarkViewModelTest { @Before fun setup() { MockKAnnotations.init(this) - viewModel = BookmarkViewModel(bookmarkRepository,recentRepository) + viewModel = BookmarkViewModel(bookmarkRepository, recentRepository) } @Test @@ -46,9 +46,8 @@ internal class BookmarkViewModelTest { viewModel.handleIntent(BookmarkUiIntent.Init) - viewModel.uiState.test { - val state = awaitItem() - assertEquals(state.files, dummy) + viewModel.uiState.testWithItem { + assertEquals(dummy, files) } } @@ -58,11 +57,9 @@ internal class BookmarkViewModelTest { viewModel.handleIntent(BookmarkUiIntent.ClickFile(file)) - viewModel.sideEffect.test { - awaitItem().also { - assert(it is BookmarkSideEffect.NavigateToPdf) - assert((it as BookmarkSideEffect.NavigateToPdf).path == file.path) - } + viewModel.sideEffect.testWithItem { + assert(this is BookmarkSideEffect.NavigateToPdf) + assertEquals(file.path, (this as BookmarkSideEffect.NavigateToPdf).path) } } diff --git a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt index 51cad01..b04a5e4 100644 --- a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt +++ b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt @@ -80,7 +80,7 @@ internal class ExploreViewModelTest { viewModel.sideEffect.testWithItem { assert(this is ExploreSideEffect.NavigateToPdf) - assertEquals((this as ExploreSideEffect.NavigateToPdf).path,file.path) + assertEquals(file.path, (this as ExploreSideEffect.NavigateToPdf).path) } } @@ -92,7 +92,7 @@ internal class ExploreViewModelTest { viewModel.sideEffect.testWithItem { assert(this is ExploreSideEffect.NavigateToFolder) - assertEquals((this as ExploreSideEffect.NavigateToFolder).path,folder.path) + assertEquals(folder.path, (this as ExploreSideEffect.NavigateToFolder).path) } } companion object { diff --git a/feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt b/feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt index ce0ca17..48da71b 100644 --- a/feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt +++ b/feature/recent/src/test/kotlin/kr/co/recent/RecentViewModelTest.kt @@ -43,7 +43,7 @@ internal class RecentViewModelTest { viewModel.handleIntent(RecentUiIntent.Init) viewModel.uiState.testWithItem { - assertEquals(files, dummy) + assertEquals(dummy, files) } } @@ -55,7 +55,7 @@ internal class RecentViewModelTest { viewModel.sideEffect.testWithItem { assert(this is RecentSideEffect.NavigateToPdf) - assertEquals((this as RecentSideEffect.NavigateToPdf).path,PDF_DUMMY.path) + assertEquals(PDF_DUMMY.path, (this as RecentSideEffect.NavigateToPdf).path) } } From 9469b985fb46a23e9920f3582c818bd92a9c33fc Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Sun, 19 Jan 2025 17:02:31 +0900 Subject: [PATCH 3/4] =?UTF-8?q?Fix:=20=EB=B3=B5=EC=88=98=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=ED=8C=8C=EC=9D=BC=20=EA=B2=80=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev-test.yml | 42 +++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/.github/workflows/dev-test.yml b/.github/workflows/dev-test.yml index 1fbf0d1..b4f5b57 100644 --- a/.github/workflows/dev-test.yml +++ b/.github/workflows/dev-test.yml @@ -19,13 +19,24 @@ jobs: - name: Filter out test files id: test_files run: | + echo "Changed files: ${{ steps.changed_files.outputs.all_changed_files }}" + TEST_FILES=$(echo "${{ steps.changed_files.outputs.all_changed_files }}" \ | tr ' ' '\n' \ - | grep -E '^.+/src/test/(java|kotlin)/.+\.(kt|java)$' \ - | sed -E 's|^(.+)/src/test/(java|kotlin)/(.+)\.(kt|java)$|\1 \3|' \ - | sed 's|/|.|g') - echo "TEST_FILES=$TEST_FILES" >> $GITHUB_ENV - if [ -n "$TEST_FILES" ]; then + | grep -E '^.+/src/test/(java|kotlin)/.+\.(kt|java)$') + echo "Filtered by grep: $TEST_FILES" + + TEST_FILES=$(echo "$TEST_FILES" \ + | sed -E 's/^(.+)\/src\/test\/(java|kotlin)\/(.+)\.(kt|java)$/\1 \3/' \ + | sed 's/\//./g') + echo "Processed by sed: $TEST_FILES" + + TEST_FILES_SINGLE_LINE=$(echo "$TEST_FILES" | tr '\n' ' ') + echo "TEST_FILES=$TEST_FILES_SINGLE_LINE" >> $GITHUB_ENV + + echo "$TEST_FILES" > test_files.txt + + if [ -s test_files.txt ]; then echo "any_changed=true" >> $GITHUB_ENV else echo "any_changed=false" >> $GITHUB_ENV @@ -33,7 +44,14 @@ jobs: - name: Debug Changed Files run: | - echo "changed files: ${{ env.TEST_FILES }}" + if [ -f test_files.txt ]; then + echo "changed files:" + cat test_files.txt + else + echo "No changed files found." + fi + + # Check the "any_changed" environment variable echo "files changed: ${{ env.any_changed }}" - name: setup JDK @@ -52,15 +70,17 @@ jobs: if: env.any_changed == 'true' run: | chmod +x gradlew - echo "$TEST_FILES" | while read FILE; do - MODULE=$(echo "$FILE" | cut -d' ' -f1) - TEST_CLASS=$(echo "$FILE" | cut -d' ' -f2) + cat test_files.txt | while IFS=$'\n' read -r LINE; do + MODULE=$(echo "$LINE" | awk '{print $1}') + TEST_CLASS=$(echo "$LINE" | awk '{print $2}') + MODULE=$(echo "$MODULE" | sed 's|\.|:|g') - TEST_CLASS="kr.co.explore.ExploreViewModelTest" + echo "MODULE: $MODULE" echo "TEST_CLASS: $TEST_CLASS" + echo "Running: ./gradlew :$MODULE:testDebugUnitTest --tests \"$TEST_CLASS\"" - ./gradlew :$MODULE:testDebugUnitTest --tests "$TEST_CLASS" --scan + ./gradlew :$MODULE:testDebugUnitTest --tests "$TEST_CLASS" --scan || exit 1 done - name: Test Result From 724f580b7798097b94de2e0143f79c03da5a5708 Mon Sep 17 00:00:00 2001 From: Guri999 <116724657+Guri999@users.noreply.github.com> Date: Sun, 19 Jan 2025 17:02:42 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Fix:=20mock=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt | 2 ++ .../src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt | 4 +--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt index 03fa142..46e9c37 100644 --- a/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt +++ b/feature/bookmark/src/test/kotlin/kr/co/bookmark/BookmarkViewModelTest.kt @@ -55,6 +55,8 @@ internal class BookmarkViewModelTest { fun `Given a file when ClickFile intent is handled then navigate to pdf`() = runTest { val file = PDF_DUMMY + coEvery { recentRepository.insert(file) } returns Unit + viewModel.handleIntent(BookmarkUiIntent.ClickFile(file)) viewModel.sideEffect.testWithItem { diff --git a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt index b04a5e4..0ea2f4e 100644 --- a/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt +++ b/feature/explore/src/test/kotlin/kr/co/explore/ExploreViewModelTest.kt @@ -72,12 +72,10 @@ internal class ExploreViewModelTest { fun `Given a file when ClickFile intent is handled then navigate to pdf`() = runTest { val file = PDF_DUMMY - recentRepository.insert(file) + coEvery { recentRepository.insert(file) } returns Unit viewModel.handleIntent(ExploreUiIntent.ClickFile(file)) - recentRepository.insert(file) - viewModel.sideEffect.testWithItem { assert(this is ExploreSideEffect.NavigateToPdf) assertEquals(file.path, (this as ExploreSideEffect.NavigateToPdf).path)