-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
comment_with_falsy_values.test.ts
213 lines (194 loc) · 5.62 KB
/
comment_with_falsy_values.test.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { expect } from 'chai';
import { Collection, CommandStartedEvent, Long, MongoClient } from '../../../src';
import { TestBuilder, UnifiedTestSuiteBuilder } from '../../tools/utils';
const falsyValues = [0, false, '', Long.ZERO, null, NaN] as const;
const falsyToString = (value: typeof falsyValues[number]) => {
if (Number.isNaN(value)) {
return 'NaN';
}
if (value === '') {
return "''";
}
if (value?._bsontype === 'Long') {
return 'Long.ZERO';
}
return JSON.stringify(value);
};
function* generateTestCombinations() {
for (const [name, args] of [
['find', { filter: { _id: 1 } }] as const,
['aggregate', { pipeline: [] }] as const,
['insertMany', { documents: [{ name: 'john' }] }] as const,
['deleteOne', { filter: { toBeDeleted: true } }] as const,
['findOneAndReplace', { filter: { _id: 1 }, replacement: { x: 12 } }] as const,
['estimatedDocumentCount', {}] as const
]) {
for (const falsyValue of falsyValues) {
yield { name, args: { ...args, comment: falsyValue } };
}
}
}
const tests = Array.from(generateTestCombinations()).map(({ name, args }) => {
const description = `${name} should pass falsy value ${falsyToString(
args.comment
)} for comment option`;
return new TestBuilder(description)
.operation({
name,
object: 'collection0',
arguments: args
})
.expectEvents({
client: 'client0',
events: [
{
commandStartedEvent: {
command: {
comment: args.comment
}
}
}
]
})
.toJSON();
});
const testsForChangeStreamsAggregate = falsyValues.map(falsyValue => {
const description = `ChangeStreams should pass falsy value ${falsyToString(
falsyValue
)} for comment option on initial aggregate`;
return new TestBuilder(description)
.operation({
name: 'createChangeStream',
object: 'collection0',
arguments: {
pipeline: [],
comment: falsyValue
},
saveResultAsEntity: 'changeStream0'
})
.expectEvents({
client: 'client0',
events: [
{
commandStartedEvent: {
command: {
comment: falsyValue
}
}
}
]
})
.toJSON();
});
const testsForGetMore = falsyValues.map(falsyValue => {
const description = `ChangeStreams should pass falsy value ${falsyToString(
falsyValue
)} for comment option on getMore`;
return new TestBuilder(description)
.runOnRequirement({ topologies: ['replicaset'] })
.operation({
name: 'createChangeStream',
object: 'collection0',
arguments: {
pipeline: [],
comment: falsyValue
},
saveResultAsEntity: 'changeStream0'
})
.operation({
name: 'insertOne',
object: 'collection0',
arguments: {
document: {
a: 1
}
}
})
.operation({
name: 'iterateUntilDocumentOrError',
object: 'changeStream0',
arguments: {}
})
.expectEvents({
client: 'client0',
events: [
{
commandStartedEvent: {
command: {
comment: falsyValue
}
}
},
{
commandStartedEvent: {}
},
{
commandStartedEvent: {
command: {
comment: falsyValue
}
}
}
]
})
.toJSON();
});
describe('Comment with falsy values', () => {
UnifiedTestSuiteBuilder.describe('Comment with Falsy Values')
.runOnRequirement({ minServerVersion: '4.4.0' })
.createEntities(UnifiedTestSuiteBuilder.defaultEntities)
.initialData({
collectionName: 'collection0',
databaseName: 'database0',
documents: [
{ _id: 1, x: 11 },
{ _id: 2, toBeDeleted: true } // This should only be used by the delete test
]
})
.test(tests)
.run();
UnifiedTestSuiteBuilder.describe('Change Streams Comment with Falsy Values')
.schemaVersion('1.0')
.createEntities(UnifiedTestSuiteBuilder.defaultEntities)
.initialData({
collectionName: 'collection0',
databaseName: 'database0',
documents: []
})
.runOnRequirement({
minServerVersion: '4.4.0',
topologies: ['replicaset', 'sharded-replicaset']
})
.test(testsForChangeStreamsAggregate)
.test(testsForGetMore)
.run();
context('Collection.distinct()', function () {
let client: MongoClient;
let collection: Collection;
let commands: CommandStartedEvent[] = [];
beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
client.on('commandStarted', e => commands.push(e));
await client.connect();
collection = await client.db('comment-falsy-values').createCollection('collection');
commands = [];
});
afterEach(async function () {
await collection.drop();
await client.close();
});
for (const falsyValue of falsyValues) {
it(`distinct should send falsy value ${falsyToString(
falsyValue
)} on the command`, async function () {
await collection.distinct('some-key', {}, { comment: falsyValue }).catch(() => null);
expect(commands).to.have.lengthOf(1);
const distinctCommand = commands.find(command => command.commandName === 'distinct');
expect(distinctCommand).to.exist;
// chai does not narrow types, so TS doesn't know the distinct command exists at this point.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(distinctCommand!.command).to.haveOwnProperty('comment');
});
}
});
});