Search in sources :

Example 6 with Employee

use of com.squareup.sqlbrite.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;
        }
    };
    TestSubscriber<Employee> subscriber = new TestSubscriber<>();
    Observable.just(nully).lift(Query.mapToOneOrDefault(Employee.MAPPER, defaultEmployee)).subscribe(subscriber);
    subscriber.assertValues(defaultEmployee);
    subscriber.assertCompleted();
}
Also used : Employee(com.squareup.sqlbrite.TestDb.Employee) Query(com.squareup.sqlbrite.SqlBrite.Query) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 7 with Employee

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

the class QueryTest method mapToList.

@Test
public void mapToList() {
    List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).lift(Query.mapToList(Employee.MAPPER)).toBlocking().first();
    //
    assertThat(employees).containsExactly(//
    new Employee("alice", "Alice Allison"), //
    new Employee("bob", "Bob Bobberson"), new Employee("eve", "Eve Evenson"));
}
Also used : Employee(com.squareup.sqlbrite.TestDb.Employee) Test(org.junit.Test)

Example 8 with Employee

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

the class BriteDatabaseTest method queryMapToOneOrDefault.

@Test
public void queryMapToOneOrDefault() {
    Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1").mapToOneOrDefault(Employee.MAPPER, null).toBlocking().first();
    assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
}
Also used : Employee(com.squareup.sqlbrite.TestDb.Employee) Test(org.junit.Test)

Example 9 with Employee

use of com.squareup.sqlbrite.TestDb.Employee 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;
        }
    };
    TestSubscriber<Employee> subscriber = new TestSubscriber<>();
    Observable.just(nully).lift(Query.mapToOne(Employee.MAPPER)).subscribe(subscriber);
    subscriber.assertNoValues();
    subscriber.assertCompleted();
}
Also used : Employee(com.squareup.sqlbrite.TestDb.Employee) Query(com.squareup.sqlbrite.SqlBrite.Query) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 10 with Employee

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

the class QueryTest method mapToOneOrDefaultReturnsDefaultAndDoesNotReRequestWhenNoRows.

@Test
public void mapToOneOrDefaultReturnsDefaultAndDoesNotReRequestWhenNoRows() {
    final List<Long> requests = new ArrayList<>();
    Employee defaultEmployee = new Employee("bob", "Bob Bobberson");
    List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " WHERE 1=2").take(1).doOnRequest(new Action1<Long>() {

        @Override
        public void call(Long n) {
            requests.add(n);
        }
    }).lift(Query.mapToOneOrDefault(Employee.MAPPER, defaultEmployee)).toList().toBlocking().first();
    assertThat(employees).containsExactly(defaultEmployee);
    assertThat(requests).containsExactly(Long.MAX_VALUE);
}
Also used : Employee(com.squareup.sqlbrite.TestDb.Employee) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Employee (com.squareup.sqlbrite.TestDb.Employee)14 Test (org.junit.Test)14 Cursor (android.database.Cursor)3 Func1 (rx.functions.Func1)3 Query (com.squareup.sqlbrite.SqlBrite.Query)2 TestSubscriber (rx.observers.TestSubscriber)2 TargetApi (android.annotation.TargetApi)1 SdkSuppress (android.support.test.filters.SdkSuppress)1 Transaction (com.squareup.sqlbrite.BriteDatabase.Transaction)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1