Skip to content

Commit

Permalink
Merge pull request #243 from GoogleCloudPlatform/swast-ds
Browse files Browse the repository at this point in the history
ae/datastore: Remove unnecessary `getKeys()` calls.
  • Loading branch information
aozarov committed May 18, 2016
2 parents 224123b + 573aa32 commit 098d3f8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.common.collect.ImmutableList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -90,14 +89,6 @@ public void propertyFilterExample_returnsMatchingEntities() throws Exception {
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
// [END unindexed_properties_1]

assertThat(getKeys(results)).named("query result keys").containsExactly(tom.getKey());
}

private ImmutableList<Key> getKeys(List<Entity> entities) {
ImmutableList.Builder<Key> keys = ImmutableList.builder();
for (Entity entity : entities) {
keys.add(entity.getKey());
}
return keys.build();
assertThat(results).named("query results").containsExactly(tom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ public void propertyFilterExample_returnsMatchingEntities() throws Exception {
// [END property_filter_example]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
.containsExactly(p2.getKey(), p3.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(p2, p3);
}

@Test
Expand All @@ -120,16 +119,14 @@ public void keyFilterExample_returnsMatchingEntities() throws Exception {
// [END key_filter_example]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
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.getKey(),
// Ancestor path "b/bb/bbb" is greater than "b/bb".
bbb.getKey(),
// Key name identifier "c" is greater than b.
c.getKey());
aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
c); // Key name identifier "c" is greater than b.
}

@Test
Expand All @@ -155,18 +152,15 @@ public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception
// [END kindless_query_example]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
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.getKey(),
// Ancestor path "b/bb/bbb" is greater than "b/bb".
bbb.getKey(),
// Kind "ZooAnimal" is greater than "Child"
zooAnimal.getKey(),
// Key name identifier "c" is greater than b.
c.getKey());
aaa, // Ancestor path "b/bb/aaa" is greater than "b/bb".
bbb, // Ancestor path "b/bb/bbb" is greater than "b/bb".
zooAnimal, // Kind "ZooAnimal" is greater than "Child"
c); // Key name identifier "c" is greater than b.
}

@Test
Expand All @@ -184,10 +178,9 @@ public void ancestorFilterExample_returnsMatchingEntities() throws Exception {
// [END ancestor_filter_example]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
.containsExactly(a.getKey(), aa.getKey(), ab.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(a, aa, ab);
}

@Test
Expand Down Expand Up @@ -222,9 +215,7 @@ public void ancestorQueryExample_returnsMatchingEntities() throws Exception {
datastore.prepare(photoQuery).asList(FetchOptions.Builder.withDefaults());
// [END ancestor_query_example]

assertThat(getKeys(results))
.named("query result keys")
.containsExactly(weddingPhoto.getKey(), babyPhoto.getKey(), dancePhoto.getKey());
assertThat(results).named("query results").containsExactly(weddingPhoto, babyPhoto, dancePhoto);
}

@Test
Expand Down Expand Up @@ -252,10 +243,9 @@ public void ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities() thr
// [END kindless_ancestor_key_query_example]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
.containsExactly(bc.getKey(), bbb.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(bc, bbb);
}

@Test
Expand Down Expand Up @@ -290,9 +280,7 @@ public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities()
datastore.prepare(mediaQuery).asList(FetchOptions.Builder.withDefaults());
// [END kindless_ancestor_query_example]

assertThat(getKeys(results))
.named("query result keys")
.containsExactly(weddingPhoto.getKey(), weddingVideo.getKey());
assertThat(results).named("query result keys").containsExactly(weddingPhoto, weddingVideo);
}

@Test
Expand All @@ -309,7 +297,7 @@ public void keysOnlyExample_returnsMatchingEntities() throws Exception {

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results)).named("query result keys").containsExactly(a.getKey(), c.getKey());
assertThat(results).named("query results").containsExactly(a, c);
}

@Test
Expand Down Expand Up @@ -337,16 +325,11 @@ public void sortOrderExample_returnsSortedEntities() throws Exception {

// Assert
List<Entity> lastNameResults =
datastore.prepare(q1).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(lastNameResults))
.named("last name query result keys")
.containsExactly(a.getKey(), b.getKey(), c.getKey())
.inOrder();
List<Entity> heightResults = datastore.prepare(q2).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(heightResults))
.named("height query result keys")
.containsExactly(c.getKey(), b.getKey(), a.getKey())
.inOrder();
datastore.prepare(q1.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(lastNameResults).named("last name query results").containsExactly(a, b, c).inOrder();
List<Entity> heightResults =
datastore.prepare(q2.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(heightResults).named("height query results").containsExactly(c, b, a).inOrder();
}

@Test
Expand Down Expand Up @@ -375,11 +358,9 @@ public void sortOrderExample_multipleSortOrders_returnsSortedEntities() throws E
// [END multiple_sort_orders_example]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
.containsExactly(a.getKey(), b2.getKey(), b1.getKey(), c.getKey())
.inOrder();
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(a, b2, b1, c).inOrder();
}

@Test
Expand Down Expand Up @@ -460,8 +441,9 @@ public void queryInterface_singleFilter_returnsMatchedEntities() throws Exceptio
// [END interface_2]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey(), c.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b, c);
}

@Test
Expand Down Expand Up @@ -492,8 +474,9 @@ public void queryInterface_orFilter_printsMatchedEntities() throws Exception {
// [END interface_3]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results)).named("query result keys").containsExactly(a.getKey(), c.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(a, c);
}

@Test
Expand Down Expand Up @@ -524,8 +507,9 @@ public void queryRestrictions_compositeFilter_returnsMatchedEntities() throws Ex
// [END inequality_filters_one_property_valid_example_1]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b);
}

@Test
Expand Down Expand Up @@ -598,8 +582,9 @@ public void queryRestrictions_compositeEqualFilter_returnsMatchedEntities() thro
// [END inequality_filters_one_property_valid_example_2]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b);
}

@Test
Expand Down Expand Up @@ -632,11 +617,9 @@ public void queryRestrictions_inequalitySortedFirst_returnsMatchedEntities() thr
// [END inequality_filters_sort_orders_valid_example]

// Assert
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
.containsExactly(c.getKey(), d.getKey(), b.getKey())
.inOrder();
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(c, d, b).inOrder();
}

@Test
Expand Down Expand Up @@ -700,7 +683,8 @@ public void queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEnti
// Entity "a" will not match because no individual value matches all filters.
// See the documentation for more details:
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").isEmpty();
}

Expand Down Expand Up @@ -731,8 +715,9 @@ public void queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntit
// Only "a" and "e" have both 1 and 2 in the "x" array-valued property.
// See the documentation for more details:
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results)).named("query result keys").containsExactly(a.getKey(), e.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(a, e);
}

@Test
Expand All @@ -757,10 +742,9 @@ public void queryRestrictions_surprisingMultipleValuesNotEquals_returnsMatchedEn
// The query matches any entity that has a some value other than 1. Only
// entity "e" is not matched. See the documentation for more details:
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results))
.named("query result keys")
.containsExactly(a.getKey(), b.getKey(), c.getKey(), d.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(a, b, c, d);
}

@Test
Expand Down Expand Up @@ -788,8 +772,9 @@ public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatche
//
// See the documentation for more details:
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(getKeys(results)).named("query result keys").containsExactly(b.getKey());
List<Entity> results =
datastore.prepare(q.setKeysOnly()).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").containsExactly(b);
}

private Entity retrievePersonWithLastName(String targetLastName) {
Expand All @@ -814,7 +799,7 @@ public void singleRetrievalExample_singleEntity_returnsEntity() throws Exception

Entity result = retrievePersonWithLastName("Johnson");

assertThat(result.getKey()).named("result key").isEqualTo(a.getKey());
assertThat(result).named("result").isEqualTo(a); // Note: Entity.equals() only checks the Key.
}

@Test
Expand Down Expand Up @@ -866,17 +851,6 @@ public void queryLimitExample_returnsLimitedEntities() throws Exception {

List<Entity> results = getTallestPeople();

assertThat(getKeys(results))
.named("result keys")
.containsExactly(g.getKey(), e.getKey(), c.getKey(), a.getKey(), b.getKey())
.inOrder();
}

private ImmutableList<Key> getKeys(List<Entity> entities) {
ImmutableList.Builder<Key> keys = ImmutableList.builder();
for (Entity entity : entities) {
keys.add(entity.getKey());
}
return keys.build();
assertThat(results).named("results").containsExactly(g, e, c, a, b).inOrder();
}
}

0 comments on commit 098d3f8

Please sign in to comment.