Search in sources :

Example 6 with Transaction

use of com.squareup.sqlbrite3.BriteDatabase.Transaction in project sqlbrite by square.

the class BriteDatabaseTest method synchronousQueryWithSupportSQLiteQueryDuringTransaction.

@Test
public void synchronousQueryWithSupportSQLiteQueryDuringTransaction() {
    Transaction transaction = db.newTransaction();
    try {
        transaction.markSuccessful();
        assertCursor(db.query(new SimpleSQLiteQuery(SELECT_EMPLOYEES))).hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
    } finally {
        transaction.end();
    }
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) Test(org.junit.Test)

Example 7 with Transaction

use of com.squareup.sqlbrite3.BriteDatabase.Transaction 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 8 with Transaction

use of com.squareup.sqlbrite3.BriteDatabase.Transaction 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)

Example 9 with Transaction

use of com.squareup.sqlbrite3.BriteDatabase.Transaction in project sqlbrite by square.

the class BriteDatabaseTest method synchronousQueryDuringTransaction.

@Test
public void synchronousQueryDuringTransaction() {
    Transaction transaction = db.newTransaction();
    try {
        transaction.markSuccessful();
        assertCursor(db.query(SELECT_EMPLOYEES)).hasRow("alice", "Alice Allison").hasRow("bob", "Bob Bobberson").hasRow("eve", "Eve Evenson").isExhausted();
    } finally {
        transaction.end();
    }
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) Test(org.junit.Test)

Example 10 with Transaction

use of com.squareup.sqlbrite3.BriteDatabase.Transaction in project sqlbrite by square.

the class BriteDatabaseTest method nestedTransactionsOnMultipleTables.

@Test
public void nestedTransactionsOnMultipleTables() {
    db.createQuery(BOTH_TABLES, SELECT_MANAGER_LIST).subscribe(o);
    o.assertCursor().hasRow("Eve Evenson", "Alice Allison").isExhausted();
    Transaction transactionOuter = db.newTransaction();
    try {
        Transaction transactionInner = db.newTransaction();
        try {
            db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("john", "John Johnson"));
            transactionInner.markSuccessful();
        } finally {
            transactionInner.end();
        }
        transactionInner = db.newTransaction();
        try {
            db.insert(TABLE_MANAGER, CONFLICT_NONE, manager(testDb.aliceId, testDb.bobId));
            transactionInner.markSuccessful();
        } finally {
            transactionInner.end();
        }
        transactionOuter.markSuccessful();
    } finally {
        transactionOuter.end();
    }
    o.assertCursor().hasRow("Eve Evenson", "Alice Allison").hasRow("Alice Allison", "Bob Bobberson").isExhausted();
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) Test(org.junit.Test)

Aggregations

Transaction (com.squareup.sqlbrite3.BriteDatabase.Transaction)16 Test (org.junit.Test)16 SimpleSQLiteQuery (android.arch.persistence.db.SimpleSQLiteQuery)4 Query (com.squareup.sqlbrite3.SqlBrite.Query)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TargetApi (android.annotation.TargetApi)1 SdkSuppress (android.support.test.filters.SdkSuppress)1 Employee (com.squareup.sqlbrite3.TestDb.Employee)1 Closeable (java.io.Closeable)1