@@ -6,6 +6,7 @@ import kotlin.test.Test
6
6
import kotlin.test.assertEquals
7
7
import kotlin.test.assertIs
8
8
import kotlin.test.assertNotNull
9
+ import kotlin.test.assertTrue
9
10
import kotlin.time.Duration.Companion.minutes
10
11
import kotlinx.coroutines.flow.first
11
12
import kotlinx.coroutines.flow.flow
@@ -270,59 +271,90 @@ class UpdaterTests {
270
271
}
271
272
272
273
@Test
273
- fun collectResponseAfterWriting () = testScope.runTest {
274
+ fun collectResponseAfterWritingWithSourceOfTruth () {
274
275
val ttl = inHours(1 )
275
276
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
278
293
)
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 >(
281
312
Updater .by(
282
313
{ _, v -> UpdaterResult .Success .Typed (v) },
283
314
),
284
315
null ,
285
316
)
286
317
318
+ testCollectResponseAfterWriting(store, ttl)
319
+ }
320
+
321
+ private fun testCollectResponseAfterWriting (
322
+ store : MutableStore <NotesKey , OutputNote >,
323
+ ttl : Long ,
324
+ ) = testScope.runTest {
287
325
val readRequest = StoreReadRequest .fresh(NotesKey .Single (Notes .One .id))
288
326
289
327
store.stream<NotesWriteResponse >(readRequest).test {
290
328
assertEquals(StoreReadResponse .Loading (origin = StoreReadResponseOrigin .Fetcher ()), awaitItem())
291
329
assertEquals(
292
330
StoreReadResponse .Data (
293
- NetworkNote (NoteData .Single (Notes .One ), ttl = ttl),
331
+ OutputNote (NoteData .Single (Notes .One ), ttl = ttl),
294
332
StoreReadResponseOrigin .Fetcher ()
295
333
),
296
334
awaitItem()
297
335
)
298
336
299
337
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 >(
301
339
key = NotesKey .Single (Notes .One .id),
302
- value = NetworkNote (NoteData .Single (newNote), 0 )
340
+ value = OutputNote (NoteData .Single (newNote), 0 )
303
341
)
304
342
305
343
val storeWriteResponse = store.write(writeRequest)
306
344
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 )
314
346
315
347
// New data added by 'write' is collected
316
348
317
349
assertEquals(
318
- NetworkNote (NoteData .Single (newNote), 0 ),
350
+ OutputNote (NoteData .Single (newNote), 0 ),
319
351
awaitItem().requireData()
320
352
)
321
353
322
354
// different key, not collected
323
- store.write(StoreWriteRequest .of<NotesKey , NetworkNote , NotesWriteResponse >(
355
+ store.write(StoreWriteRequest .of<NotesKey , OutputNote , NotesWriteResponse >(
324
356
key = NotesKey .Single (Notes .Five .id),
325
- value = NetworkNote (NoteData .Single (newNote), 0 )
357
+ value = OutputNote (NoteData .Single (newNote), 0 )
326
358
))
327
359
}
328
360
@@ -331,7 +363,7 @@ class UpdaterTests {
331
363
val cachedStream = store.stream<NotesWriteResponse >(cachedReadRequest)
332
364
333
365
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 ),
335
367
cachedStream.first().requireData()
336
368
)
337
369
}
0 commit comments