use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.
the class BriteDatabaseTest method setUp.
@Before
public void setUp() throws IOException {
Configuration configuration = Configuration.builder(InstrumentationRegistry.getContext()).callback(testDb).name(dbFolder.newFile().getPath()).build();
Factory factory = new FrameworkSQLiteOpenHelperFactory();
SupportSQLiteOpenHelper helper = factory.create(configuration);
real = helper.getWritableDatabase();
SqlBrite.Logger logger = new SqlBrite.Logger() {
@Override
public void log(String message) {
logs.add(message);
}
};
ObservableTransformer<Query, Query> queryTransformer = new ObservableTransformer<Query, Query>() {
@Override
public ObservableSource<Query> apply(Observable<Query> upstream) {
return upstream.takeUntil(killSwitch);
}
};
db = new BriteDatabase(helper, logger, scheduler, queryTransformer);
}
use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.
the class BriteDatabaseTest method queryCreatedBeforeTransactionButSubscribedAfter.
@Test
public void queryCreatedBeforeTransactionButSubscribedAfter() {
Observable<Query> query = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES);
Transaction transaction = db.newTransaction();
try {
db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("john", "John Johnson"));
db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("nick", "Nick Nickers"));
transaction.markSuccessful();
} finally {
transaction.end();
}
query.subscribe(o);
o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").hasRow("john", "John Johnson").hasRow("nick", "Nick Nickers").isExhausted();
}
use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.
the class BriteDatabaseTest method nonExclusiveTransactionWorks.
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test
public void nonExclusiveTransactionWorks() throws InterruptedException {
final CountDownLatch transactionStarted = new CountDownLatch(1);
final CountDownLatch transactionProceed = new CountDownLatch(1);
final CountDownLatch transactionCompleted = new CountDownLatch(1);
new Thread() {
@Override
public void run() {
Transaction transaction = db.newNonExclusiveTransaction();
transactionStarted.countDown();
try {
db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("hans", "Hans Hanson"));
transactionProceed.await(10, SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException("Exception in transaction thread", e);
}
transaction.markSuccessful();
transaction.close();
transactionCompleted.countDown();
}
}.start();
assertThat(transactionStarted.await(10, SECONDS)).isTrue();
// Simple query
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1").lift(Query.mapToOne(Employee.MAPPER)).blockingFirst();
assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
transactionProceed.countDown();
assertThat(transactionCompleted.await(10, SECONDS)).isTrue();
}
use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.
the class QueryObservableTest method mapToListThrowsFromQueryRun.
@Test
public void mapToListThrowsFromQueryRun() {
final IllegalStateException error = new IllegalStateException("test exception");
Query query = new Query() {
@Override
public Cursor run() {
throw error;
}
};
//
new QueryObservable(Observable.just(query)).mapToList(new Function<Cursor, Object>() {
@Override
public Object apply(Cursor cursor) {
throw new AssertionError("Must not be called");
}
}).test().assertNoValues().assertError(error);
}
use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.
the class QueryTest method mapToOneIgnoresNullCursor.
@Test
public void mapToOneIgnoresNullCursor() {
Query nully = new Query() {
@Nullable
@Override
public Cursor run() {
return null;
}
};
TestObserver<Employee> observer = new TestObserver<>();
Observable.just(nully).lift(Query.mapToOne(MAPPER)).subscribe(observer);
observer.assertNoValues();
observer.assertComplete();
}
Aggregations