|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.appengine.api.datastore.DatastoreService; |
| 22 | +import com.google.appengine.api.datastore.DatastoreServiceFactory; |
| 23 | +import com.google.appengine.api.datastore.Entity; |
| 24 | +import com.google.appengine.api.datastore.FetchOptions; |
| 25 | +import com.google.appengine.api.datastore.PropertyProjection; |
| 26 | +import com.google.appengine.api.datastore.Query; |
| 27 | +import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; |
| 28 | +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | +import org.junit.runners.JUnit4; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +/** |
| 38 | + * Unit tests to demonstrate App Engine Datastore projection queries. |
| 39 | + */ |
| 40 | +@RunWith(JUnit4.class) |
| 41 | +public class ProjectionTest { |
| 42 | + |
| 43 | + private final LocalServiceTestHelper helper = |
| 44 | + new LocalServiceTestHelper( |
| 45 | + // Set no eventual consistency, that way queries return all results. |
| 46 | + // https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests |
| 47 | + new LocalDatastoreServiceTestConfig() |
| 48 | + .setDefaultHighRepJobPolicyUnappliedJobPercentage(0)); |
| 49 | + |
| 50 | + private DatastoreService datastore; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() throws Exception { |
| 54 | + helper.setUp(); |
| 55 | + datastore = DatastoreServiceFactory.getDatastoreService(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void tearDown() { |
| 60 | + helper.tearDown(); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Test |
| 65 | + public void projectionQuery_grouping_filtersDuplicates() { |
| 66 | + putTestData("some duplicate", 0L); |
| 67 | + putTestData("some duplicate", 0L); |
| 68 | + putTestData("too big", 1L); |
| 69 | + |
| 70 | + Query q = new Query("TestKind"); |
| 71 | + q.addProjection(new PropertyProjection("A", String.class)); |
| 72 | + q.addProjection(new PropertyProjection("B", Long.class)); |
| 73 | + q.setDistinct(true); |
| 74 | + q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L)); |
| 75 | + q.addSort("B", Query.SortDirection.DESCENDING); |
| 76 | + q.addSort("A"); |
| 77 | + |
| 78 | + List<Entity> entities = |
| 79 | + datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5)); |
| 80 | + assertThat(entities).hasSize(1); |
| 81 | + // [START_EXCLUDE silent] |
| 82 | + Entity entity = entities.get(0); |
| 83 | + assertThat((String) entity.getProperty("A")).named("entity.A").isEqualTo("some duplicate"); |
| 84 | + assertThat((long) entity.getProperty("B")).named("entity.B").isEqualTo(0L); |
| 85 | + // [END_EXCLUDE] |
| 86 | + } |
| 87 | + |
| 88 | + private void putTestData(String a, long b) { |
| 89 | + Entity entity = new Entity("TestKind"); |
| 90 | + entity.setProperty("A", a); |
| 91 | + entity.setProperty("B", b); |
| 92 | + datastore.put(entity); |
| 93 | + } |
| 94 | +} |
0 commit comments