Search in sources :

Example 11 with StorIOSQLite

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

the class PreparedPutContentValuesIterableTest method verifyBehaviorInCaseOfExceptionWithoutTransactionBlocking.

@Test
public void verifyBehaviorInCaseOfExceptionWithoutTransactionBlocking() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    // 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"));
    try {
        new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(false).prepare().executeAsBlocking();
        failBecauseExceptionWasNotThrown(StorIOException.class);
    } catch (StorIOException expected) {
        IllegalStateException cause = (IllegalStateException) expected.getCause();
        assertThat(cause).hasMessage("test exception");
        // Main check of this test
        verify(lowLevel, never()).endTransaction();
        verify(storIOSQLite).lowLevel();
        verify(storIOSQLite).interceptors();
        verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
        verifyNoMoreInteractions(storIOSQLite, lowLevel, putResolver);
    }
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio3.StorIOException) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 12 with StorIOSQLite

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

the class PreparedPutContentValuesIterableTest method verifyBehaviorInCaseOfExceptionWithoutTransactionCompletable.

@Test
public void verifyBehaviorInCaseOfExceptionWithoutTransactionCompletable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    // 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 TestObserver<PutResults<ContentValues>> testObserver = new TestObserver<PutResults<ContentValues>>();
    new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(false).prepare().asRxCompletable().subscribe(testObserver);
    testObserver.awaitTerminalEvent();
    testObserver.assertNoValues();
    testObserver.assertError(StorIOException.class);
    // noinspection ThrowableResultOfMethodCallIgnored
    StorIOException expected = (StorIOException) testObserver.errors().get(0);
    IllegalStateException cause = (IllegalStateException) expected.getCause();
    assertThat(cause).hasMessage("test exception");
    // Main check of this test
    verify(lowLevel, never()).endTransaction();
    verify(storIOSQLite).lowLevel();
    verify(storIOSQLite).defaultRxScheduler();
    verify(storIOSQLite).interceptors();
    verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
    verifyNoMoreInteractions(storIOSQLite, lowLevel, putResolver);
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio3.StorIOException) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 13 with StorIOSQLite

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

the class PreparedPutContentValuesIterableTest method verifyBehaviorInCaseOfExceptionWithoutTransactionFlowable.

@Test
public void verifyBehaviorInCaseOfExceptionWithoutTransactionFlowable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    // 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(false).prepare().asRxFlowable(MISSING).subscribe(testSubscriber);
    testSubscriber.awaitTerminalEvent();
    testSubscriber.assertNoValues();
    testSubscriber.assertError(StorIOException.class);
    // noinspection ThrowableResultOfMethodCallIgnored
    StorIOException expected = (StorIOException) testSubscriber.errors().get(0);
    IllegalStateException cause = (IllegalStateException) expected.getCause();
    assertThat(cause).hasMessage("test exception");
    // Main check of this test
    verify(lowLevel, never()).endTransaction();
    verify(storIOSQLite).lowLevel();
    verify(storIOSQLite).defaultRxScheduler();
    verify(storIOSQLite).interceptors();
    verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
    verifyNoMoreInteractions(storIOSQLite, lowLevel, putResolver);
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio3.StorIOException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 14 with StorIOSQLite

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

the class DefaultGetResolverTest method query.

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

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

Example 15 with StorIOSQLite

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

the class DefaultGetResolverTest method rawQuery.

@Test
public void rawQuery() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    final RawQuery rawQuery = RawQuery.builder().query("test sql").build();
    final Cursor expectedCursor = mock(Cursor.class);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    when(lowLevel.rawQuery(rawQuery)).thenReturn(expectedCursor);
    final DefaultGetResolver<TestItem> defaultGetResolver = new DefaultGetResolver<TestItem>() {

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

Aggregations

Test (org.junit.Test)34 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)32 NonNull (android.support.annotation.NonNull)18 StorIOException (com.pushtorefresh.storio3.StorIOException)18 ContentValues (android.content.ContentValues)13 Query (com.pushtorefresh.storio3.sqlite.queries.Query)9 Cursor (android.database.Cursor)8 TestObserver (io.reactivex.observers.TestObserver)8 DeleteQuery (com.pushtorefresh.storio3.sqlite.queries.DeleteQuery)7 TestSubscriber (io.reactivex.subscribers.TestSubscriber)7 TypeMappingFinder (com.pushtorefresh.storio3.TypeMappingFinder)4 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)4 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)4 RawQuery (com.pushtorefresh.storio3.sqlite.queries.RawQuery)4 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)3 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)3 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)3 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)3 User (com.pushtorefresh.storio3.sample.db.entities.User)2 BaseTest (com.pushtorefresh.storio3.sqlite.integration.BaseTest)2