|
10 | 10 |
|
11 | 11 | import com.google.gcloud.datastore.Query.ResultClass; |
12 | 12 | import com.google.gcloud.datastore.StructuredQuery.OrderBy; |
| 13 | +import com.google.gcloud.datastore.StructuredQuery.Projection; |
| 14 | +import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; |
13 | 15 |
|
14 | 16 | import org.junit.Before; |
15 | 17 | import org.junit.Test; |
@@ -48,7 +50,7 @@ public class DatastoreServiceTest { |
48 | 50 | .set("bool", BOOL_VALUE).set("partial1", EntityValue.of(PARTIAL_ENTITY1)) |
49 | 51 | .set("list", LIST_VALUE2).build(); |
50 | 52 | private static final Entity ENTITY2 = Entity.builder(ENTITY1).key(KEY2).remove("str") |
51 | | - .setNull("null").build(); |
| 53 | + .set("name", "koko").setNull("null").set("age", 20).build(); |
52 | 54 | private static final Entity ENTITY3 = Entity.builder(ENTITY1).key(KEY3).remove("str") |
53 | 55 | .set("null", NULL_VALUE).set("partial1", PARTIAL_ENTITY2).set("partial2", ENTITY2).build(); |
54 | 56 |
|
@@ -138,7 +140,29 @@ public void testTransactionWithRead() { |
138 | 140 |
|
139 | 141 | @Test |
140 | 142 | public void testTransactionWithQuery() { |
141 | | - fail("not implemented"); |
| 143 | + Query<Entity> query = |
| 144 | + StructuredQuery.builder().kind(KIND2).filter(PropertyFilter.hasAncestor(KEY2)).build(); |
| 145 | + Transaction transaction = datastore.newTransaction(); |
| 146 | + QueryResult<Entity> results = transaction.runQuery(query); |
| 147 | + assertEquals(ENTITY2, results.next()); |
| 148 | + assertFalse(results.hasNext()); |
| 149 | + transaction.add(ENTITY3); |
| 150 | + transaction.commit(); |
| 151 | + assertEquals(ENTITY3, datastore.get(KEY3)); |
| 152 | + |
| 153 | + transaction = datastore.newTransaction(); |
| 154 | + results = transaction.runQuery(query); |
| 155 | + assertEquals(ENTITY2, results.next()); |
| 156 | + transaction.delete(ENTITY3.key()); |
| 157 | + // update entity2 during the transaction |
| 158 | + datastore.put(Entity.builder(ENTITY2).clear().build()); |
| 159 | + try { |
| 160 | + transaction.commit(); |
| 161 | + fail("Expecting a failure"); |
| 162 | + } catch (DatastoreServiceException expected) { |
| 163 | + expected.printStackTrace(); |
| 164 | + assertEquals(DatastoreServiceException.Code.ABORTED, expected.code()); |
| 165 | + } |
142 | 166 | } |
143 | 167 |
|
144 | 168 | @Test |
@@ -311,19 +335,30 @@ public void testRunStructuredQuery() { |
311 | 335 | assertFalse(results1.hasNext()); |
312 | 336 |
|
313 | 337 | StructuredQuery<Key> keyOnlyQuery = StructuredQuery.keyOnlyBuilder().kind(KIND1).build(); |
| 338 | + QueryResult<Key> results2 = datastore.runQuery(keyOnlyQuery); |
| 339 | + assertTrue(results2.hasNext()); |
| 340 | + assertEquals(ENTITY1.key(), results2.next()); |
| 341 | + assertFalse(results2.hasNext()); |
314 | 342 |
|
| 343 | + StructuredQuery<PartialEntity> projectionQuery = StructuredQuery.projectionBuilder() |
| 344 | + .kind(KIND2) |
| 345 | + .projection(Projection.property("age"), Projection.first("name")) |
| 346 | + .filter(PropertyFilter.gt("age", 18)) |
| 347 | + .groupBy("age") |
| 348 | + .orderBy(OrderBy.asc("age")) |
| 349 | + .limit(10) |
| 350 | + .build(); |
315 | 351 |
|
316 | | - // todo(ozarov): construct a test to very nextQuery/pagination |
317 | | - } |
318 | | - |
319 | | - @Test |
320 | | - public void testRunStructuredQueryProjection() { |
321 | | - fail("Not yet implemented"); |
322 | | - } |
| 352 | + QueryResult<PartialEntity> results3 = datastore.runQuery(projectionQuery); |
| 353 | + assertTrue(results3.hasNext()); |
| 354 | + PartialEntity entity = results3.next(); |
| 355 | + assertEquals(ENTITY2.key(), entity.key()); |
| 356 | + assertEquals(20, entity.getLong("age")); |
| 357 | + assertEquals("koko", entity.getString("name")); |
| 358 | + assertEquals(2, entity.properties().size()); |
| 359 | + assertFalse(results3.hasNext()); |
323 | 360 |
|
324 | | - @Test |
325 | | - public void testRunStructuredQueryKeysOnly() { |
326 | | - fail("Not yet implemented"); |
| 361 | + // TODO(ozarov): construct a test to very nextQuery/pagination |
327 | 362 | } |
328 | 363 |
|
329 | 364 | @Test |
|
0 commit comments