Search in sources :

Example 16 with StorIOSQLite

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

the class PreparedDeleteByQueryTest method shouldWrapExceptionIntoStorIOExceptionBlocking.

@Test
public void shouldWrapExceptionIntoStorIOExceptionBlocking() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    //noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    when(deleteResolver.performDelete(same(storIOSQLite), any(DeleteQuery.class))).thenThrow(new IllegalStateException("test exception"));
    try {
        new PreparedDeleteByQuery.Builder(storIOSQLite, DeleteQuery.builder().table("test_table").build()).withDeleteResolver(deleteResolver).prepare().executeAsBlocking();
        failBecauseExceptionWasNotThrown(StorIOException.class);
    } catch (StorIOException expected) {
        IllegalStateException cause = (IllegalStateException) expected.getCause();
        assertThat(cause).hasMessage("test exception");
        verify(deleteResolver).performDelete(same(storIOSQLite), any(DeleteQuery.class));
        verifyNoMoreInteractions(storIOSQLite, internal, deleteResolver);
    }
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) DeleteQuery(com.pushtorefresh.storio.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 17 with StorIOSQLite

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

the class PreparedDeleteByQueryTest method shouldWrapExceptionIntoStorIOExceptionObservable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionObservable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    //noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    when(deleteResolver.performDelete(same(storIOSQLite), any(DeleteQuery.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<DeleteResult> testSubscriber = new TestSubscriber<DeleteResult>();
    new PreparedDeleteByQuery.Builder(storIOSQLite, DeleteQuery.builder().table("test_table").build()).withDeleteResolver(deleteResolver).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(deleteResolver).performDelete(same(storIOSQLite), any(DeleteQuery.class));
    verify(storIOSQLite).defaultScheduler();
    verifyNoMoreInteractions(storIOSQLite, internal, deleteResolver);
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) DeleteQuery(com.pushtorefresh.storio.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 18 with StorIOSQLite

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

the class PreparedDeleteByQueryTest method shouldNotNotifyIfWasNotDeleted.

@Test
public void shouldNotNotifyIfWasNotDeleted() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    final DeleteQuery deleteQuery = DeleteQuery.builder().table("test_table").where("column1 = ?").whereArgs(1).build();
    //noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    // No items were deleted
    final DeleteResult expectedDeleteResult = DeleteResult.newInstance(0, deleteQuery.table());
    when(deleteResolver.performDelete(same(storIOSQLite), same(deleteQuery))).thenReturn(expectedDeleteResult);
    final DeleteResult actualDeleteResult = new PreparedDeleteByQuery.Builder(storIOSQLite, deleteQuery).withDeleteResolver(deleteResolver).prepare().executeAsBlocking();
    assertThat(actualDeleteResult).isEqualTo(expectedDeleteResult);
    verify(deleteResolver).performDelete(same(storIOSQLite), same(deleteQuery));
    verify(internal, never()).notifyAboutChanges(any(Changes.class));
    verifyNoMoreInteractions(storIOSQLite, internal, deleteResolver);
}
Also used : Changes(com.pushtorefresh.storio.sqlite.Changes) DeleteQuery(com.pushtorefresh.storio.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 19 with StorIOSQLite

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

the class PreparedDeleteByQueryTest method shouldWrapExceptionIntoStorIOExceptionSingle.

@Test
public void shouldWrapExceptionIntoStorIOExceptionSingle() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    //noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    when(deleteResolver.performDelete(same(storIOSQLite), any(DeleteQuery.class))).thenThrow(new IllegalStateException("test exception"));
    final TestSubscriber<DeleteResult> testSubscriber = new TestSubscriber<DeleteResult>();
    new PreparedDeleteByQuery.Builder(storIOSQLite, DeleteQuery.builder().table("test_table").build()).withDeleteResolver(deleteResolver).prepare().asRxSingle().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(deleteResolver).performDelete(same(storIOSQLite), any(DeleteQuery.class));
    verify(storIOSQLite).defaultScheduler();
    verifyNoMoreInteractions(storIOSQLite, internal, deleteResolver);
}
Also used : StorIOException(com.pushtorefresh.storio.StorIOException) TestSubscriber(rx.observers.TestSubscriber) DeleteQuery(com.pushtorefresh.storio.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 20 with StorIOSQLite

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

the class DefaultGetResolverTest method rawQuery.

@Test
public void rawQuery() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.Internal internal = mock(StorIOSQLite.Internal.class);
    final RawQuery rawQuery = RawQuery.builder().query("test sql").build();
    final Cursor expectedCursor = mock(Cursor.class);
    when(storIOSQLite.lowLevel()).thenReturn(internal);
    when(internal.rawQuery(rawQuery)).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, rawQuery);
    // only one request should occur
    verify(internal, times(1)).rawQuery(any(RawQuery.class));
    // and this request should be equals to original
    verify(internal, times(1)).rawQuery(rawQuery);
    assertThat(actualCursor).isSameAs(expectedCursor);
}
Also used : NonNull(android.support.annotation.NonNull) RawQuery(com.pushtorefresh.storio.sqlite.queries.RawQuery) Cursor(android.database.Cursor) 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