@@ -6,6 +6,7 @@ import kotlin.test.Test
66import kotlin.test.assertEquals
77import kotlin.test.assertIs
88import kotlin.test.assertNotNull
9+ import kotlin.test.assertTrue
910import kotlin.time.Duration.Companion.minutes
1011import kotlinx.coroutines.flow.first
1112import kotlinx.coroutines.flow.flow
@@ -270,59 +271,90 @@ class UpdaterTests {
270271 }
271272
272273 @Test
273- fun collectResponseAfterWriting () = testScope.runTest {
274+ fun collectResponseAfterWritingWithSourceOfTruth () {
274275 val ttl = inHours(1 )
275276
276- val store = StoreBuilder .from<NotesKey , NetworkNote >(
277- fetcher = Fetcher .of { key -> api.get(key, ttl = ttl) },
277+ val converter = NotesConverterProvider ().provide()
278+ val validator = NotesValidator ()
279+ val updater = NotesUpdaterProvider (api).provide()
280+
281+ val store = MutableStoreBuilder .from<NotesKey , NetworkNote , InputNote , OutputNote >(
282+ fetcher = Fetcher .ofFlow { key ->
283+ val network = api.get(key, ttl = ttl)
284+ flow { emit(network) }
285+ },
286+ sourceOfTruth = SourceOfTruth .of(
287+ nonFlowReader = { key -> notes.get(key) },
288+ writer = { key, sot -> notes.put(key, sot) },
289+ delete = { key -> notes.clear(key) },
290+ deleteAll = { notes.clear() }
291+ ),
292+ converter
278293 )
279- .cachePolicy(MemoryPolicy .builder<NotesKey , NetworkNote >().setExpireAfterWrite(10 .minutes).build())
280- .build().asMutableStore<NotesKey , NetworkNote , NetworkNote , NetworkNote , NetworkNote >(
294+ .validator(validator)
295+ .build(
296+ updater = updater,
297+ bookkeeper = null
298+ )
299+
300+ testCollectResponseAfterWriting(store, ttl)
301+ }
302+
303+ @Test
304+ fun collectResponseAfterWritingWithoutSourceOfTruth () {
305+ val ttl = inHours(1 )
306+
307+ val store = StoreBuilder .from<NotesKey , OutputNote >(
308+ fetcher = Fetcher .of { key -> OutputNote (api.get(key, ttl = ttl).data, ttl = ttl) },
309+ )
310+ .cachePolicy(MemoryPolicy .builder<NotesKey , OutputNote >().setExpireAfterWrite(10 .minutes).build())
311+ .build().asMutableStore<NotesKey , OutputNote , OutputNote , OutputNote , OutputNote >(
281312 Updater .by(
282313 { _, v -> UpdaterResult .Success .Typed (v) },
283314 ),
284315 null ,
285316 )
286317
318+ testCollectResponseAfterWriting(store, ttl)
319+ }
320+
321+ private fun testCollectResponseAfterWriting (
322+ store : MutableStore <NotesKey , OutputNote >,
323+ ttl : Long ,
324+ ) = testScope.runTest {
287325 val readRequest = StoreReadRequest .fresh(NotesKey .Single (Notes .One .id))
288326
289327 store.stream<NotesWriteResponse >(readRequest).test {
290328 assertEquals(StoreReadResponse .Loading (origin = StoreReadResponseOrigin .Fetcher ()), awaitItem())
291329 assertEquals(
292330 StoreReadResponse .Data (
293- NetworkNote (NoteData .Single (Notes .One ), ttl = ttl),
331+ OutputNote (NoteData .Single (Notes .One ), ttl = ttl),
294332 StoreReadResponseOrigin .Fetcher ()
295333 ),
296334 awaitItem()
297335 )
298336
299337 val newNote = Notes .One .copy(title = " New Title-1" )
300- val writeRequest = StoreWriteRequest .of<NotesKey , NetworkNote , NotesWriteResponse >(
338+ val writeRequest = StoreWriteRequest .of<NotesKey , OutputNote , NotesWriteResponse >(
301339 key = NotesKey .Single (Notes .One .id),
302- value = NetworkNote (NoteData .Single (newNote), 0 )
340+ value = OutputNote (NoteData .Single (newNote), 0 )
303341 )
304342
305343 val storeWriteResponse = store.write(writeRequest)
306344
307- // Write is success
308- assertEquals(
309- StoreWriteResponse .Success .Typed (
310- NetworkNote (NoteData .Single (newNote), 0 )
311- ),
312- storeWriteResponse
313- )
345+ assertTrue(storeWriteResponse is StoreWriteResponse .Success )
314346
315347 // New data added by 'write' is collected
316348
317349 assertEquals(
318- NetworkNote (NoteData .Single (newNote), 0 ),
350+ OutputNote (NoteData .Single (newNote), 0 ),
319351 awaitItem().requireData()
320352 )
321353
322354 // different key, not collected
323- store.write(StoreWriteRequest .of<NotesKey , NetworkNote , NotesWriteResponse >(
355+ store.write(StoreWriteRequest .of<NotesKey , OutputNote , NotesWriteResponse >(
324356 key = NotesKey .Single (Notes .Five .id),
325- value = NetworkNote (NoteData .Single (newNote), 0 )
357+ value = OutputNote (NoteData .Single (newNote), 0 )
326358 ))
327359 }
328360
@@ -331,7 +363,7 @@ class UpdaterTests {
331363 val cachedStream = store.stream<NotesWriteResponse >(cachedReadRequest)
332364
333365 assertEquals(
334- NetworkNote (NoteData .Single (Notes .One .copy(title = " New Title-1" )), 0 ),
366+ OutputNote (NoteData .Single (Notes .One .copy(title = " New Title-1" )), 0 ),
335367 cachedStream.first().requireData()
336368 )
337369 }
0 commit comments