Search in sources :

Example 21 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class DefaultGetResolverTest method query.

@Test
public void query() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    final Query query = Query.builder().uri(mock(Uri.class)).build();
    final Cursor expectedCursor = mock(Cursor.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    when(lowLevel.query(query)).thenReturn(expectedCursor);
    final GetResolver<TestItem> defaultGetResolver = new DefaultGetResolver<TestItem>() {

        @NonNull
        @Override
        public TestItem mapFromCursor(@NonNull Cursor cursor) {
            assertThat(cursor).isSameAs(expectedCursor);
            return new TestItem();
        }
    };
    final Cursor actualCursor = defaultGetResolver.performGet(storIOContentResolver, query);
    // only one request should occur
    verify(lowLevel, times(1)).query(any(Query.class));
    // and this request should be equals to original
    verify(lowLevel, times(1)).query(query);
    assertThat(actualCursor).isSameAs(expectedCursor);
}
Also used : Query(com.pushtorefresh.storio.contentresolver.queries.Query) NonNull(android.support.annotation.NonNull) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 22 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver in project storio by pushtorefresh.

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForObservable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForObservable() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    Uri testUri = mock(Uri.class);
    when(storIOContentResolver.observeChangesOfUri(eq(testUri))).thenReturn(Observable.<Changes>empty());
    //noinspection unchecked
    final GetResolver<Integer> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOContentResolver), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
    new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(testUri).build()).withGetResolver(getResolver).prepare().asRxObservable().subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    assertThat(testSubscriber.getOnErrorEvents()).hasSize(1);
    StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
    testSubscriber.unsubscribe();
}
Also used : Query(com.pushtorefresh.storio.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Uri(android.net.Uri) Test(org.junit.Test)

Example 23 with StorIOContentResolver

use of com.pushtorefresh.storio.contentresolver.StorIOContentResolver 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 TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
    new PreparedGetNumberOfResults.Builder(storIOContentResolver).withQuery(Query.builder().uri(testUri).build()).withGetResolver(getResolver).prepare().asRxSingle().subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    assertThat(testSubscriber.getOnErrorEvents()).hasSize(1);
    StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
    testSubscriber.unsubscribe();
}
Also used : Query(com.pushtorefresh.storio.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) StorIOContentResolver(com.pushtorefresh.storio.contentresolver.StorIOContentResolver) Uri(android.net.Uri) Test(org.junit.Test)

Aggregations

StorIOContentResolver (com.pushtorefresh.storio.contentresolver.StorIOContentResolver)23 Test (org.junit.Test)20 Query (com.pushtorefresh.storio.contentresolver.queries.Query)11 NonNull (android.support.annotation.NonNull)9 ContentResolver (android.content.ContentResolver)8 Cursor (android.database.Cursor)6 Uri (android.net.Uri)6 ContentValues (android.content.ContentValues)5 StorIOException (com.pushtorefresh.storio.StorIOException)5 InsertQuery (com.pushtorefresh.storio.contentresolver.queries.InsertQuery)5 UpdateQuery (com.pushtorefresh.storio.contentresolver.queries.UpdateQuery)5 TypeMappingFinder (com.pushtorefresh.storio.TypeMappingFinder)4 GetResolver (com.pushtorefresh.storio.contentresolver.operations.get.GetResolver)3 WorkerThread (android.support.annotation.WorkerThread)2 ContentResolverTypeMapping (com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping)2 TypeMappingFinderImpl (com.pushtorefresh.storio.internal.TypeMappingFinderImpl)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 HashMap (java.util.HashMap)2 TestSubscriber (rx.observers.TestSubscriber)2 Handler (android.os.Handler)1