use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class PreparedPutContentValuesIterableTest method verifyBehaviorInCaseOfExceptionWithoutTransactionBlocking.
@Test
public void verifyBehaviorInCaseOfExceptionWithoutTransactionBlocking() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
//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"));
try {
new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(false).prepare().executeAsBlocking();
failBecauseExceptionWasNotThrown(StorIOException.class);
} catch (StorIOException expected) {
IllegalStateException cause = (IllegalStateException) expected.getCause();
assertThat(cause).hasMessage("test exception");
// Main check of this test
verify(internal, never()).endTransaction();
verify(storIOSQLite).lowLevel();
verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
verifyNoMoreInteractions(storIOSQLite, internal, putResolver);
}
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class PreparedPutContentValuesIterableTest method shouldFinishTransactionIfExceptionHasOccurredCompletable.
@Test
public void shouldFinishTransactionIfExceptionHasOccurredCompletable() {
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().asRxCompletable().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);
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultStorIOSQLiteTest method observeChangesInTables.
@Test
public void observeChangesInTables() {
StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).build();
TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
Set<String> tables = new HashSet<String>(2);
tables.add("table1");
tables.add("table2");
storIOSQLite.observeChangesInTables(tables).subscribe(testSubscriber);
testSubscriber.assertNoValues();
Changes changes1 = Changes.newInstance("table1");
storIOSQLite.lowLevel().notifyAboutChanges(changes1);
testSubscriber.assertValue(changes1);
Changes changes2 = Changes.newInstance("table2");
storIOSQLite.lowLevel().notifyAboutChanges(changes2);
testSubscriber.assertValues(changes1, changes2);
Changes changes3 = Changes.newInstance("table3");
storIOSQLite.lowLevel().notifyAboutChanges(changes3);
// changes3 or any other changes are not expected here
testSubscriber.assertValues(changes1, changes2);
testSubscriber.assertNoErrors();
testSubscriber.unsubscribe();
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultStorIOSQLiteTest method shouldPassArgsToInsertWithOnConflict.
@Test
public void shouldPassArgsToInsertWithOnConflict() {
SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
InsertQuery insertQuery = InsertQuery.builder().table("test_table").nullColumnHack("custom_null_hack").build();
ContentValues contentValues = mock(ContentValues.class);
int conflictAlgorithm = SQLiteDatabase.CONFLICT_ROLLBACK;
storIOSQLite.lowLevel().insertWithOnConflict(insertQuery, contentValues, conflictAlgorithm);
verify(sqLiteDatabase).insertWithOnConflict(eq("test_table"), eq("custom_null_hack"), same(contentValues), eq(SQLiteDatabase.CONFLICT_ROLLBACK));
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultStorIOSQLiteTest method observeChangesInTableShouldNotAcceptNullAsTables.
@Test
public void observeChangesInTableShouldNotAcceptNullAsTables() {
StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).build();
try {
//noinspection ConstantConditions
storIOSQLite.observeChangesInTable(null);
failBecauseExceptionWasNotThrown(NullPointerException.class);
} catch (NullPointerException expected) {
assertThat(expected).hasMessage("Table can not be null or empty");
}
}
Aggregations