Search in sources :

Example 11 with StorIOSQLite

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();
    }
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 12 with StorIOSQLite

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();
}
Also used : Changes(com.pushtorefresh.storio.sqlite.Changes) TestSubscriber(rx.observers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 13 with StorIOSQLite

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);
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) Scheduler(rx.Scheduler) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 14 with StorIOSQLite

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());
}
Also used : StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite)

Example 15 with StorIOSQLite

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));
}
Also used : NonNull(android.support.annotation.NonNull) DeleteQuery(com.pushtorefresh.storio.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Aggregations

StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)45 Test (org.junit.Test)37 StorIOException (com.pushtorefresh.storio.StorIOException)23 TestSubscriber (rx.observers.TestSubscriber)16 ContentValues (android.content.ContentValues)13 NonNull (android.support.annotation.NonNull)12 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)9 Query (com.pushtorefresh.storio.sqlite.queries.Query)9 Cursor (android.database.Cursor)8 DeleteQuery (com.pushtorefresh.storio.sqlite.queries.DeleteQuery)6 WorkerThread (android.support.annotation.WorkerThread)5 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Changes (com.pushtorefresh.storio.sqlite.Changes)4 InsertQuery (com.pushtorefresh.storio.sqlite.queries.InsertQuery)4 RawQuery (com.pushtorefresh.storio.sqlite.queries.RawQuery)4 HashSet (java.util.HashSet)4 UpdateQuery (com.pushtorefresh.storio.sqlite.queries.UpdateQuery)3 HashMap (java.util.HashMap)3 SQLiteTypeMapping (com.pushtorefresh.storio.sqlite.SQLiteTypeMapping)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2