Search in sources :

Example 6 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class PutOperationDesignTest method putContentValuesFlowable.

@Test
public void putContentValuesFlowable() {
    ContentValues contentValues = mock(ContentValues.class);
    Flowable<PutResult> putResultFlowable = storIOContentResolver().put().contentValues(contentValues).withPutResolver(putResolverForContentValues).prepare().asRxFlowable(BackpressureStrategy.MISSING);
}
Also used : ContentValues(android.content.ContentValues) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 7 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class PutOperationDesignTest method putContentValuesSingle.

@Test
public void putContentValuesSingle() {
    ContentValues contentValues = mock(ContentValues.class);
    Single<PutResult> putResultSingle = storIOContentResolver().put().contentValues(contentValues).withPutResolver(putResolverForContentValues).prepare().asRxSingle();
}
Also used : ContentValues(android.content.ContentValues) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 8 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class DefaultPutResolverTest method insert.

/**
 * Verifies behavior of {@link DefaultPutResolver} for "insert"
 */
@Test
public void insert() throws Exception {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    final Uri expectedInsertedUri = mock(Uri.class);
    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);
    when(lowLevel.insert(any(InsertQuery.class), any(ContentValues.class))).thenReturn(expectedInsertedUri);
    final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return expectedInsertQuery;
        }

        @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);
            }
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.apply(testItem);
    // Performing Put that should "insert"
    final PutResult putResult = putResolver.performPut(storIOContentResolver, testItem);
    // 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 insert was performed
    verify(lowLevel).insert(eq(expectedInsertQuery), eq(expectedContentValues));
    // only one insert should occur
    verify(lowLevel).insert(any(InsertQuery.class), any(ContentValues.class));
    // no updates should occur
    verify(lowLevel, never()).update(any(UpdateQuery.class), any(ContentValues.class));
    // put result checks
    assertThat(putResult.wasInserted()).isTrue();
    assertThat(putResult.wasUpdated()).isFalse();
    assertThat(putResult.insertedUri()).isEqualTo(expectedInsertedUri);
    assertThat(putResult.numberOfRowsUpdated()).isNull();
}
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) Uri(android.net.Uri) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 9 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class DefaultPutResolverTest method update.

/**
 * Verifies behavior of {@link DefaultPutResolver} for "update"
 */
@Test
public void update() throws Exception {
    final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
    final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
    // item with some id, should be updated
    final TestItem testItem = new TestItem(1L);
    when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
    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(// 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 UpdateQuery expectedUpdateQuery = UpdateQuery.builder().uri(TestItem.CONTENT_URI).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 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);
            }
        }
    };
    final ContentValues expectedContentValues = TestItem.MAP_TO_CONTENT_VALUES.apply(testItem);
    // Performing Put that should "update"
    final PutResult putResult = putResolver.performPut(storIOContentResolver, testItem);
    // checks that it asks db for results
    verify(lowLevel, times(1)).query(eq(expectedQuery));
    // checks that cursor was closed
    verify(cursor, times(1)).close();
    // only one query should occur
    verify(lowLevel, times(1)).query(any(Query.class));
    // checks that required update was performed
    verify(lowLevel, times(1)).update(eq(expectedUpdateQuery), eq(expectedContentValues));
    // only one update should occur
    verify(lowLevel, times(1)).update(any(UpdateQuery.class), any(ContentValues.class));
    // no inserts should occur
    verify(lowLevel, times(0)).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.insertedUri()).isNull();
}
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) InsertQuery(com.pushtorefresh.storio3.contentresolver.queries.InsertQuery) NonNull(android.support.annotation.NonNull) StorIOContentResolver(com.pushtorefresh.storio3.contentresolver.StorIOContentResolver) Test(org.junit.Test)

Example 10 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult in project storio by pushtorefresh.

the class DefaultPutResolverTest method insert.

/**
 * Verifies behavior of {@link DefaultPutResolver} for "insert"
 */
@Test
public void insert() {
    final StorIOSQLite storIOSQLite = mock(StorIOSQLite.class);
    final StorIOSQLite.LowLevel lowLevel = mock(StorIOSQLite.LowLevel.class);
    // item without id, should be inserted
    final TestItem testItem = new TestItem(null);
    when(storIOSQLite.lowLevel()).thenReturn(lowLevel);
    final Long expectedInsertedId = 24L;
    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(// No results -> insert should be performed
    0);
    when(lowLevel.insert(any(InsertQuery.class), any(ContentValues.class))).thenReturn(expectedInsertedId);
    final Set<String> tags = singleton("test_tag");
    final InsertQuery expectedInsertQuery = InsertQuery.builder().table(TestItem.TABLE).affectsTags(tags).nullColumnHack(null).build();
    final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {

        @NonNull
        @Override
        protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
            return expectedInsertQuery;
        }

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

        @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 "insert"
    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 insert was performed
    verify(lowLevel).insert(eq(expectedInsertQuery), eq(expectedContentValues));
    // only one insert should occur
    verify(lowLevel).insert(any(InsertQuery.class), any(ContentValues.class));
    // no updates should occur
    verify(lowLevel, never()).update(any(UpdateQuery.class), any(ContentValues.class));
    // put result checks
    assertThat(putResult.wasInserted()).isTrue();
    assertThat(putResult.wasUpdated()).isFalse();
    assertThat(putResult.insertedId()).isEqualTo(expectedInsertedId);
    assertThat(putResult.numberOfRowsUpdated()).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)

Aggregations

Test (org.junit.Test)27 Cursor (android.database.Cursor)21 PutResult (com.pushtorefresh.storio3.contentresolver.operations.put.PutResult)19 Changes (com.pushtorefresh.storio3.contentresolver.Changes)15 TestSubscriber (io.reactivex.subscribers.TestSubscriber)15 ContentValues (android.content.ContentValues)14 Uri (android.net.Uri)10 NonNull (android.support.annotation.NonNull)10 PutResult (com.pushtorefresh.storio3.sqlite.operations.put.PutResult)9 StorIOSQLite (com.pushtorefresh.storio3.sqlite.StorIOSQLite)6 InsertQuery (com.pushtorefresh.storio3.sqlite.queries.InsertQuery)4 StorIOContentResolver (com.pushtorefresh.storio3.contentresolver.StorIOContentResolver)3 InsertQuery (com.pushtorefresh.storio3.contentresolver.queries.InsertQuery)3 Query (com.pushtorefresh.storio3.contentresolver.queries.Query)3 UpdateQuery (com.pushtorefresh.storio3.contentresolver.queries.UpdateQuery)3 UpdateQuery (com.pushtorefresh.storio3.sqlite.queries.UpdateQuery)3 Query (com.pushtorefresh.storio3.sqlite.queries.Query)2 ArrayList (java.util.ArrayList)2 Car (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Car)1 Person (com.pushtorefresh.storio3.sample.many_to_many_sample.entities.Person)1