Search in sources :

Example 31 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method clearFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's clear() method will propagate failures
 * faithfully from the underlying delegate.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void clearFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange a failure in the category behavior
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = onComplete, 1 = onFailure
        final int positionOfOnFailure = 1;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).clear(anyAction(), anyConsumer());
    // Act: clear the store.
    TestObserver<Void> observer = rxDataStore.clear().test();
    // Assert: failure propagates through binding.
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) Test(org.junit.Test)

Example 32 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method queryEmitsFailureWhenCategoryBehaviorDoes.

/**
 * When the DataStore emits a failure for a query, the Rx binding should terminate
 * with that failure.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void queryEmitsFailureWhenCategoryBehaviorDoes() throws InterruptedException {
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        final int positionOrFailureConsumer = 2;
        Consumer<DataStoreException> failureConsumer = invocation.getArgument(positionOrFailureConsumer);
        failureConsumer.accept(expectedFailure);
        return null;
    }).when(delegate).query(eq(Model.class), anyConsumer(), anyConsumer());
    TestObserver<Model> observer = rxDataStore.query(Model.class).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
    verify(delegate).query(eq(Model.class), anyConsumer(), anyConsumer());
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) RandomModel(com.amplifyframework.testutils.random.RandomModel) Model(com.amplifyframework.core.model.Model) Test(org.junit.Test)

Example 33 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method startFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's start() method will propagate failures
 * faithfully from the underlying delegate.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void startFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange a failure in the category behavior
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = onComplete, 1 = onFailure
        final int positionOfOnFailure = 1;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).start(anyAction(), anyConsumer());
    // Act: start the store.
    TestObserver<Void> observer = rxDataStore.start().test();
    // Assert: failure propagates through binding.
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) Test(org.junit.Test)

Example 34 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class RxDataStoreBindingTest method stopFailsWhenCategoryBehaviorDoes.

/**
 * The Rx binding for the DataStore's stop() method will propagate failures
 * faithfully from the underlying delegate.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void stopFailsWhenCategoryBehaviorDoes() throws InterruptedException {
    // Arrange a failure in the category behavior
    DataStoreException expectedFailure = new DataStoreException("Expected", "Failure");
    doAnswer(invocation -> {
        // 0 = onComplete, 1 = onFailure
        final int positionOfOnFailure = 1;
        Consumer<DataStoreException> onFailure = invocation.getArgument(positionOfOnFailure);
        onFailure.accept(expectedFailure);
        // "void"
        return null;
    }).when(delegate).stop(anyAction(), anyConsumer());
    // Act: stop the store.
    TestObserver<Void> observer = rxDataStore.stop().test();
    // Assert: failure propagates through binding.
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertError(expectedFailure);
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) Test(org.junit.Test)

Example 35 with DataStoreException

use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterDeleteTest method deleteModelCascades.

/**
 * Assert that delete deletes item in the SQLite database without
 * violating foreign key constraints.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void deleteModelCascades() 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);
    expected.add(ownerModel.getId());
    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 " + 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.
    // Deletes every saved model to prevent foreign key constraint violation
    adapter.delete(ownerModel);
    // Assert that cascaded deletions are observed.
    deleteObserver.assertValueCount(13);
    assertEquals(expected, new HashSet<>(deleteObserver.values()));
    // Get data from the database and assert that everything is deleted.
    assertTrue(adapter.query(BlogOwner.class).isEmpty());
    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)

Aggregations

DataStoreException (com.amplifyframework.datastore.DataStoreException)89 Test (org.junit.Test)52 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)36 Consumer (com.amplifyframework.core.Consumer)32 List (java.util.List)32 Cancelable (com.amplifyframework.core.async.Cancelable)31 Model (com.amplifyframework.core.model.Model)31 ArrayList (java.util.ArrayList)31 AmplifyException (com.amplifyframework.AmplifyException)29 ModelSchema (com.amplifyframework.core.model.ModelSchema)28 Collections (java.util.Collections)28 Action (com.amplifyframework.core.Action)27 QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)27 TimeUnit (java.util.concurrent.TimeUnit)25 Post (com.amplifyframework.testmodels.commentsblog.Post)23 PostStatus (com.amplifyframework.testmodels.commentsblog.PostStatus)23 Arrays (java.util.Arrays)23 Assert.assertEquals (org.junit.Assert.assertEquals)23 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)22 DataStoreQuerySnapshot (com.amplifyframework.datastore.DataStoreQuerySnapshot)21