This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 304
second part of store tests conversion to kotlin #357
Open
brianPlummer
wants to merge
2
commits into
feature/kotlin_conversion
Choose a base branch
from
feature/storeKotlinB
base: feature/kotlin_conversion
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 0 additions & 98 deletions
98
store/src/test/java/com/nytimes/android/external/store3/GetRefreshingTest.java
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
store/src/test/java/com/nytimes/android/external/store3/GetRefreshingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.nytimes.android.external.store3 | ||
|
||
import com.nytimes.android.external.store3.base.impl.BarCode | ||
import com.nytimes.android.external.store3.base.impl.Store | ||
import com.nytimes.android.external.store3.base.impl.StoreBuilder | ||
import io.reactivex.Maybe | ||
import io.reactivex.Single | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.runners.MockitoJUnitRunner | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class GetRefreshingTest { | ||
@Mock | ||
internal lateinit var persister: ClearingPersister | ||
internal lateinit var networkCalls: AtomicInteger | ||
private lateinit var store: Store<Int, BarCode> | ||
|
||
@Before | ||
fun setUp() { | ||
networkCalls = AtomicInteger(0) | ||
store = StoreBuilder.barcode<Int>() | ||
.fetcher { barCode -> Single.fromCallable { networkCalls.incrementAndGet() } } | ||
.persister(persister) | ||
.open() | ||
} | ||
|
||
@Test | ||
fun testRefreshOnClear() { | ||
val barcode = BarCode("type", "key") | ||
`when`(persister.read(barcode)) | ||
.thenReturn(Maybe.empty()) //read from disk | ||
.thenReturn(Maybe.just(1)) //read from disk after fetching from network | ||
.thenReturn(Maybe.empty()) //read from disk after clearing disk cache | ||
.thenReturn(Maybe.just(1)) //read from disk after making additional network call | ||
`when`(persister.write(barcode, 1)).thenReturn(Single.just(true)) | ||
`when`(persister.write(barcode, 2)).thenReturn(Single.just(true)) | ||
|
||
|
||
val refreshingObservable = store.getRefreshing(barcode).test() | ||
refreshingObservable.assertValueCount(1) | ||
assertThat(networkCalls.toInt()).isEqualTo(1) | ||
//clearing the store should produce another network call | ||
store.clear(barcode) | ||
refreshingObservable.assertValueCount(2) | ||
assertThat(networkCalls.toInt()).isEqualTo(2) | ||
|
||
store.get(barcode).test().awaitTerminalEvent() | ||
refreshingObservable.assertValueCount(2) | ||
assertThat(networkCalls.toInt()).isEqualTo(2) | ||
} | ||
|
||
@Test | ||
fun testRefreshOnClearAll() { | ||
val barcode1 = BarCode("type", "key") | ||
val barcode2 = BarCode("type", "key2") | ||
|
||
`when`(persister.read(barcode1)) | ||
.thenReturn(Maybe.empty()) //read from disk | ||
.thenReturn(Maybe.just(1)) //read from disk after fetching from network | ||
.thenReturn(Maybe.empty()) //read from disk after clearing disk cache | ||
.thenReturn(Maybe.just(1)) //read from disk after making additional network call | ||
`when`(persister.write(barcode1, 1)).thenReturn(Single.just(true)) | ||
`when`(persister.write(barcode1, 2)).thenReturn(Single.just(true)) | ||
|
||
`when`(persister.read(barcode2)) | ||
.thenReturn(Maybe.empty()) //read from disk | ||
.thenReturn(Maybe.just(1)) //read from disk after fetching from network | ||
.thenReturn(Maybe.empty()) //read from disk after clearing disk cache | ||
.thenReturn(Maybe.just(1)) //read from disk after making additional network call | ||
|
||
`when`(persister.write(barcode2, 1)).thenReturn(Single.just(true)) | ||
`when`(persister.write(barcode2, 2)).thenReturn(Single.just(true)) | ||
|
||
val testObservable1 = store.getRefreshing(barcode1).test() | ||
val testObservable2 = store.getRefreshing(barcode2).test() | ||
testObservable1.assertValueCount(1) | ||
testObservable2.assertValueCount(1) | ||
|
||
assertThat(networkCalls.toInt()).isEqualTo(2) | ||
|
||
store.clear() | ||
assertThat(networkCalls.toInt()).isEqualTo(4) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 0 additions & 71 deletions
71
store/src/test/java/com/nytimes/android/external/store3/ParsingFetcherTest.java
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
store/src/test/java/com/nytimes/android/external/store3/ParsingFetcherTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.nytimes.android.external.store3 | ||
|
||
import com.nytimes.android.external.store3.base.Fetcher | ||
import com.nytimes.android.external.store3.base.Parser | ||
import com.nytimes.android.external.store3.base.Persister | ||
import com.nytimes.android.external.store3.base.impl.BarCode | ||
import com.nytimes.android.external.store3.base.impl.ParsingFetcher | ||
import com.nytimes.android.external.store3.base.impl.StoreBuilder | ||
import io.reactivex.Maybe | ||
import io.reactivex.Single | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.`when` | ||
import org.mockito.Mockito.times | ||
import org.mockito.Mockito.verify | ||
import org.mockito.MockitoAnnotations | ||
|
||
class ParsingFetcherTest { | ||
|
||
@Mock | ||
internal lateinit var fetcher: Fetcher<String, BarCode> | ||
@Mock | ||
internal lateinit var parser: Parser<String, String> | ||
@Mock | ||
internal lateinit var persister: Persister<String, BarCode> | ||
private val barCode = BarCode("key", "value") | ||
|
||
@Before | ||
fun setUp() { | ||
MockitoAnnotations.initMocks(this) | ||
} | ||
|
||
@Test | ||
fun testPersistFetcher() { | ||
|
||
val simpleStore = StoreBuilder.barcode<String>() | ||
.fetcher(ParsingFetcher.from(fetcher, parser)) | ||
.persister(persister) | ||
.open() | ||
|
||
`when`(fetcher.fetch(barCode)) | ||
.thenReturn(Single.just(RAW_DATA)) | ||
|
||
`when`(parser.apply(RAW_DATA)) | ||
.thenReturn(PARSED) | ||
|
||
`when`(persister.read(barCode)) | ||
.thenReturn(Maybe.just(PARSED)) | ||
|
||
`when`(persister.write(barCode, PARSED)) | ||
.thenReturn(Single.just(true)) | ||
|
||
val value = simpleStore.fetch(barCode).blockingGet() | ||
|
||
assertThat(value).isEqualTo(PARSED) | ||
|
||
verify<Fetcher<String, BarCode>>(fetcher, times(1)).fetch(barCode) | ||
verify<Parser<String, String>>(parser, times(1)).apply(RAW_DATA) | ||
verify<Persister<String, BarCode>>(persister, times(1)).write(barCode, PARSED) | ||
} | ||
|
||
companion object { | ||
private const val RAW_DATA = "Test data." | ||
private const val PARSED = "DATA PARSED" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be private?