Skip to content

Commit 23c1629

Browse files
make tests more comprehaensive
1 parent 569e781 commit 23c1629

File tree

1 file changed

+130
-57
lines changed

1 file changed

+130
-57
lines changed

test/integration/crud/explain.test.ts

Lines changed: 130 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -140,89 +140,162 @@ describe('CRUD API explain option', function () {
140140
await client.close();
141141
});
142142

143-
describe('cursor explain commands', function () {
144-
describe('when maxTimeMS is specified via a cursor explain method, it sets the property on the command', function () {
145-
test('find()', async function () {
146-
await collection
147-
.find({ name: 'john doe' })
148-
.explain({ maxTimeMS: 2000, verbosity: 'queryPlanner' });
149-
150-
const [{ command }] = commands;
151-
expect(command).to.have.property('maxTimeMS', 2000);
143+
describe('maxTimeMS provided to explain, not to command', function () {
144+
describe('cursor commands', function () {
145+
describe('options API', function () {
146+
beforeEach(async function () {
147+
await collection
148+
.find({}, { explain: { maxTimeMS: 1000, verbosity: 'queryPlanner' } })
149+
.toArray();
150+
});
151+
152+
it('attaches maxTimeMS to the explain command', expectOnExplain(1000));
153+
154+
it('does not attach maxTimeMS to the find command', expectNotOnCommand());
152155
});
153156

154-
test('aggregate()', async function () {
155-
await collection
156-
.aggregate([{ $match: { name: 'john doe' } }])
157-
.explain({ maxTimeMS: 2000, verbosity: 'queryPlanner' });
157+
describe('fluent API', function () {
158+
beforeEach(async function () {
159+
await collection.find({}).explain({ maxTimeMS: 1000, verbosity: 'queryPlanner' });
160+
});
158161

159-
const [{ command }] = commands;
160-
expect(command).to.have.property('maxTimeMS', 2000);
162+
it('attaches maxTimeMS to the explain command', expectOnExplain(1000));
163+
164+
it('does not attach maxTimeMS to the find command', expectNotOnCommand());
161165
});
162166
});
163167

164-
it('when maxTimeMS is not specified, it is not attached to the explain command', async function () {
165-
await collection.find({ name: 'john doe' }).explain({ verbosity: 'queryPlanner' });
168+
describe('non-cursor commands', function () {
169+
beforeEach(async function () {
170+
await collection.deleteMany(
171+
{},
172+
{ explain: { maxTimeMS: 1000, verbosity: 'queryPlanner' } }
173+
);
174+
});
166175

167-
const [{ command }] = commands;
168-
expect(command).not.to.have.property('maxTimeMS');
176+
it('attaches maxTimeMS to the explain command', expectOnExplain(1000));
177+
178+
it('does not attach maxTimeMS to the explained command', expectNotOnCommand());
169179
});
180+
});
170181

171-
it('when maxTimeMS is specified as an explain option and a command-level option, the explain option takes precedence', async function () {
172-
await collection
173-
.find(
182+
describe('maxTimeMS provided to command, not explain', function () {
183+
describe('cursor commands', function () {
184+
describe('options API', function () {
185+
beforeEach(async function () {
186+
await collection
187+
.find({}, { maxTimeMS: 1000, explain: { verbosity: 'queryPlanner' } })
188+
.toArray();
189+
});
190+
191+
it('does not attach maxTimeMS to the explain command', expectNotOnExplain());
192+
193+
it('attaches maxTimeMS to the find command', expectOnCommand(1000));
194+
});
195+
196+
describe('fluent API', function () {
197+
beforeEach(async function () {
198+
await collection.find({}, { maxTimeMS: 1000 }).explain({ verbosity: 'queryPlanner' });
199+
});
200+
201+
it('does not attach maxTimeMS to the explain command', expectNotOnExplain());
202+
203+
it('attaches maxTimeMS to the find command', expectOnCommand(1000));
204+
});
205+
});
206+
207+
describe('non-cursor commands', function () {
208+
beforeEach(async function () {
209+
await collection.deleteMany(
174210
{},
175-
{
176-
maxTimeMS: 1000,
177-
explain: {
178-
verbosity: 'queryPlanner',
179-
maxTimeMS: 2000
180-
}
181-
}
182-
)
183-
.toArray();
211+
{ maxTimeMS: 1000, explain: { verbosity: 'queryPlanner' } }
212+
);
213+
});
184214

185-
const [{ command }] = commands;
186-
expect(command).to.have.property('maxTimeMS', 2000);
215+
it('does nto attach maxTimeMS to the explain command', expectNotOnExplain());
216+
217+
it('attaches maxTimeMS to the explained command', expectOnCommand(1000));
187218
});
188219
});
189220

190-
describe('regular commands w/ explain', function () {
191-
it('when maxTimeMS is specified as an explain option and a command-level option, the explain option takes precedence', async function () {
192-
await collection.deleteMany(
193-
{},
194-
{
195-
maxTimeMS: 1000,
196-
explain: {
197-
verbosity: 'queryPlanner',
198-
maxTimeMS: 2000
199-
}
200-
}
201-
);
221+
describe('maxTimeMS specified in command options and explain options', function () {
222+
describe('cursor commands', function () {
223+
describe('options API', function () {
224+
beforeEach(async function () {
225+
await collection
226+
.find(
227+
{},
228+
{ maxTimeMS: 1000, explain: { maxTimeMS: 2000, verbosity: 'queryPlanner' } }
229+
)
230+
.toArray();
231+
});
202232

203-
const [{ command }] = commands;
204-
expect(command).to.have.property('maxTimeMS', 2000);
233+
it('attaches maxTimeMS from the explain options to explain', expectOnExplain(2000));
234+
235+
it('attaches maxTimeMS from the find options to the find command', expectOnCommand(1000));
236+
});
237+
238+
describe('fluent API', function () {
239+
beforeEach(async function () {
240+
await collection
241+
.find({}, { maxTimeMS: 1000 })
242+
.explain({ maxTimeMS: 2000, verbosity: 'queryPlanner' });
243+
});
244+
245+
it('attaches maxTimeMS from the explain options to explain', expectOnExplain(2000));
246+
247+
it('attaches maxTimeMS from the find options to the find command', expectOnCommand(1000));
248+
});
205249
});
206250

207-
describe('when maxTimeMS is specified as an explain option', function () {
208-
it('attaches maxTimeMS to the explain command', async function () {
251+
describe('non-cursor commands', function () {
252+
beforeEach(async function () {
209253
await collection.deleteMany(
210254
{},
211-
{ explain: { maxTimeMS: 2000, verbosity: 'queryPlanner' } }
255+
{ maxTimeMS: 1000, explain: { maxTimeMS: 2000, verbosity: 'queryPlanner' } }
212256
);
213-
214-
const [{ command }] = commands;
215-
expect(command).to.have.property('maxTimeMS', 2000);
216257
});
258+
259+
it('attaches maxTimeMS to the explain command', expectOnExplain(2000));
260+
261+
it('attaches maxTimeMS to the explained command', expectOnCommand(1000));
217262
});
263+
});
218264

219-
it('when maxTimeMS is not specified, it is not attached to the explain command', async function () {
220-
await collection.deleteMany({}, { explain: { verbosity: 'queryPlanner' } });
265+
function expectOnExplain(value: number) {
266+
return function () {
267+
const [{ command }] = commands;
268+
expect(command).to.have.property('maxTimeMS', value);
269+
};
270+
}
221271

272+
function expectNotOnExplain() {
273+
return function () {
222274
const [{ command }] = commands;
223275
expect(command).not.to.have.property('maxTimeMS');
224-
});
225-
});
276+
};
277+
}
278+
279+
function expectOnCommand(value: number) {
280+
return function () {
281+
const [
282+
{
283+
command: { explain }
284+
}
285+
] = commands;
286+
expect(explain).to.have.property('maxTimeMS', value);
287+
};
288+
}
289+
function expectNotOnCommand() {
290+
return function () {
291+
const [
292+
{
293+
command: { explain }
294+
}
295+
] = commands;
296+
expect(explain).not.to.have.property('maxTimeMS');
297+
};
298+
}
226299
});
227300
});
228301

0 commit comments

Comments
 (0)