Search in sources :

Example 26 with PutResult

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

the class PutOperationTest method insertObjectAsSingle.

@Test
public void insertObjectAsSingle() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItem = TestItem.create(null, "value");
    PutResult insertResult = storIOContentResolver.put().object(testItem).prepare().asRxSingle().blockingGet();
    assertThat(insertResult.wasInserted()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    cursor.close();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 27 with PutResult

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

the class PutOperationTest method insertObjectExecuteAsBlocking.

@Test
public void insertObjectExecuteAsBlocking() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItem = TestItem.create(null, "value");
    PutResult insertResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
    assertThat(insertResult.wasInserted()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem.equalsWithoutId(TestItem.fromCursor(cursor))).isTrue();
    cursor.close();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 28 with PutResult

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

the class PutOperationDesignTest method putObjectBlocking.

@Test
public void putObjectBlocking() {
    Article article = Article.newInstance(null, "test");
    PutResult putResult = storIOContentResolver().put().object(article).withPutResolver(ArticleMeta.PUT_RESOLVER).prepare().executeAsBlocking();
}
Also used : PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 29 with PutResult

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

the class PutOperationDesignTest method putContentValuesBlocking.

@Test
public void putContentValuesBlocking() {
    ContentValues contentValues = mock(ContentValues.class);
    PutResult putResult = storIOContentResolver().put().contentValues(contentValues).withPutResolver(putResolverForContentValues).prepare().executeAsBlocking();
}
Also used : ContentValues(android.content.ContentValues) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 30 with PutResult

use of com.pushtorefresh.storio3.contentresolver.operations.put.PutResult 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)

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