summaryrefslogtreecommitdiff
path: root/jstests/core/api/api_version_find_and_modify.js
blob: 78e9a53a89821ae20ea8ffcb5d08835b95d82161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Tests the findAndModify command under different scenarios with API versioning enabled.
 *
 * @tags: [
 *   assumes_unsharded_collection,
 *   uses_api_parameters,
 * ]
 */
(function() {
"use strict";

const testDB = db.getSiblingDB("findAndModifyAPIVersion");
testDB.dropDatabase();

const collName = "testColl";
const coll = testDB.getCollection(collName);
const incStockFactor = 10;
let curStock = 10;

assert.commandWorked(coll.insert({itemNumber: 1, stockUnit: curStock}));

const assertStockUnits = function() {
    const result = coll.find({itemNumber: 1}).toArray();
    assert.eq(result.length, 1);
    curStock += incStockFactor;
    assert.eq(result[0].stockUnit, curStock);
};

// Test the command with latest command name and 'apiStrict'.
assert.commandWorked(testDB.runCommand({
    findAndModify: collName,
    query: {itemNumber: 1},
    update: {"$inc": {stockUnit: incStockFactor}},
    apiVersion: "1",
    apiStrict: true
}));
assertStockUnits();

// Test the command with command alias 'findandmodify' and 'apiStrict'.
const result = testDB.runCommand({
    findandmodify: collName,
    query: {itemNumber: 1},
    update: {"$inc": {stockUnit: incStockFactor}},
    apiVersion: "1",
    apiStrict: true
});
assert.commandFailedWithCode(result, ErrorCodes.APIStrictError);

// Test the command with latest command name without 'apiStrict'.
assert.commandWorked(testDB.runCommand({
    findAndModify: collName,
    query: {itemNumber: 1},
    update: {"$inc": {stockUnit: incStockFactor}},
    apiVersion: "1"
}));
assertStockUnits();

// Test the command with command alias 'findandmodify' without 'apiStrict'.
assert.commandWorked(testDB.runCommand({
    findandmodify: collName,
    query: {itemNumber: 1},
    update: {"$inc": {stockUnit: incStockFactor}},
    apiVersion: "1"
}));
assertStockUnits();
})();