summaryrefslogtreecommitdiff
path: root/jstests/core/shell_writeconcern.js
blob: fb30601fbcd435b1c893dd084ce82dce3ea9f531 (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
// Cannot implicitly shard accessed collections because of following errmsg: A single
// update/delete on a sharded collection must contain an exact match on _id or contain the shard
// key.
//
// @tags: [
//   assumes_unsharded_collection,
//   assumes_write_concern_unchanged,
// ]

"use strict";
// check that shell writeconcern work correctly
// 1.) tests that it can be set on each level and is inherited
// 2.) tests that each operation (update/insert/remove/save) take and ensure a write concern

var collA = db.shell_wc_a;
var collB = db.shell_wc_b;
collA.drop();
collB.drop();

// test inheritance
db.setWriteConcern({w: 1});
assert.eq(1, db.getWriteConcern().toJSON().w);
assert.eq(1, collB.getWriteConcern().toJSON().w);

collA.setWriteConcern({w: 2});
assert.eq(2, collA.getWriteConcern().toJSON().w);
collA.unsetWriteConcern();
assert.eq(1, collA.getWriteConcern().toJSON().w);

db.unsetWriteConcern();
assert.eq(undefined, collA.getWriteConcern());
assert.eq(undefined, collB.getWriteConcern());
assert.eq(undefined, db.getWriteConcern());

// test methods, by generating an error
var res = assert.commandWorked(collA.save({_id: 1}, {writeConcern: {w: 1}}));
assert.eq(1, res.nUpserted, tojson(res));

var res = assert.commandWorked(collA.update({_id: 1}, {_id: 1}, {writeConcern: {w: 1}}));
assert.eq(1, res.nMatched, tojson(res));

var res = assert.commandWorked(collA.update({_id: 1}, {_id: 1}, {writeConcern: {w: 1}}));
assert.eq(1, res.nMatched, tojson(res));

var res = assert.commandWorked(collA.insert({_id: 2}, {writeConcern: {w: 1}}));
assert.eq(1, res.nInserted, tojson(res));

var res = assert.commandWorked(collA.remove({_id: 3}, {writeConcern: {w: 1}}));
assert.eq(0, res.nRemoved, tojson(res));

var res = assert.commandWorked(collA.remove({_id: 1}, {writeConcern: {w: 1}}));
assert.eq(1, res.nRemoved, tojson(res));

// Test ordered write concern, and that the write concern isn't run/error.
assert.commandWorked(collA.insert({_id: 1}));

var res =
    assert.writeError(collA.insert([{_id: 1}, {_id: 1}], {ordered: true, writeConcern: {w: 1}}));
assert.eq(1, res.getWriteErrors().length, tojson(res));
assert.eq(undefined, res.writeConcernErrors, tojson(res));

var res =
    assert.writeError(collA.insert([{_id: 1}, {_id: 1}], {ordered: false, writeConcern: {w: 1}}));
assert.eq(2, res.getWriteErrors().length, tojson(res));
assert.eq(undefined, res.writeConcernErrors, tojson(res));