Merge pull request #3 from friendlyrobotnyc/feature/remove-getWithResult
Remove getWithResult and freshWithResult
This commit is contained in:
commit
7981500102
8 changed files with 10 additions and 291 deletions
|
@ -1,42 +0,0 @@
|
|||
package com.nytimes.android.external.store.util
|
||||
|
||||
/**
|
||||
* Result is a container with meta data for the parsed object.
|
||||
*
|
||||
* @param <Parsed> data type after parsing
|
||||
</Parsed> */
|
||||
data class Result<Parsed>(val source: Source, val value: Parsed?) {
|
||||
|
||||
val isFromNetwork: Boolean
|
||||
get() = source == Source.NETWORK
|
||||
|
||||
val isFromCache: Boolean
|
||||
get() = source == Source.CACHE
|
||||
|
||||
enum class Source {
|
||||
CACHE, NETWORK
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* Convenient method to create a result object from {SOURCE_CACHE}.
|
||||
* @param value data type after parsing
|
||||
* @param <T> data type after parsing
|
||||
* @return a Result object with T passed
|
||||
</T> */
|
||||
fun <T> createFromCache(value: T): Result<T> {
|
||||
return Result(Source.CACHE, value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenient method to create a result object from {SOURCE_NETWORK}.
|
||||
* @param value data type after parsing
|
||||
* @param <T> data type after parsing
|
||||
* @return a Result object with T passed
|
||||
</T> */
|
||||
fun <T> createFromNetwork(value: T): Result<T> {
|
||||
return Result(Source.NETWORK, value)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -67,9 +67,6 @@ open class RealStore<Parsed, Key> : Store<Parsed, Key> {
|
|||
return internalStore.get(key)
|
||||
}
|
||||
|
||||
// fun getWithResult(key: Key): Single<Result<Parsed>> {
|
||||
// TODO("not implemented")
|
||||
// }
|
||||
//
|
||||
// fun getRefreshing(key: Key): Observable<Parsed> {
|
||||
// TODO("not implemented")
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.nytimes.android.external.store3.base.impl
|
||||
|
||||
import com.nytimes.android.external.store.util.Result
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
|
@ -23,13 +22,6 @@ interface Store<T, V> {
|
|||
*/
|
||||
suspend fun get(key: V): T
|
||||
|
||||
/**
|
||||
* Return an Observable of [Result]<T> for request Barcode
|
||||
* Data will be returned from oldest non expired source
|
||||
* Sources are Memory Cache, Disk Cache, Inflight, Network Response
|
||||
</T> */
|
||||
// suspend fun getWithResult(key: V): Result<T>
|
||||
|
||||
/**
|
||||
* Calls store.get(), additionally will repeat anytime store.clear(barcode) is called
|
||||
* WARNING: getRefreshing(barcode) is an endless observable, be careful when combining
|
||||
|
@ -44,11 +36,6 @@ interface Store<T, V> {
|
|||
*/
|
||||
suspend fun fresh(key: V): T
|
||||
|
||||
/**
|
||||
* Return an Observable of [Result]<T> for requested Barcode skipping Memory & Disk Cache
|
||||
</T> */
|
||||
// suspend fun freshWithResult(key: V): Result<T>
|
||||
|
||||
/**
|
||||
* @return an Observable that emits "fresh" new response from the store that hit the fetcher
|
||||
* WARNING: stream is an endless observable, be careful when combining
|
||||
|
|
|
@ -36,21 +36,4 @@ class DontCacheErrorsTest {
|
|||
shouldThrow = false
|
||||
store.get(barcode)
|
||||
}
|
||||
|
||||
// TODO storeWithResult test
|
||||
// @Test
|
||||
// fun testStoreDoesntCacheErrorsWithResult() = runBlocking<Unit> {
|
||||
// val barcode = BarCode("bar", "code")
|
||||
//
|
||||
// shouldThrow = true
|
||||
// store.getWithResult(barcode).test()
|
||||
// .assertTerminated()
|
||||
// .assertError(Exception::class.java)
|
||||
// .awaitTerminalEvent()
|
||||
//
|
||||
// shouldThrow = false
|
||||
// store.get(barcode).test()
|
||||
// .assertNoErrors()
|
||||
// .awaitTerminalEvent()
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -25,14 +25,6 @@ class NoNetworkTest {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO getWithResult test
|
||||
// @Test
|
||||
// fun testNoNetworkWithResult() = runBlocking<Unit> {
|
||||
// store.getWithResult(BarCode("test", "test"))
|
||||
// .test()
|
||||
// .assertError(EXCEPTION)
|
||||
// }
|
||||
|
||||
companion object {
|
||||
private val EXCEPTION = RuntimeException("abc")
|
||||
}
|
||||
|
|
|
@ -23,15 +23,6 @@ class SequentialTest {
|
|||
assertThat(networkCalls).isEqualTo(1)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun sequentiallyWithResult() = runBlocking<Unit> {
|
||||
// val b = BarCode("one", "two")
|
||||
// store.getWithResult(b)
|
||||
// store.getWithResult(b)
|
||||
//
|
||||
// assertThat(networkCalls).isEqualTo(1)
|
||||
// }
|
||||
|
||||
@Test
|
||||
fun parallel() = runBlocking<Unit> {
|
||||
val b = BarCode("one", "two")
|
||||
|
@ -41,13 +32,4 @@ class SequentialTest {
|
|||
|
||||
assertThat(networkCalls).isEqualTo(1)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun parallelWithResult() = runBlocking<Unit> {
|
||||
// val b = BarCode("one", "two")
|
||||
// val first = store.getWithResult(b)
|
||||
// val second = store.getWithResult(b)
|
||||
//
|
||||
// assertThat(networkCalls).isEqualTo(1)
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -46,40 +46,9 @@ class StoreTest {
|
|||
assertThat(value).isEqualTo(DISK)
|
||||
value = simpleStore.get(barCode)
|
||||
assertThat(value).isEqualTo(DISK)
|
||||
verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
verify(fetcher, times(1)).fetch(barCode)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testSimpleWithResult() = runBlocking<Unit> {
|
||||
//
|
||||
// val simpleStore = StoreBuilder.barcode<String>()
|
||||
// .persister(persister)
|
||||
// .fetcher(fetcher)
|
||||
// .open()
|
||||
//
|
||||
//
|
||||
// whenever(fetcher.fetch(barCode))
|
||||
// .thenReturn(NETWORK)
|
||||
//
|
||||
// whenever(persister.read(barCode))
|
||||
// .thenReturn(null)
|
||||
// .thenReturn(DISK)
|
||||
//
|
||||
// whenever(persister.write(barCode, NETWORK))
|
||||
// .thenReturn(true)
|
||||
//
|
||||
// var result = simpleStore.getWithResult(barCode)
|
||||
//
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.NETWORK)
|
||||
// assertThat(result.value()).isEqualTo(DISK)
|
||||
//
|
||||
// result = simpleStore.getWithResult(barCode)
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.CACHE)
|
||||
// assertThat(result.value()).isEqualTo(DISK)
|
||||
// verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
// }
|
||||
|
||||
|
||||
@Test
|
||||
fun testDoubleTap() = runBlocking<Unit> {
|
||||
|
||||
|
@ -112,44 +81,6 @@ class StoreTest {
|
|||
verify(fetcher, times(1)).fetch(barCode)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testDoubleTapWithResult() = runBlocking<Unit> {
|
||||
//
|
||||
// val simpleStore = StoreBuilder.barcode<String>()
|
||||
// .persister(persister)
|
||||
// .fetcher(fetcher)
|
||||
// .open()
|
||||
//
|
||||
// val networkSingle = Single.create<String> { emitter ->
|
||||
// if (counter.incrementAndGet() == 1) {
|
||||
// emitter.onSuccess(NETWORK)
|
||||
// } else {
|
||||
// emitter.onError(RuntimeException("Yo Dawg your inflight is broken"))
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// whenever(fetcher.fetch(barCode))
|
||||
// .thenReturn(networkSingle)
|
||||
//
|
||||
// whenever(persister.read(barCode))
|
||||
// .thenReturn(null)
|
||||
// .thenReturn(DISK)
|
||||
//
|
||||
// whenever(persister.write(barCode, NETWORK))
|
||||
// .thenReturn(true)
|
||||
//
|
||||
//
|
||||
// val response = simpleStore.getWithResult(barCode)
|
||||
// .zipWith(simpleStore.getWithResult(barCode), { s, s2 -> Result.createFromNetwork<T>("hello") })
|
||||
//
|
||||
//
|
||||
// assertThat(response.source()).isEqualTo(Result.Source.NETWORK)
|
||||
// assertThat(response.value()).isEqualTo("hello")
|
||||
// verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
// }
|
||||
|
||||
|
||||
@Test
|
||||
fun testSubclass() = runBlocking<Unit> {
|
||||
|
||||
|
@ -168,34 +99,9 @@ class StoreTest {
|
|||
assertThat(value).isEqualTo(DISK)
|
||||
value = simpleStore.get(barCode)
|
||||
assertThat(value).isEqualTo(DISK)
|
||||
verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
verify(fetcher, times(1)).fetch(barCode)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testSubclassWithResult() = runBlocking<Unit> {
|
||||
//
|
||||
// val simpleStore = SampleStore(fetcher, persister)
|
||||
// simpleStore.clear()
|
||||
//
|
||||
// whenever(fetcher.fetch(barCode))
|
||||
// .thenReturn(NETWORK)
|
||||
//
|
||||
// whenever(persister.read(barCode))
|
||||
// .thenReturn(null)
|
||||
// .thenReturn(DISK)
|
||||
// whenever(persister.write(barCode, NETWORK)).thenReturn(true)
|
||||
//
|
||||
// var result = simpleStore.getWithResult(barCode)
|
||||
//
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.NETWORK)
|
||||
// assertThat(result.value()).isEqualTo(DISK)
|
||||
//
|
||||
// result = simpleStore.getWithResult(barCode)
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.CACHE)
|
||||
// assertThat(result.value()).isEqualTo(DISK)
|
||||
// verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
// }
|
||||
|
||||
@Test
|
||||
fun testNoopAndDefault() = runBlocking<Unit> {
|
||||
|
||||
|
@ -207,47 +113,20 @@ class StoreTest {
|
|||
.thenReturn(NETWORK)
|
||||
|
||||
var value = simpleStore.get(barCode)
|
||||
verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
verify<Persister<String, BarCode>>(persister, times(1)).write(barCode, NETWORK)
|
||||
verify<Persister<String, BarCode>>(persister, times(2)).read(barCode)
|
||||
verify(fetcher, times(1)).fetch(barCode)
|
||||
verify(persister, times(1)).write(barCode, NETWORK)
|
||||
verify(persister, times(2)).read(barCode)
|
||||
assertThat(value).isEqualTo(NETWORK)
|
||||
|
||||
|
||||
value = simpleStore.get(barCode)
|
||||
verify<Persister<String, BarCode>>(persister, times(2)).read(barCode)
|
||||
verify<Persister<String, BarCode>>(persister, times(1)).write(barCode, NETWORK)
|
||||
verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
verify(persister, times(2)).read(barCode)
|
||||
verify(persister, times(1)).write(barCode, NETWORK)
|
||||
verify(fetcher, times(1)).fetch(barCode)
|
||||
|
||||
assertThat(value).isEqualTo(NETWORK)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testNoopAndDefaultWithResult() = runBlocking<Unit> {
|
||||
//
|
||||
// val persister = spy(NoopPersister.create<String, BarCode>())
|
||||
// val simpleStore = SampleStore(fetcher, persister)
|
||||
//
|
||||
//
|
||||
// whenever(fetcher.fetch(barCode))
|
||||
// .thenReturn(NETWORK)
|
||||
//
|
||||
// var value = simpleStore.getWithResult(barCode)
|
||||
// verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
// verify<Persister<String, BarCode>>(persister, times(1)).write(barCode, NETWORK)
|
||||
// verify<Persister<String, BarCode>>(persister, times(2)).read(barCode)
|
||||
// assertThat(value.source()).isEqualTo(Result.Source.NETWORK)
|
||||
// assertThat(value.value()).isEqualTo(NETWORK)
|
||||
//
|
||||
//
|
||||
// value = simpleStore.getWithResult(barCode)
|
||||
// verify<Persister<String, BarCode>>(persister, times(2)).read(barCode)
|
||||
// verify<Persister<String, BarCode>>(persister, times(1)).write(barCode, NETWORK)
|
||||
// verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode)
|
||||
//
|
||||
// assertThat(value.source()).isEqualTo(Result.Source.CACHE)
|
||||
// assertThat(value.value()).isEqualTo(NETWORK)
|
||||
// }
|
||||
|
||||
@Test
|
||||
fun testEquivalence() = runBlocking<Unit> {
|
||||
val cache = CacheBuilder.newBuilder()
|
||||
|
|
|
@ -48,36 +48,6 @@ class StoreWithParserTest {
|
|||
verify(fetcher, times(1)).fetch(barCode)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testSimpleWithResult() = runBlocking<Unit> {
|
||||
// val simpleStore = ParsingStoreBuilder.builder<String, String>()
|
||||
// .persister(persister)
|
||||
// .fetcher(fetcher)
|
||||
// .parser(parser)
|
||||
// .open()
|
||||
//
|
||||
// whenever(fetcher.fetch(barCode))
|
||||
// .thenReturn(NETWORK)
|
||||
//
|
||||
// whenever(persister.read(barCode))
|
||||
// .thenReturn(null)
|
||||
// .thenReturn(DISK)
|
||||
//
|
||||
// whenever(persister.write(barCode, NETWORK))
|
||||
// .thenReturn(true)
|
||||
//
|
||||
// whenever(parser.apply(DISK)).thenReturn(barCode.key)
|
||||
//
|
||||
// var result = simpleStore.getWithResult(barCode)
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.NETWORK)
|
||||
// assertThat(result.value()).isEqualTo(barCode.key)
|
||||
//
|
||||
// result = simpleStore.getWithResult(barCode)
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.CACHE)
|
||||
// assertThat(result.value()).isEqualTo(barCode.key)
|
||||
// verify(fetcher, times(1)).fetch(barCode)
|
||||
// }
|
||||
|
||||
@Test
|
||||
fun testSubclass() = runBlocking<Unit> {
|
||||
MockitoAnnotations.initMocks(this)
|
||||
|
@ -103,37 +73,8 @@ class StoreWithParserTest {
|
|||
verify(fetcher, times(1)).fetch(barCode)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun testSubclassWithResult() = runBlocking<Unit> {
|
||||
// MockitoAnnotations.initMocks(this)
|
||||
//
|
||||
// val simpleStore = SampleParsingStore(fetcher, persister, parser)
|
||||
//
|
||||
// whenever(fetcher.fetch(barCode))
|
||||
// .thenReturn(NETWORK)
|
||||
//
|
||||
// whenever(persister.read(barCode))
|
||||
// .thenReturn(null)
|
||||
// .thenReturn(DISK)
|
||||
//
|
||||
// whenever(persister.write(barCode, NETWORK))
|
||||
// .thenReturn(true)
|
||||
//
|
||||
// whenever(parser.apply(DISK)).thenReturn(barCode.key)
|
||||
//
|
||||
// var result = simpleStore.getWithResult(barCode)
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.NETWORK)
|
||||
// assertThat(result.value()).isEqualTo(barCode.key)
|
||||
//
|
||||
// result = simpleStore.getWithResult(barCode)
|
||||
// assertThat(result.source()).isEqualTo(Result.Source.CACHE)
|
||||
// assertThat(result.value()).isEqualTo(barCode.key)
|
||||
// verify(fetcher, times(1)).fetch(barCode)
|
||||
// }
|
||||
|
||||
companion object {
|
||||
|
||||
private val DISK = "persister"
|
||||
private val NETWORK = "fresh"
|
||||
private const val DISK = "persister"
|
||||
private const val NETWORK = "fresh"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue