Search in sources :

Example 1 with DeleteQuery

use of com.pushtorefresh.storio.sqlite.queries.DeleteQuery in project storio by pushtorefresh.

the class DefaultDeleteResolver method performDelete.

/**
     * {@inheritDoc}
     */
@NonNull
@Override
public DeleteResult performDelete(@NonNull StorIOSQLite storIOSQLite, @NonNull T object) {
    final DeleteQuery deleteQuery = mapToDeleteQuery(object);
    final int numberOfRowsDeleted = storIOSQLite.lowLevel().delete(deleteQuery);
    return DeleteResult.newInstance(numberOfRowsDeleted, deleteQuery.table());
}
Also used : DeleteQuery(com.pushtorefresh.storio.sqlite.queries.DeleteQuery) NonNull(android.support.annotation.NonNull)

Example 2 with DeleteQuery

use of com.pushtorefresh.storio.sqlite.queries.DeleteQuery 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)

Example 3 with DeleteQuery

use of com.pushtorefresh.storio.sqlite.queries.DeleteQuery 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 4 with DeleteQuery

use of com.pushtorefresh.storio.sqlite.queries.DeleteQuery 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 5 with DeleteQuery

use of com.pushtorefresh.storio.sqlite.queries.DeleteQuery 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)

Aggregations

DeleteQuery (com.pushtorefresh.storio.sqlite.queries.DeleteQuery)7 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)6 Test (org.junit.Test)6 StorIOException (com.pushtorefresh.storio.StorIOException)4 TestSubscriber (rx.observers.TestSubscriber)3 NonNull (android.support.annotation.NonNull)2 Changes (com.pushtorefresh.storio.sqlite.Changes)1