Search in sources :

Example 16 with Query

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

the class DefaultStorIOSQLiteTest method shouldPassSQLWithoutArgsToExecSQL.

@Test
public void shouldPassSQLWithoutArgsToExecSQL() {
    RawQuery rawQuery = RawQuery.builder().query("DROP TABLE IF EXISTS someTable").build();
    storIOSQLite.lowLevel().executeSQL(rawQuery);
    verify(sqLiteOpenHelper).getWritableDatabase();
    verify(sqLiteDatabase).execSQL(eq(rawQuery.query()));
    verifyNoMoreInteractions(sqLiteOpenHelper, sqLiteDatabase);
}
Also used : RawQuery(com.pushtorefresh.storio3.sqlite.queries.RawQuery) Test(org.junit.Test)

Example 17 with Query

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

the class DefaultPutResolverTest method shouldCloseCursorIfInsertThrowsException.

@Test
public void shouldCloseCursorIfInsertThrowsException() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// No results -> insert should be performed
    0);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
    when(lowLevel.insert(eq(expectedInsertQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            try {
                return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    try {
        putResolver.performPut(storIOContentResolver, testItem);
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Fake exception from ContentResolver");
        verify(storIOContentResolver).lowLevel();
        // Checks that it asks actual ContentResolver for results
        verify(lowLevel).query(eq(expectedQuery));
        verify(cursor).getCount();
        // Cursor must be closed in case of exception!
        verify(cursor).close();
        verify(lowLevel).insert(eq(expectedInsertQuery), any(ContentValues.class));
        verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) Query(com.pushtorefresh.storio3.contentresolver.queries.Query) UpdateQuery(com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 18 with Query

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

the class DefaultPutResolverTest method shouldCloseCursorIfUpdateThrowsException.

@Test
public void shouldCloseCursorIfUpdateThrowsException() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    // item with some id, should be updated
    final TestItem testItem = new TestItem(1L);
    final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final Cursor cursor = mock(Cursor.class);
    when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
    when(cursor.getCount()).thenReturn(// One result -> update should be performed
    1);
    final UpdateQuery expectedUpdateQuery = UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    when(lowLevel.update(eq(expectedUpdateQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            try {
                return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    try {
        putResolver.performPut(storIOContentResolver, testItem);
        failBecauseExceptionWasNotThrown(IllegalStateException.class);
    } catch (IllegalStateException expected) {
        assertThat(expected).hasMessage("Fake exception from ContentResolver");
        verify(storIOContentResolver).lowLevel();
        // Checks that it asks actual ContentResolver for results
        verify(lowLevel).query(eq(expectedQuery));
        verify(cursor).getCount();
        // Cursor must be closed in case of exception!
        verify(cursor).close();
        verify(lowLevel).update(eq(expectedUpdateQuery), any(ContentValues.class));
        verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
    }
}
Also used : ContentValues(android.content.ContentValues) 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) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 19 with Query

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

the class PreparedGetCursorTest method shouldUseStandardGetResolverWithoutExplicitlyPassed.

@Test
public void shouldUseStandardGetResolverWithoutExplicitlyPassed() {
    StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    Query query = Query.builder().uri(mock(Uri.class)).build();
    new PreparedGetCursor.Builder(storIOContentResolver).withQuery(query).prepare().executeAsBlocking();
    verify(storIOContentResolver).lowLevel();
    verify(storIOContentResolver).interceptors();
    verify(lowLevel).query(query);
    verifyNoMoreInteractions(storIOContentResolver, lowLevel);
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 20 with Query

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

the class DefaultGetResolverTest method query.

@Test
public void query() {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    final Query query = Query.builder().uri(mock(Uri.class)).build();
    final Cursor expectedCursor = mock(Cursor.class);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    when(lowLevel.query(query)).thenReturn(expectedCursor);
    final GetResolver<TestItem> defaultGetResolver = new DefaultGetResolver<TestItem>() {

        @NonNull
        @Override
        public TestItem mapFromCursor(@NonNull StorIOContentResolver storIOContentResolver, @NonNull Cursor cursor) {
            assertThat(cursor).isSameAs(expectedCursor);
            return new TestItem();
        }
    };
    final Cursor actualCursor = defaultGetResolver.performGet(storIOContentResolver, query);
    // only one request should occur
    verify(lowLevel, times(1)).query(any(Query.class));
    // and this request should be equals to original
    verify(lowLevel, times(1)).query(query);
    assertThat(actualCursor).isSameAs(expectedCursor);
}
Also used : Query(com.pushtorefresh.storio3.contentresolver.queries.Query) NonNull(android.support.annotation.NonNull) Cursor(android.database.Cursor) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) 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