Search in sources :

Example 1 with Transaction

use of com.squareup.sqlbrite.BriteDatabase.Transaction 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, employee("john", "John Johnson"));
        db.insert(TABLE_EMPLOYEE, 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.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Example 2 with Transaction

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

the class BriteDatabaseTest method transactionDoesNotThrow.

@Test
public void transactionDoesNotThrow() {
    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, employee("john", "John Johnson"));
        db.insert(TABLE_EMPLOYEE, employee("nick", "Nick Nickers"));
        transaction.markSuccessful();
    } finally {
        // Transactions should not throw on close().
        transaction.close();
    }
    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.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Example 3 with Transaction

use of com.squareup.sqlbrite.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.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Example 4 with Transaction

use of com.squareup.sqlbrite.BriteDatabase.Transaction 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, employee("john", "John Johnson"));
        db.insert(TABLE_EMPLOYEE, 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.sqlbrite.SqlBrite.Query) Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Example 5 with Transaction

use of com.squareup.sqlbrite.BriteDatabase.Transaction 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, employee("john", "John Johnson"));
        db.insert(TABLE_EMPLOYEE, employee("nick", "Nick Nickers"));
    // No call to set successful.
    } finally {
        transaction.end();
    }
    o.assertNoMoreEvents();
}
Also used : Transaction(com.squareup.sqlbrite.BriteDatabase.Transaction) Test(org.junit.Test)

Aggregations

Transaction (com.squareup.sqlbrite.BriteDatabase.Transaction)14 Test (org.junit.Test)14 Query (com.squareup.sqlbrite.SqlBrite.Query)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TargetApi (android.annotation.TargetApi)1 SdkSuppress (android.support.test.filters.SdkSuppress)1 Employee (com.squareup.sqlbrite.TestDb.Employee)1 Closeable (java.io.Closeable)1