Search in sources :

Example 66 with TestObserver

use of io.reactivex.observers.TestObserver in project apollo-android by apollographql.

the class Rx2ApolloTest method queryWatcherNotCalledWhenCanceled.

@Test
public void queryWatcherNotCalledWhenCanceled() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestObserver<EpisodeHeroNameQuery.Data> testObserver = new TestObserver<>();
    TestScheduler scheduler = new TestScheduler();
    Disposable disposable = Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {

        @Override
        public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            return response.data();
        }
    }).observeOn(scheduler).subscribeWith(testObserver);
    scheduler.triggerActions();
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_CHANGE));
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    disposable.dispose();
    scheduler.triggerActions();
    testObserver.assertValueCount(1).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {

        @Override
        public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
            assertThat(data.hero().name()).isEqualTo("R2-D2");
            return true;
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) TestObserver(io.reactivex.observers.TestObserver) Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 67 with TestObserver

use of io.reactivex.observers.TestObserver in project storio by pushtorefresh.

the class SingleOnSubscribeExecuteAsBlockingOptionalTest method shouldCallOnErrorIfExceptionOccurred.

@SuppressWarnings("CheckResult")
@Test
public void shouldCallOnErrorIfExceptionOccurred() {
    // noinspection unchecked
    final PreparedOperation<Object, Optional<Object>, Object> preparedOperation = mock(PreparedOperation.class);
    StorIOException expectedException = new StorIOException("test exception");
    when(preparedOperation.executeAsBlocking()).thenThrow(expectedException);
    TestObserver<Optional<Object>> testObserver = new TestObserver<Optional<Object>>();
    Single<Optional<Object>> single = Single.create(new SingleOnSubscribeExecuteAsBlockingOptional<Object, Object>(preparedOperation));
    verifyZeroInteractions(preparedOperation);
    single.subscribe(testObserver);
    testObserver.assertError(expectedException);
    testObserver.assertNotComplete();
    verify(preparedOperation).executeAsBlocking();
}
Also used : Optional(com.pushtorefresh.storio3.Optional) StorIOException(com.pushtorefresh.storio3.StorIOException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 68 with TestObserver

use of io.reactivex.observers.TestObserver in project storio by pushtorefresh.

the class CompletableOnSubscribeExecuteAsBlockingTest method shouldExecuteAsBlockingAfterSubscription.

@SuppressWarnings("ResourceType")
@Test
public void shouldExecuteAsBlockingAfterSubscription() {
    final PreparedCompletableOperation preparedOperation = mock(PreparedCompletableOperation.class);
    TestObserver testObserver = new TestObserver();
    verifyZeroInteractions(preparedOperation);
    Completable completable = Completable.create(new CompletableOnSubscribeExecuteAsBlocking(preparedOperation));
    verifyZeroInteractions(preparedOperation);
    completable.subscribe(testObserver);
    testObserver.assertNoErrors();
    testObserver.assertComplete();
    verify(preparedOperation).executeAsBlocking();
    verify(preparedOperation, never()).asRxFlowable(any(BackpressureStrategy.class));
    verify(preparedOperation, never()).asRxSingle();
    verify(preparedOperation, never()).asRxCompletable();
}
Also used : Completable(io.reactivex.Completable) PreparedCompletableOperation(com.pushtorefresh.storio3.operations.PreparedCompletableOperation) BackpressureStrategy(io.reactivex.BackpressureStrategy) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 69 with TestObserver

use of io.reactivex.observers.TestObserver in project storio by pushtorefresh.

the class MaybeOnSubscribeExecuteAsBlockingTest method shouldCallOnErrorIfExceptionOccurred.

@SuppressWarnings("CheckResult")
@Test
public void shouldCallOnErrorIfExceptionOccurred() {
    // noinspection unchecked
    final PreparedMaybeOperation<Object, Object, Object> preparedOperation = mock(PreparedMaybeOperation.class);
    StorIOException expectedException = new StorIOException("test exception");
    when(preparedOperation.executeAsBlocking()).thenThrow(expectedException);
    TestObserver<Object> testObserver = new TestObserver<Object>();
    Maybe<Object> maybe = Maybe.create(new MaybeOnSubscribeExecuteAsBlocking<Object, Object, Object>(preparedOperation));
    verifyZeroInteractions(preparedOperation);
    maybe.subscribe(testObserver);
    testObserver.assertError(expectedException);
    testObserver.assertNotComplete();
    verify(preparedOperation).executeAsBlocking();
}
Also used : StorIOException(com.pushtorefresh.storio3.StorIOException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 70 with TestObserver

use of io.reactivex.observers.TestObserver in project storio by pushtorefresh.

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForSingle.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForSingle() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    Uri testUri = mock(Uri.class);
    // noinspection unchecked
    final GetResolver<Integer> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestObserver<Integer> testObserver = new TestObserver<Integer>();
    new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(testUri).build()).withGetResolver(getResolver).prepare().asRxSingle().subscribe(testObserver);
    testObserver.awaitTerminalEvent(60, SECONDS);
    testObserver.assertError(StorIOException.class);
    assertThat(testObserver.errors()).hasSize(1);
    StorIOException storIOException = (StorIOException) testObserver.errors().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
    testObserver.dispose();
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Uri(android.net.Uri) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

TestObserver (io.reactivex.observers.TestObserver)158 Test (org.junit.Test)128 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 TargetApi (android.annotation.TargetApi)21 Matchers.anyString (org.mockito.Matchers.anyString)21 StorIOException (com.pushtorefresh.storio3.StorIOException)19 TestException (io.reactivex.exceptions.TestException)19 Observable (io.reactivex.Observable)12 BaseTest (io.rx_cache2.internal.common.BaseTest)12 Completable (io.reactivex.Completable)11 Disposable (io.reactivex.disposables.Disposable)11 InOrder (org.mockito.InOrder)10 TestScheduler (io.reactivex.schedulers.TestScheduler)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)8 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)7 Schedulers (io.reactivex.schedulers.Schedulers)6 Activity (android.app.Activity)5 Instrumentation (android.app.Instrumentation)5 ContentValues (android.content.ContentValues)5 Context (android.content.Context)5