Search in sources :

Example 11 with Employee

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

the class BriteDatabaseTest method transactionRollbackDoesNotNotify.

@Test
public void transactionRollbackDoesNotNotify() {
    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"));
    // No call to set successful.
    } finally {
        transaction.end();
    }
    o.assertNoMoreEvents();
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) Test(org.junit.Test)

Example 12 with Employee

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

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

the class BriteDatabaseTest method queryMapToOne.

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

Example 14 with Employee

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

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

the class BriteDatabaseTest method nestedTransactionsOnlyNotifyOnce.

@Test
public void nestedTransactionsOnlyNotifyOnce() {
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
    o.assertCursor().hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
    Transaction transactionOuter = db.newTransaction();
    try {
        db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("john", "John Johnson"));
        Transaction transactionInner = db.newTransaction();
        try {
            db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("nick", "Nick Nickers"));
            transactionInner.markSuccessful();
        } finally {
            transactionInner.end();
        }
        transactionOuter.markSuccessful();
    } finally {
        transactionOuter.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)

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