Search in sources :

Example 41 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultGetResolverTest method query.

@Test
public void query() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    final Query query = Query.builder().table("test_table").build();
    final Cursor expectedCursor = mock(Cursor.class);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    when(internal.query(query)).thenReturn(expectedCursor);
    final DefaultGetResolver<TestItem> defaultGetResolver = new DefaultGetResolver<TestItem>() {

        @NonNull
        @Override
        public TestItem mapFromCursor(@NonNull Cursor cursor) {
            return mock(TestItem.class);
        }
    };
    final Cursor actualCursor = defaultGetResolver.performGet(storIOSQLite, query);
    // only one request should occur
    verify(internal, times(1)).query(any(Query.class));
    // and this request should be equals to original
    verify(internal, times(1)).query(query);
    assertThat(actualCursor).isSameAs(expectedCursor);
}
Also used : RawQuery(com.pushtorefresh.storio.sqlite.queries.RawQuery) Query(com.pushtorefresh.storio.sqlite.queries.Query) NonNull(android.support.annotation.NonNull) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 42 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class PreparedGetNumberOfResultsTest method shouldWrapExceptionIntoStorIOExceptionForSingle.

@Test
public void shouldWrapExceptionIntoStorIOExceptionForSingle() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    //noinspection unchecked
    final GetResolver<Integer> getResolver = mock(GetResolver.class);
    when(getResolver.performGet(eq(storIOSQLite), any(Query.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>();
    new PreparedGetNumberOfResults.Builder(storIOSQLite).withQuery(Query.builder().table("test_table").build()).withGetResolver(getResolver).prepare().asRxSingle().subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent(60, SECONDS);
    testSubscriber.assertError(StorIOException.class);
    assertThat(testSubscriber.getOnErrorEvents()).hasSize(1);
    StorIOException storIOException = (StorIOException) testSubscriber.getOnErrorEvents().get(0);
    IllegalStateException cause = (IllegalStateException) storIOException.getCause();
    assertThat(cause).hasMessage("test exception");
}
Also used : Query(com.pushtorefresh.storio.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 43 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method observeChangesAndNotifyAboutChangesShouldWorkCorrectly.

@Test
public void observeChangesAndNotifyAboutChangesShouldWorkCorrectly() {
    SQLiteOpenHelper sqLiteOpenHelper = mock(SQLiteOpenHelper.class);
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(sqLiteOpenHelper).build();
    TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>();
    storIOSQLite.observeChanges().subscribe(testSubscriber);
    testSubscriber.assertNoValues();
    Changes changes = Changes.newInstance("test_table");
    storIOSQLite.lowLevel().notifyAboutChanges(changes);
    testSubscriber.assertValue(changes);
    testSubscriber.assertNoErrors();
    testSubscriber.unsubscribe();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) Changes(com.pushtorefresh.storio.sqlite.Changes) TestSubscriber(rx.observers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 44 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method defaultSchedulerReturnsNullIfSpecifiedSchedulerNull.

@Test
public void defaultSchedulerReturnsNullIfSpecifiedSchedulerNull() {
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).defaultScheduler(null).build();
    assertThat(storIOSQLite.defaultScheduler()).isNull();
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 45 with StorIOSQLite

use of com.pushtorefresh.storio.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method observeChangesInTablesShouldNotAcceptNullAsTables.

@Test
public void observeChangesInTablesShouldNotAcceptNullAsTables() {
    StorIOSQLite storIOSQLite = DefaultStorIOSQLite.builder().sqliteOpenHelper(mock(SQLiteOpenHelper.class)).build();
    try {
        //noinspection ConstantConditions
        storIOSQLite.observeChangesInTables(null);
        failBecauseExceptionWasNotThrown(NullPointerException.class);
    } catch (NullPointerException expected) {
        assertThat(expected).hasMessage("Set of tables can not be null");
    }
}
Also used : 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