Search in sources :

Example 21 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterSaveTest method saveModelWithPredicateUpdatesConditionally.

/**
 * Test save with predicate. Conditional write is useful for making sure that
 * no data is overwritten with outdated assumptions.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void saveModelWithPredicateUpdatesConditionally() throws DataStoreException {
    final BlogOwner john = BlogOwner.builder().name("John").build();
    final BlogOwner jane = BlogOwner.builder().name("Jane").build();
    final BlogOwner mark = BlogOwner.builder().name("Mark").build();
    adapter.save(john);
    adapter.save(jane);
    adapter.save(mark);
    // Only update John and Jane
    final QueryPredicate predicate = BlogOwner.NAME.beginsWith("J");
    final BlogOwner newJohn = john.copyOfBuilder().name("John Doe").build();
    final BlogOwner newJane = jane.copyOfBuilder().name("Jane Doe").build();
    final BlogOwner newMark = mark.copyOfBuilder().name("Mark Doe").build();
    adapter.save(newJohn, predicate);
    adapter.save(newJane, predicate);
    // noinspection ThrowableNotThrown
    // Should not update
    adapter.saveExpectingError(newMark, predicate);
    assertEquals(Observable.fromArray(newJohn, newJane, mark).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(adapter.query(BlogOwner.class)).toList().map(HashSet::new).blockingGet());
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterDeleteTest method deleteModelWithPredicateDeletesConditionally.

/**
 * Test delete with predicate. Conditional delete is useful for making sure that
 * no data is removed with outdated assumptions.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void deleteModelWithPredicateDeletesConditionally() throws DataStoreException {
    final BlogOwner john = BlogOwner.builder().name("John").build();
    final BlogOwner jane = BlogOwner.builder().name("Jane").build();
    final BlogOwner mark = BlogOwner.builder().name("Mark").build();
    adapter.save(john);
    adapter.save(jane);
    adapter.save(mark);
    // Delete everybody but Mark
    final QueryPredicate predicate = BlogOwner.NAME.ne(mark.getName());
    adapter.delete(john, predicate);
    adapter.delete(jane, predicate);
    // noinspection ThrowableNotThrown This is the point of this method.
    // Should not be deleted
    adapter.deleteExpectingError(mark, predicate);
    List<BlogOwner> blogOwners = adapter.query(BlogOwner.class);
    assertEquals(Collections.singletonList(mark), blogOwners);
}
Also used : QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Example 23 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterObserveQueryTest method queryWithMaliciousPredicates.

/**
 * Test query with SQL injection.
 *
 * @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 queryWithMaliciousPredicates() throws DataStoreException, InterruptedException {
    final BlogOwner jane = BlogOwner.builder().name("Jane Doe").build();
    adapter.save(jane);
    QueryPredicate predicate = BlogOwner.NAME.eq("Jane; DROP TABLE Person; --");
    CountDownLatch latch = new CountDownLatch(2);
    Consumer<Cancelable> observationStarted = NoOpConsumer.create();
    Consumer<DataStoreQuerySnapshot<BlogOwner>> onMaliciousQuerySnapshot = resultOfMaliciousQuery -> {
        assertTrue(resultOfMaliciousQuery.getItems().isEmpty());
        latch.countDown();
    };
    Consumer<DataStoreException> onObservationError = value -> {
    };
    Action onObservationComplete = NoOpAction.create();
    adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(predicate, null), observationStarted, onMaliciousQuerySnapshot, onObservationError, onObservationComplete);
    Consumer<DataStoreQuerySnapshot<BlogOwner>> onAfterMaliciousQuery = resultAfterMaliciousQuery -> {
        assertTrue(resultAfterMaliciousQuery.getItems().contains(jane));
        latch.countDown();
    };
    adapter.observeQuery(BlogOwner.class, new ObserveQueryOptions(null, null), observationStarted, onAfterMaliciousQuery, 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) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) ObserveQueryOptions(com.amplifyframework.core.model.query.ObserveQueryOptions) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) CountDownLatch(java.util.concurrent.CountDownLatch) Cancelable(com.amplifyframework.core.async.Cancelable) DataStoreQuerySnapshot(com.amplifyframework.datastore.DataStoreQuerySnapshot) Test(org.junit.Test)

Example 24 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLiteStorageAdapterQueryTest method querySavedDataWithTimePredicates.

/**
 * Test querying the saved item in the SQLite database with Time
 * predicate conditions.
 * @throws DataStoreException On unexpected failure manipulating items in/out of DataStore
 */
@Test
public void querySavedDataWithTimePredicates() throws DataStoreException {
    setupForCallModel();
    final Person personCalling = Person.builder().name("Alan Turing").build();
    final Person personCalled = Person.builder().name("Grace Hopper").build();
    final Phone phoneCalling = Phone.builder().number("123-456-7890").ownedBy(personCalling).build();
    final Phone phoneCalled = Phone.builder().number("567-890-1234").ownedBy(personCalled).build();
    adapter.save(personCalling);
    adapter.save(personCalled);
    adapter.save(phoneCalling);
    adapter.save(phoneCalled);
    final List<Call> savedModels = new ArrayList<>();
    final int numModels = 8;
    final List<Temporal.Time> callStartTimes = Arrays.asList(new Temporal.Time("19:30:45.000000000"), new Temporal.Time("19:30:45.100000000Z"), new Temporal.Time("19:30:45.100250000Z"), new Temporal.Time("19:30:45.1000Z"), new Temporal.Time("20:30:45.111Z"), new Temporal.Time("19:30:45.111+00:00"), new Temporal.Time("19:30:45.111+01:00"), new Temporal.Time("19:30:45.111222333Z"));
    for (int counter = 0; counter < numModels; counter++) {
        Call phoneCall = Call.builder().startTime(callStartTimes.get(counter)).caller(phoneCalling).callee(phoneCalled).build();
        adapter.save(phoneCall);
        savedModels.add(phoneCall);
    }
    // 0, 1, 3, 6
    QueryPredicate predicate = Call.STARTTIME.le(new Temporal.Time("19:30:45.100000000Z"));
    assertEquals(Observable.fromArray(0, 1, 3, 6).map(savedModels::get).map(Call::getId).toList().map(HashSet::new).blockingGet(), Observable.fromIterable(adapter.query(Call.class, Where.matches(predicate))).map(Call::getId).toList().map(HashSet::new).blockingGet());
}
Also used : Call(com.amplifyframework.testmodels.phonecall.Call) Temporal(com.amplifyframework.core.model.temporal.Temporal) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) Phone(com.amplifyframework.testmodels.phonecall.Phone) ArrayList(java.util.ArrayList) Person(com.amplifyframework.testmodels.phonecall.Person) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with QueryPredicate

use of com.amplifyframework.core.model.query.predicate.QueryPredicate in project amplify-android by aws-amplify.

the class SQLCommandProcessorTest method executeExistsReturnsTrueWhenItemExists.

/**
 * Create and insert a BlogOwner, and then verify that executeExists return true.
 * @throws AmplifyException on failure to create ModelSchema from class.
 */
@Test
public void executeExistsReturnsTrueWhenItemExists() throws AmplifyException {
    // Insert a BlogOwner
    ModelSchema blogOwnerSchema = ModelSchema.fromModelClass(BlogOwner.class);
    BlogOwner abigailMcGregor = BlogOwner.builder().name("Abigail McGregor").build();
    sqlCommandProcessor.execute(sqlCommandFactory.insertFor(blogOwnerSchema, abigailMcGregor));
    // Check that the BlogOwner exists
    QueryPredicate predicate = BlogOwner.ID.eq(abigailMcGregor.getId());
    SqlCommand existsCommand = sqlCommandFactory.existsFor(blogOwnerSchema, predicate);
    assertTrue(sqlCommandProcessor.executeExists(existsCommand));
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) Test(org.junit.Test)

Aggregations

QueryPredicate (com.amplifyframework.core.model.query.predicate.QueryPredicate)31 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)12 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)11 ModelSchema (com.amplifyframework.core.model.ModelSchema)9 DataStoreException (com.amplifyframework.datastore.DataStoreException)9 HashSet (java.util.HashSet)8 List (java.util.List)7 NonNull (androidx.annotation.NonNull)6 SQLPredicate (com.amplifyframework.datastore.storage.sqlite.adapter.SQLPredicate)6 Consumer (com.amplifyframework.core.Consumer)5 Cancelable (com.amplifyframework.core.async.Cancelable)5 Model (com.amplifyframework.core.model.Model)5 SerializedModel (com.amplifyframework.core.model.SerializedModel)5 SQLiteTable (com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)5 Action (com.amplifyframework.core.Action)4 TimeUnit (java.util.concurrent.TimeUnit)4 AmplifyException (com.amplifyframework.AmplifyException)3 Amplify (com.amplifyframework.core.Amplify)3 ModelProvider (com.amplifyframework.core.model.ModelProvider)3