Skip to content

Commit e49fed4

Browse files
committed
Fix "surprising queries" samples.
We were setting the filter twice (overwriting the first) rather than using a composite filter. This was causing the query results to be even more surprising than we intending.
1 parent b7a60d0 commit e49fed4

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

appengine/datastore/src/test/java/com/example/appengine/QueriesTest.java

+20-23
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,15 @@ public void queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEnti
694694
// [START surprising_behavior_example_1]
695695
Query q =
696696
new Query("Widget")
697-
.setFilter(new FilterPredicate("x", FilterOperator.GREATER_THAN, 1))
698-
.setFilter(new FilterPredicate("x", FilterOperator.LESS_THAN, 2));
697+
.setFilter(
698+
CompositeFilterOperator.and(
699+
new FilterPredicate("x", FilterOperator.GREATER_THAN, 1),
700+
new FilterPredicate("x", FilterOperator.LESS_THAN, 2)));
699701
// [END surprising_behavior_example_1]
700702

701-
// Note: The documentation describes that the entity "a" will not match
702-
// because no value matches all filters. When run with the local test
703-
// runner, the entity "a" *is* matched. This may be a difference in
704-
// behavior between the local devserver and Cloud Datastore, so there
705-
// aren't any assertions we can make in this test.
703+
// Entity "a" will not match because no individual value matches all filters.
704+
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
705+
assertThat(results).named("query results").isEmpty();
706706
}
707707

708708
@Test
@@ -716,21 +716,21 @@ public void queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntit
716716
c.setProperty("x", ImmutableList.<Long>of(-6L, 2L));
717717
Entity d = new Entity("Widget", "d");
718718
d.setProperty("x", ImmutableList.<Long>of(-6L, 4L));
719-
datastore.put(ImmutableList.<Entity>of(a, b, c, d));
719+
Entity e = new Entity("Widget", "e");
720+
e.setProperty("x", ImmutableList.<Long>of(1L, 2L, 3L));
721+
datastore.put(ImmutableList.<Entity>of(a, b, c, d, e));
720722

721723
// [START surprising_behavior_example_2]
722724
Query q =
723725
new Query("Widget")
724-
.setFilter(new FilterPredicate("x", FilterOperator.EQUAL, 1))
725-
.setFilter(new FilterPredicate("x", FilterOperator.EQUAL, 2));
726+
.setFilter(
727+
CompositeFilterOperator.and(
728+
new FilterPredicate("x", FilterOperator.EQUAL, 1),
729+
new FilterPredicate("x", FilterOperator.EQUAL, 2)));
726730
// [END surprising_behavior_example_2]
727731

728732
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
729-
assertThat(getKeys(results)).named("query result keys").contains(a.getKey());
730-
731-
// Note: When run in the test server, this matches "c" as expected and does
732-
// not match "d" as expected. For some reason it does *not* match "b".
733-
// The behavior of queries on repeated values is definitely surprising.
733+
assertThat(getKeys(results)).named("query result keys").containsExactly(a.getKey(), e.getKey());
734734
}
735735

736736
@Test
@@ -770,17 +770,14 @@ public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatche
770770
// [START surprising_behavior_example_4]
771771
Query q =
772772
new Query("Widget")
773-
.setFilter(new FilterPredicate("x", FilterOperator.NOT_EQUAL, 1))
774-
.setFilter(new FilterPredicate("x", FilterOperator.NOT_EQUAL, 2));
773+
.setFilter(
774+
CompositeFilterOperator.and(
775+
new FilterPredicate("x", FilterOperator.NOT_EQUAL, 1),
776+
new FilterPredicate("x", FilterOperator.NOT_EQUAL, 2)));
775777
// [END surprising_behavior_example_4]
776778

777779
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
778-
assertThat(getKeys(results)).named("query result keys").contains(b.getKey());
779-
780-
// Note: The documentation describes that the entity "a" will not match.
781-
// When run with the local test runner, the entity "a" *is* matched. This
782-
// may be a difference in behavior between the local devserver and Cloud
783-
// Datastore.
780+
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey());
784781
}
785782

786783
private Entity retrievePersonWithLastName(String targetLastName) {

0 commit comments

Comments
 (0)