Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
softartdev committed Nov 6, 2024
1 parent 445f68a commit 0cace9c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion iosApp/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 0dc93a6f6109335ea8cd3f91d2c87cc8c99f04a3

COCOAPODS: 1.15.2
COCOAPODS: 1.12.1
2 changes: 1 addition & 1 deletion iosApp/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions iosApp/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.softartdev.notedelight.shared.presentation.main

import android.database.sqlite.SQLiteException
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import app.cash.paging.PagingSource
import app.cash.paging.PagingState
import androidx.paging.PagingData
import app.cash.turbine.test
import com.softartdev.notedelight.shared.CoroutineDispatchersStub
import com.softartdev.notedelight.shared.db.Note
Expand All @@ -13,6 +12,8 @@ import com.softartdev.notedelight.shared.navigation.AppNavGraph
import com.softartdev.notedelight.shared.navigation.Router
import com.softartdev.notedelight.shared.presentation.MainDispatcherRule
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -46,16 +47,10 @@ class MainViewModelTest {
mainViewModel.stateFlow.test {
assertEquals(NoteListResult.Loading, awaitItem())

val pagingSource = object : PagingSource<Int, Note>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Note> {
return LoadResult.Page(data = emptyList(), prevKey = null, nextKey = null)
}
override fun getRefreshKey(state: PagingState<Int, Note>): Int? = null
}
Mockito.`when`(mockNoteDAO.pagingSource).thenReturn(pagingSource)
val pagingFlow = flowOf(PagingData.empty<Note>())
Mockito.`when`(mockNoteDAO.pagingDataFlow).thenReturn(pagingFlow)

mainViewModel.updateNotes()

assertTrue(awaitItem() is NoteListResult.Success)

cancelAndIgnoreRemainingEvents()
Expand All @@ -67,7 +62,9 @@ class MainViewModelTest {
mainViewModel.stateFlow.test {
assertEquals(NoteListResult.Loading, awaitItem())

Mockito.`when`(mockNoteDAO.pagingSource).thenThrow(SQLiteException())
val failingFlow = flow<PagingData<Note>> { throw SQLiteException() }
Mockito.`when`(mockNoteDAO.pagingDataFlow).thenReturn(failingFlow)

mainViewModel.updateNotes()
assertEquals(NoteListResult.Error(null), awaitItem())
Mockito.verify(mockRouter).navigateClearingBackStack(route = AppNavGraph.SignIn)
Expand All @@ -82,12 +79,20 @@ class MainViewModelTest {
Mockito.verify(mockRouter).navigate(route = AppNavGraph.Details(noteId = 1))
}

@Test
fun onSettingsClicked() {
mainViewModel.onSettingsClicked()
Mockito.verify(mockRouter).navigate(route = AppNavGraph.Settings)
}

@Test
fun error() = runTest {
mainViewModel.stateFlow.test {
assertEquals(NoteListResult.Loading, awaitItem())

Mockito.`when`(mockNoteDAO.pagingSource).thenThrow(RuntimeException())
val failingFlow = flow<PagingData<Note>> { throw RuntimeException() }
Mockito.`when`(mockNoteDAO.pagingDataFlow).thenReturn(failingFlow)

mainViewModel.updateNotes()
assertEquals(NoteListResult.Error(null), awaitItem())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.softartdev.notedelight.shared.db

import app.cash.paging.Pager
import app.cash.paging.PagingConfig
import app.cash.paging.PagingData
import app.cash.paging.PagingSource
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
Expand All @@ -21,6 +24,9 @@ class NoteDAO(private val noteQueries: NoteQueries) {
val listFlow: Flow<List<Note>>
get() = noteQueries.getAll().asFlow().mapToList(Dispatchers.IO).distinctUntilChanged()

/**
* Get a [PagingSource] for the notes table.
*/
val pagingSource: PagingSource<Int, Note>
get() = QueryPagingSource(
countQuery = noteQueries.countNotes(),
Expand All @@ -29,10 +35,19 @@ class NoteDAO(private val noteQueries: NoteQueries) {
queryProvider = noteQueries::pagedNotes,
)

/**
* Get a [Flow] of [PagingData] for the notes table.
*/
val pagingDataFlow: Flow<PagingData<Note>>
get() = Pager(
config = PagingConfig(pageSize = 20),
pagingSourceFactory = this::pagingSource
).flow

/**
* Select a note by id.
*
* @param noteId the note id.
* @param id the note id.
* @return the note with noteId.
*/
fun load(id: Long): Note = noteQueries.getById(noteId = id).executeAsOne()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package com.softartdev.notedelight.shared.presentation.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.paging.Pager
import app.cash.paging.PagingConfig
import app.cash.paging.PagingData
import app.cash.paging.cachedIn
import com.softartdev.notedelight.shared.db.Note
import com.softartdev.notedelight.shared.db.SafeRepo
import com.softartdev.notedelight.shared.navigation.AppNavGraph
import com.softartdev.notedelight.shared.navigation.Router
import com.softartdev.notedelight.shared.util.CoroutineDispatchers
import io.github.aakira.napier.Napier
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
Expand All @@ -37,12 +31,6 @@ class MainViewModel(

private var job: Job? = null

private val pagingDataFlow: Flow<PagingData<Note>>
get() = Pager(
config = PagingConfig(pageSize = 20),
pagingSourceFactory = safeRepo.noteDAO::pagingSource
).flow.cachedIn(viewModelScope)

init {
safeRepo.relaunchListFlowCallback = this::updateNotes
}
Expand All @@ -51,7 +39,8 @@ class MainViewModel(
if (job?.isActive == true) {
job?.cancel()
}
job = flowOf(pagingDataFlow)
job = safeRepo.noteDAO.pagingDataFlow
.let(block = ::flowOf)
.onStart { mutableStateFlow.value = NoteListResult.Loading }
.map(transform = NoteListResult::Success)
.onEach(action = mutableStateFlow::emit)
Expand Down

0 comments on commit 0cace9c

Please sign in to comment.