Skip to content

Commit e25a78f

Browse files
committed
Resynced Crud tests
JAVA-2371
1 parent 893eb99 commit e25a78f

32 files changed

+2881
-1938
lines changed

driver-async/src/test/functional/com/mongodb/async/client/CrudTest.java

+114-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
package com.mongodb.async.client;
1818

1919
import com.mongodb.MongoNamespace;
20+
import com.mongodb.client.model.Collation;
21+
import com.mongodb.client.model.CollationAlternate;
22+
import com.mongodb.client.model.CollationCaseFirst;
23+
import com.mongodb.client.model.CollationMaxVariable;
24+
import com.mongodb.client.model.CollationStrength;
2025
import com.mongodb.client.model.CountOptions;
26+
import com.mongodb.client.model.DeleteOptions;
2127
import com.mongodb.client.model.FindOneAndDeleteOptions;
2228
import com.mongodb.client.model.FindOneAndReplaceOptions;
2329
import com.mongodb.client.model.FindOneAndUpdateOptions;
@@ -106,6 +112,10 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
106112
List<Object[]> data = new ArrayList<Object[]>();
107113
for (File file : JsonPoweredTestHelper.getTestFiles("/crud")) {
108114
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
115+
if (testDocument.containsKey("minServerVersion")
116+
&& !serverAtLeastMinVersion(testDocument.getString("minServerVersion").getValue())) {
117+
continue;
118+
}
109119
for (BsonValue test : testDocument.getArray("tests")) {
110120
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
111121
testDocument.getArray("data"), test.asDocument()});
@@ -114,6 +124,17 @@ public static Collection<Object[]> data() throws URISyntaxException, IOException
114124
return data;
115125
}
116126

127+
private static boolean serverAtLeastMinVersion(final String minServerVersionString) {
128+
List<Integer> versionList = new ArrayList<Integer>();
129+
for (String s : minServerVersionString.split("\\.")) {
130+
versionList.add(Integer.valueOf(s));
131+
}
132+
while (versionList.size() < 3) {
133+
versionList.add(0);
134+
}
135+
return serverVersionAtLeast(versionList.subList(0, 3));
136+
}
137+
117138
private boolean checkResult() {
118139
if (filename.contains("insert")) {
119140
// We don't return any id's for insert commands
@@ -236,7 +257,15 @@ private AggregateIterable<BsonDocument> getAggregateMongoOperation(final BsonDoc
236257
for (BsonValue stage : arguments.getArray("pipeline")) {
237258
pipeline.add(stage.asDocument());
238259
}
239-
return collection.aggregate(pipeline).batchSize(arguments.getNumber("batchSize").intValue());
260+
AggregateIterable<BsonDocument> iterable = collection.aggregate(pipeline);
261+
if (arguments.containsKey("batchSize")) {
262+
iterable.batchSize(arguments.getNumber("batchSize").intValue());
263+
}
264+
if (arguments.containsKey("collation")) {
265+
iterable.collation(getCollation(arguments.getDocument("collation")));
266+
}
267+
268+
return iterable;
240269
}
241270

242271
private MongoOperationLong getCountMongoOperation(final BsonDocument arguments) {
@@ -250,34 +279,51 @@ public void execute() {
250279
if (arguments.containsKey("limit")) {
251280
options.limit(arguments.getNumber("limit").intValue());
252281
}
282+
if (arguments.containsKey("collation")) {
283+
options.collation(getCollation(arguments.getDocument("collation")));
284+
}
253285
collection.count(arguments.getDocument("filter"), options, getCallback());
254286
}
255287
};
256288
}
257289

258-
private DistinctIterable<BsonInt32> getDistinctMongoOperation(final BsonDocument arguments) {
259-
return collection.distinct(arguments.getString("fieldName").getValue(), arguments.getDocument("filter"), BsonInt32.class);
290+
private DistinctIterable<BsonValue> getDistinctMongoOperation(final BsonDocument arguments) {
291+
DistinctIterable<BsonValue> iterable = collection.distinct(arguments.getString("fieldName").getValue(), BsonValue.class);
292+
if (arguments.containsKey("filter")) {
293+
iterable.filter(arguments.getDocument("filter"));
294+
}
295+
if (arguments.containsKey("collation")) {
296+
iterable.collation(getCollation(arguments.getDocument("collation")));
297+
}
298+
return iterable;
260299
}
261300

262301
private FindIterable<BsonDocument> getFindMongoOperation(final BsonDocument arguments) {
263-
FindIterable<BsonDocument> findIterable = collection.find(arguments.getDocument("filter"));
302+
FindIterable<BsonDocument> iterable = collection.find(arguments.getDocument("filter"));
264303
if (arguments.containsKey("skip")) {
265-
findIterable.skip(arguments.getNumber("skip").intValue());
304+
iterable.skip(arguments.getNumber("skip").intValue());
266305
}
267306
if (arguments.containsKey("limit")) {
268-
findIterable.limit(arguments.getNumber("limit").intValue());
307+
iterable.limit(arguments.getNumber("limit").intValue());
269308
}
270309
if (arguments.containsKey("batchSize")) {
271-
findIterable.batchSize(arguments.getNumber("batchSize").intValue());
310+
iterable.batchSize(arguments.getNumber("batchSize").intValue());
311+
}
312+
if (arguments.containsKey("collation")) {
313+
iterable.collation(getCollation(arguments.getDocument("collation")));
272314
}
273-
return findIterable;
315+
return iterable;
274316
}
275317

276318
private MongoOperationDeleteResult getDeleteManyMongoOperation(final BsonDocument arguments) {
277319
return new MongoOperationDeleteResult() {
278320
@Override
279321
public void execute() {
280-
collection.deleteMany(arguments.getDocument("filter"), getCallback());
322+
DeleteOptions options = new DeleteOptions();
323+
if (arguments.containsKey("collation")) {
324+
options.collation(getCollation(arguments.getDocument("collation")));
325+
}
326+
collection.deleteMany(arguments.getDocument("filter"), options, getCallback());
281327
}
282328
};
283329
}
@@ -286,7 +332,11 @@ private MongoOperationDeleteResult getDeleteOneMongoOperation(final BsonDocument
286332
return new MongoOperationDeleteResult() {
287333
@Override
288334
public void execute() {
289-
collection.deleteOne(arguments.getDocument("filter"), getCallback());
335+
DeleteOptions options = new DeleteOptions();
336+
if (arguments.containsKey("collation")) {
337+
options.collation(getCollation(arguments.getDocument("collation")));
338+
}
339+
collection.deleteOne(arguments.getDocument("filter"), options, getCallback());
290340
}
291341
};
292342
}
@@ -302,6 +352,9 @@ public void execute() {
302352
if (arguments.containsKey("sort")) {
303353
options.sort(arguments.getDocument("sort"));
304354
}
355+
if (arguments.containsKey("collation")) {
356+
options.collation(getCollation(arguments.getDocument("collation")));
357+
}
305358
collection.findOneAndDelete(arguments.getDocument("filter"), options, getCallback());
306359
}
307360
};
@@ -327,6 +380,9 @@ public void execute() {
327380
options.returnDocument(arguments.getString("returnDocument").getValue().equals("After") ? ReturnDocument.AFTER
328381
: ReturnDocument.BEFORE);
329382
}
383+
if (arguments.containsKey("collation")) {
384+
options.collation(getCollation(arguments.getDocument("collation")));
385+
}
330386
collection.findOneAndReplace(arguments.getDocument("filter"), arguments.getDocument("replacement"), options, getCallback());
331387
}
332388
};
@@ -351,6 +407,9 @@ public void execute() {
351407
options.returnDocument(arguments.getString("returnDocument").getValue().equals("After") ? ReturnDocument.AFTER
352408
: ReturnDocument.BEFORE);
353409
}
410+
if (arguments.containsKey("collation")) {
411+
options.collation(getCollation(arguments.getDocument("collation")));
412+
}
354413
collection.findOneAndUpdate(arguments.getDocument("filter"), arguments.getDocument("update"), options, getCallback());
355414
}
356415
};
@@ -386,6 +445,9 @@ public void execute() {
386445
if (arguments.containsKey("upsert")) {
387446
options.upsert(arguments.getBoolean("upsert").getValue());
388447
}
448+
if (arguments.containsKey("collation")) {
449+
options.collation(getCollation(arguments.getDocument("collation")));
450+
}
389451
collection.replaceOne(arguments.getDocument("filter"), arguments.getDocument("replacement"), options, getCallback());
390452
}
391453
};
@@ -399,6 +461,9 @@ public void execute() {
399461
if (arguments.containsKey("upsert")) {
400462
options.upsert(arguments.getBoolean("upsert").getValue());
401463
}
464+
if (arguments.containsKey("collation")) {
465+
options.collation(getCollation(arguments.getDocument("collation")));
466+
}
402467
collection.updateMany(arguments.getDocument("filter"), arguments.getDocument("update"), options, getCallback());
403468
}
404469
};
@@ -412,11 +477,50 @@ public void execute() {
412477
if (arguments.containsKey("upsert")) {
413478
options.upsert(arguments.getBoolean("upsert").getValue());
414479
}
480+
if (arguments.containsKey("collation")) {
481+
options.collation(getCollation(arguments.getDocument("collation")));
482+
}
415483
collection.updateOne(arguments.getDocument("filter"), arguments.getDocument("update"), options, getCallback());
416484
}
417485
};
418486
}
419487

488+
489+
Collation getCollation(final BsonDocument bsonCollation) {
490+
Collation.Builder builder = Collation.builder();
491+
if (bsonCollation.containsKey("locale")) {
492+
builder.locale(bsonCollation.getString("locale").getValue());
493+
}
494+
if (bsonCollation.containsKey("caseLevel")) {
495+
builder.caseLevel(bsonCollation.getBoolean("caseLevel").getValue());
496+
}
497+
if (bsonCollation.containsKey("caseFirst")) {
498+
builder.collationCaseFirst(CollationCaseFirst.fromString(bsonCollation.getString("caseFirst").getValue()));
499+
}
500+
if (bsonCollation.containsKey("strength")) {
501+
builder.collationStrength(CollationStrength.fromInt(bsonCollation.getInt32("strength").getValue()));
502+
}
503+
if (bsonCollation.containsKey("numericOrdering")) {
504+
builder.numericOrdering(bsonCollation.getBoolean("numericOrdering").getValue());
505+
}
506+
if (bsonCollation.containsKey("strength")) {
507+
builder.collationStrength(CollationStrength.fromInt(bsonCollation.getInt32("strength").getValue()));
508+
}
509+
if (bsonCollation.containsKey("alternate")) {
510+
builder.collationAlternate(CollationAlternate.fromString(bsonCollation.getString("alternate").getValue()));
511+
}
512+
if (bsonCollation.containsKey("maxVariable")) {
513+
builder.collationMaxVariable(CollationMaxVariable.fromString(bsonCollation.getString("maxVariable").getValue()));
514+
}
515+
if (bsonCollation.containsKey("normalization")) {
516+
builder.normalization(bsonCollation.getBoolean("normalization").getValue());
517+
}
518+
if (bsonCollation.containsKey("backwards")) {
519+
builder.backwards(bsonCollation.getBoolean("backwards").getValue());
520+
}
521+
return builder.build();
522+
}
523+
420524
abstract class MongoOperationLong extends MongoOperation<Long> {
421525
}
422526

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"data": [
3+
{
4+
"_id": 1,
5+
"x": "ping"
6+
}
7+
],
8+
"minServerVersion": "3.4",
9+
"tests": [
10+
{
11+
"description": "Aggregate with collation",
12+
"operation": {
13+
"arguments": {
14+
"collation": {
15+
"locale": "en_US",
16+
"strength": 2
17+
},
18+
"pipeline": [
19+
{
20+
"$match": {
21+
"x": "PING"
22+
}
23+
}
24+
]
25+
},
26+
"name": "aggregate"
27+
},
28+
"outcome": {
29+
"result": [
30+
{
31+
"_id": 1,
32+
"x": "ping"
33+
}
34+
]
35+
}
36+
}
37+
]
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"data": [
3+
{
4+
"_id": 1,
5+
"x": 11
6+
},
7+
{
8+
"_id": 2,
9+
"x": 22
10+
},
11+
{
12+
"_id": 3,
13+
"x": 33
14+
}
15+
],
16+
"minServerVersion": "2.6",
17+
"tests": [
18+
{
19+
"description": "Aggregate with $out",
20+
"operation": {
21+
"arguments": {
22+
"batchSize": 2,
23+
"pipeline": [
24+
{
25+
"$sort": {
26+
"x": 1
27+
}
28+
},
29+
{
30+
"$match": {
31+
"_id": {
32+
"$gt": 1
33+
}
34+
}
35+
},
36+
{
37+
"$out": "other_test_collection"
38+
}
39+
]
40+
},
41+
"name": "aggregate"
42+
},
43+
"outcome": {
44+
"collection": {
45+
"data": [
46+
{
47+
"_id": 2,
48+
"x": 22
49+
},
50+
{
51+
"_id": 3,
52+
"x": 33
53+
}
54+
],
55+
"name": "other_test_collection"
56+
},
57+
"result": [
58+
{
59+
"_id": 2,
60+
"x": 22
61+
},
62+
{
63+
"_id": 3,
64+
"x": 33
65+
}
66+
]
67+
}
68+
}
69+
]
70+
}

0 commit comments

Comments
 (0)