Fix tests

This commit is contained in:
fabioCollini 2019-05-29 09:14:21 +02:00
parent 53f823548c
commit ae2576922c
3 changed files with 65 additions and 104 deletions

View file

@ -43,6 +43,7 @@ dependencies {
testImplementation libraries.mockito
testImplementation libraries.assertJ
testImplementation libraries.junit
testImplementation libraries.coroutinesTest
testCompileOnly libraries.jsr305
implementation libraries.supportRecyclerView
implementation libraries.supportAppCompat

View file

@ -2,62 +2,50 @@ package com.nytimes.android.sample
import com.nytimes.android.external.store3.base.Fetcher
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 kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import java.util.concurrent.atomic.AtomicInteger
class ClearStoreMemoryTest {
lateinit var networkCalls: AtomicInteger
lateinit var store: Store<Int, BarCode>
private val networkCalls = AtomicInteger(0)
private val store = StoreBuilder.barcode<Int>()
.fetcher(object : Fetcher<Int, BarCode> {
override suspend fun fetch(key: BarCode) = networkCalls.incrementAndGet()
@Before
fun setUp() {
networkCalls = AtomicInteger(0)
store = StoreBuilder.barcode<Int>()
.fetcher(object : Fetcher<Int, BarCode> {
override suspend fun fetch(key: BarCode) = networkCalls.incrementAndGet()
})
.open()
})
.open()
@Test
fun testClearSingleBarCode() = runBlocking<Unit> {
//one request should produce one call
val barcode = BarCode("type", "key")
val result = store.get(barcode)
assertThat(networkCalls.get()).isEqualTo(1)
store.clear(barcode)
val anotherResult = store.get(barcode)
assertThat(networkCalls.get()).isEqualTo(2)
}
@Test
fun testClearSingleBarCode() {
runBlocking{
//one request should produce one call
val barcode = BarCode("type", "key")
val result = store.get(barcode)
assertThat(networkCalls.get()).isEqualTo(1)
fun testClearAllBarCodes() = runBlocking<Unit> {
val b1 = BarCode("type1", "key1")
val b2 = BarCode("type2", "key2")
store.clearMemory(barcode)
val anotherResult = store.get(barcode)
assertThat(networkCalls.get()).isEqualTo(2)
}
}
//each request should produce one call
store.get(b1)
store.get(b2)
assertThat(networkCalls.get()).isEqualTo(2)
@Test
fun testClearAllBarCodes() {
runBlocking {
store.clear(b1)
store.clear(b2)
val b1 = BarCode("type1", "key1")
val b2 = BarCode("type2", "key2")
//each request should produce one call
store.get(b1)
store.get(b2)
assertThat(networkCalls.get()).isEqualTo(2)
store.clearMemory()
//after everything is cleared each request should produce another 2 calls
store.get(b1)
store.get(b2)
assertThat(networkCalls.get()).isEqualTo(4)
}
//after everything is cleared each request should produce another 2 calls
store.get(b1)
store.get(b2)
assertThat(networkCalls.get()).isEqualTo(4)
}
}

View file

@ -4,11 +4,9 @@ import com.nytimes.android.external.store3.base.Clearable
import com.nytimes.android.external.store3.base.Fetcher
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.Store
import com.nytimes.android.external.store3.base.impl.StoreBuilder
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.runners.MockitoJUnitRunner
@ -21,81 +19,55 @@ val barcode2 = BarCode("type2", "key2")
@RunWith(MockitoJUnitRunner::class)
class ClearStoreTest {
val persister = ClearingPersister()
lateinit var networkCalls: AtomicInteger
lateinit var store: Store<Int, BarCode>
private val persister = ClearingPersister()
private val networkCalls = AtomicInteger(0)
private val store= StoreBuilder.barcode<Int>()
.fetcher(object : Fetcher<Int, BarCode> {
override suspend fun fetch(key: BarCode): Int {
return networkCalls.incrementAndGet()
}
@Before
fun setUp() {
networkCalls = AtomicInteger(0)
store = StoreBuilder.barcode<Int>()
.fetcher(object : Fetcher<Int, BarCode> {
override suspend fun fetch(key: BarCode): Int {
return networkCalls.incrementAndGet()
}
})
.persister(persister)
.open()
})
.persister(persister)
.open()
@Test
fun testClearSingleBarCode() = runBlocking<Unit> {
// one request should produce one call
val barcode = BarCode("type", "key")
store.get(barcode)
assertThat(networkCalls.toInt()).isEqualTo(1)
// after clearing the memory another call should be made
store.clear(barcode)
store.get(barcode)
assertThat(persister.isClear).isTrue()
assertThat(networkCalls.toInt()).isEqualTo(2)
}
@Test
fun testClearSingleBarCode() {
runBlocking {
// one request should produce one call
val barcode = BarCode("type", "key")
store.get(barcode)
assertThat(networkCalls.toInt()).isEqualTo(1)
// after clearing the memory another call should be made
store.clear(barcode)
store.get(barcode)
assertThat(persister.isClear).isTrue()
assertThat(networkCalls.toInt()).isEqualTo(2)
}
}
@Test
fun testClearAllBarCodes() {
runBlocking {
// each request should produce one call
val result = store.get(barcode1)
store.get(barcode2)
assertThat(networkCalls.toInt()).isEqualTo(2)
store.clear()
// after everything is cleared each request should produce another 2 calls
store.get(barcode1)
store.get(barcode2)
assertThat(networkCalls.toInt()).isEqualTo(4)
}
fun testClearAllBarCodes() = runBlocking<Unit> {
// each request should produce one call
val result = store.get(barcode1)
store.get(barcode2)
assertThat(networkCalls.toInt()).isEqualTo(2)
store.clear(barcode1)
store.clear(barcode2)
// after everything is cleared each request should produce another 2 calls
store.get(barcode1)
store.get(barcode2)
assertThat(networkCalls.toInt()).isEqualTo(4)
}
}
//everything will be mocked
class ClearingPersister : Persister<Int, BarCode>, Clearable<BarCode> {
var isClear = false
val barcode1Responses = LinkedList<Int?>()
val barcode2Responses = LinkedList<Int?>()
init {
barcode1Responses.add(null)
barcode1Responses.add(1)
barcode1Responses.add(null)
barcode1Responses.add(1)
barcode2Responses.add(null)
barcode2Responses.add(1)
barcode2Responses.add(null)
barcode2Responses.add(1)
}
private val barcode1Responses = LinkedList(mutableListOf(null, 1, null, 1))
private val barcode2Responses = LinkedList(mutableListOf(null, 1, null, 1))
override fun clear(key: BarCode) {
isClear = true