Java Code Examples for com.google.appengine.api.datastore.Query.FilterOperator#GREATER_THAN_OR_EQUAL

The following examples show how to use com.google.appengine.api.datastore.Query.FilterOperator#GREATER_THAN_OR_EQUAL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@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 gae_java8_datastore_property_filter]
  Filter propertyFilter =
      new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);
  Query q = new Query("Person").setFilter(propertyFilter);
  // [END gae_java8_datastore_property_filter]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(p2, p3);
}
 
Example 2
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryInterface_singleFilter_returnsMatchedEntities() 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", 300);
  datastore.put(ImmutableList.<Entity>of(a, b, c));

  // Act
  long minHeight = 150;
  // [START gae_java8_datastore_interface_2]
  Filter heightMinFilter =
      new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);

  Query q = new Query("Person").setFilter(heightMinFilter);
  // [END gae_java8_datastore_interface_2]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(b, c);
}
 
Example 3
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRestrictions_compositeFilter_isInvalid() throws Exception {
  long minBirthYear = 1940;
  long maxHeight = 200;
  // [START gae_java8_datastore_inequality_filters_one_property_invalid]
  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  Filter heightMaxFilter =
      new FilterPredicate("height", FilterOperator.LESS_THAN_OR_EQUAL, maxHeight);

  Filter invalidFilter = CompositeFilterOperator.and(birthYearMinFilter, heightMaxFilter);

  Query q = new Query("Person").setFilter(invalidFilter);
  // [END gae_java8_datastore_inequality_filters_one_property_invalid]

  // 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.
}
 
Example 4
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRestrictions_missingSortOnInequality_isInvalid() throws Exception {
  long minBirthYear = 1940;
  // [START gae_java8_datastore_inequality_filters_sort_orders_invalid_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 gae_java8_datastore_inequality_filters_sort_orders_invalid_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.
}
 
Example 5
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRestrictions_sortWrongOrderOnInequality_isInvalid() throws Exception {
  long minBirthYear = 1940;
  // [START gae_java8_datastore_inequality_filters_sort_orders_invalid_2]
  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  // Not valid. Sort on birthYear needs to be first.
  Query q =
      new Query("Person")
          .setFilter(birthYearMinFilter)
          .addSort("lastName", SortDirection.ASCENDING)
          .addSort("birthYear", SortDirection.ASCENDING);
  // [END gae_java8_datastore_inequality_filters_sort_orders_invalid_2]

  // 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.
}
 
Example 6
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void queryRestrictions_compositeFilter_returnsMatchedEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Person", "a");
  a.setProperty("birthYear", 1930);
  Entity b = new Entity("Person", "b");
  b.setProperty("birthYear", 1960);
  Entity c = new Entity("Person", "c");
  c.setProperty("birthYear", 1990);
  datastore.put(ImmutableList.<Entity>of(a, b, c));

  // Act
  long minBirthYear = 1940;
  long maxBirthYear = 1980;
  // [START gae_java8_datastore_inequality_filters_one_property_valid_1]
  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  Filter birthYearMaxFilter =
      new FilterPredicate("birthYear", FilterOperator.LESS_THAN_OR_EQUAL, maxBirthYear);

  Filter birthYearRangeFilter =
      CompositeFilterOperator.and(birthYearMinFilter, birthYearMaxFilter);

  Query q = new Query("Person").setFilter(birthYearRangeFilter);
  // [END gae_java8_datastore_inequality_filters_one_property_valid_1]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(b);
}
 
Example 7
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void queryRestrictions_inequalitySortedFirst_returnsMatchedEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Person", "a");
  a.setProperty("birthYear", 1930);
  a.setProperty("lastName", "Someone");
  Entity b = new Entity("Person", "b");
  b.setProperty("birthYear", 1990);
  b.setProperty("lastName", "Bravo");
  Entity c = new Entity("Person", "c");
  c.setProperty("birthYear", 1960);
  c.setProperty("lastName", "Charlie");
  Entity d = new Entity("Person", "d");
  d.setProperty("birthYear", 1960);
  d.setProperty("lastName", "Delta");
  datastore.put(ImmutableList.<Entity>of(a, b, c, d));
  long minBirthYear = 1940;

  // [START gae_java8_datastore_inequality_filters_sort_orders_valid]
  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  Query q =
      new Query("Person")
          .setFilter(birthYearMinFilter)
          .addSort("birthYear", SortDirection.ASCENDING)
          .addSort("lastName", SortDirection.ASCENDING);
  // [END gae_java8_datastore_inequality_filters_sort_orders_valid]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(c, d, b).inOrder();
}
 
Example 8
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Test
public void queryRestrictions_compositeEqualFilter_returnsMatchedEntities() throws Exception {
  // Arrange
  Entity a = new Entity("Person", "a");
  a.setProperty("birthYear", 1930);
  a.setProperty("city", "Somewhere");
  a.setProperty("lastName", "Someone");
  Entity b = new Entity("Person", "b");
  b.setProperty("birthYear", 1960);
  b.setProperty("city", "Somewhere");
  b.setProperty("lastName", "Someone");
  Entity c = new Entity("Person", "c");
  c.setProperty("birthYear", 1990);
  c.setProperty("city", "Somewhere");
  c.setProperty("lastName", "Someone");
  Entity d = new Entity("Person", "d");
  d.setProperty("birthYear", 1960);
  d.setProperty("city", "Nowhere");
  d.setProperty("lastName", "Someone");
  Entity e = new Entity("Person", "e");
  e.setProperty("birthYear", 1960);
  e.setProperty("city", "Somewhere");
  e.setProperty("lastName", "Noone");
  datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));
  long minBirthYear = 1940;
  long maxBirthYear = 1980;
  String targetCity = "Somewhere";
  String targetLastName = "Someone";

  // [START gae_java8_datastore_inequality_filters_one_property_valid_2]
  Filter lastNameFilter = new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName);

  Filter cityFilter = new FilterPredicate("city", FilterOperator.EQUAL, targetCity);

  Filter birthYearMinFilter =
      new FilterPredicate("birthYear", FilterOperator.GREATER_THAN_OR_EQUAL, minBirthYear);

  Filter birthYearMaxFilter =
      new FilterPredicate("birthYear", FilterOperator.LESS_THAN_OR_EQUAL, maxBirthYear);

  Filter validFilter =
      CompositeFilterOperator.and(
          lastNameFilter, cityFilter, birthYearMinFilter, birthYearMaxFilter);

  Query q = new Query("Person").setFilter(validFilter);
  // [END gae_java8_datastore_inequality_filters_one_property_valid_2]

  // Assert
  List<Entity> results =
      datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
  assertWithMessage("query results").that(results).containsExactly(b);
}