Search in sources :

Example 1 with Employee

use of com.squareup.sqlbrite3.TestDb.Employee in project sqlbrite by square.

the class BriteDatabaseTest method transactionOnlyNotifiesOnce.

@Test
public void transactionOnlyNotifiesOnce() {
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
    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"));
        o.assertNoMoreEvents();
        transaction.markSuccessful();
    } finally {
        transaction.end();
    }
    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 : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) Test(org.junit.Test)

Example 2 with Employee

use of com.squareup.sqlbrite3.TestDb.Employee 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 3 with Employee

use of com.squareup.sqlbrite3.TestDb.Employee in project sqlbrite by square.

the class QueryTest method mapToListReturnsNullOnMapperNull.

@Test
public void mapToListReturnsNullOnMapperNull() {
    Function<Cursor, Employee> mapToNull = new Function<Cursor, Employee>() {

        private int count;

        @Override
        public Employee apply(Cursor cursor) throws Exception {
            return count++ == 2 ? null : MAPPER.apply(cursor);
        }
    };
    List<Employee> employees = // 
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).lift(// 
    Query.mapToList(mapToNull)).blockingFirst();
    assertThat(employees).containsExactly(new Employee("alice", "Alice Allison"), new Employee("bob", "Bob Bobberson"), null);
}
Also used : Function(io.reactivex.functions.Function) Employee(com.squareup.sqlbrite3.TestDb.Employee) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 4 with Employee

use of com.squareup.sqlbrite3.TestDb.Employee 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 5 with Employee

use of com.squareup.sqlbrite3.TestDb.Employee in project sqlbrite by square.

the class QueryTest method mapToOneOrDefault.

@Test
public void mapToOneOrDefault() {
    Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1").lift(Query.mapToOneOrDefault(MAPPER, new Employee("fred", "Fred Frederson"))).blockingFirst();
    assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
}
Also used : Employee(com.squareup.sqlbrite3.TestDb.Employee) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 Transaction (com.squareup.sqlbrite3.BriteDatabase.Transaction)11 Employee (com.squareup.sqlbrite3.TestDb.Employee)10 Query (com.squareup.sqlbrite3.SqlBrite.Query)6 SimpleSQLiteQuery (android.arch.persistence.db.SimpleSQLiteQuery)3 TestObserver (io.reactivex.observers.TestObserver)3 SdkSuppress (android.support.test.filters.SdkSuppress)2 TargetApi (android.annotation.TargetApi)1 Cursor (android.database.Cursor)1 Function (io.reactivex.functions.Function)1 Closeable (java.io.Closeable)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1