Search in sources :

Example 6 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method keyFilterExample_returnsMatchingEntities.

@Test
public void keyFilterExample_returnsMatchingEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Person", "a");
    Entity b = new Entity("Person", "b");
    Entity c = new Entity("Person", "c");
    Entity aa = new Entity("Person", "aa", b.getKey());
    Entity bb = new Entity("Person", "bb", b.getKey());
    Entity aaa = new Entity("Person", "aaa", bb.getKey());
    Entity bbb = new Entity("Person", "bbb", bb.getKey());
    datastore.put(ImmutableList.<Entity>of(a, b, c, aa, bb, aaa, bbb));
    // Act
    Key lastSeenKey = bb.getKey();
    // [START key_filter_example]
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
    Query q = new Query("Person").setFilter(keyFilter);
    // [END key_filter_example]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(// Ancestor path "b/bb/aaa" is greater than "b/bb".
    aaa, // Ancestor path "b/bb/bbb" is greater than "b/bb".
    bbb, // Key name identifier "c" is greater than b.
    c);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 7 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryInterface_orFilter_printsMatchedEntities.

@Test
public void queryInterface_orFilter_printsMatchedEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Person", "a");
    a.setProperty("height", 100);
    Entity b = new Entity("Person", "b");
    b.setProperty("height", 150);
    Entity c = new Entity("Person", "c");
    c.setProperty("height", 200);
    datastore.put(ImmutableList.<Entity>of(a, b, c));
    StringWriter buf = new StringWriter();
    PrintWriter out = new PrintWriter(buf);
    long minHeight = 125;
    long maxHeight = 175;
    // Act
    // [START interface_3]
    Filter tooShortFilter = new FilterPredicate("height", FilterOperator.LESS_THAN, minHeight);
    Filter tooTallFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN, maxHeight);
    Filter heightOutOfRangeFilter = CompositeFilterOperator.or(tooShortFilter, tooTallFilter);
    Query q = new Query("Person").setFilter(heightOutOfRangeFilter);
    // [END interface_3]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(a, c);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) StringWriter(java.io.StringWriter) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 8 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method queryRestrictions_missingSortOnInequality_isInvalid.

@Test
public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception {
    long minBirthYear = 1940;
    // [START inequality_filters_sort_orders_invalid_example_1]
    Filter birthYearMinFilter = new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);
    // Not valid. Missing sort on birthYear.
    Query q = new Query("Person").setFilter(birthYearMinFilter).addSort("lastName", SortDirection.ASCENDING);
// [END inequality_filters_sort_orders_invalid_example_1]
// Note: The local devserver behavior is different than the production
// version of Cloud Datastore, so there aren't any assertions we can make
// in this test.  The query appears to work with the local test runner,
// but will fail in production.
}
Also used : Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Example 9 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities.

@Test
public void ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities() throws Exception {
    // Arrange
    Entity a = new Entity("Grandparent", "a");
    Entity b = new Entity("Grandparent", "b");
    Entity c = new Entity("Grandparent", "c");
    Entity aa = new Entity("Parent", "aa", a.getKey());
    Entity ba = new Entity("Parent", "ba", b.getKey());
    Entity bb = new Entity("Parent", "bb", b.getKey());
    Entity bc = new Entity("Parent", "bc", b.getKey());
    Entity cc = new Entity("Parent", "cc", c.getKey());
    Entity aaa = new Entity("Child", "aaa", aa.getKey());
    Entity bbb = new Entity("Child", "bbb", bb.getKey());
    datastore.put(ImmutableList.<Entity>of(a, b, c, aa, ba, bb, bc, cc, aaa, bbb));
    // Act
    Key ancestorKey = b.getKey();
    Key lastSeenKey = bb.getKey();
    // [START kindless_ancestor_key_query_example]
    Filter keyFilter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, lastSeenKey);
    Query q = new Query().setAncestor(ancestorKey).setFilter(keyFilter);
    // [END kindless_ancestor_key_query_example]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(bc, bbb);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) Test(org.junit.Test)

Example 10 with FilterPredicate

use of com.google.appengine.api.datastore.Query.FilterPredicate in project java-docs-samples by GoogleCloudPlatform.

the class QueriesTest method propertyFilterExample_returnsMatchingEntities.

@Test
public void propertyFilterExample_returnsMatchingEntities() throws Exception {
    // Arrange
    Entity p1 = new Entity("Person");
    p1.setProperty("height", 120);
    Entity p2 = new Entity("Person");
    p2.setProperty("height", 180);
    Entity p3 = new Entity("Person");
    p3.setProperty("height", 160);
    datastore.put(ImmutableList.<Entity>of(p1, p2, p3));
    // Act
    long minHeight = 160;
    // [START property_filter_example]
    Filter propertyFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
    Query q = new Query("Person").setFilter(propertyFilter);
    // [END property_filter_example]
    // Assert
    List<Entity> results = datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
    assertThat(results).named("query results").containsExactly(p2, p3);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Filter(com.google.appengine.api.datastore.Query.Filter) CompositeFilter(com.google.appengine.api.datastore.Query.CompositeFilter) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Test(org.junit.Test)

Aggregations

FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)38 Query (com.google.appengine.api.datastore.Query)31 Entity (com.google.appengine.api.datastore.Entity)26 Filter (com.google.appengine.api.datastore.Query.Filter)24 Test (org.junit.Test)21 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)20 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)20 Key (com.google.appengine.api.datastore.Key)6 DatastoreService (com.google.appengine.api.datastore.DatastoreService)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 StringWriter (java.io.StringWriter)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)1 Transaction (com.google.appengine.api.datastore.Transaction)1 Collection (java.util.Collection)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1