Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#855 Add asRxCompletable to PreparedExecuteSQL #905

Merged
merged 2 commits into from
Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ _2018_05_09_
* AutoValue 1.0-rc4 and AutoParcel 0.3.1.
* Private constructor checker 1.2.0.
* EqualsVerifier 1.7.8.

* Gradle Nexus plugin 0.12.0.
* Nexus publish plugin.
* `executeSQL()` now for sure can be executed via `asRxCompletable()`.

**Changes:**

* [PR 905](https://github.com/pushtorefresh/storio/pull/905): executeSQL() can be executed via asRxCompletable().
* [PR 903](https://github.com/pushtorefresh/storio/pull/903): Nexus publish plugin.
* [PR 901](https://github.com/pushtorefresh/storio/pull/901): Gradle Nexus plugin 0.12.0.
* [PR 899](https://github.com/pushtorefresh/storio/pull/899): AndroidX support.
* [PR 898](https://github.com/pushtorefresh/storio/pull/898): Dagger 2.18 and temporary disable SQLDelight.
* [PR 897](https://github.com/pushtorefresh/storio/pull/897): Compile sdk 28 and support library 28.0.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.WorkerThread;

import com.pushtorefresh.storio3.StorIOException;
import com.pushtorefresh.storio3.operations.PreparedCompletableOperation;
import com.pushtorefresh.storio3.operations.PreparedOperation;
import com.pushtorefresh.storio3.sqlite.Changes;
import com.pushtorefresh.storio3.Interceptor;
Expand All @@ -15,6 +16,7 @@
import java.util.Set;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.Single;

Expand All @@ -24,7 +26,7 @@
/**
* Prepared Execute SQL Operation for {@link StorIOSQLite}.
*/
public class PreparedExecuteSQL implements PreparedOperation<Object, Object, RawQuery> {
public class PreparedExecuteSQL implements PreparedCompletableOperation<Object, RawQuery> {

@NonNull
private final StorIOSQLite storIOSQLite;
Expand Down Expand Up @@ -98,6 +100,21 @@ public Single<Object> asRxSingle() {
return RxJavaUtils.createSingle(storIOSQLite, this);
}

/**
* Creates {@link Completable} which will perform Execute SQL Operation lazily when somebody subscribes to it.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>Operates on {@link StorIOSQLite#defaultRxScheduler()} if not {@code null}.</dd>
* </dl>
*
* @return non-null {@link Completable} which will perform Execute SQL Operation.
*/
@NonNull
@Override
public Completable asRxCompletable() {
return RxJavaUtils.createCompletable(storIOSQLite, this);
}

private class RealCallInterceptor implements Interceptor {
@NonNull
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashSet;
import java.util.List;

import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.functions.Consumer;
Expand Down Expand Up @@ -136,6 +137,34 @@ public void executeSQLSingleWithoutNotifications() {
stub.verifyBehavior(single);
}

@Test
public void executeSQLCompletableWithNotification() {
final Stub stub = Stub.newInstanceApplyNotEmptyAffectedTablesAndTags();

final Completable completable = stub.storIOSQLite
.executeSQL()
.withQuery(stub.rawQuery)
.prepare()
.asRxCompletable();

verify(stub.storIOSQLite).defaultRxScheduler();
stub.verifyBehavior(completable);
}

@Test
public void executeSQLCompletableWithoutNotifications() {
final Stub stub = Stub.newInstanceDoNotApplyAffectedTablesAndTags();

final Completable completable = stub.storIOSQLite
.executeSQL()
.withQuery(stub.rawQuery)
.prepare()
.asRxCompletable();

verify(stub.storIOSQLite).defaultRxScheduler();
stub.verifyBehavior(completable);
}

@Test
public void executeSQLFlowableWithNotification() {
final Stub stub = Stub.newInstanceApplyNotEmptyAffectedTablesAndTags();
Expand Down Expand Up @@ -176,6 +205,19 @@ public void executeSQLSingleExecutesOnSpecifiedScheduler() {
schedulerChecker.checkAsSingle(operation);
}

@Test
public void executeSQLCompletableExecutesOnSpecifiedScheduler() {
final Stub stub = Stub.newInstanceApplyNotEmptyAffectedTablesAndTags();
final SchedulerChecker schedulerChecker = SchedulerChecker.create(stub.storIOSQLite);

final PreparedExecuteSQL operation = stub.storIOSQLite
.executeSQL()
.withQuery(stub.rawQuery)
.prepare();

schedulerChecker.checkAsCompletable(operation);
}

@Test
public void shouldWrapExceptionIntoStorIOExceptionBlocking() {
final Stub stub = Stub.newInstanceApplyNotEmptyAffectedTablesAndTags();
Expand Down Expand Up @@ -264,6 +306,40 @@ public void shouldWrapExceptionIntoStorIOExceptionSingle() {
verifyNoMoreInteractions(stub.storIOSQLite, stub.lowLevel);
}

@Test
public void shouldWrapExceptionIntoStorIOExceptionCompletable() {
final Stub stub = Stub.newInstanceApplyNotEmptyAffectedTablesAndTags();

IllegalStateException testException = new IllegalStateException("test exception");
doThrow(testException).when(stub.lowLevel).executeSQL(stub.rawQuery);

final TestObserver<Object> testObserver = new TestObserver<Object>();

stub.storIOSQLite
.executeSQL()
.withQuery(stub.rawQuery)
.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(stub.storIOSQLite).executeSQL();
verify(stub.storIOSQLite).defaultRxScheduler();
verify(stub.storIOSQLite).lowLevel();
verify(stub.lowLevel).executeSQL(stub.rawQuery);
verify(stub.storIOSQLite).interceptors();
verifyNoMoreInteractions(stub.storIOSQLite, stub.lowLevel);
}

static class Stub {

private final StorIOSQLite storIOSQLite;
Expand Down Expand Up @@ -375,5 +451,18 @@ public void accept(Object anObject) {
})
.checkBehaviorOfFlowable();
}

void verifyBehavior(@NonNull Completable completable) {
new FlowableBehaviorChecker<Object>()
.flowable(completable.toFlowable())
.expectedNumberOfEmissions(1)
.testAction(new Consumer<Object>() {
@Override
public void accept(Object anObject) {
verifyBehavior();
}
})
.checkBehaviorOfFlowable();
}
}
}