Search in sources :

Example 16 with Blog

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));
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 17 with Blog

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);
}
Also used : HashMap(java.util.HashMap) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 18 with Blog

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());
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) Test(org.junit.Test)

Example 19 with Blog

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());
}
Also used : BeforeClass(org.junit.BeforeClass) Blog(com.amplifyframework.testmodels.commentsblog.Blog) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StrictMode(com.amplifyframework.datastore.StrictMode) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) TestObserver(io.reactivex.rxjava3.observers.TestObserver) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Post(com.amplifyframework.testmodels.commentsblog.Post) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with Blog

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());
}
Also used : BeforeClass(org.junit.BeforeClass) Blog(com.amplifyframework.testmodels.commentsblog.Blog) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Model(com.amplifyframework.core.model.Model) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StrictMode(com.amplifyframework.datastore.StrictMode) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) TestObserver(io.reactivex.rxjava3.observers.TestObserver) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Aggregations

Blog (com.amplifyframework.testmodels.commentsblog.Blog)30 Test (org.junit.Test)30 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)26 Post (com.amplifyframework.testmodels.commentsblog.Post)16 HashSet (java.util.HashSet)12 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)11 DataStoreException (com.amplifyframework.datastore.DataStoreException)10 StrictMode (com.amplifyframework.datastore.StrictMode)10 SynchronousStorageAdapter (com.amplifyframework.datastore.storage.SynchronousStorageAdapter)10 AmplifyModelProvider (com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider)10 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)10 Collections (java.util.Collections)10 List (java.util.List)10 After (org.junit.After)10 Assert.assertEquals (org.junit.Assert.assertEquals)10 Assert.assertTrue (org.junit.Assert.assertTrue)10 Before (org.junit.Before)10 BeforeClass (org.junit.BeforeClass)10 Comment (com.amplifyframework.testmodels.commentsblog.Comment)9 ArrayList (java.util.ArrayList)9