Search in sources :

Example 21 with Query

use of com.pushtorefresh.storio3.contentresolver.queries.Query 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 22 with Query

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

the class RxJavaUtilsTest method createGetFlowableOptionalCompletedAfterInitialEmissionIfNoTablesAndTags.

@Test
public void createGetFlowableOptionalCompletedAfterInitialEmissionIfNoTablesAndTags() {
    RawQuery queryWithoutTablesAnTags = RawQuery.builder().query("select * from " + UserTableMeta.TABLE + " where " + UserTableMeta.COLUMN_ID + "=?").args(1).build();
    PreparedGetObject<User> preparedGet = storIOSQLite.get().object(User.class).withQuery(queryWithoutTablesAnTags).prepare();
    TestSubscriber<Optional<User>> subscriber = new TestSubscriber<Optional<User>>();
    RxJavaUtils.createGetFlowableOptional(storIOSQLite, preparedGet, null, queryWithoutTablesAnTags, BackpressureStrategy.LATEST).subscribe(subscriber);
    subscriber.assertNoErrors();
    subscriber.assertValues(Optional.<User>empty());
    subscriber.assertComplete();
}
Also used : User(com.pushtorefresh.storio3.sqlite.integration.User) Optional(com.pushtorefresh.storio3.Optional) RawQuery(com.pushtorefresh.storio3.sqlite.queries.RawQuery) TestSubscriber(io.reactivex.subscribers.TestSubscriber) BaseTest(com.pushtorefresh.storio3.sqlite.integration.BaseTest) Test(org.junit.Test)

Example 23 with Query

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

the class RxJavaUtilsTest method createGetFlowableCompletedAfterInitialEmissionIfNoTablesAndTags.

@Test
public void createGetFlowableCompletedAfterInitialEmissionIfNoTablesAndTags() {
    RawQuery queryWithoutTablesAnTags = RawQuery.builder().query("select * from " + UserTableMeta.TABLE).build();
    PreparedGetListOfObjects<User> preparedGet = storIOSQLite.get().listOfObjects(User.class).withQuery(queryWithoutTablesAnTags).prepare();
    TestSubscriber<List<User>> subscriber = new TestSubscriber<List<User>>();
    RxJavaUtils.createGetFlowable(storIOSQLite, preparedGet, null, queryWithoutTablesAnTags, BackpressureStrategy.LATEST).subscribe(subscriber);
    subscriber.assertNoErrors();
    subscriber.assertValues(EMPTY_LIST);
    subscriber.assertComplete();
}
Also used : User(com.pushtorefresh.storio3.sqlite.integration.User) RawQuery(com.pushtorefresh.storio3.sqlite.queries.RawQuery) TestSubscriber(io.reactivex.subscribers.TestSubscriber) List(java.util.List) BaseTest(com.pushtorefresh.storio3.sqlite.integration.BaseTest) 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