use of com.amplifyframework.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class RxDataStoreBindingTest method observeQueryCompletesWhenCategoryBehaviorDoes.
/**
* The Rx binding for the DataStore's observeQuery method is an Observable. It should
* complete when the Rx binding's completion callback is triggered.
* @throws InterruptedException If interrupted while test observer is awaiting terminal event
*/
@Test
public void observeQueryCompletesWhenCategoryBehaviorDoes() throws InterruptedException {
// Category behavior is arranged to complete
doAnswer(invocation -> {
// 0 = clazz, 1 = options, 2 = start consumer, 3 = item consumer, 4 = failure consumer, 5 = onComplete
final int positionOfOnStart = 3;
Consumer<Cancelable> onStart = invocation.getArgument(positionOfOnStart);
onStart.accept(new NoOpCancelable());
final int positionOfOnComplete = 5;
Action onComplete = invocation.getArgument(positionOfOnComplete);
onComplete.call();
// "void"
return null;
}).when(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
// Act: observe via Rx binding
TestObserver<DataStoreQuerySnapshot<Model>> observer = rxDataStore.observeQuery(Model.class, new ObserveQueryOptions()).test();
observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
observer.assertComplete();
verify(delegate).observeQuery(eq(Model.class), any(), anyConsumer(), anyConsumer(), anyConsumer(), anyAction());
}
use of com.amplifyframework.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest 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
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
@Test
public void querySavedDataWithNumericalPredicates() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(1);
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)));
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
AtomicInteger count = new AtomicInteger(0);
Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value -> {
if (count.get() == 0) {
List<Post> expected = Arrays.asList(savedModels.get(1), savedModels.get(4), savedModels.get(5), savedModels.get(6));
assertEquals(new HashSet<>(expected), new HashSet<>(value.getItems()));
latch.countDown();
} else if (count.get() == 2) {
assertEquals(5, value.getItems().size());
assertTrue(value.getItems().contains(savedModels.get(1)));
assertTrue(value.getItems().contains(savedModels.get(4)));
assertTrue(value.getItems().contains(savedModels.get(5)));
assertTrue(value.getItems().contains(savedModels.get(6)));
assertTrue(value.getItems().contains(savedModels.get(11)));
changeLatch.countDown();
}
count.incrementAndGet();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(Post.class, new ObserveQueryOptions(predicate, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(15, TimeUnit.SECONDS));
for (int counter = 3; counter < 5; counter++) {
final Post post = Post.builder().title("titlePrefix:" + counter + "change").status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
adapter.save(post);
savedModels.add(post);
}
assertTrue(changeLatch.await(30, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest 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
* @throws InterruptedException On unexpected failure manipulating items in/out of DataStore
*/
// @Test
public void querySavedDataWithMultiLevelJoins() 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();
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);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<Comment>> onQuerySnapshot = value -> {
assertTrue(value.getItems().contains(comment));
assertEquals(value.getItems().get(0).getPost(), post);
assertEquals(value.getItems().get(0).getPost().getBlog(), blog);
assertEquals(value.getItems().get(0).getPost().getBlog().getOwner(), blogOwner);
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(Comment.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithMultipleItemsThenUpdateOnItemSoItNoLongerSatisfiesPredicate.
/**
* Test querying the saved item in the SQLite database with observeQuery.
*
* @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 querySavedDataWithMultipleItemsThenUpdateOnItemSoItNoLongerSatisfiesPredicate() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch changeLatch = new CountDownLatch(1);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
final List<BlogOwner> savedModels = new ArrayList<>();
final int numModels = 5;
// Blogowner to be updated
final BlogOwner blogOwnerToBeUpdated = BlogOwner.builder().name("namePrefix:").build();
adapter.save(blogOwnerToBeUpdated);
savedModels.add(blogOwnerToBeUpdated);
AtomicInteger count = new AtomicInteger(0);
for (int counter = 1; counter < numModels; counter++) {
final BlogOwner blogOwner = BlogOwner.builder().name("namePrefix:" + counter).build();
adapter.save(blogOwner);
savedModels.add(blogOwner);
}
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
if (count.get() == 0) {
for (BlogOwner blogOwner : savedModels) {
assertTrue(value.getItems().contains(blogOwner));
}
assertEquals(5, value.getItems().size());
latch.countDown();
} else {
assertEquals(4, value.getItems().size());
changeLatch.countDown();
}
count.incrementAndGet();
};
adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(BlogOwner.NAME.beginsWith("namePrefix"), null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(30, TimeUnit.SECONDS));
final BlogOwner blogOwnerUpdated = blogOwnerToBeUpdated.copyOfBuilder().name("updated").build();
try {
adapter.save(blogOwnerUpdated);
} catch (DataStoreException exception) {
exception.printStackTrace();
}
savedModels.add(blogOwnerUpdated);
assertTrue(changeLatch.await(30, TimeUnit.SECONDS));
}
use of com.amplifyframework.core.model.query.ObserveQueryOptions in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithStringPredicates.
/**
* Test querying the saved item in the SQLite database with
* predicate conditions.
*
* @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 querySavedDataWithStringPredicates() throws DataStoreException, InterruptedException {
final List<Post> savedModels = new ArrayList<>();
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(counter + "-title").status(PostStatus.INACTIVE).rating(counter).blog(blog).build();
adapter.save(post);
savedModels.add(post);
}
CountDownLatch latch = new CountDownLatch(1);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<Post>> onQuerySnapshot = value -> {
assertTrue(value.getItems().contains(savedModels.get(4)));
assertTrue(value.getItems().contains(savedModels.get(7)));
assertFalse(value.getItems().contains(savedModels.get(9)));
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(Post.class, new ObserveQueryOptions(Post.TITLE.beginsWith("4").or(Post.TITLE.beginsWith("7")).or(Post.TITLE.beginsWith("9")).and(not(Post.TITLE.gt(8))), null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(15, TimeUnit.SECONDS));
}
Aggregations