Search in sources :

Example 11 with Query

use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.

the class DefaultStorIOSQLiteTest method shouldPassSQLWithArgsToExecSQL.

@Test
public void shouldPassSQLWithArgsToExecSQL() {
    RawQuery rawQuery = RawQuery.builder().query("DROP TABLE users").args("arg1", "arg2").build();
    storIOSQLite.lowLevel().executeSQL(rawQuery);
    verify(sqLiteOpenHelper).getWritableDatabase();
    verify(sqLiteDatabase).execSQL(eq(rawQuery.query()), eq(new String[] { "arg1", "arg2" }));
    verifyNoMoreInteractions(sqLiteOpenHelper, sqLiteDatabase);
}
Also used : RawQuery(com.pushtorefresh.storio3.sqlite.queries.RawQuery) Test(org.junit.Test)

Example 12 with Query

use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.

the class DefaultStorIOContentResolverTest method shouldThrowExceptionIfContentResolverReturnsNull.

@Test
public void shouldThrowExceptionIfContentResolverReturnsNull() {
    ContentResolver contentResolver = mock(ContentResolver.class);
    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder().contentResolver(contentResolver).build();
    Query query = Query.builder().uri(mock(Uri.class)).build();
    when(contentResolver.query(any(Uri.class), any(String[].class), anyString(), any(String[].class), anyString())).thenReturn(// Notice, we return null instead of Cursor
    null);
    try {
        storIOContentResolver.lowLevel().query(query);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Cursor returned by content provider is null");
    }
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Uri(android.net.Uri) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 13 with Query

use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.

the class DefaultPutResolver method performPut.

/**
 * {@inheritDoc}
 */
@NonNull
@Override
public PutResult performPut(@NonNull StorIOContentResolver storIOContentResolver, @NonNull T object) {
    final UpdateQuery updateQuery = mapToUpdateQuery(object);
    final Query query = Query.builder().uri(updateQuery.uri()).where(nullableString(updateQuery.where())).whereArgs((Object[]) nullableArrayOfStringsFromListOfStrings(updateQuery.whereArgs())).build();
    final StorIOContentResolver.LowLevel lowLevel = storIOContentResolver.lowLevel();
    final Cursor cursor = lowLevel.query(query);
    try {
        final ContentValues contentValues = mapToContentValues(object);
        if (cursor.getCount() == 0) {
            final InsertQuery insertQuery = mapToInsertQuery(object);
            final Uri insertedUri = lowLevel.insert(insertQuery, contentValues);
            return PutResult.newInsertResult(insertedUri, insertQuery.uri());
        } else {
            final int numberOfRowsUpdated = lowLevel.update(updateQuery, contentValues);
            return PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.uri());
        }
    } finally {
        cursor.close();
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull)

Example 14 with Query

use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.

the class GetNumberOfResultsObserveChangesTest method repeatsOperationWithQueryByChangeOfTable.

@Test
public void repeatsOperationWithQueryByChangeOfTable() {
    putUserBlocking();
    PreparedGetNumberOfResults operation = storIOSQLite.get().numberOfResults().withQuery(query).prepare();
    verifyChangesReceived(operation, tableChanges, 1);
}
Also used : PreparedGetNumberOfResults(com.pushtorefresh.storio3.sqlite.operations.get.PreparedGetNumberOfResults) Test(org.junit.Test)

Example 15 with Query

use of com.pushtorefresh.storio3.sqlite.queries.Query in project storio by pushtorefresh.

the class ExecSQLTest method shouldReturnQueryInGetData.

@Test
public void shouldReturnQueryInGetData() {
    final RawQuery query = RawQuery.builder().query(// we don't want to really delete table
    "DROP TABLE IF EXISTS no_such_table").build();
    final PreparedExecuteSQL operation = storIOSQLite.executeSQL().withQuery(query).prepare();
    assertThat(operation.getData()).isEqualTo(query);
}
Also used : RawQuery(com.pushtorefresh.storio3.sqlite.queries.RawQuery) PreparedExecuteSQL(com.pushtorefresh.storio3.sqlite.operations.execute.PreparedExecuteSQL) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 NonNull (android.support.annotation.NonNull)12 Cursor (android.database.Cursor)10 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)8 Query (com.pushtorefresh.storio3.contentresolver.queries.Query)8 ContentValues (android.content.ContentValues)7 RawQuery (com.pushtorefresh.storio3.sqlite.queries.RawQuery)7 InsertQuery (com.pushtorefresh.storio3.contentresolver.queries.InsertQuery)5 UpdateQuery (com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery)5 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)4 Uri (android.net.Uri)3 Query (com.pushtorefresh.storio3.sqlite.queries.Query)3 List (java.util.List)3 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)2 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)2 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)2 BaseTest (com.pushtorefresh.storio3.sqlite.integration.BaseTest)2 User (com.pushtorefresh.storio3.sqlite.integration.User)2 PreparedGetNumberOfResults (com.pushtorefresh.storio3.sqlite.operations.get.PreparedGetNumberOfResults)2 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)2