Search in sources :

Example 11 with Transaction

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

the class BriteDatabaseTest method emptyTransactionDoesNotNotify.

@Test
public void emptyTransactionDoesNotNotify() {
    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 {
        transaction.markSuccessful();
    } finally {
        transaction.end();
    }
    o.assertNoMoreEvents();
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) Test(org.junit.Test)

Example 12 with Transaction

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

Example 13 with Transaction

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

the class BriteDatabaseTest method transactionIsCloseable.

@Test
public void transactionIsCloseable() throws IOException {
    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();
    // noinspection UnnecessaryLocalVariable
    // Verify type is implemented.
    Closeable closeableTransaction = transaction;
    try {
        db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("john", "John Johnson"));
        db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("nick", "Nick Nickers"));
        transaction.markSuccessful();
    } finally {
        closeableTransaction.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.sqlbrite3.BriteDatabase.Transaction) Closeable(java.io.Closeable) Test(org.junit.Test)

Example 14 with Transaction

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

the class BriteDatabaseTest method transactionCreatedFromTransactionNotificationWorks.

@Test
public void transactionCreatedFromTransactionNotificationWorks() {
    // Tests the case where a transaction is created in the subscriber to a query which gets
    // notified as the result of another transaction being committed. With improper ordering, this
    // can result in creating a new transaction before the old is committed on the underlying DB.
    db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(new Consumer<Query>() {

        @Override
        public void accept(Query query) {
            db.newTransaction().end();
        }
    });
    Transaction transaction = db.newTransaction();
    try {
        db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("john", "John Johnson"));
        transaction.markSuccessful();
    } finally {
        transaction.end();
    }
}
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 15 with Transaction

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

the class BriteDatabaseTest method synchronousQueryDuringTransactionSeesChanges.

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