Search in sources :

Example 1 with Optional

use of com.pushtorefresh.storio3.Optional in project storio by pushtorefresh.

the class SingleOnSubscribeExecuteAsBlockingOptionalTest method shouldCallOnErrorIfExceptionOccurred.

@SuppressWarnings("CheckResult")
@Test
public void shouldCallOnErrorIfExceptionOccurred() {
    // noinspection unchecked
    final PreparedOperation<Object, Optional<Object>, Object> preparedOperation = mock(PreparedOperation.class);
    StorIOException expectedException = new StorIOException("test exception");
    when(preparedOperation.executeAsBlocking()).thenThrow(expectedException);
    TestObserver<Optional<Object>> testObserver = new TestObserver<Optional<Object>>();
    Single<Optional<Object>> single = Single.create(new SingleOnSubscribeExecuteAsBlockingOptional<Object, Object>(preparedOperation));
    verifyZeroInteractions(preparedOperation);
    single.subscribe(testObserver);
    testObserver.assertError(expectedException);
    testObserver.assertNotComplete();
    verify(preparedOperation).executeAsBlocking();
}
Also used : Optional(com.pushtorefresh.storio3.Optional) StorIOException(com.pushtorefresh.storio3.StorIOException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 2 with Optional

use of com.pushtorefresh.storio3.Optional in project storio by pushtorefresh.

the class PutOperationTest method insertOneWithNullField.

@Test
public void insertOneWithNullField() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    // optional value is null
    TestItem testItem = TestItem.create(null, "value", null);
    PutResult insertResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
    assertThat(insertResult.wasInserted()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    cursor.close();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 3 with Optional

use of com.pushtorefresh.storio3.Optional in project storio by pushtorefresh.

the class PutOperationTest method updateNullFieldToNotNull.

@Test
public void updateNullFieldToNotNull() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    Uri insertedUri = // firstly, optional value is null
    contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", null).toContentValues());
    // change to not null
    TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", "optionalValue");
    PutResult updateResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
    assertThat(updateResult.wasUpdated()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
    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) Uri(android.net.Uri) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 4 with Optional

use of com.pushtorefresh.storio3.Optional in project storio by pushtorefresh.

the class PutOperationTest method updateNotNullFieldToNull.

@Test
public void updateNotNullFieldToNull() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    Uri insertedUri = contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", "optionalValue").toContentValues());
    // optional value changes to null
    TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", null);
    PutResult updateResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
    assertThat(updateResult.wasUpdated()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
    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) Uri(android.net.Uri) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 5 with Optional

use of com.pushtorefresh.storio3.Optional in project storio by pushtorefresh.

the class RxJavaUtilsTest method createGetFlowableOptionalCompletedAfterInitialEmissionIfNoTablesAndTags.

@Test
public void createGetFlowableOptionalCompletedAfterInitialEmissionIfNoTablesAndTags() {
    RawQuery queryWithoutTablesAnTags = RawQuery.builder().query("select * from " + UserTableMeta.TABLE + " where " + UserTableMeta.COLUMN_ID + "=?").args(1).build();
    PreparedGetObject<User> preparedGet = storIOSQLite.get().object(User.class).withQuery(queryWithoutTablesAnTags).prepare();
    TestSubscriber<Optional<User>> subscriber = new TestSubscriber<Optional<User>>();
    RxJavaUtils.createGetFlowableOptional(storIOSQLite, preparedGet, null, queryWithoutTablesAnTags, BackpressureStrategy.LATEST).subscribe(subscriber);
    subscriber.assertNoErrors();
    subscriber.assertValues(Optional.<User>empty());
    subscriber.assertComplete();
}
Also used : User(com.pushtorefresh.storio3.sqlite.integration.User) Optional(com.pushtorefresh.storio3.Optional) RawQuery(com.pushtorefresh.storio3.sqlite.queries.RawQuery) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BaseTest(com.pushtorefresh.storio3.sqlite.integration.BaseTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 TestSubscriber (io.reactivex.subscribers.TestSubscriber)4 Cursor (android.database.Cursor)3 Changes (com.pushtorefresh.storio3.contentresolver.Changes)3 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)3 Uri (android.net.Uri)2 Optional (com.pushtorefresh.storio3.Optional)2 StorIOException (com.pushtorefresh.storio3.StorIOException)1 BaseTest (com.pushtorefresh.storio3.sqlite.integration.BaseTest)1 User (com.pushtorefresh.storio3.sqlite.integration.User)1 RawQuery (com.pushtorefresh.storio3.sqlite.queries.RawQuery)1 TestObserver (io.reactivex.observers.TestObserver)1