Search in sources :

Example 1 with Query

use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.

the class QueryTest method mapToOneOrDefaultReturnsDefaultWhenNullCursor.

@Test
public void mapToOneOrDefaultReturnsDefaultWhenNullCursor() {
    Employee defaultEmployee = new Employee("bob", "Bob Bobberson");
    Query nully = new Query() {

        @Nullable
        @Override
        public Cursor run() {
            return null;
        }
    };
    TestObserver<Employee> observer = new TestObserver<>();
    Observable.just(nully).lift(Query.mapToOneOrDefault(MAPPER, defaultEmployee)).subscribe(observer);
    observer.assertValues(defaultEmployee);
    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)

Example 2 with Query

use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.

the class QueryTest method mapToListIgnoresNullCursor.

@Test
public void mapToListIgnoresNullCursor() {
    Query nully = new Query() {

        @Nullable
        @Override
        public Cursor run() {
            return null;
        }
    };
    TestObserver<List<Employee>> subscriber = new TestObserver<>();
    Observable.just(nully).lift(Query.mapToList(MAPPER)).subscribe(subscriber);
    subscriber.assertNoValues();
    subscriber.assertComplete();
}
Also used : Query(com.squareup.sqlbrite3.SqlBrite.Query) List(java.util.List) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 3 with Query

use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.

the class QueryTest method mapToOptionalIgnoresNullCursor.

@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@Test
public void mapToOptionalIgnoresNullCursor() {
    Query nully = new Query() {

        @Nullable
        @Override
        public Cursor run() {
            return null;
        }
    };
    Observable.just(nully).lift(Query.mapToOptional(MAPPER)).test().assertValue(Optional.<Employee>empty());
}
Also used : Query(com.squareup.sqlbrite3.SqlBrite.Query) Test(org.junit.Test) SdkSuppress(android.support.test.filters.SdkSuppress)

Example 4 with Query

use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.

the class SqlBriteTest method asRowsStopsWhenUnsubscribed.

@Test
public void asRowsStopsWhenUnsubscribed() {
    MatrixCursor cursor = new MatrixCursor(COLUMN_NAMES);
    cursor.addRow(new Object[] { "Alice", "Allison" });
    cursor.addRow(new Object[] { "Bob", "Bobberson" });
    Query query = new CursorQuery(cursor);
    final AtomicInteger count = new AtomicInteger();
    query.asRows(new Function<Cursor, Name>() {

        @Override
        public Name apply(Cursor cursor) throws Exception {
            count.incrementAndGet();
            return Name.MAP.apply(cursor);
        }
    }).take(1).blockingFirst();
    assertThat(count.get()).isEqualTo(1);
}
Also used : Query(com.squareup.sqlbrite3.SqlBrite.Query) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 5 with Query

use of com.squareup.sqlbrite3.SqlBrite.Query in project sqlbrite by square.

the class SqlBriteTest method asRowsEmpty.

@Test
public void asRowsEmpty() {
    MatrixCursor cursor = new MatrixCursor(COLUMN_NAMES);
    Query query = new CursorQuery(cursor);
    List<Name> names = query.asRows(Name.MAP).toList().blockingGet();
    assertThat(names).isEmpty();
}
Also used : Query(com.squareup.sqlbrite3.SqlBrite.Query) MatrixCursor(android.database.MatrixCursor) 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