Search in sources :

Example 71 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterQueryTest method querySavedDataWithMultiLevelJoins.

/**
 * Test that querying the saved item with a foreign key
 * also populates that instance variable with object.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithMultiLevelJoins() throws DataStoreException {
    final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
    final Blog blog = Blog.builder().name("Alan's Software Blog").owner(blogOwner).build();
    final Post post = Post.builder().title("Alan's first post").status(PostStatus.ACTIVE).rating(2).blog(blog).build();
    final Comment comment = Comment.builder().content("Alan's first comment").post(post).build();
    adapter.save(blogOwner);
    adapter.save(blog);
    adapter.save(post);
    adapter.save(comment);
    final List<Comment> comments = adapter.query(Comment.class);
    assertTrue(comments.contains(comment));
    assertEquals(comments.get(0).getPost(), post);
    assertEquals(comments.get(0).getPost().getBlog(), blog);
    assertEquals(comments.get(0).getPost().getBlog().getOwner(), blogOwner);
}
Also used : Comment(com.amplifyframework.testmodels.commentsblog.Comment) Post(com.amplifyframework.testmodels.commentsblog.Post) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 72 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterQueryTest method querySavedDataWithNumericalPredicates.

/**
 * Test querying the saved item in the SQLite database with
 * predicate conditions.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithNumericalPredicates() throws DataStoreException {
    final List<Post> savedModels = new ArrayList<>();
    final int numModels = 10;
    BlogOwner blogOwner = BlogOwner.builder().name("Test Dummy").build();
    adapter.save(blogOwner);
    Blog blog = Blog.builder().name("Blogging for Dummies").owner(blogOwner).build();
    adapter.save(blog);
    for (int counter = 0; counter < numModels; counter++) {
        final Post post = Post.builder().title("titlePrefix:" + counter).status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
        adapter.save(post);
        savedModels.add(post);
    }
    // 1, 4, 5, 6
    QueryPredicate predicate = Post.RATING.ge(4).and(Post.RATING.lt(7)).or(Post.RATING.eq(1).and(Post.RATING.ne(7)));
    assertEquals(Observable.fromArray(1, 4, 5, 6).map(savedModels::get).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(adapter.query(Post.class, Where.matches(predicate))).toList().map(HashSet::new).blockingGet());
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Post(com.amplifyframework.testmodels.commentsblog.Post) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 73 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterQueryTest method queryWithOrderByRelatedModel.

/**
 * Test query with order by.  Validate that a list of Blog can be sorted by the names of BlogOwners.
 * @throws DataStoreException On failure to arrange items into store, or from the query action itself
 */
@Test
public void queryWithOrderByRelatedModel() throws DataStoreException {
    // Expect: Create BlogOwners and their respective blogs
    List<String> names = Arrays.asList("Joe", "Bob", "Dan", "Jane");
    List<Blog> blogs = new ArrayList<>();
    for (String name : names) {
        BlogOwner owner = BlogOwner.builder().name(name).build();
        adapter.save(owner);
        Blog blog = Blog.builder().name("").owner(owner).build();
        adapter.save(blog);
        blogs.add(blog);
    }
    // Act: Query Blogs sorted by owner's name
    List<Blog> result = adapter.query(Blog.class, Where.sorted(BlogOwner.NAME.ascending()));
    // Verify: Query result is sorted by owner's name
    List<Blog> sorted = new ArrayList<>(blogs);
    Collections.sort(sorted, Comparator.comparing(blog -> blog.getOwner().getName()));
    assertEquals(sorted, result);
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Observable(io.reactivex.rxjava3.core.Observable) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) Before(org.junit.Before) Page(com.amplifyframework.core.model.query.Page) Phone(com.amplifyframework.testmodels.phonecall.Phone) Assert.assertNotNull(org.junit.Assert.assertNotNull) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Where(com.amplifyframework.core.model.query.Where) StrictMode(com.amplifyframework.datastore.StrictMode) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) Person(com.amplifyframework.testmodels.phonecall.Person) Temporal(com.amplifyframework.core.model.temporal.Temporal) Comparator(java.util.Comparator) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryField.field(com.amplifyframework.core.model.query.predicate.QueryField.field) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) Call(com.amplifyframework.testmodels.phonecall.Call) ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 74 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterQueryTest method querySavedDataWithMultipleItems.

/**
 * Test querying the saved item in the SQLite database.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithMultipleItems() throws DataStoreException {
    final List<BlogOwner> savedModels = new ArrayList<>();
    final int numModels = 10;
    for (int counter = 0; counter < numModels; counter++) {
        final BlogOwner blogOwner = BlogOwner.builder().name("namePrefix:" + counter).build();
        adapter.save(blogOwner);
        savedModels.add(blogOwner);
    }
    assertEquals(Observable.fromIterable(savedModels).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(adapter.query(BlogOwner.class)).toList().map(HashSet::new).blockingGet());
}
Also used : ArrayList(java.util.ArrayList) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 75 with BlogOwner

use of com.amplifyframework.testmodels.commentsblog.BlogOwner in project amplify-android by aws-amplify.

the class HybridAssociationSyncInstrumentationTest method associatedModelAreSyncedDownFromCloud.

/**
 * When the cloud sees an update to its data, the new data should be reflected in the
 * local store. What's more, we should be able to query for the updated data by its model names,
 * and expect to see the result, that way. This should hold for associated models, too.
 * @throws AmplifyException For a variety of reasons, including failure to build schema,
 *                          or bad interaction with API or DataStore
 */
@Ignore("It passes. Not automating due to operational concerns as noted in class-level @Ignore.")
@Test
public void associatedModelAreSyncedDownFromCloud() throws AmplifyException {
    // Create a BlogOwner on the remote system,
    // and wait for it to trickle back to the client.
    BlogOwner owner = BlogOwner.builder().name("Agent Texas").build();
    String ownerModelName = BlogOwner.class.getSimpleName();
    ModelSchema ownerSchema = schemaProvider.modelSchemas().get(ownerModelName);
    assertNotNull(ownerSchema);
    HubAccumulator ownerAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(owner.getId()), 1).start();
    appSync.create(owner, ownerSchema);
    ownerAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Now, validate that we see the data locally, when we query for serialized models
    // and by Java BlogOwners.
    Map<String, Object> expectedOwnerData = new HashMap<>();
    expectedOwnerData.put("id", owner.getId());
    expectedOwnerData.put("name", owner.getName());
    List<SerializedModel> actualSerializedOwners = hybridBehaviors.list(ownerSchema.getName());
    assertTrue(actualSerializedOwners.contains(SerializedModel.builder().serializedData(expectedOwnerData).modelSchema(ownerSchema).build()));
    assertTrue(normalBehaviors.list(BlogOwner.class).contains(owner));
    // Now, remotely save a model that has an association to the owner above.
    Blog blog = Blog.builder().name("Blog about Texas").owner(owner).build();
    String blogModelName = Blog.class.getSimpleName();
    ModelSchema blogSchema = schemaProvider.modelSchemas().get(blogModelName);
    assertNotNull(blogSchema);
    HubAccumulator blogAccumulator = HubAccumulator.create(HubChannel.DATASTORE, receiptOf(blog.getId()), 1).start();
    appSync.create(blog, blogSchema);
    blogAccumulator.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    // Validate that we can find the newly associated model locally, now.
    Map<String, Object> expectedBlogData = new HashMap<>();
    expectedBlogData.put("id", blog.getId());
    expectedBlogData.put("name", blog.getName());
    expectedBlogData.put("owner", SerializedModel.builder().serializedData(Collections.singletonMap("id", owner.getId())).modelSchema(null).build());
    List<SerializedModel> expectedSerializedBlogs = hybridBehaviors.list(blogSchema.getName());
    assertTrue(expectedSerializedBlogs.contains(SerializedModel.builder().serializedData(expectedBlogData).modelSchema(blogSchema).build()));
    assertTrue(normalBehaviors.list(Blog.class).contains(blog));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) HashMap(java.util.HashMap) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HubAccumulator(com.amplifyframework.testutils.HubAccumulator) SerializedModel(com.amplifyframework.core.model.SerializedModel) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)150 Test (org.junit.Test)146 DataStoreException (com.amplifyframework.datastore.DataStoreException)35 Blog (com.amplifyframework.testmodels.commentsblog.Blog)32 ModelSchema (com.amplifyframework.core.model.ModelSchema)31 Post (com.amplifyframework.testmodels.commentsblog.Post)31 ArrayList (java.util.ArrayList)29 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)25 Assert.assertEquals (org.junit.Assert.assertEquals)25 ModelMetadata (com.amplifyframework.datastore.appsync.ModelMetadata)24 Collections (java.util.Collections)24 HashSet (java.util.HashSet)23 List (java.util.List)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)22 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)22 Arrays (java.util.Arrays)22 TimeUnit (java.util.concurrent.TimeUnit)22 Consumer (com.amplifyframework.core.Consumer)21 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)21 Before (org.junit.Before)21