use of com.amplifyframework.core.model.SerializedModel in project amplify-android by aws-amplify.
the class SelectionSetTest method nestedSerializedModel.
/**
* Test generating SelectionSet for nested ModelSchema using SerializedModel.
* @throws AmplifyException if a ModelSchema can't be derived from postSchema
*/
@Test
public void nestedSerializedModel() throws AmplifyException {
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
ModelField blogModelId = ModelField.builder().isRequired(true).targetType("ID").build();
ModelField blogName = ModelField.builder().isRequired(true).targetType("String").build();
Map<String, ModelField> blogFields = new HashMap<>();
blogFields.put("id", blogModelId);
blogFields.put("name", blogName);
ModelSchema blogSchema = ModelSchema.builder().name("Blog").pluralName("Blogs").modelClass(SerializedModel.class).fields(blogFields).build();
ModelField postModelId = ModelField.builder().isRequired(true).targetType("ID").build();
ModelField postTitle = ModelField.builder().isRequired(true).targetType("String").build();
ModelField postBlog = ModelField.builder().isRequired(true).targetType("Blog").isModel(true).build();
Map<String, ModelField> postFields = new HashMap<>();
postFields.put("id", postModelId);
postFields.put("title", postTitle);
postFields.put("blog", postBlog);
Map<String, ModelAssociation> associations = new HashMap<>();
associations.put("blog", ModelAssociation.builder().name("BelongsTo").targetName("blogId").associatedType("Blog").build());
ModelSchema postSchema = ModelSchema.builder().name("Post").pluralName("Posts").modelClass(SerializedModel.class).fields(postFields).associations(associations).build();
schemaRegistry.register("Blog", blogSchema);
schemaRegistry.register("Post", postSchema);
SelectionSet selectionSet = SelectionSet.builder().modelClass(SerializedModel.class).modelSchema(postSchema).operation(QueryType.SYNC).requestOptions(new JustIDGraphQLRequestOptions()).build();
assertEquals(Resources.readAsString("selection-set-post-nested.txt"), selectionSet.toString() + "\n");
}
use of com.amplifyframework.core.model.SerializedModel 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