Skip to content

Commit

Permalink
test(crud): update to latest specs #1622
Browse files Browse the repository at this point in the history
Fixes bulkWrite to pass on arrayFilters
Fixes insertMany to return insertedIds in the correct format
Adds tests for bulkWrite, insertOne and insertMany
Updates the tests files from the specification directory.
NODE-1228
  • Loading branch information
Jessica Lord authored Dec 21, 2017
1 parent 44373f3 commit 6eede98
Show file tree
Hide file tree
Showing 81 changed files with 312 additions and 1,070 deletions.
13 changes: 13 additions & 0 deletions CHANGES_3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ testCollection.updateOne({_id: 'test'}, {});

Wherever it occurs, the option `keepAlive` has been changed. `keepAlive` is now a boolean that enables/disables `keepAlive`, while `keepAliveInitialDelay` specifies how long to wait before initiating keepAlive. This brings the API in line with [NodeJS's socket api](https://nodejs.org/dist/latest-v9.x/docs/api/all.html#net_socket_setkeepalive_enable_initialdelay)

### `insertMany`

Now `insertMany` returns `insertedIds` in a map of the index of the inserted document to the id of the inserted document:

```js
{
"0": 2,
"1": 3
}
```

Previously an array of ids was returned: `[ 2, 3 ]`. This change occurs with both ordered and unordered `insertMany` calls, see the [CRUD specifications](https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#results) for more details.

### `geoNear` command helper

The functionality of the geoNear command is duplicated elsewhere in the language, in the `$near`/`$nearSphere` query operators on unsharded collections, and in the `$geoNear` aggregation stage on all collections. Maintaining this command increases our test surface, and creates additional work when adding features that must be supported on all read commands. As a result, the command will be fully
Expand Down
1 change: 1 addition & 0 deletions lib/bulk/ordered.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ OrderedBulkOperation.prototype.raw = function(op) {
var operation = { q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi };
operation.upsert = op[key].upsert ? true : false;
if (op.collation) operation.collation = op.collation;
if (op[key].arrayFilters) operation.arrayFilters = op[key].arrayFilters;
return addToOperationsList(this, common.UPDATE, operation);
}

Expand Down
1 change: 1 addition & 0 deletions lib/bulk/unordered.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ UnorderedBulkOperation.prototype.raw = function(op) {
var multi = op.updateOne || op.replaceOne ? false : true;
var operation = { q: op[key].filter, u: op[key].update || op[key].replacement, multi: multi };
if (op[key].upsert) operation.upsert = true;
if (op[key].arrayFilters) operation.arrayFilters = op[key].arrayFilters;
return addToOperationsList(this, common.UPDATE, operation);
}

Expand Down
14 changes: 2 additions & 12 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,21 +427,11 @@ var insertOne = function(self, doc, options, callback) {
};

var mapInserManyResults = function(docs, r) {
var ids = r.getInsertedIds();
var keys = Object.keys(ids);
var finalIds = new Array(keys.length);

for (var i = 0; i < keys.length; i++) {
if (ids[keys[i]]._id) {
finalIds[ids[keys[i]].index] = ids[keys[i]]._id;
}
}

var finalResult = {
result: { ok: 1, n: r.insertedCount },
ops: docs,
insertedCount: r.insertedCount,
insertedIds: finalIds
insertedIds: r.insertedIds
};

if (r.getLastOp()) {
Expand Down Expand Up @@ -724,7 +714,7 @@ define.classMethod('bulkWrite', { callback: true, promise: true });
* @typedef {Object} Collection~insertWriteOpResult
* @property {Number} insertedCount The total amount of documents inserted.
* @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
* @property {ObjectId[]} insertedIds All the generated _id's for the inserted documents.
* @property {Object.<Number, ObjectId>} insertedIds Map of the index of the inserted document to the id of the inserted document.
* @property {object} connection The connection object used for the operation.
* @property {object} result The raw command result object returned from MongoDB (content might vary by server version).
* @property {Number} result.ok Is 1 if the command executed correctly.
Expand Down
2 changes: 1 addition & 1 deletion test/functional/crud_api_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe('CRUD API', function() {
test.equal(null, err);
test.equal(2, r.result.n);
test.equal(2, r.insertedCount);
test.equal(2, r.insertedIds.length);
test.equal(2, Object.keys(r.insertedIds).length);

// Ordered bulk unordered
bulkWriteUnOrdered();
Expand Down
51 changes: 51 additions & 0 deletions test/functional/crud_spec_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,52 @@ describe('CRUD spec', function() {
});
}

function executeInsertTest(scenarioTest, db, collection) {
const args = scenarioTest.operation.arguments;
const documents = args.document || args.documents;
let options = Object.assign({}, args);
delete options.document;
delete options.documents;

return collection[scenarioTest.operation.name](documents, options).then(function(result) {
Object.keys(scenarioTest.outcome.result).forEach(function(resultName) {
test.deepEqual(result[resultName], scenarioTest.outcome.result[resultName]);
});

if (scenarioTest.outcome.collection) {
return collection
.find({})
.toArray()
.then(function(results) {
test.deepEqual(results, scenarioTest.outcome.collection.data);
});
}
});
}

function executeBulkTest(scenarioTest, db, collection) {
const args = scenarioTest.operation.arguments;
const operations = args.requests.map(function(operation) {
let op = {};
op[operation.name] = operation['arguments'];
return op;
});
const options = Object.assign({}, args.options);

collection.bulkWrite(operations, options).then(function(result) {
Object.keys(scenarioTest.outcome.result).forEach(function(resultName) {
test.deepEqual(result[resultName], scenarioTest.outcome.result[resultName]);
});
});

return collection
.find({})
.toArray()
.then(function(results) {
test.deepEqual(results, scenarioTest.outcome.collection.data);
});
}

function executeReplaceTest(scenarioTest, db, collection) {
var args = scenarioTest.operation.arguments;
var filter = args.filter;
Expand Down Expand Up @@ -326,6 +372,11 @@ describe('CRUD spec', function() {
case 'findOneAndUpdate':
case 'findOneAndDelete':
return executeFindOneTest(scenarioTest, context.db, collection);
case 'insertOne':
case 'insertMany':
return executeInsertTest(scenarioTest, context.db, collection);
case 'bulkWrite':
return executeBulkTest(scenarioTest, context.db, collection);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/functional/gridfs_stream_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ describe('GridFS Stream', function() {

db.collection('fs.files').insert({ length: 0 }, function(error, result) {
test.equal(error, null);
test.equal(result.insertedIds.length, 1);
test.equal(Object.keys(result.insertedIds).length, 1);
var id = result.insertedIds[0];

var stream = bucket.openDownloadStream(id);
Expand Down
6 changes: 3 additions & 3 deletions test/functional/insert_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Insert', function() {
test.equal(2, r.result.n);
test.equal(2, r.ops.length);
test.equal(2, r.insertedCount);
test.equal(2, r.insertedIds.length);
test.equal(2, Object.keys(r.insertedIds).length);
test.ok(r.insertedIds[0]._bsontype === 'ObjectID');
test.ok(r.insertedIds[1]._bsontype === 'ObjectID');

Expand Down Expand Up @@ -2802,7 +2802,7 @@ describe('Insert', function() {
.collection('inserted_ids_test')
.insertMany([{}, {}, {}], { ordered: true })
.then(function(r) {
test.equal(3, r.insertedIds.length);
test.equal(3, Object.keys(r.insertedIds).length);
client.close();
done();
});
Expand All @@ -2823,7 +2823,7 @@ describe('Insert', function() {
.insertMany([{}, {}, {}], { ordered: false })
.then(function(r) {
test.equal(null, err);
test.equal(3, r.insertedIds.length);
test.equal(3, Object.keys(r.insertedIds).length);
client.close();
done();
});
Expand Down
5 changes: 2 additions & 3 deletions test/functional/spec/crud/README.rst
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,5 @@ Use as integration tests

Running these as integration tests will require a running mongod server. Each of
these tests is valid against a standalone mongod, a replica set, and a sharded
system for server version 3.0.0. Many of them will run against 2.4 and 2.6, but
some will require conditional code. For instance, ``$out`` is not supported in
an aggregation pipeline in server 2.4, so that test must be skipped.
system for server version 3.0 and later. Many of them will run against 2.6, but
some will require conditional code.
Empty file modified test/functional/spec/crud/read/aggregate-collation.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/aggregate-collation.yml
100755 → 100644
Empty file.
51 changes: 51 additions & 0 deletions test/functional/spec/crud/read/aggregate-out.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,57 @@
]
}
}
},
{
"description": "Aggregate with $out and batch size of 0",
"operation": {
"name": "aggregate",
"arguments": {
"pipeline": [
{
"$sort": {
"x": 1
}
},
{
"$match": {
"_id": {
"$gt": 1
}
}
},
{
"$out": "other_test_collection"
}
],
"batchSize": 0
}
},
"outcome": {
"result": [
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
],
"collection": {
"name": "other_test_collection",
"data": [
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
}
}
]
}
21 changes: 21 additions & 0 deletions test/functional/spec/crud/read/aggregate-out.yml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,24 @@ tests:
data:
- {_id: 2, x: 22}
- {_id: 3, x: 33}
-
description: "Aggregate with $out and batch size of 0"
operation:
name: aggregate
arguments:
pipeline:
- $sort: {x: 1}
- $match:
_id: {$gt: 1}
- $out: "other_test_collection"
batchSize: 0

outcome:
result:
- {_id: 2, x: 22}
- {_id: 3, x: 33}
collection:
name: "other_test_collection"
data:
- {_id: 2, x: 22}
- {_id: 3, x: 33}
Empty file modified test/functional/spec/crud/read/aggregate.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/aggregate.yml
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/count-collation.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/count-collation.yml
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/count.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/count.yml
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/distinct-collation.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/distinct-collation.yml
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/distinct.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/distinct.yml
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/find-collation.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/find-collation.yml
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/find.json
100755 → 100644
Empty file.
Empty file modified test/functional/spec/crud/read/find.yml
100755 → 100644
Empty file.
110 changes: 110 additions & 0 deletions test/functional/spec/crud/write/bulkWrite-arrayFilters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"data": [
{
"_id": 1,
"y": [
{
"b": 3
},
{
"b": 1
}
]
},
{
"_id": 2,
"y": [
{
"b": 0
},
{
"b": 1
}
]
}
],
"minServerVersion": "3.5.6",
"tests": [
{
"description": "BulkWrite with arrayFilters",
"operation": {
"arguments": {
"options": {
"ordered": true
},
"requests": [
{
"arguments": {
"arrayFilters": [
{
"i.b": 3
}
],
"filter": {},
"update": {
"$set": {
"y.$[i].b": 2
}
}
},
"name": "updateOne"
},
{
"arguments": {
"arrayFilters": [
{
"i.b": 1
}
],
"filter": {},
"update": {
"$set": {
"y.$[i].b": 2
}
}
},
"name": "updateMany"
}
]
},
"name": "bulkWrite"
},
"outcome": {
"collection": {
"data": [
{
"_id": 1,
"y": [
{
"b": 2
},
{
"b": 2
}
]
},
{
"_id": 2,
"y": [
{
"b": 0
},
{
"b": 2
}
]
}
]
},
"result": {
"deletedCount": 0,
"insertedIds": {},
"matchedCount": 3,
"modifiedCount": 3,
"upsertedCount": 0,
"upsertedIds": {}
}
}
}
]
}
Loading

0 comments on commit 6eede98

Please sign in to comment.