use of com.amplifyframework.datastore.storage.StorageItemChange in project amplify-android by aws-amplify.
the class MutationPersistenceInstrumentationTest method saveIsObservedForPersistentRecord.
/**
* When {@link LocalStorageAdapter#save(Model, StorageItemChange.Initiator, QueryPredicate, Consumer, Consumer)}
* is called to save a {@link PendingMutation.PersistentRecord}, we should see an event emitted on the
* {@link LocalStorageAdapter#observe(Consumer, Consumer, Action)}'s item consumer.
* @throws DataStoreException On failure to convert received value out of record format, or
* on failure to manipulate data in/out of DataStore
*/
@Test
public void saveIsObservedForPersistentRecord() throws DataStoreException {
// Start watching observe().
// The storage adapter emits StorageItemChange.
TestObserver<StorageItemChange<? extends Model>> saveObserver = storage.observe().test();
// Save something ..
BlogOwner juan = BlogOwner.builder().name("Juan Gonzales").build();
ModelSchema schema = schemaRegistry.getModelSchemaForModelClass(BlogOwner.class);
PendingMutation<BlogOwner> change = PendingMutation.creation(juan, schema);
PendingMutation.PersistentRecord thingWeSaved = converter.toRecord(change);
// Wait for it to save...
storage.save(thingWeSaved);
// Assert that our observer got the item;
// The observed change makes reference to an item. That referred item is our
// PendingMutation.PersistentRecord. It should be identical to the item that we saved.
assertEquals(thingWeSaved, saveObserver.awaitCount(1).values().get(0).item());
saveObserver.dispose();
}
use of com.amplifyframework.datastore.storage.StorageItemChange in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterSaveTest method patchItemOnlyHasChangedFields.
/**
* Verify that saving an item that already exists emits a StorageItemChange event with a patchItem that only
* contains the fields that are different.
*
* @throws AmplifyException On failure to obtain ModelSchema from model class.
* @throws InterruptedException If interrupted while awaiting terminal result in test observer
*/
@Test
public void patchItemOnlyHasChangedFields() throws AmplifyException, InterruptedException {
// Create a BlogOwner.
final BlogOwner johnSmith = BlogOwner.builder().name("John Smith").wea("ther").build();
adapter.save(johnSmith);
// Start observing for changes
TestObserver<StorageItemChange<? extends Model>> observer = adapter.observe().test();
// Update one field on the BlogOwner.
BlogOwner johnAdams = johnSmith.copyOfBuilder().name("John Adams").build();
adapter.save(johnAdams);
// Observe that the StorageItemChange contains an item with only the fields that changed (`id`, and `name`, but
// not `wea`)
Map<String, Object> serializedData = new HashMap<>();
serializedData.put("id", johnAdams.getId());
serializedData.put("name", "John Adams");
SerializedModel expectedItem = SerializedModel.builder().serializedData(serializedData).modelSchema(ModelSchema.fromModelClass(BlogOwner.class)).build();
observer.await(1, TimeUnit.SECONDS);
observer.assertValueCount(1);
observer.assertValueAt(0, storageItemChange -> storageItemChange.patchItem().equals(expectedItem));
}
Aggregations