Search in sources :

Example 16 with DeleteResult

use of com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult in project storio by pushtorefresh.

the class DeleteOperationTest method deleteByQueryExecuteAsBlocking.

@Test
public void deleteByQueryExecuteAsBlocking() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    TestItem testItemToInsert = TestItem.create(null, "value");
    contentResolver.insert(TestItem.CONTENT_URI, testItemToInsert.toContentValues());
    Cursor firstDbState = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(firstDbState).hasCount(1);
    DeleteResult deleteResult = storIOContentResolver.delete().byQuery(DeleteQuery.builder().uri(TestItem.CONTENT_URI).build()).prepare().executeAsBlocking();
    assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(1);
    Cursor secondDbState = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(secondDbState).hasCount(0);
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) DeleteResult(com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult) Test(org.junit.Test)

Example 17 with DeleteResult

use of com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult in project storio by pushtorefresh.

the class DeleteOperationTest method deleteByQueryAsSingle.

@Test
public void deleteByQueryAsSingle() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    TestItem testItemToInsert = TestItem.create(null, "value");
    contentResolver.insert(TestItem.CONTENT_URI, testItemToInsert.toContentValues());
    Cursor firstDbState = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(firstDbState).hasCount(1);
    DeleteResult deleteResult = storIOContentResolver.delete().byQuery(DeleteQuery.builder().uri(TestItem.CONTENT_URI).build()).prepare().asRxSingle().blockingGet();
    assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(1);
    Cursor secondDbState = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(secondDbState).hasCount(0);
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) DeleteResult(com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult) Test(org.junit.Test)

Example 18 with DeleteResult

use of com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult in project storio by pushtorefresh.

the class DeleteOperationDesignTest method deleteByQueryFlowable.

@Test
public void deleteByQueryFlowable() {
    final DeleteQuery deleteQuery = DeleteQuery.builder().uri(mock(Uri.class)).where("some_field = ?").whereArgs("someValue").build();
    Flowable<DeleteResult> deleteResultFlowable = storIOContentResolver().delete().byQuery(deleteQuery).prepare().asRxFlowable(BackpressureStrategy.MISSING);
}
Also used : DeleteQuery(com.pushtorefresh.storio3.contentresolver.queries.DeleteQuery) DeleteResult(com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult) Test(org.junit.Test)

Example 19 with DeleteResult

use of com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult in project storio by pushtorefresh.

the class DefaultDeleteResolverTest method performDelete.

@Test
public void performDelete() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    final String testTable = "test_table";
    final Set<String> testTags = singleton("test_tag");
    final DeleteQuery deleteQuery = DeleteQuery.builder().table(testTable).affectsTags(testTags).build();
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    when(lowLevel.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(lowLevel).delete(any(DeleteQuery.class));
    verify(lowLevel).delete(deleteQuery);
    assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(1);
    assertThat(deleteResult.affectedTables()).isEqualTo(singleton(testTable));
    assertThat(deleteResult.affectedTags()).isEqualTo(testTags);
}
Also used : NonNull(android.support.annotation.NonNull) DeleteQuery(com.pushtorefresh.storio3.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 20 with DeleteResult

use of com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult in project storio by pushtorefresh.

the class PreparedDeleteByQueryTest method shouldWrapExceptionIntoStorIOExceptionCompletable.

@Test
public void shouldWrapExceptionIntoStorIOExceptionCompletable() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    // noinspection unchecked
    final DeleteResolver<DeleteQuery> deleteResolver = mock(DeleteResolver.class);
    when(deleteResolver.performDelete(same(storIOSQLite), any(DeleteQuery.class))).thenThrow(new IllegalStateException("test exception"));
    final TestObserver<DeleteResult> testObserver = new TestObserver<DeleteResult>();
    new PreparedDeleteByQuery.Builder(storIOSQLite, DeleteQuery.builder().table("test_table").build()).withDeleteResolver(deleteResolver).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");
    verify(storIOSQLite).defaultRxScheduler();
    verify(deleteResolver).performDelete(same(storIOSQLite), any(DeleteQuery.class));
    verify(storIOSQLite).interceptors();
    verifyNoMoreInteractions(storIOSQLite, lowLevel, deleteResolver);
}
Also used : StorIOException(com.pushtorefresh.storio3.StorIOException) DeleteQuery(com.pushtorefresh.storio3.sqlite.queries.DeleteQuery) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 DeleteResult (com.pushtorefresh.storio3.contentresolver.operations.delete.DeleteResult)10 NonNull (android.support.annotation.NonNull)7 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)7 TestSubscriber (io.reactivex.subscribers.TestSubscriber)7 Cursor (android.database.Cursor)6 Changes (com.pushtorefresh.storio3.contentresolver.Changes)6 DeleteQuery (com.pushtorefresh.storio3.sqlite.queries.DeleteQuery)6 DeleteQuery (com.pushtorefresh.storio3.contentresolver.queries.DeleteQuery)5 DeleteResult (com.pushtorefresh.storio3.sqlite.operations.delete.DeleteResult)5 StorIOException (com.pushtorefresh.storio3.StorIOException)3 TestObserver (io.reactivex.observers.TestObserver)2 Uri (android.net.Uri)1 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)1 Changes (com.pushtorefresh.storio3.sqlite.Changes)1 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)1