forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_monitoring_events.test.js
More file actions
94 lines (85 loc) · 3.52 KB
/
command_monitoring_events.test.js
File metadata and controls
94 lines (85 loc) · 3.52 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
const { OpQueryRequest, OpMsgRequest } = require('../../../src/cmap/commands');
const { CommandStartedEvent } = require('../../../src/cmap/command_monitoring_events');
const { expect } = require('chai');
describe('Command Monitoring Events - unit/cmap', function () {
const commands = [
new OpQueryRequest('admin', { a: { b: 10 }, $query: { b: 10 } }, {}),
new OpQueryRequest('hello', { a: { b: 10 }, $query: { b: 10 } }, {}),
new OpMsgRequest('admin', { b: { c: 20 } }, {}),
new OpMsgRequest('hello', { b: { c: 20 } }, {}),
{ ns: 'admin.$cmd', query: { $query: { a: 16 } } },
{ ns: 'hello there', f1: { h: { a: 52, b: { c: 10, d: [1, 2, 3, 5] } } } }
];
for (const command of commands) {
it(`should make a deep copy of object of type: ${command.constructor.name}`, () => {
const ev = new CommandStartedEvent({ id: 'someId', address: 'someHost' }, command);
if (command instanceof OpQueryRequest) {
if (command.ns === 'admin.$cmd') {
expect(ev.command !== command.query.$query).to.equal(true);
for (const k in command.query.$query) {
expect(ev.command[k]).to.deep.equal(command.query.$query[k]);
}
} else {
expect(ev.command.filter !== command.query.$query).to.equal(true);
for (const k in command.query.$query) {
expect(ev.command.filter[k]).to.deep.equal(command.query.$query[k]);
}
}
} else if (command instanceof OpMsgRequest) {
expect(ev.command !== command.command).to.equal(true);
expect(ev.command).to.deep.equal(command.command);
} else if (typeof command === 'object') {
if (command.ns === 'admin.$cmd') {
expect(ev.command !== command.query.$query).to.equal(true);
for (const k in command.query.$query) {
expect(ev.command[k]).to.deep.equal(command.query.$query[k]);
}
}
}
});
}
describe('CommandStartedEvent', function () {
const conn = { id: '<some id>', address: '<some address>' };
it('should wrap a basic query option', function () {
const db = 'test1';
const query = new OpQueryRequest(
`${db}`,
{
testCmd: 1,
fizz: 'buzz',
star: 'trek'
},
{}
);
const startEvent = new CommandStartedEvent(conn, query);
expect(startEvent).to.have.property('commandName', 'testCmd');
expect(startEvent).to.have.property('databaseName', db);
expect(startEvent).to.have.property('requestId', query.requestId);
expect(startEvent).to.have.property('connectionId').that.is.a('string');
expect(startEvent).to.have.property('command').that.deep.equals(query.query);
});
it('should upconvert a Query wrapping a command into the corresponding command', function () {
const db = 'admin';
const query = new OpQueryRequest(
`${db}`,
{
$query: {
testCmd: 1,
fizz: 'buzz',
star: 'trek',
batchSize: 0,
skip: 0
}
},
{}
);
const startEvent = new CommandStartedEvent(conn, query);
expect(startEvent).to.have.property('commandName', 'testCmd');
expect(startEvent).to.have.property('databaseName', db);
expect(startEvent).to.have.property('requestId', query.requestId);
expect(startEvent).to.have.property('connectionId').that.is.a('string');
expect(startEvent).to.have.property('command').that.deep.equals(query.query.$query);
});
});
});