use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultStorIOSQLiteTest method nestedTransactionShouldWorkNormally.
// See https://github.com/pushtorefresh/storio/issues/478
@Test
public void nestedTransactionShouldWorkNormally() {
SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
SQLiteDatabase sqLiteDatabase = mock(SQLiteDatabase.class);
when(sqLiteOpenHelper.getWritableDatabase()).thenReturn(sqLiteDatabase);
StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
// External transaction
storIOSQLite.lowLevel().beginTransaction();
try {
try {
// Nested transaction
storIOSQLite.lowLevel().beginTransaction();
storIOSQLite.lowLevel().notifyAboutChanges(Changes.newInstance("table1"));
storIOSQLite.lowLevel().notifyAboutChanges(Changes.newInstance("table2"));
// Finishing nested transaction
storIOSQLite.lowLevel().setTransactionSuccessful();
} finally {
storIOSQLite.lowLevel().endTransaction();
}
// Marking external transaction as successful
storIOSQLite.lowLevel().setTransactionSuccessful();
} finally {
// Finishing external transaction
storIOSQLite.lowLevel().endTransaction();
}
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultStorIOSQLiteTest method observeChangesInTable.
@Test
public void observeChangesInTable() {
StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).build();
TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
storIOSQLite.observeChangesInTable("table1").subscribe(testSubscriber);
testSubscriber.assertNoValues();
Changes changes1 = Changes.newInstance("table2");
storIOSQLite.lowLevel().notifyAboutChanges(changes1);
testSubscriber.assertNoValues();
Changes changes2 = Changes.newInstance("table1");
storIOSQLite.lowLevel().notifyAboutChanges(changes2);
testSubscriber.assertValue(changes2);
Changes changes3 = Changes.newInstance("table3");
storIOSQLite.lowLevel().notifyAboutChanges(changes3);
// Subscriber should not see changes of table2 and table3
testSubscriber.assertValue(changes2);
testSubscriber.assertNoErrors();
testSubscriber.unsubscribe();
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultStorIOSQLiteTest method defaultSchedulerReturnsSpecifiedScheduler.
@Test
public void defaultSchedulerReturnsSpecifiedScheduler() {
Scheduler scheduler = mock(Scheduler.class);
StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).defaultScheduler(scheduler).build();
assertThat(storIOSQLite.defaultScheduler()).isSameAs(scheduler);
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultStorIOSQLiteTest method defaultSchedulerReturnsIOSchedulerIfNotSpecified.
public void defaultSchedulerReturnsIOSchedulerIfNotSpecified() {
StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).build();
assertThat(storIOSQLite.defaultScheduler()).isSameAs(io());
}
use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.
the class DefaultDeleteResolverTest method performDelete.
@Test
public void performDelete() {
final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
final String testTable = "test_table";
final DeleteQuery deleteQuery = DeleteQuery.builder().table(testTable).build();
when(storIOSQLite.lowLevel()).thenReturn(internal);
when(internal.delete(deleteQuery)).thenReturn(1);
final TestItem testItem = new TestItem();
final DefaultDeleteResolver<TestItem> defaultDeleteResolver = new DefaultDeleteResolver<TestItem>() {
@NonNull
@Override
public DeleteQuery mapToDeleteQuery(@NonNull TestItem testItem) {
return deleteQuery;
}
};
final DeleteResult deleteResult = defaultDeleteResolver.performDelete(storIOSQLite, testItem);
verify(internal, times(1)).delete(any(DeleteQuery.class));
verify(internal, times(1)).delete(deleteQuery);
assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(1);
assertThat(deleteResult.affectedTables()).isEqualTo(Collections.singleton(testTable));
}
Aggregations