Search in sources :

Example 36 with SerializedModel

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");
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelAssociation(com.amplifyframework.core.model.ModelAssociation) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Example 37 with SerializedModel

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));
}
Also used : HashMap(java.util.HashMap) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SerializedModel(com.amplifyframework.core.model.SerializedModel) Test(org.junit.Test)

Aggregations

SerializedModel (com.amplifyframework.core.model.SerializedModel)37 Test (org.junit.Test)21 HashMap (java.util.HashMap)18 ModelSchema (com.amplifyframework.core.model.ModelSchema)16 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)8 AmplifyException (com.amplifyframework.AmplifyException)7 Model (com.amplifyframework.core.model.Model)7 DataStoreException (com.amplifyframework.datastore.DataStoreException)6 ModelField (com.amplifyframework.core.model.ModelField)5 ArrayList (java.util.ArrayList)5 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)4 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)4 SerializedCustomType (com.amplifyframework.core.model.SerializedCustomType)4 StorageItemChange (com.amplifyframework.datastore.storage.StorageItemChange)4 Blog (com.amplifyframework.testmodels.commentsblog.Blog)4 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)4 Map (java.util.Map)4 Ignore (org.junit.Ignore)4 Temporal (com.amplifyframework.core.model.temporal.Temporal)3 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)3