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();
}
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);
}
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();
}
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();
}
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();
}
Aggregations