Search in sources :

Example 16 with Employee

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

the class BriteDatabaseTest method queryMapToOneOrDefault.

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

Example 17 with Employee

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

Example 18 with Employee

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

the class QueryTest method mapToOneIgnoresNullCursor.

@Test
public void mapToOneIgnoresNullCursor() {
    Query nully = new Query() {

        @Nullable
        @Override
        public Cursor run() {
            return null;
        }
    };
    TestObserver<Employee> observer = new TestObserver<>();
    Observable.just(nully).lift(Query.mapToOne(MAPPER)).subscribe(observer);
    observer.assertNoValues();
    observer.assertComplete();
}
Also used : Employee(com.squareup.sqlbrite3.TestDb.Employee) Query(com.squareup.sqlbrite3.SqlBrite.Query) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 19 with Employee

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

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

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