Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Datastore retrieving query samples. #204

Merged
merged 1 commit into from
Apr 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package com.example.appengine;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.PreparedQuery.TooManyResultsException;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilter;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
Expand Down Expand Up @@ -781,6 +783,86 @@ public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatche
// Datastore.
}

private Entity retrievePersonWithLastName(String targetLastName) {
// [START single_retrieval_example]
Query q =
new Query("Person")
.setFilter(new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName));

PreparedQuery pq = datastore.prepare(q);
Entity result = pq.asSingleEntity();
// [END single_retrieval_example]
return result;
}

@Test
public void singleRetrievalExample_singleEntity_returnsEntity() throws Exception {
Entity a = new Entity("Person", "a");
a.setProperty("lastName", "Johnson");
Entity b = new Entity("Person", "b");
b.setProperty("lastName", "Smith");
datastore.put(ImmutableList.<Entity>of(a, b));

Entity result = retrievePersonWithLastName("Johnson");

assertThat(result.getKey()).named("result key").isEqualTo(a.getKey());
}

@Test
public void singleRetrievalExample_multitpleEntities_throwsException() throws Exception {
Entity a = new Entity("Person", "a");
a.setProperty("lastName", "Johnson");
Entity b = new Entity("Person", "b");
b.setProperty("lastName", "Johnson");
datastore.put(ImmutableList.<Entity>of(a, b));

try {
Entity result = retrievePersonWithLastName("Johnson");
fail("Expected TooManyResultsException");
} catch (TooManyResultsException expected) {
// TooManyResultsException does not provide addition details.
}
}

// [START query_limit_example]
private List<Entity> getTallestPeople() {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

Query q = new Query("Person").addSort("height", SortDirection.DESCENDING);

PreparedQuery pq = datastore.prepare(q);
return pq.asList(FetchOptions.Builder.withLimit(5));
}
// [END query_limit_example]

@Test
public void queryLimitExample_returnsLimitedEntities() throws Exception {
Entity a = new Entity("Person", "a");
a.setProperty("height", 200);
Entity b = new Entity("Person", "b");
b.setProperty("height", 199);
Entity c = new Entity("Person", "c");
c.setProperty("height", 201);
Entity d = new Entity("Person", "d");
d.setProperty("height", 198);
Entity e = new Entity("Person", "e");
e.setProperty("height", 202);
Entity f = new Entity("Person", "f");
f.setProperty("height", 197);
Entity g = new Entity("Person", "g");
g.setProperty("height", 203);
Entity h = new Entity("Person", "h");
h.setProperty("height", 196);
datastore.put(ImmutableList.<Entity>of(a, b, c, d, e, f, g, h));

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) {
Expand Down