26
26
import com .google .cloud .datastore .AggregationResult ;
27
27
import com .google .cloud .datastore .Datastore ;
28
28
import com .google .cloud .datastore .Datastore .TransactionCallable ;
29
- import com .google .cloud .datastore .DatastoreOptions ;
30
29
import com .google .cloud .datastore .Entity ;
31
30
import com .google .cloud .datastore .EntityQuery ;
32
31
import com .google .cloud .datastore .GqlQuery ;
33
32
import com .google .cloud .datastore .Key ;
34
33
import com .google .cloud .datastore .Query ;
35
34
import com .google .cloud .datastore .QueryResults ;
36
35
import com .google .cloud .datastore .Transaction ;
37
- import com .google .cloud .datastore .testing .RemoteDatastoreHelper ;
38
36
import com .google .common .collect .ImmutableList ;
39
37
import com .google .datastore .v1 .TransactionOptions ;
40
38
import com .google .datastore .v1 .TransactionOptions .ReadOnly ;
43
41
import java .util .concurrent .Executors ;
44
42
import java .util .concurrent .Future ;
45
43
import org .junit .After ;
46
- import org .junit .AfterClass ;
44
+ import org .junit .BeforeClass ;
45
+ import org .junit .ClassRule ;
47
46
import org .junit .Test ;
48
47
49
48
// TODO(jainsahab) Move all the aggregation related tests from ITDatastoreTest to this file
50
49
public class ITDatastoreAggregationsTest {
51
50
52
- private static final RemoteDatastoreHelper HELPER = RemoteDatastoreHelper . create ();
53
- private static final DatastoreOptions OPTIONS = HELPER . getOptions ();
54
- private static final Datastore DATASTORE = OPTIONS . getService () ;
51
+ @ ClassRule public static MultiDbRule multiDbRule = new MultiDbRule ();
52
+
53
+ private static Datastore DATASTORE ;
55
54
56
55
private static final String KIND = "Marks" ;
57
56
57
+ @ BeforeClass
58
+ public static void beforeClass () throws Exception {
59
+ DATASTORE = multiDbRule .getDatastore ();
60
+ }
61
+
58
62
@ After
59
63
public void tearDown () {
60
64
EntityQuery allEntitiesQuery = Query .newEntityQueryBuilder ().build ();
@@ -64,11 +68,6 @@ public void tearDown() {
64
68
DATASTORE .delete (keysToDelete );
65
69
}
66
70
67
- @ AfterClass
68
- public static void afterClass () throws Exception {
69
- DATASTORE .close ();
70
- }
71
-
72
71
Key key1 = DATASTORE .newKeyFactory ().setKind (KIND ).newKey (1 );
73
72
Key key2 = DATASTORE .newKeyFactory ().setKind (KIND ).newKey (2 );
74
73
Key key3 = DATASTORE .newKeyFactory ().setKind (KIND ).newKey (3 );
@@ -89,7 +88,6 @@ public void testSumAggregation() {
89
88
Query .newAggregationQueryBuilder ()
90
89
.over (baseQuery )
91
90
.addAggregations (sum ("marks" ).as ("total_marks" ))
92
- .setNamespace (OPTIONS .getNamespace ())
93
91
.build ();
94
92
95
93
// sum of 2 entities
@@ -108,11 +106,7 @@ public void testSumAggregationWithAutoGeneratedAlias() {
108
106
109
107
EntityQuery baseQuery = Query .newEntityQueryBuilder ().setKind (KIND ).build ();
110
108
AggregationQuery aggregationQuery =
111
- Query .newAggregationQueryBuilder ()
112
- .over (baseQuery )
113
- .addAggregations (sum ("marks" ))
114
- .setNamespace (OPTIONS .getNamespace ())
115
- .build ();
109
+ Query .newAggregationQueryBuilder ().over (baseQuery ).addAggregations (sum ("marks" )).build ();
116
110
117
111
// sum of 2 entities
118
112
assertThat (getOnlyElement (DATASTORE .runAggregation (aggregationQuery )).getLong ("property_1" ))
@@ -133,11 +127,7 @@ public void testSumAggregationInGqlQuery() {
133
127
"AGGREGATE SUM(marks) AS total_marks OVER (SELECT * FROM Marks)" )
134
128
.build ();
135
129
136
- AggregationQuery aggregationQuery =
137
- Query .newAggregationQueryBuilder ()
138
- .over (gqlQuery )
139
- .setNamespace (OPTIONS .getNamespace ())
140
- .build ();
130
+ AggregationQuery aggregationQuery = Query .newAggregationQueryBuilder ().over (gqlQuery ).build ();
141
131
142
132
// sum of 2 entities
143
133
assertThat (getOnlyElement (DATASTORE .runAggregation (aggregationQuery )).getLong ("total_marks" ))
@@ -158,7 +148,6 @@ public void testSumAggregationWithResultOfDoubleType() {
158
148
Query .newAggregationQueryBuilder ()
159
149
.over (baseQuery )
160
150
.addAggregations (sum ("cgpa" ).as ("total_cgpa" ))
161
- .setNamespace (OPTIONS .getNamespace ())
162
151
.build ();
163
152
164
153
// sum of 2 entities
@@ -180,7 +169,6 @@ public void testAvgAggregation() {
180
169
Query .newAggregationQueryBuilder ()
181
170
.over (baseQuery )
182
171
.addAggregations (avg ("marks" ).as ("avg_marks" ))
183
- .setNamespace (OPTIONS .getNamespace ())
184
172
.build ();
185
173
186
174
// avg of 2 entities
@@ -199,11 +187,7 @@ public void testAvgAggregationWithAutoGeneratedAlias() {
199
187
200
188
EntityQuery baseQuery = Query .newEntityQueryBuilder ().setKind (KIND ).build ();
201
189
AggregationQuery aggregationQuery =
202
- Query .newAggregationQueryBuilder ()
203
- .over (baseQuery )
204
- .addAggregations (avg ("marks" ))
205
- .setNamespace (OPTIONS .getNamespace ())
206
- .build ();
190
+ Query .newAggregationQueryBuilder ().over (baseQuery ).addAggregations (avg ("marks" )).build ();
207
191
208
192
// avg of 2 entities
209
193
assertThat (getOnlyElement (DATASTORE .runAggregation (aggregationQuery )).getDouble ("property_1" ))
@@ -223,11 +207,7 @@ public void testAvgAggregationInGqlQuery() {
223
207
Query .newGqlQueryBuilder ("AGGREGATE AVG(marks) AS avg_marks OVER (SELECT * FROM Marks)" )
224
208
.build ();
225
209
226
- AggregationQuery aggregationQuery =
227
- Query .newAggregationQueryBuilder ()
228
- .over (gqlQuery )
229
- .setNamespace (OPTIONS .getNamespace ())
230
- .build ();
210
+ AggregationQuery aggregationQuery = Query .newAggregationQueryBuilder ().over (gqlQuery ).build ();
231
211
232
212
// avg of 2 entities
233
213
assertThat (getOnlyElement (DATASTORE .runAggregation (aggregationQuery )).getDouble ("avg_marks" ))
@@ -249,7 +229,6 @@ public void testSumAndAvgAggregationTogether() {
249
229
.over (baseQuery )
250
230
.addAggregations (sum ("marks" ).as ("total_marks" ))
251
231
.addAggregations (avg ("marks" ).as ("avg_marks" ))
252
- .setNamespace (OPTIONS .getNamespace ())
253
232
.build ();
254
233
255
234
// sum of 2 entities
@@ -271,7 +250,6 @@ public void testTransactionShouldReturnAConsistentSnapshot() {
271
250
.addAggregation (count ().as ("count" ))
272
251
.addAggregations (sum ("marks" ).as ("total_marks" ))
273
252
.addAggregations (avg ("marks" ).as ("avg_marks" ))
274
- .setNamespace (OPTIONS .getNamespace ())
275
253
.build ();
276
254
277
255
// original entity count is 2
@@ -332,7 +310,6 @@ public void testReadOnlyTransactionShouldNotLockTheDocuments()
332
310
.addAggregation (count ().as ("count" ))
333
311
.addAggregations (sum ("marks" ).as ("total_marks" ))
334
312
.addAggregations (avg ("marks" ).as ("avg_marks" ))
335
- .setNamespace (OPTIONS .getNamespace ())
336
313
.build ();
337
314
338
315
TransactionOptions transactionOptions =
0 commit comments