use of com.pushtorefresh.storio3.contentresolver.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;
}
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 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();
}
}
use of com.pushtorefresh.storio3.contentresolver.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));
}
use of com.pushtorefresh.storio3.contentresolver.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));
}
use of com.pushtorefresh.storio3.contentresolver.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));
}
Aggregations