Skip to content

Instantly share code, notes, and snippets.

@hadilq
Last active October 27, 2018 16:07
Show Gist options
  • Save hadilq/0d06cac69e23e28dc4e55bbf2978a84d to your computer and use it in GitHub Desktop.
Save hadilq/0d06cac69e23e28dc4e55bbf2978a84d to your computer and use it in GitHub Desktop.
class BaseRepositoryTest {
@Test
fun `test loading with success`() {
val persist = { _: EntityTest -> }
val entityTest = EntityTest()
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
Flowable.just(listOf(entityTest)),
Single.just(entityTest),
persist
)
val testSubscriber = usecaseFlowable.test()
testSubscriber.assertNoErrors()
testSubscriber.assertValue(ResultState.Loading(entityTest))
}
@Test
fun `test loading with error`() {
val persist = { _: EntityTest -> }
val entityTest = EntityTest()
val ioException = IOException()
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
Flowable.just(listOf(entityTest)),
Single.error(ioException),
persist
)
val testSubscriber = usecaseFlowable.test()
testSubscriber.assertNoErrors()
testSubscriber.assertValues(
ResultState.Loading(entityTest),
ResultState.Error(ioException, entityTest)
)
}
@Test
fun `test loading without error and check success`() {
val entityTest = EntityTest()
val database = PublishProcessor.create<List<EntityTest>>()
val net = PublishSubject.create<EntityTest>()
val persist = { d: EntityTest -> database.onNext(listOf(d)) }
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
database,
net.firstOrError(),
persist
)
val testSubscriber = usecaseFlowable.test()
database.onNext(listOf(entityTest))
net.onNext(entityTest)
testSubscriber.assertNotComplete()
testSubscriber.assertNoErrors()
testSubscriber.assertValues(
ResultState.Loading(entityTest),
ResultState.Success(entityTest)
)
}
@Test
fun `test loading without error and empty list and check success`() {
val entityTest = EntityTest()
val database = PublishProcessor.create<List<EntityTest>>()
val net = PublishSubject.create<EntityTest>()
val persist = { d: EntityTest -> database.onNext(listOf(d)) }
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
database,
net.firstOrError(),
persist
)
val testSubscriber = usecaseFlowable.test()
database.onNext(listOf())
net.onNext(entityTest)
testSubscriber.assertNotComplete()
testSubscriber.assertNoErrors()
testSubscriber.assertValues(ResultState.Success(entityTest))
}
@Test
fun `test loading with error and empty list`() {
val ioException = IOException()
val database = PublishProcessor.create<List<EntityTest>>()
val net = PublishSubject.create<EntityTest>()
val persist = { d: EntityTest -> database.onNext(listOf(d)) }
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
database,
net.firstOrError(),
persist
)
val testSubscriber = usecaseFlowable.test()
database.onNext(listOf())
net.onError(ioException)
testSubscriber.assertNotComplete()
testSubscriber.assertNoErrors()
testSubscriber.assertValues(ResultState.Error(ioException, null))
}
@Test
fun `test loading with error and check complete`() {
val database = PublishProcessor.create<List<EntityTest>>()
val net = PublishSubject.create<EntityTest>()
val persist = { d: EntityTest -> database.onNext(listOf(d)) }
val entityTest = EntityTest()
val ioException = IOException()
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
database,
net.firstOrError(),
persist
)
val testSubscriber = usecaseFlowable.test()
database.onNext(listOf(entityTest))
net.onError(ioException)
testSubscriber.assertNoErrors()
testSubscriber.assertNotComplete()
testSubscriber.assertValues(
ResultState.Loading(entityTest),
ResultState.Error(ioException, entityTest)
)
}
@Test
fun `test loading with error and check complete and update`() {
val database = PublishProcessor.create<List<EntityTest>>()
val net = PublishSubject.create<EntityTest>()
val persist = { d: EntityTest -> database.onNext(listOf(d)) }
val entityTest = EntityTest()
val ioException = IOException()
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
database,
net.firstOrError(),
persist
)
val testSubscriber = usecaseFlowable.test()
database.onNext(listOf(entityTest))
net.onError(ioException)
database.onNext(listOf(entityTest))
testSubscriber.assertNoErrors()
testSubscriber.assertNotComplete()
testSubscriber.assertValues(
ResultState.Loading(entityTest),
ResultState.Error(ioException, entityTest),
ResultState.Success(entityTest)
)
}
@Test
fun `test loading with both error`() {
val database = PublishProcessor.create<List<EntityTest>>()
val net = PublishSubject.create<EntityTest>()
val persist = { d: EntityTest -> database.onNext(listOf(d)) }
val sqlException = RuntimeException()
val ioException = IOException()
val usecaseFlowable: Flowable<ResultState<EntityTest>> = TestRepository().performTest(
database,
net.firstOrError(),
persist
)
val testSubscriber = usecaseFlowable.test()
database.onError(sqlException)
net.onError(ioException)
testSubscriber.assertNoErrors()
testSubscriber.assertNotComplete()
testSubscriber.assertValues(ResultState.Error(sqlException, null))
}
class EntityTest : Entity
class TestRepository : BaseRepository<EntityTest>() {
fun <D> performTest(
databaseFlowable: Flowable<List<D>>, netSingle: Single<D>,
persist: (D) -> Unit): Flowable<ResultState<D>> {
return perform(databaseFlowable, netSingle, persist)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment