Skip to content

Commit

Permalink
Add EagerConflictResolutionTests
Browse files Browse the repository at this point in the history
Signed-off-by: matt-ramotar <[email protected]>
  • Loading branch information
matt-ramotar committed Nov 18, 2024
1 parent 8a7cefa commit c6f7d6c
Showing 1 changed file with 141 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.mokkery.matcher.eq
import dev.mokkery.mock
import dev.mokkery.verify
import dev.mokkery.verify.VerifyMode.Companion.exactly
import dev.mokkery.verify.VerifyMode.Companion.not
import dev.mokkery.verifySuspend
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.TestScope
Expand All @@ -27,7 +28,7 @@ import org.mobilenativefoundation.store.store5.test_utils.model.Note
import kotlin.test.Test
import kotlin.test.assertEquals

class EagerConflictResolutionLoggingTests {
class EagerConflictResolutionTests {
private val testScope = TestScope()

private val delegate = mock<RealStore<String, Note, Note, Note>>(autoUnit)
Expand Down Expand Up @@ -82,6 +83,10 @@ class EagerConflictResolutionLoggingTests {
updater.post(eq("id"), eq(latestNote))
}

verifySuspend(not) {
bookkeeper.clear(eq("id"))
}

verify(exactly(1)) {
logger.error(eq(exception.toString()))
}
Expand Down Expand Up @@ -136,6 +141,10 @@ class EagerConflictResolutionLoggingTests {
updater.post(eq("id"), eq(latestNote))
}

verifySuspend(not) {
bookkeeper.clear(eq("id"))
}

verify(exactly(1)) {
logger.error(eq(errorMessage))
}
Expand All @@ -151,4 +160,135 @@ class EagerConflictResolutionLoggingTests {

}

@Test
fun stream_givenNoConflicts_whenCalled_thenShouldLog() = testScope.runTest {
// Given
val latestNote = Note("id", "Title", "Content")
val readResponse = StoreReadResponse.Data(latestNote, StoreReadResponseOrigin.Cache)
val delegateFlow = flowOf(readResponse)
val readRequest = StoreReadRequest.fresh("id")

every {
delegate.stream(any())
} returns delegateFlow

everySuspend {
delegate.latestOrNull(any())
} returns latestNote

everySuspend {
bookkeeper.getLastFailedSync(any())
} returns null

everySuspend {
updater.post(any(), any())
} returns UpdaterResult.Success.Typed(true)

every {
updater.onCompletion
} returns null

everySuspend {
bookkeeper.clear(any())
} returns true


// When
val stream = mutableStore.stream<Boolean>(
readRequest
)

// Then

stream.test {

verifySuspend(not) {
updater.post(eq("id"), eq(latestNote))
}

verify(exactly(1)) {
logger.debug(eq("No conflicts."))
}

verifySuspend(not) {
bookkeeper.clear(eq("id"))
}

verify(exactly(1)) {
delegate.stream(eq(readRequest))
}

assertEquals(readResponse, awaitItem())

awaitComplete()
}

}

@Test
fun stream_givenConflicts_whenSuccessResolvingConflicts_thenShouldLog() = testScope.runTest {
// Given
val latestNote = Note("id", "Title", "Content")
val readResponse = StoreReadResponse.Data(latestNote, StoreReadResponseOrigin.Cache)
val delegateFlow = flowOf(readResponse)
val readRequest = StoreReadRequest.fresh("id")

every {
delegate.stream(any())
} returns delegateFlow

everySuspend {
delegate.latestOrNull(any())
} returns latestNote

everySuspend {
bookkeeper.getLastFailedSync(any())
} returns 1L

everySuspend {
updater.post(any(), any())
} returns UpdaterResult.Success.Typed(true)

every {
updater.onCompletion
} returns null

everySuspend {
bookkeeper.clear(any())
} returns true


// When
val stream = mutableStore.stream<Boolean>(
readRequest
)

// Then

stream.test {

verifySuspend(exactly(1)) {
updater.post(eq("id"), eq(latestNote))
}

verify(exactly(1)) {
logger.debug(eq("true"))
}

verifySuspend(exactly(1)) {
bookkeeper.clear(eq("id"))
}

verify(exactly(1)) {
delegate.stream(eq(readRequest))
}

assertEquals(readResponse, awaitItem())

awaitComplete()
}

}


}

0 comments on commit c6f7d6c

Please sign in to comment.