Search in sources :

Example 21 with PutResult

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

the class BaseTest method putUserBlocking.

@NonNull
User putUserBlocking(@NonNull final User user) {
    final PutResult putResult = storIOSQLite.put().object(user).prepare().executeAsBlocking();
    assertThat(putResult).isNotNull();
    assertThat(putResult.wasInserted()).isTrue();
    return user;
}
Also used : PutResult(com.pushtorefresh.storio3.sqlite.operations.put.PutResult) NonNull(android.support.annotation.NonNull)

Example 22 with PutResult

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

the class DefaultPutResolver method performPut.

/**
 * {@inheritDoc}
 */
@NonNull
@Override
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull T object) {
    final UpdateQuery updateQuery = mapToUpdateQuery(object);
    final StorIOSQLite.LowLevel lowLevel = storIOSQLite.lowLevel();
    // for data consistency in concurrent environment, encapsulate Put Operation into transaction
    lowLevel.beginTransaction();
    try {
        final Cursor cursor = lowLevel.query(Query.builder().table(updateQuery.table()).where(nullableString(updateQuery.where())).whereArgs((Object[]) nullableArrayOfStringsFromListOfStrings(updateQuery.whereArgs())).build());
        final PutResult putResult;
        try {
            final ContentValues contentValues = mapToContentValues(object);
            if (cursor.getCount() == 0) {
                final InsertQuery insertQuery = mapToInsertQuery(object);
                final long insertedId = lowLevel.insert(insertQuery, contentValues);
                putResult = PutResult.newInsertResult(insertedId, insertQuery.table(), insertQuery.affectsTags());
            } else {
                final int numberOfRowsUpdated = lowLevel.update(updateQuery, contentValues);
                putResult = PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table(), updateQuery.affectsTags());
            }
        } finally {
            cursor.close();
        }
        // everything okay
        lowLevel.setTransactionSuccessful();
        return putResult;
    } finally {
        // in case of bad situations, db won't be affected
        lowLevel.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) InsertQuery(com.pushtorefresh.storio3.sqlite.queries.InsertQuery) UpdateQuery(com.pushtorefresh.storio3.sqlite.queries.UpdateQuery) Cursor(android.database.Cursor) StorIOSQLite(com.pushtorefresh.storio3.sqlite.StorIOSQLite) NonNull(android.support.annotation.NonNull)

Example 23 with PutResult

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

the class PutOperationTest method updateNotNullFieldToNull.

@Test
public void updateNotNullFieldToNull() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    Uri insertedUri = contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value", "optionalValue").toContentValues());
    // optional value changes to null
    TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value", null);
    PutResult updateResult = storIOContentResolver.put().object(testItem).prepare().executeAsBlocking();
    assertThat(updateResult.wasUpdated()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) Uri(android.net.Uri) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 24 with PutResult

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

the class PutOperationTest method updateContentValuesAsRxFlowable.

@Test
public void updateContentValuesAsRxFlowable() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(2).subscribe(changesTestSubscriber);
    Uri insertedUri = contentResolver.insert(TestItem.CONTENT_URI, TestItem.create(null, "value").toContentValues());
    TestItem testItem = TestItem.create(ContentUris.parseId(insertedUri), "value2");
    PutResult updateResult = storIOContentResolver.put().contentValues(testItem.toContentValues()).withPutResolver(testItemContentValuesPutResolver).prepare().asRxFlowable(BackpressureStrategy.MISSING).blockingFirst();
    assertThat(updateResult.wasUpdated()).isTrue();
    Cursor cursor = contentResolver.query(TestItem.CONTENT_URI, null, null, null, null);
    Assertions.assertThat(cursor).hasCount(1);
    cursor.moveToFirst();
    assertThat(testItem).isEqualTo(TestItem.fromCursor(cursor));
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValues(Changes.newInstance(TestItem.CONTENT_URI), Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) Uri(android.net.Uri) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) Test(org.junit.Test)

Example 25 with PutResult

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

the class PutOperationTest method insertContentValuesExecuteAsBlocking.

@Test
public void insertContentValuesExecuteAsBlocking() {
    TestSubscriber<Changes> changesTestSubscriber = new TestSubscriber<Changes>();
    storIOContentResolver.observeChangesOfUri(TestItem.CONTENT_URI, BackpressureStrategy.MISSING).take(1).subscribe(changesTestSubscriber);
    TestItem testItem = TestItem.create(null, "value");
    ContentValues cv = testItem.toContentValues();
    PutResult insertResult = storIOContentResolver.put().contentValues(cv).withPutResolver(testItemContentValuesPutResolver).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();
    changesTestSubscriber.awaitTerminalEvent(60, SECONDS);
    changesTestSubscriber.assertNoErrors();
    changesTestSubscriber.assertValue(Changes.newInstance(TestItem.CONTENT_URI));
}
Also used : Changes(com.pushtorefresh.storio3.contentresolver.Changes) ContentValues(android.content.ContentValues) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Cursor(android.database.Cursor) PutResult(com.pushtorefresh.storio3.contentresolver.operations.put.PutResult) 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