Search in sources :

Example 21 with Blog

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

the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithPredicatesOnForeignKey.

/**
 * Test querying with predicate condition on connected model.
 *
 * @throws DataStoreException   On unexpected failure manipulating items in/out of DataStore
 * @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithPredicatesOnForeignKey() throws DataStoreException, InterruptedException {
    final BlogOwner blogOwner = BlogOwner.builder().name("Jane Doe").build();
    adapter.save(blogOwner);
    final Blog blog = Blog.builder().name("Jane's Commercial Real Estate Blog").owner(blogOwner).build();
    adapter.save(blog);
    CountDownLatch latch = new CountDownLatch(1);
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreQuerySnapshot<Blog>> onQuerySnapshot = value -> {
        assertTrue(value.getItems().contains(blog));
        latch.countDown();
    };
    Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
    Action onObservationComplete = NoOpAction.create();
    adapter.observeQuery(Blog.class, new ObserveQueryOptions(BlogOwner.NAME.eq("Jane Doe"), null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) CountDownLatch(java.util.concurrent.CountDownLatch) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Example 22 with Blog

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

the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithForeignKey.

/**
 * Test that querying the saved item with a foreign key with observeQuery
 * also populates that instance variable with object.
 *
 * @throws DataStoreException   On unexpected failure manipulating items in/out of DataStore
 * @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithForeignKey() throws DataStoreException, InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
    final Blog blog = Blog.builder().name("Alan's Software Blog").owner(blogOwner).build();
    adapter.save(blogOwner);
    adapter.save(blog);
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreQuerySnapshot<Blog>> onQuerySnapshot = value -> {
        assertTrue(value.getItems().contains(blog));
        latch.countDown();
    };
    Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
    Action onObservationComplete = NoOpAction.create();
    adapter.observeQuery(Blog.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) Blog(com.amplifyframework.testmodels.commentsblog.Blog) ArrayList(java.util.ArrayList) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) HashSet(java.util.HashSet) Consumer(com.amplifyframework.core.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) PostStatus(com.amplifyframework.testmodels.commentsblog.PostStatus) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Before(org.junit.Before) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Comment(com.amplifyframework.testmodels.commentsblog.Comment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Action(com.amplifyframework.core.Action) StrictMode(com.amplifyframework.datastore.StrictMode) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) Assert.assertFalse(org.junit.Assert.assertFalse) QuerySortBy(com.amplifyframework.core.model.query.QuerySortBy) Comparator(java.util.Comparator) NoOpAction(com.amplifyframework.core.NoOpAction) AmplifyModelProvider(com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider) Post(com.amplifyframework.testmodels.commentsblog.Post) Collections(java.util.Collections) QueryPredicate.not(com.amplifyframework.core.model.query.predicate.QueryPredicate.not) Assert.assertEquals(org.junit.Assert.assertEquals) DataStoreException(com.amplifyframework.datastore.DataStoreException) Action(com.amplifyframework.core.Action) NoOpAction(com.amplifyframework.core.NoOpAction) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) CountDownLatch(java.util.concurrent.CountDownLatch) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Example 23 with Blog

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

the class SQLiteStorageAdapterQueryTest method querySavedDataWithPredicatesOnForeignKey.

/**
 * Test querying with predicate condition on connected model.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithPredicatesOnForeignKey() throws DataStoreException {
    final BlogOwner blogOwner = BlogOwner.builder().name("Jane Doe").build();
    adapter.save(blogOwner);
    final Blog blog = Blog.builder().name("Jane's Commercial Real Estate Blog").owner(blogOwner).build();
    adapter.save(blog);
    final List<Blog> blogsOwnedByJaneDoe = adapter.query(Blog.class, Where.matches(BlogOwner.NAME.eq("Jane Doe")));
    assertTrue(blogsOwnedByJaneDoe.contains(blog));
}
Also used : BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 24 with Blog

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

the class SQLiteStorageAdapterQueryTest method queryFieldsAreBackwardsCompatible.

/**
 * Test that new QueryField with explicit model name produces the same result as old QueryField.
 * @throws DataStoreException On failure to arrange items into store, or from the query action itself
 */
@Test
public void queryFieldsAreBackwardsCompatible() throws DataStoreException {
    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);
    final int numModels = 10;
    for (int counter = 0; counter < numModels; counter++) {
        final Post post = Post.builder().title("title " + counter).status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
        adapter.save(post);
    }
    // Assert that using QueryField without model name yields same results if there is no column ambiguity
    assertEquals(adapter.query(Post.class, Where.matches(field("Post", "title").contains("4"))), adapter.query(Post.class, Where.matches(field("title").contains("4"))));
    assertEquals(adapter.query(Post.class, Where.matches(field("Post", "rating").gt(3))), adapter.query(Post.class, Where.matches(field("rating").gt(3))));
}
Also used : Post(com.amplifyframework.testmodels.commentsblog.Post) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Blog(com.amplifyframework.testmodels.commentsblog.Blog) Test(org.junit.Test)

Example 25 with Blog

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

the class SQLiteStorageAdapterQueryTest method querySavedDataWithForeignKey.

/**
 * 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 querySavedDataWithForeignKey() throws DataStoreException {
    final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
    final Blog blog = Blog.builder().name("Alan's Software Blog").owner(blogOwner).build();
    adapter.save(blogOwner);
    adapter.save(blog);
    final List<Blog> blogs = adapter.query(Blog.class);
    assertTrue(blogs.contains(blog));
}
Also used : 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