Search in sources :

Example 1 with Transaction

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

Example 2 with Transaction

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

the class BriteDatabaseTest method synchronousQueryWithSupportSQLiteQueryDuringTransactionSeesChanges.

@Test
public void synchronousQueryWithSupportSQLiteQueryDuringTransactionSeesChanges() {
    Transaction transaction = db.newTransaction();
    try {
        assertCursor(db.query(new SimpleSQLiteQuery(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(new SimpleSQLiteQuery(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) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) Test(org.junit.Test)

Example 3 with Transaction

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

the class BriteDatabaseTest method callingEndMultipleTimesThrows.

@Test
public void callingEndMultipleTimesThrows() {
    Transaction transaction = db.newTransaction();
    transaction.end();
    try {
        transaction.end();
        fail();
    } catch (IllegalStateException e) {
        assertThat(e).hasMessage("Not in transaction.");
    }
}
Also used : Transaction(com.squareup.sqlbrite3.BriteDatabase.Transaction) Test(org.junit.Test)

Example 4 with Transaction

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

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

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