Search in sources :

Example 6 with Query

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);
}
Also used : Configuration(android.arch.persistence.db.SupportSQLiteOpenHelper.Configuration) Query(com.squareup.sqlbrite3.SqlBrite.Query) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) Factory(android.arch.persistence.db.SupportSQLiteOpenHelper.Factory) FrameworkSQLiteOpenHelperFactory(android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory) Observable(io.reactivex.Observable) FrameworkSQLiteOpenHelperFactory(android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory) SupportSQLiteOpenHelper(android.arch.persistence.db.SupportSQLiteOpenHelper) ObservableTransformer(io.reactivex.ObservableTransformer) Before(org.junit.Before)

Example 7 with Query

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();
}
Also used : Query(com.squareup.sqlbrite3.SqlBrite.Query) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) Test(org.junit.Test)

Example 8 with Query

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();
}
Also used : Employee(com.squareup.sqlbrite3.TestDb.Employee) Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) SdkSuppress(android.support.test.filters.SdkSuppress) TargetApi(android.annotation.TargetApi)

Example 9 with Query

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);
}
Also used : Function(io.reactivex.functions.Function) Query(com.squareup.sqlbrite3.SqlBrite.Query) QueryObservable(com.squareup.sqlbrite3.QueryObservable) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 10 with Query

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();
}
Also used : Employee(com.squareup.sqlbrite3.TestDb.Employee) Query(com.squareup.sqlbrite3.SqlBrite.Query) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

Query (com.squareup.sqlbrite3.SqlBrite.Query)15 Test (org.junit.Test)14 Cursor (android.database.Cursor)6 MatrixCursor (android.database.MatrixCursor)6 Transaction (com.squareup.sqlbrite3.BriteDatabase.Transaction)4 SimpleSQLiteQuery (android.arch.persistence.db.SimpleSQLiteQuery)3 Employee (com.squareup.sqlbrite3.TestDb.Employee)3 Function (io.reactivex.functions.Function)3 TestObserver (io.reactivex.observers.TestObserver)3 SdkSuppress (android.support.test.filters.SdkSuppress)2 QueryObservable (com.squareup.sqlbrite3.QueryObservable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TargetApi (android.annotation.TargetApi)1 SupportSQLiteOpenHelper (android.arch.persistence.db.SupportSQLiteOpenHelper)1 Configuration (android.arch.persistence.db.SupportSQLiteOpenHelper.Configuration)1 Factory (android.arch.persistence.db.SupportSQLiteOpenHelper.Factory)1 FrameworkSQLiteOpenHelperFactory (android.arch.persistence.db.framework.FrameworkSQLiteOpenHelperFactory)1 ContentObserver (android.database.ContentObserver)1 CheckResult (android.support.annotation.CheckResult)1