Search in sources :

Example 11 with Employee

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

the class QueryTest method mapToOne.

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

Example 12 with Employee

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

the class QueryTest method mapToOneOrDefaultAllowsMapperNull.

@Test
public void mapToOneOrDefaultAllowsMapperNull() {
    Func1<Cursor, Employee> mapToNull = new Func1<Cursor, Employee>() {

        @Override
        public Employee call(Cursor cursor) {
            return null;
        }
    };
    Employee employee = //
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1").lift(//
    Query.mapToOneOrDefault(mapToNull, new Employee("bob", "Bob Bobberson"))).toBlocking().first();
    assertThat(employee).isNull();
}
Also used : Employee(com.squareup.sqlbrite.TestDb.Employee) Func1(rx.functions.Func1) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 13 with Employee

use of com.squareup.sqlbrite.TestDb.Employee 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, 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)).toBlocking().first();
    assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
    transactionProceed.countDown();
    assertThat(transactionCompleted.await(10, SECONDS)).isTrue();
}
Also used : Employee(com.squareup.sqlbrite.TestDb.Employee) Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) SdkSuppress(android.support.test.filters.SdkSuppress) TargetApi(android.annotation.TargetApi)

Example 14 with Employee

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

the class BriteDatabaseTest method queryMapToList.

@Test
public void queryMapToList() {
    List<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).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)

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