Search in sources :

Example 6 with StorIOContentResolver

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

the class PutOperationDesignTest method putContentValuesSingle.

@Test
public void putContentValuesSingle() {
    ContentValues contentValues = mock(ContentValues.class);
    Single<PutResult> putResultSingle = storIOContentResolver().put().contentValues(contentValues).withPutResolver(putResolverForContentValues).prepare().asRxSingle();
}
Also used : ContentValues(android.content.ContentValues) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 7 with StorIOContentResolver

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

the class DefaultDeleteResolverTest method performDelete.

@Test
public void performDelete() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    final int expectedNumberOfRowsDeleted = 1;
    when(lowLevel.delete(any(DeleteQuery.class))).thenReturn(expectedNumberOfRowsDeleted);
    final Uri expectedUri = mock(Uri.class);
    final DeleteQuery expectedDeleteQuery = DeleteQuery.builder().uri(expectedUri).where("test where clause").whereArgs("test").build();
    final TestItem testItem = TestItem.newInstance();
    final DefaultDeleteResolver<TestItem> defaultDeleteResolver = new DefaultDeleteResolver<TestItem>() {

        @NonNull
        @Override
        protected DeleteQuery mapToDeleteQuery(@NonNull TestItem object) {
            assertThat(object).isSameAs(testItem);
            return expectedDeleteQuery;
        }
    };
    // Performing Delete Operation
    final DeleteResult deleteResult = defaultDeleteResolver.performDelete(storIOContentResolver, testItem);
    // checks that required delete was performed
    verify(lowLevel, times(1)).delete(expectedDeleteQuery);
    // only one delete should be performed
    verify(lowLevel, times(1)).delete(any(DeleteQuery.class));
    // delete result checks
    assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(expectedNumberOfRowsDeleted);
    assertThat(deleteResult.affectedUris()).hasSize(1);
    assertThat(deleteResult.affectedUris()).contains(expectedUri);
}
Also used : NonNull(android.support.annotation.NonNull) DeleteQuery(com.pushtorefresh.storio3.contentresolver.queries.DeleteQuery) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Uri(android.net.Uri) Test(org.junit.Test)

Example 8 with StorIOContentResolver

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

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForFlowable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForFlowable() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    Uri testUri = mock(Uri.class);
    when(storIOContentResolver.observeChangesOfUri(eq(testUri), eq(BackpressureStrategy.MISSING))).thenReturn(Flowable.<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().asRxFlowable(BackpressureStrategy.MISSING).subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    assertThat(testSubscriber.errors()).hasSize(1);
    StorIOException storIOException = (StorIOException) testSubscriber.errors().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
    testSubscriber.dispose();
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOException(com.pushtorefresh.storio3.StorIOException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Uri(android.net.Uri) Test(org.junit.Test)

Example 9 with StorIOContentResolver

use of com.pushtorefresh.storio3.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 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)

Example 10 with StorIOContentResolver

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

the class DefaultPutResolverTest method insert.

/**
 * Verifies behavior of {@link DefaultPutResolver} for "insert"
 */
@Test
public void insert() throws Exception {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    final Uri expectedInsertedUri = mock(Uri.class);
    final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// No results -> insert should be performed
    0);
    when(lowLevel.insert(any(InsertQuery.class), any(ContentValues.class))).thenReturn(expectedInsertedUri);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return expectedInsertQuery;
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            try {
                return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.apply(testItem);
    // Performing Put that should "insert"
    final PutResult putResult = putResolver.performPut(storIOContentResolver, testItem);
    // checks that it asks db for results
    verify(lowLevel).query(eq(expectedQuery));
    // checks that cursor was closed
    verify(cursor).close();
    // only one query should occur
    verify(lowLevel).query(any(Query.class));
    // checks that required insert was performed
    verify(lowLevel).insert(eq(expectedInsertQuery), eq(expectedContentValues));
    // only one insert should occur
    verify(lowLevel).insert(any(InsertQuery.class), any(ContentValues.class));
    // no updates should occur
    verify(lowLevel, never()).update(any(UpdateQuery.class), any(ContentValues.class));
    // put result checks
    assertThat(putResult.wasInserted()).isTrue();
    assertThat(putResult.wasUpdated()).isFalse();
    assertThat(putResult.insertedUri()).isEqualTo(expectedInsertedUri);
    assertThat(putResult.numberOfRowsUpdated()).isNull();
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) Uri(android.net.Uri) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)31 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)23 Query (com.pushtorefresh.storio3.contentresolver.queries.Query)11 ContentResolver (android.content.ContentResolver)9 NonNull (android.support.annotation.NonNull)9 ContentValues (android.content.ContentValues)8 Cursor (android.database.Cursor)7 Uri (android.net.Uri)7 DeleteQuery (com.pushtorefresh.storio3.contentresolver.queries.DeleteQuery)6 TypeMappingFinder (com.pushtorefresh.storio3.TypeMappingFinder)5 InsertQuery (com.pushtorefresh.storio3.contentresolver.queries.InsertQuery)5 UpdateQuery (com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery)5 DeleteResult (com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult)4 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)4 StorIOException (com.pushtorefresh.storio3.StorIOException)3 GetResolver (com.pushtorefresh.storio3.contentresolver.operations.get.GetResolver)3 Handler (android.os.Handler)2 TypeMappingFinderImpl (com.pushtorefresh.storio3.internal.TypeMappingFinderImpl)2 Completable (io.reactivex.Completable)2 ContentObserver (android.database.ContentObserver)1