Search in sources :

Example 21 with StorIOSQLite

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();
}
Also used : Query(com.pushtorefresh.storio.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 22 with StorIOSQLite

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");
}
Also used : Query(com.pushtorefresh.storio.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 23 with StorIOSQLite

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");
    }
}
Also used : Query(com.pushtorefresh.storio.sqlite.queries.Query) StorIOException(com.pushtorefresh.storio.StorIOException) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 24 with StorIOSQLite

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();
}
Also used : ContentValues(android.content.ContentValues) Query(com.pushtorefresh.storio.sqlite.queries.Query) InsertQuery(com.pushtorefresh.storio.sqlite.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio.sqlite.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio.sqlite.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio.sqlite.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 25 with StorIOSQLite

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);
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) 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