use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterSaveTest method saveModelWithPredicateUpdatesForNestedModels.
/**
* Test save with predicate. Confirms that conditionally updating a nested model also works.
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void saveModelWithPredicateUpdatesForNestedModels() throws DataStoreException {
// Save a model
final BlogOwner mark = BlogOwner.builder().name("Mark").build();
adapter.save(mark);
// Save a model that belongs to another model
final Blog marksBlog = Blog.builder().name("Mark's very first blog.").owner(mark).build();
adapter.save(marksBlog);
// Update a model that belongs to another model
final Blog marksBlogEdit = marksBlog.copyOfBuilder().name("Mark's edited blog.").build();
adapter.save(marksBlogEdit);
// Assert that update went through successfully
assertEquals(Collections.singletonList(marksBlogEdit), adapter.query(Blog.class));
}
use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.
the class ModelConverterTest method toMapForModelWithChildrenReturnsExpectedMap.
/**
* Verify that a Java model with children converted to a Map returns the expected value.
* @throws AmplifyException On failure to derive ModelSchema
*/
@Test
public void toMapForModelWithChildrenReturnsExpectedMap() throws AmplifyException {
Blog blog = Blog.builder().name("A neat blog").owner(BlogOwner.builder().name("Joe Swanson").build()).build();
ModelSchema schema = ModelSchema.fromModelClass(Blog.class);
Map<String, Object> actual = ModelConverter.toMap(blog, schema);
Map<String, Object> expected = new HashMap<>();
expected.put("id", blog.getId());
expected.put("name", "A neat blog");
expected.put("createdAt", null);
expected.put("owner", SerializedModel.builder().serializedData(Collections.singletonMap("id", blog.getOwner().getId())).modelSchema(null).build());
assertEquals(expected, actual);
}
use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.
the class MergerTest method orphanedItemIsNotMerged.
/**
* Assume item A is dependent on item B, but the remote store has an
* orphaned item A without item B. Then, we try to merge a save for a
* item A. This should gracefully fail, with A not being in the local
* store, at the end.
* @throws DataStoreException On failure to query results for assertions
* @throws InterruptedException If interrupted while awaiting terminal result in test observer
*/
@Test
public void orphanedItemIsNotMerged() throws DataStoreException, InterruptedException {
// Arrange: an item and its parent are not in the local store
BlogOwner badOwner = BlogOwner.builder().name("Raphael").build();
Blog orphanedBlog = Blog.builder().name("How Not To Save Blogs").owner(badOwner).build();
ModelMetadata metadata = new ModelMetadata(orphanedBlog.getId(), false, 1, Temporal.Timestamp.now());
// Enforce foreign key constraint on in-memory storage adapter
doThrow(SQLiteConstraintException.class).when(inMemoryStorageAdapter).save(eq(orphanedBlog), any(), any(), any(), any());
// Act: merge a creation for an item
TestObserver<Void> observer = merger.merge(new ModelWithMetadata<>(orphanedBlog, metadata)).test();
assertTrue(observer.await(REASONABLE_WAIT_TIME, TimeUnit.MILLISECONDS));
observer.assertNoErrors().assertComplete();
// Assert: orphaned model was not merged locally
final List<Blog> blogsInStorage = storageAdapter.query(Blog.class);
assertTrue(blogsInStorage.isEmpty());
}
use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterDeleteTest method deleteModelTypeWithPredicateCascades.
/**
* Assert that delete model type with predicate deletes items in
* the SQLite database without violating foreign key constraints.
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void deleteModelTypeWithPredicateCascades() throws DataStoreException {
// Create 1 blog owner, which has 3 blogs each, which has 3 posts each.
// Insert 1 blog owner, 3 blogs, 9 posts
Set<String> expected = new HashSet<>();
BlogOwner ownerModel = BlogOwner.builder().name("Blog Owner 1").build();
adapter.save(ownerModel);
for (int blog = 1; blog <= 3; blog++) {
Blog blogModel = Blog.builder().name("Blog " + blog).owner(ownerModel).build();
adapter.save(blogModel);
expected.add(blogModel.getId());
for (int post = 1; post <= 3; post++) {
Post postModel = Post.builder().title("Post " + blog + "-" + post).status(PostStatus.INACTIVE).rating(5).blog(blogModel).build();
adapter.save(postModel);
expected.add(postModel.getId());
}
}
// Observe deletions
TestObserver<String> deleteObserver = adapter.observe().filter(change -> StorageItemChange.Type.DELETE.equals(change.type())).map(StorageItemChange::item).map(Model::getId).test();
// Triggers a delete of all blogs.
// All posts will be deleted by cascade.
adapter.delete(Blog.class, QueryPredicates.all());
// Assert 3 blogs and 9 posts are deleted.
deleteObserver.assertValueCount(12);
assertEquals(expected, new HashSet<>(deleteObserver.values()));
// Get the BlogOwner from the database. Should not have been deleted.
final List<BlogOwner> blogOwners = adapter.query(BlogOwner.class);
assertEquals(Collections.singletonList(ownerModel), blogOwners);
// Get the Blogs and Posts from the database. Should be deleted.
assertTrue(adapter.query(Blog.class).isEmpty());
assertTrue(adapter.query(Post.class).isEmpty());
}
use of com.amplifyframework.testmodels.commentsblog.Blog in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterDeleteTest method deleteModelWithManyChildrenSucceeds.
/**
* Test deleting item with many children does not trigger SQLite exception.
* SQLite places a hard limit of 1000 on expression tree depth, so chaining too many
* predicates can potentially trigger an error.
* @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void deleteModelWithManyChildrenSucceeds() throws DataStoreException {
// Create model with more than 1000 child models
BlogOwner ownerModel = BlogOwner.builder().name("BlogOwner").build();
adapter.save(ownerModel);
for (int blog = 0; blog < 1001; blog++) {
Blog blogModel = Blog.builder().name("Blog " + blog).owner(ownerModel).build();
adapter.save(blogModel);
}
// Observe deletions
TestObserver<String> deleteObserver = adapter.observe().filter(change -> StorageItemChange.Type.DELETE.equals(change.type())).map(StorageItemChange::item).map(Model::getId).test();
// Delete parent model
adapter.delete(ownerModel);
// Assert 1 blog owner and 1001 blogs are deleted.
deleteObserver.assertValueCount(1002);
// Assert the local deletion happened.
assertTrue(adapter.query(BlogOwner.class).isEmpty());
assertTrue(adapter.query(Blog.class).isEmpty());
}
Aggregations