Skip to content

Commit

Permalink
#855 Add asRxCompletable to PreparedExecuteSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitin-da committed Nov 21, 2018
1 parent 9f467b5 commit 2f561af
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
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();
}
}
}

0 comments on commit 2f561af

Please sign in to comment.