use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SyncStatusTest method syncStatusGetReturnNotSyncedStatus.
/**
* Get returns false for sync status.
*/
@Test
public void syncStatusGetReturnNotSyncedStatus() {
final LastSyncMetadata lastSyncMetadata = LastSyncMetadata.baseSyncedAt(BlogOwner.class.getName(), Time.now());
List<LastSyncMetadata> resultList = new ArrayList<>();
resultList.add(lastSyncMetadata);
Consumer<DataStoreException> onObservationError = value -> {
};
SqlQueryProcessor mockSqlQueryProcessor = mock(SqlQueryProcessor.class);
when(mockSqlQueryProcessor.queryOfflineData(eq(LastSyncMetadata.class), any(), any())).thenReturn(resultList);
DataStoreConfiguration mockDataStoreConfig = mock(DataStoreConfiguration.class);
when(mockDataStoreConfig.getSyncIntervalInMinutes()).thenReturn(0L);
SyncStatus subject = new SyncStatus(mockSqlQueryProcessor, mockDataStoreConfig);
boolean result = subject.get(LastSyncMetadata.class.getName(), onObservationError);
Assert.assertFalse(result);
}
use of com.amplifyframework.datastore.DataStoreException 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());
}
use of com.amplifyframework.datastore.DataStoreException 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());
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method querySavedDataWithSingleItem.
/**
* 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 querySavedDataWithSingleItem() throws DataStoreException, InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
final BlogOwner blogOwner = BlogOwner.builder().name("Alan Turing").build();
adapter.save(blogOwner);
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
assertTrue(value.getItems().contains(blogOwner));
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SQLiteStorageAdapterObserveQueryTest method observeQueryWithOrderBy.
/**
* Test observe query with order by.
* Validate that a list of BlogOwners can be sorted first by name in descending order,
* then by wea in ascending order.
*
* @throws DataStoreException On failure to arrange items into store, or from the
* query action itself
* @throws InterruptedException interruptedException.
*/
@Test
public void observeQueryWithOrderBy() throws DataStoreException, InterruptedException {
// Expect
List<String> names = Arrays.asList("Joe", "Joe", "Joe", "Bob", "Bob", "Bob", "Dan", "Dan", "Dan");
List<String> weas = Arrays.asList("pon", "lth", "ver", "kly", "ken", "sel", "ner", "rer", "ned");
List<BlogOwner> owners = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(1);
for (int i = 0; i < names.size(); i++) {
BlogOwner owner = BlogOwner.builder().name(names.get(i)).wea(weas.get(i)).build();
adapter.save(owner);
owners.add(owner);
}
List<BlogOwner> sorted = new ArrayList<>(owners);
Collections.sort(sorted, Comparator.comparing(BlogOwner::getName).reversed().thenComparing(BlogOwner::getWea));
Consumer<Cancelable> observationStarted = NoOpConsumer.create();
Consumer<DataStoreQuerySnapshot<BlogOwner>> onQuerySnapshot = value -> {
assertEquals(sorted, value.getItems());
latch.countDown();
};
Consumer<DataStoreException> onObservationError = NoOpConsumer.create();
Action onObservationComplete = NoOpAction.create();
List<QuerySortBy> sortBy = new ArrayList<>();
sortBy.add(BlogOwner.NAME.descending());
sortBy.add(BlogOwner.WEA.ascending());
// Act
adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, sortBy), observationStarted, onQuerySnapshot, onObservationError, onObservationComplete);
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Aggregations