Search in sources :

Example 41 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class UserWithTweetsGetResolver method mapFromCursor.

@NonNull
@Override
public UserWithTweets mapFromCursor(@NonNull StorIOSQLite storIOSQLite, @NonNull Cursor cursor) {
    // Or you can manually parse cursor (it will be sliiightly faster)
    final User user = userGetResolver.mapFromCursor(storIOSQLite, cursor);
    // Yep, you can reuse StorIO here!
    // Or, you can do manual low level requests here
    // BTW, if you profiled your app and found that such queries are not very fast
    // You can always add some optimized version for particular queries to improve the performance
    final List<Tweet> tweetsOfTheUser = storIOSQLite.get().listOfObjects(Tweet.class).withQuery(Query.builder().table(TweetsTable.TABLE).where(TweetsTable.COLUMN_AUTHOR + "=?").whereArgs(user.nick()).build()).prepare().executeAsBlocking();
    return new UserWithTweets(user, tweetsOfTheUser);
}
Also used : User(com.pushtorefresh.storio3.sample.db.entities.User) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) UserWithTweets(com.pushtorefresh.storio3.sample.db.entities.UserWithTweets) NonNull(android.support.annotation.NonNull)

Example 42 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class TweetWithUserGetResolver method mapFromCursor.

// We expect that cursor will contain both Tweet and User: SQL JOIN
@NonNull
@Override
public TweetWithUser mapFromCursor(@NonNull StorIOSQLite storIOSQLite, @NonNull Cursor cursor) {
    final Tweet tweet = Tweet.newTweet(cursor.getLong(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_ID)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_AUTHOR)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_CONTENT)));
    final User user = User.newUser(cursor.getLong(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_USER_ID)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_USER_NICK)));
    return new TweetWithUser(tweet, user);
}
Also used : TweetWithUser(com.pushtorefresh.storio3.sample.db.entities.TweetWithUser) User(com.pushtorefresh.storio3.sample.db.entities.User) TweetWithUser(com.pushtorefresh.storio3.sample.db.entities.TweetWithUser) Tweet(com.pushtorefresh.storio3.sample.db.entities.Tweet) NonNull(android.support.annotation.NonNull)

Example 43 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class CarPersonRelationPutResolver method performPut.

@Override
@NonNull
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull ContentValues contentValues) {
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    lowLevel.beginTransaction();
    try {
        final InsertQuery insertQuery = InsertQuery.builder().table(TABLE).build();
        final long insertedId = lowLevel.insert(insertQuery, contentValues);
        final PutResult putResult = PutResult.newInsertResult(insertedId, insertQuery.table());
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        lowLevel.endTransaction();
    }
}
Also used : InsertQuery(com.pushtorefresh.storio3.sqlite.queries.InsertQuery) PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 44 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class DefaultPutResolverTest method update.

/**
 * Verifies behavior of {@link DefaultPutResolver} for "update"
 */
@Test
public void update() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    // item with some id, should be updated
    final TestItem testItem = new TestItem(null);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    final Query expectedQuery = Query.builder().table(TestItem.TABLE).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(// Some rows already in db -> update should be performed
    1);
    final Integer expectedNumberOfRowsUpdated = 1;
    when(lowLevel.update(any(UpdateQuery.class), any(ContentValues.class))).thenReturn(expectedNumberOfRowsUpdated);
    final Set<String> tags = singleton("test_tag");
    final UpdateQuery expectedUpdateQuery = UpdateQuery.builder().table(TestItem.TABLE).affectsTags(tags).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            fail("Should not be called");
            return null;
        }

        @NonNull
        @Override
        protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
            return expectedUpdateQuery;
        }

        @NonNull
        @Override
        protected ContentValues mapToContentValues(@NonNull TestItem object) {
            return TestItem.MAP_TO_CONTENT_VALUES.apply(object);
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.apply(testItem);
    // Performing Put that should "update"
    final PutResult putResult = putResolver.performPut(storIOSQLite, testItem);
    verify(lowLevel).beginTransaction();
    verify(lowLevel).setTransactionSuccessful();
    verify(lowLevel).endTransaction();
    // checks that it asks db for results
    verify(lowLevel).query(eq(expectedQuery));
    // checks that cursor was closed
    verify(cursor).close();
    // only one query should occur
    verify(lowLevel).query(any(Query.class));
    // checks that required update was performed
    verify(lowLevel).update(eq(expectedUpdateQuery), eq(expectedContentValues));
    // only one update should occur
    verify(lowLevel).update(any(UpdateQuery.class), any(ContentValues.class));
    // no inserts should occur
    verify(lowLevel, never()).insert(any(InsertQuery.class), any(ContentValues.class));
    // put result checks
    assertThat(putResult.wasInserted()).isFalse();
    assertThat(putResult.wasUpdated()).isTrue();
    assertThat(putResult.numberOfRowsUpdated()).isEqualTo(expectedNumberOfRowsUpdated);
    assertThat(putResult.insertedId()).isNull();
    assertThat(putResult.affectedTables()).containsExactly(TestItem.TABLE);
    assertThat(putResult.affectedTags()).isEqualTo(tags);
}
Also used : ContentValues(android.content.ContentValues) Query(com.pushtorefresh.storio3.sqlite.queries.Query) UpdateQuery(com.pushtorefresh.storio3.sqlite.queries.UpdateQuery) InsertQuery(com.pushtorefresh.storio3.sqlite.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio3.sqlite.queries.UpdateQuery) Cursor(android.database.Cursor) InsertQuery(com.pushtorefresh.storio3.sqlite.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Example 45 with StorIOSQLite

use of com.pushtorefresh.storio3.sqlite.StorIOSQLite in project storio by pushtorefresh.

the class PreparedPutContentValuesIterableTest method shouldFinishTransactionIfExceptionHasOccurredBlocking.

@Test
public void shouldFinishTransactionIfExceptionHasOccurredBlocking() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    // noinspection unchecked
    final PutResolver<ContentValues> putResolver = mock(PutResolver.class);
    final List<ContentValues> contentValues = singletonList(mock(ContentValues.class));
    when(putResolver.performPut(same(storIOSQLite), any(ContentValues.class))).thenThrow(new IllegalStateException("test exception"));
    try {
        new PreparedPutContentValuesIterable.Builder(storIOSQLite, contentValues).withPutResolver(putResolver).useTransaction(true).prepare().executeAsBlocking();
        failBecauseExceptionWasNotThrown(StorIOException.class);
    } catch (StorIOException expected) {
        IllegalStateException cause = (IllegalStateException) expected.getCause();
        assertThat(cause).hasMessage("test exception");
        verify(lowLevel).beginTransaction();
        verify(lowLevel, never()).setTransactionSuccessful();
        verify(lowLevel).endTransaction();
        verify(storIOSQLite).lowLevel();
        verify(storIOSQLite).interceptors();
        verify(putResolver).performPut(same(storIOSQLite), any(ContentValues.class));
        verifyNoMoreInteractions(storIOSQLite, lowLevel, putResolver);
    }
}
Also used : ContentValues(android.content.ContentValues) StorIOException(com.pushtorefresh.storio3.StorIOException) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)34 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)32 NonNull (android.support.annotation.NonNull)18 StorIOException (com.pushtorefresh.storio3.StorIOException)18 ContentValues (android.content.ContentValues)13 Query (com.pushtorefresh.storio3.sqlite.queries.Query)9 Cursor (android.database.Cursor)8 TestObserver (io.reactivex.observers.TestObserver)8 DeleteQuery (com.pushtorefresh.storio3.sqlite.queries.DeleteQuery)7 TestSubscriber (io.reactivex.subscribers.TestSubscriber)7 TypeMappingFinder (com.pushtorefresh.storio3.TypeMappingFinder)4 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)4 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)4 RawQuery (com.pushtorefresh.storio3.sqlite.queries.RawQuery)4 Tweet (com.pushtorefresh.storio3.sample.db.entities.Tweet)3 TweetWithUser (com.pushtorefresh.storio3.sample.db.entities.TweetWithUser)3 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)3 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)3 User (com.pushtorefresh.storio3.sample.db.entities.User)2 BaseTest (com.pushtorefresh.storio3.sqlite.integration.BaseTest)2