Skip to content

Commit 188aab1

Browse files
tswastlesv
authored andcommitted
1 parent 42b774a commit 188aab1

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package com.example.appengine;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.fail;
2021

2122
import com.google.appengine.api.datastore.DatastoreService;
2223
import com.google.appengine.api.datastore.DatastoreServiceFactory;
2324
import com.google.appengine.api.datastore.Entity;
2425
import com.google.appengine.api.datastore.FetchOptions;
2526
import com.google.appengine.api.datastore.Key;
2627
import com.google.appengine.api.datastore.PreparedQuery;
28+
import com.google.appengine.api.datastore.PreparedQuery.TooManyResultsException;
2729
import com.google.appengine.api.datastore.Query;
2830
import com.google.appengine.api.datastore.Query.CompositeFilter;
2931
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
@@ -781,6 +783,86 @@ public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatche
781783
// Datastore.
782784
}
783785

786+
private Entity retrievePersonWithLastName(String targetLastName) {
787+
// [START single_retrieval_example]
788+
Query q =
789+
new Query("Person")
790+
.setFilter(new FilterPredicate("lastName", FilterOperator.EQUAL, targetLastName));
791+
792+
PreparedQuery pq = datastore.prepare(q);
793+
Entity result = pq.asSingleEntity();
794+
// [END single_retrieval_example]
795+
return result;
796+
}
797+
798+
@Test
799+
public void singleRetrievalExample_singleEntity_returnsEntity() throws Exception {
800+
Entity a = new Entity("Person", "a");
801+
a.setProperty("lastName", "Johnson");
802+
Entity b = new Entity("Person", "b");
803+
b.setProperty("lastName", "Smith");
804+
datastore.put(ImmutableList.<Entity>of(a, b));
805+
806+
Entity result = retrievePersonWithLastName("Johnson");
807+
808+
assertThat(result.getKey()).named("result key").isEqualTo(a.getKey());
809+
}
810+
811+
@Test
812+
public void singleRetrievalExample_multitpleEntities_throwsException() throws Exception {
813+
Entity a = new Entity("Person", "a");
814+
a.setProperty("lastName", "Johnson");
815+
Entity b = new Entity("Person", "b");
816+
b.setProperty("lastName", "Johnson");
817+
datastore.put(ImmutableList.<Entity>of(a, b));
818+
819+
try {
820+
Entity result = retrievePersonWithLastName("Johnson");
821+
fail("Expected TooManyResultsException");
822+
} catch (TooManyResultsException expected) {
823+
// TooManyResultsException does not provide addition details.
824+
}
825+
}
826+
827+
// [START query_limit_example]
828+
private List<Entity> getTallestPeople() {
829+
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
830+
831+
Query q = new Query("Person").addSort("height", SortDirection.DESCENDING);
832+
833+
PreparedQuery pq = datastore.prepare(q);
834+
return pq.asList(FetchOptions.Builder.withLimit(5));
835+
}
836+
// [END query_limit_example]
837+
838+
@Test
839+
public void queryLimitExample_returnsLimitedEntities() throws Exception {
840+
Entity a = new Entity("Person", "a");
841+
a.setProperty("height", 200);
842+
Entity b = new Entity("Person", "b");
843+
b.setProperty("height", 199);
844+
Entity c = new Entity("Person", "c");
845+
c.setProperty("height", 201);
846+
Entity d = new Entity("Person", "d");
847+
d.setProperty("height", 198);
848+
Entity e = new Entity("Person", "e");
849+
e.setProperty("height", 202);
850+
Entity f = new Entity("Person", "f");
851+
f.setProperty("height", 197);
852+
Entity g = new Entity("Person", "g");
853+
g.setProperty("height", 203);
854+
Entity h = new Entity("Person", "h");
855+
h.setProperty("height", 196);
856+
datastore.put(ImmutableList.<Entity>of(a, b, c, d, e, f, g, h));
857+
858+
List<Entity> results = getTallestPeople();
859+
860+
assertThat(getKeys(results))
861+
.named("result keys")
862+
.containsExactly(g.getKey(), e.getKey(), c.getKey(), a.getKey(), b.getKey())
863+
.inOrder();
864+
}
865+
784866
private ImmutableList<Key> getKeys(List<Entity> entities) {
785867
ImmutableList.Builder<Key> keys = ImmutableList.builder();
786868
for (Entity entity : entities) {

0 commit comments

Comments
 (0)