@@ -93,10 +93,9 @@ public void propertyFilterExample_returnsMatchingEntities() throws Exception {
93
93
// [END property_filter_example]
94
94
95
95
// Assert
96
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
97
- assertThat (getKeys (results ))
98
- .named ("query result keys" )
99
- .containsExactly (p2 .getKey (), p3 .getKey ());
96
+ List <Entity > results =
97
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
98
+ assertThat (results ).named ("query results" ).containsExactly (p2 , p3 );
100
99
}
101
100
102
101
@ Test
@@ -120,16 +119,14 @@ public void keyFilterExample_returnsMatchingEntities() throws Exception {
120
119
// [END key_filter_example]
121
120
122
121
// Assert
123
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
124
- assertThat (getKeys (results ))
125
- .named ("query result keys" )
122
+ List <Entity > results =
123
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
124
+ assertThat (results )
125
+ .named ("query results" )
126
126
.containsExactly (
127
- // Ancestor path "b/bb/aaa" is greater than "b/bb".
128
- aaa .getKey (),
129
- // Ancestor path "b/bb/bbb" is greater than "b/bb".
130
- bbb .getKey (),
131
- // Key name identifier "c" is greater than b.
132
- c .getKey ());
127
+ aaa , // Ancestor path "b/bb/aaa" is greater than "b/bb".
128
+ bbb , // Ancestor path "b/bb/bbb" is greater than "b/bb".
129
+ c ); // Key name identifier "c" is greater than b.
133
130
}
134
131
135
132
@ Test
@@ -155,18 +152,15 @@ public void keyFilterExample_kindless_returnsMatchingEntities() throws Exception
155
152
// [END kindless_query_example]
156
153
157
154
// Assert
158
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
159
- assertThat (getKeys (results ))
160
- .named ("query result keys" )
155
+ List <Entity > results =
156
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
157
+ assertThat (results )
158
+ .named ("query results" )
161
159
.containsExactly (
162
- // Ancestor path "b/bb/aaa" is greater than "b/bb".
163
- aaa .getKey (),
164
- // Ancestor path "b/bb/bbb" is greater than "b/bb".
165
- bbb .getKey (),
166
- // Kind "ZooAnimal" is greater than "Child"
167
- zooAnimal .getKey (),
168
- // Key name identifier "c" is greater than b.
169
- c .getKey ());
160
+ aaa , // Ancestor path "b/bb/aaa" is greater than "b/bb".
161
+ bbb , // Ancestor path "b/bb/bbb" is greater than "b/bb".
162
+ zooAnimal , // Kind "ZooAnimal" is greater than "Child"
163
+ c ); // Key name identifier "c" is greater than b.
170
164
}
171
165
172
166
@ Test
@@ -184,10 +178,9 @@ public void ancestorFilterExample_returnsMatchingEntities() throws Exception {
184
178
// [END ancestor_filter_example]
185
179
186
180
// Assert
187
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
188
- assertThat (getKeys (results ))
189
- .named ("query result keys" )
190
- .containsExactly (a .getKey (), aa .getKey (), ab .getKey ());
181
+ List <Entity > results =
182
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
183
+ assertThat (results ).named ("query results" ).containsExactly (a , aa , ab );
191
184
}
192
185
193
186
@ Test
@@ -222,9 +215,7 @@ public void ancestorQueryExample_returnsMatchingEntities() throws Exception {
222
215
datastore .prepare (photoQuery ).asList (FetchOptions .Builder .withDefaults ());
223
216
// [END ancestor_query_example]
224
217
225
- assertThat (getKeys (results ))
226
- .named ("query result keys" )
227
- .containsExactly (weddingPhoto .getKey (), babyPhoto .getKey (), dancePhoto .getKey ());
218
+ assertThat (results ).named ("query results" ).containsExactly (weddingPhoto , babyPhoto , dancePhoto );
228
219
}
229
220
230
221
@ Test
@@ -252,10 +243,9 @@ public void ancestorQueryExample_kindlessKeyFilter_returnsMatchingEntities() thr
252
243
// [END kindless_ancestor_key_query_example]
253
244
254
245
// Assert
255
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
256
- assertThat (getKeys (results ))
257
- .named ("query result keys" )
258
- .containsExactly (bc .getKey (), bbb .getKey ());
246
+ List <Entity > results =
247
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
248
+ assertThat (results ).named ("query results" ).containsExactly (bc , bbb );
259
249
}
260
250
261
251
@ Test
@@ -290,9 +280,7 @@ public void ancestorQueryExample_kindlessKeyFilterFull_returnsMatchingEntities()
290
280
datastore .prepare (mediaQuery ).asList (FetchOptions .Builder .withDefaults ());
291
281
// [END kindless_ancestor_query_example]
292
282
293
- assertThat (getKeys (results ))
294
- .named ("query result keys" )
295
- .containsExactly (weddingPhoto .getKey (), weddingVideo .getKey ());
283
+ assertThat (results ).named ("query result keys" ).containsExactly (weddingPhoto , weddingVideo );
296
284
}
297
285
298
286
@ Test
@@ -309,7 +297,7 @@ public void keysOnlyExample_returnsMatchingEntities() throws Exception {
309
297
310
298
// Assert
311
299
List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
312
- assertThat (getKeys ( results )) .named ("query result keys " ).containsExactly (a . getKey () , c . getKey () );
300
+ assertThat (results ).named ("query results " ).containsExactly (a , c );
313
301
}
314
302
315
303
@ Test
@@ -337,16 +325,11 @@ public void sortOrderExample_returnsSortedEntities() throws Exception {
337
325
338
326
// Assert
339
327
List <Entity > lastNameResults =
340
- datastore .prepare (q1 ).asList (FetchOptions .Builder .withDefaults ());
341
- assertThat (getKeys (lastNameResults ))
342
- .named ("last name query result keys" )
343
- .containsExactly (a .getKey (), b .getKey (), c .getKey ())
344
- .inOrder ();
345
- List <Entity > heightResults = datastore .prepare (q2 ).asList (FetchOptions .Builder .withDefaults ());
346
- assertThat (getKeys (heightResults ))
347
- .named ("height query result keys" )
348
- .containsExactly (c .getKey (), b .getKey (), a .getKey ())
349
- .inOrder ();
328
+ datastore .prepare (q1 .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
329
+ assertThat (lastNameResults ).named ("last name query results" ).containsExactly (a , b , c ).inOrder ();
330
+ List <Entity > heightResults =
331
+ datastore .prepare (q2 .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
332
+ assertThat (heightResults ).named ("height query results" ).containsExactly (c , b , a ).inOrder ();
350
333
}
351
334
352
335
@ Test
@@ -375,11 +358,9 @@ public void sortOrderExample_multipleSortOrders_returnsSortedEntities() throws E
375
358
// [END multiple_sort_orders_example]
376
359
377
360
// Assert
378
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
379
- assertThat (getKeys (results ))
380
- .named ("query result keys" )
381
- .containsExactly (a .getKey (), b2 .getKey (), b1 .getKey (), c .getKey ())
382
- .inOrder ();
361
+ List <Entity > results =
362
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
363
+ assertThat (results ).named ("query results" ).containsExactly (a , b2 , b1 , c ).inOrder ();
383
364
}
384
365
385
366
@ Test
@@ -460,8 +441,9 @@ public void queryInterface_singleFilter_returnsMatchedEntities() throws Exceptio
460
441
// [END interface_2]
461
442
462
443
// Assert
463
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
464
- assertThat (getKeys (results )).named ("query result keys" ).containsExactly (b .getKey (), c .getKey ());
444
+ List <Entity > results =
445
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
446
+ assertThat (results ).named ("query results" ).containsExactly (b , c );
465
447
}
466
448
467
449
@ Test
@@ -492,8 +474,9 @@ public void queryInterface_orFilter_printsMatchedEntities() throws Exception {
492
474
// [END interface_3]
493
475
494
476
// Assert
495
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
496
- assertThat (getKeys (results )).named ("query result keys" ).containsExactly (a .getKey (), c .getKey ());
477
+ List <Entity > results =
478
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
479
+ assertThat (results ).named ("query results" ).containsExactly (a , c );
497
480
}
498
481
499
482
@ Test
@@ -524,8 +507,9 @@ public void queryRestrictions_compositeFilter_returnsMatchedEntities() throws Ex
524
507
// [END inequality_filters_one_property_valid_example_1]
525
508
526
509
// Assert
527
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
528
- assertThat (getKeys (results )).named ("query result keys" ).containsExactly (b .getKey ());
510
+ List <Entity > results =
511
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
512
+ assertThat (results ).named ("query results" ).containsExactly (b );
529
513
}
530
514
531
515
@ Test
@@ -598,8 +582,9 @@ public void queryRestrictions_compositeEqualFilter_returnsMatchedEntities() thro
598
582
// [END inequality_filters_one_property_valid_example_2]
599
583
600
584
// Assert
601
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
602
- assertThat (getKeys (results )).named ("query result keys" ).containsExactly (b .getKey ());
585
+ List <Entity > results =
586
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
587
+ assertThat (results ).named ("query results" ).containsExactly (b );
603
588
}
604
589
605
590
@ Test
@@ -632,11 +617,9 @@ public void queryRestrictions_inequalitySortedFirst_returnsMatchedEntities() thr
632
617
// [END inequality_filters_sort_orders_valid_example]
633
618
634
619
// Assert
635
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
636
- assertThat (getKeys (results ))
637
- .named ("query result keys" )
638
- .containsExactly (c .getKey (), d .getKey (), b .getKey ())
639
- .inOrder ();
620
+ List <Entity > results =
621
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
622
+ assertThat (results ).named ("query results" ).containsExactly (c , d , b ).inOrder ();
640
623
}
641
624
642
625
@ Test
@@ -700,7 +683,8 @@ public void queryRestrictions_surprisingMultipleValuesAllMustMatch_returnsNoEnti
700
683
// Entity "a" will not match because no individual value matches all filters.
701
684
// See the documentation for more details:
702
685
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
703
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
686
+ List <Entity > results =
687
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
704
688
assertThat (results ).named ("query results" ).isEmpty ();
705
689
}
706
690
@@ -731,8 +715,9 @@ public void queryRestrictions_surprisingMultipleValuesEquals_returnsMatchedEntit
731
715
// Only "a" and "e" have both 1 and 2 in the "x" array-valued property.
732
716
// See the documentation for more details:
733
717
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
734
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
735
- assertThat (getKeys (results )).named ("query result keys" ).containsExactly (a .getKey (), e .getKey ());
718
+ List <Entity > results =
719
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
720
+ assertThat (results ).named ("query results" ).containsExactly (a , e );
736
721
}
737
722
738
723
@ Test
@@ -757,10 +742,9 @@ public void queryRestrictions_surprisingMultipleValuesNotEquals_returnsMatchedEn
757
742
// The query matches any entity that has a some value other than 1. Only
758
743
// entity "e" is not matched. See the documentation for more details:
759
744
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
760
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
761
- assertThat (getKeys (results ))
762
- .named ("query result keys" )
763
- .containsExactly (a .getKey (), b .getKey (), c .getKey (), d .getKey ());
745
+ List <Entity > results =
746
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
747
+ assertThat (results ).named ("query results" ).containsExactly (a , b , c , d );
764
748
}
765
749
766
750
@ Test
@@ -788,8 +772,9 @@ public void queryRestrictions_surprisingMultipleValuesTwoNotEquals_returnsMatche
788
772
//
789
773
// See the documentation for more details:
790
774
// https://cloud.google.com/appengine/docs/java/datastore/query-restrictions#properties_with_multiple_values_can_behave_in_surprising_ways
791
- List <Entity > results = datastore .prepare (q ).asList (FetchOptions .Builder .withDefaults ());
792
- assertThat (getKeys (results )).named ("query result keys" ).containsExactly (b .getKey ());
775
+ List <Entity > results =
776
+ datastore .prepare (q .setKeysOnly ()).asList (FetchOptions .Builder .withDefaults ());
777
+ assertThat (results ).named ("query results" ).containsExactly (b );
793
778
}
794
779
795
780
private Entity retrievePersonWithLastName (String targetLastName ) {
@@ -814,7 +799,7 @@ public void singleRetrievalExample_singleEntity_returnsEntity() throws Exception
814
799
815
800
Entity result = retrievePersonWithLastName ("Johnson" );
816
801
817
- assertThat (result . getKey ()) .named ("result key " ).isEqualTo (a . getKey ());
802
+ assertThat (result ) .named ("result" ).isEqualTo (a ); // Note: Entity.equals() only checks the Key.
818
803
}
819
804
820
805
@ Test
@@ -866,17 +851,6 @@ public void queryLimitExample_returnsLimitedEntities() throws Exception {
866
851
867
852
List <Entity > results = getTallestPeople ();
868
853
869
- assertThat (getKeys (results ))
870
- .named ("result keys" )
871
- .containsExactly (g .getKey (), e .getKey (), c .getKey (), a .getKey (), b .getKey ())
872
- .inOrder ();
873
- }
874
-
875
- private ImmutableList <Key > getKeys (List <Entity > entities ) {
876
- ImmutableList .Builder <Key > keys = ImmutableList .builder ();
877
- for (Entity entity : entities ) {
878
- keys .add (entity .getKey ());
879
- }
880
- return keys .build ();
854
+ assertThat (results ).named ("results" ).containsExactly (g , e , c , a , b ).inOrder ();
881
855
}
882
856
}
0 commit comments