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());
}
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);
}
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));
}
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());
}
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));
}
Aggregations