use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class PreparedGetCursorTest method shouldWrapExceptionIntoStorIOExceptionForObservable.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForObservable() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
when(storIOSQLite.observeChangesInTables(eq(singleton("test_table")))).thenReturn(Observable.<Changes>empty());
//noinspection unchecked
final GetResolver<Cursor> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<Cursor> testSubscriber = new TestSubscriber<Cursor>();
new PreparedGetCursor.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().asRxObservable().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent(60, SECONDS);
testSubscriber.assertError(StorIOException.class);
StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) storIOException.getCause();
assertThat(cause).hasMessage("test exception");
testSubscriber.unsubscribe();
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class PreparedGetCursorTest method shouldWrapExceptionIntoStorIOExceptionForSingle.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForSingle() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
//noinspection unchecked
final GetResolver<Cursor> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<Cursor> testSubscriber = new TestSubscriber<Cursor>();
new PreparedGetCursor.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().asRxSingle().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent(60, SECONDS);
testSubscriber.assertError(StorIOException.class);
StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) storIOException.getCause();
assertThat(cause).hasMessage("test exception");
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class PreparedGetCursorTest method shouldWrapExceptionIntoStorIOExceptionForBlocking.
@Test
public void shouldWrapExceptionIntoStorIOExceptionForBlocking() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
//noinspection unchecked
final GetResolver<Cursor> getResolver = mock(GetResolver.class);
when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
try {
new PreparedGetCursor.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().executeAsBlocking();
failBecauseExceptionWasNotThrown(StorIOException.class);
} catch (StorIOException expected) {
IllegalStateException cause = (IllegalStateException) expected.getCause();
assertThat(cause).hasMessage("test exception");
}
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultPutResolverTest method insert.
/**
* Verifies behavior of {@link DefaultPutResolver} for "insert"
*/
@Test
public void insert() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
// item without id, should be inserted
final TestItem testItem = new TestItem(null);
when(storIOSQLite.lowLevel()).thenReturn(internal);
final Long expectedInsertedId = 24L;
final Query expectedQuery = Query.builder().table(TestItem.TABLE).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
final Cursor cursor = mock(Cursor.class);
when(internal.query(eq(expectedQuery))).thenReturn(cursor);
when(cursor.getCount()).thenReturn(// No results -> insert should be performed
0);
when(internal.insert(any(InsertQuery.class), any(ContentValues.class))).thenReturn(expectedInsertedId);
final InsertQuery expectedInsertQuery = InsertQuery.builder().table(TestItem.TABLE).nullColumnHack(null).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().table(TestItem.TABLE).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
}
@NonNull
@Override
protected ContentValues mapToContentValues(@NonNull TestItem object) {
return TestItem.MAP_TO_CONTENT_VALUES.call(object);
}
};
final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.call(testItem);
// Performing Put that should "insert"
final PutResult putResult = putResolver.performPut(storIOSQLite, testItem);
verify(internal, times(1)).beginTransaction();
verify(internal, times(1)).setTransactionSuccessful();
verify(internal, times(1)).endTransaction();
// checks that it asks db for results
verify(internal, times(1)).query(eq(expectedQuery));
// checks that cursor was closed
verify(cursor, times(1)).close();
// only one query should occur
verify(internal, times(1)).query(any(Query.class));
// checks that required insert was performed
verify(internal, times(1)).insert(eq(expectedInsertQuery), eq(expectedContentValues));
// only one insert should occur
verify(internal, times(1)).insert(any(InsertQuery.class), any(ContentValues.class));
// no updates should occur
verify(internal, times(0)).update(any(UpdateQuery.class), any(ContentValues.class));
// put result checks
assertThat(putResult.wasInserted()).isTrue();
assertThat(putResult.wasUpdated()).isFalse();
assertThat(putResult.insertedId()).isEqualTo(expectedInsertedId);
assertThat(putResult.numberOfRowsUpdated()).isNull();
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class PreparedPutContentValuesIterableTest method shouldFinishTransactionIfExceptionHasOccurredObservable.
@Test
public void shouldFinishTransactionIfExceptionHasOccurredObservable() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
when(storIOSQLite.lowLevel()).thenReturn(internal);
//noinspection unchecked
final PutResolver<ContentValues> putResolver = mock(PutResolver.class);
final List<ContentValues> contentValues = singletonList(mock(ContentValues.class));
when(putResolver.performPut(same(storIOSQLite), any(ContentValues.class))).thenThrow(new IllegalStateException("test exception"));
final TestSubscriber<PutResults<ContentValues>> testSubscriber = new TestSubscriber<PutResults<ContentValues>>();
new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(true).prepare().asRxObservable().subscribe(testSubscriber);
testSubscriber.awaitTerminalEvent();
testSubscriber.assertNoValues();
testSubscriber.assertError(StorIOException.class);
//noinspection ThrowableResultOfMethodCallIgnored
StorIOException expected = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
IllegalStateException cause = (IllegalStateException) expected.getCause();
assertThat(cause).hasMessage("test exception");
verify(internal).beginTransaction();
verify(internal, never()).setTransactionSuccessful();
verify(internal).endTransaction();
verify(storIOSQLite).lowLevel();
verify(storIOSQLite).defaultScheduler();
verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
verifyNoMoreInteractions(storIOSQLite, internal, putResolver);
}
Aggregations