|
17 | 17 | package com.example.appengine;
|
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat;
|
| 20 | +import static org.junit.Assert.fail; |
20 | 21 |
|
21 | 22 | import com.google.appengine.api.datastore.DatastoreService;
|
22 | 23 | import com.google.appengine.api.datastore.DatastoreServiceFactory;
|
23 | 24 | import com.google.appengine.api.datastore.Entity;
|
24 | 25 | import com.google.appengine.api.datastore.FetchOptions;
|
25 | 26 | import com.google.appengine.api.datastore.Key;
|
26 | 27 | import com.google.appengine.api.datastore.PreparedQuery;
|
| 28 | +import com.google.appengine.api.datastore.PreparedQuery.TooManyResultsException; |
27 | 29 | import com.google.appengine.api.datastore.Query;
|
28 | 30 | import com.google.appengine.api.datastore.Query.CompositeFilter;
|
29 | 31 | import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
|
@@ -781,6 +783,86 @@ public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatche
|
781 | 783 | // Datastore.
|
782 | 784 | }
|
783 | 785 |
|
| 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 | + |
784 | 866 | private ImmutableList<Key> getKeys(List<Entity> entities) {
|
785 | 867 | ImmutableList.Builder<Key> keys = ImmutableList.builder();
|
786 | 868 | for (Entity entity : entities) {
|
|
0 commit comments