(commandEvents, spec, savedSessionData)
| 438 | } |
| 439 | |
| 440 | function validateExpectations(commandEvents, spec, savedSessionData) { |
| 441 | if (!spec.expectations || !Array.isArray(spec.expectations) || spec.expectations.length === 0) { |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | const actualEvents = normalizeCommandShapes(commandEvents); |
| 446 | const rawExpectedEvents = spec.expectations.map(x => x.command_started_event); |
| 447 | const expectedEvents = normalizeCommandShapes(rawExpectedEvents); |
| 448 | |
| 449 | expect(actualEvents).to.have.lengthOf(expectedEvents.length); |
| 450 | |
| 451 | for (const [idx, expectedEvent] of expectedEvents.entries()) { |
| 452 | const actualEvent = actualEvents[idx]; |
| 453 | |
| 454 | if (typeof expectedEvent.commandName === 'string') { |
| 455 | expect(actualEvent).to.have.property('commandName', expectedEvent.commandName); |
| 456 | } |
| 457 | |
| 458 | if (typeof expectedEvent.databaseName === 'string') { |
| 459 | expect(actualEvent).to.have.property('databaseName', expectedEvent.databaseName); |
| 460 | } |
| 461 | |
| 462 | const actualCommand = actualEvent.command; |
| 463 | const expectedCommand = expectedEvent.command; |
| 464 | if (expectedCommand.sort) { |
| 465 | // TODO(NODE-3235): This is a workaround that works because all sorts in the specs |
| 466 | // are objects with one key; ideally we'd want to adjust the spec definitions |
| 467 | // to indicate whether order matters for any given key and set general |
| 468 | // expectations accordingly |
| 469 | expect(Object.keys(expectedCommand.sort)).to.have.lengthOf(1); |
| 470 | expect(actualCommand.sort).to.be.instanceOf(Map); |
| 471 | expect(actualCommand.sort.size).to.equal(1); |
| 472 | const expectedKey = Object.keys(expectedCommand.sort)[0]; |
| 473 | expect(actualCommand.sort).to.have.all.keys(expectedKey); |
| 474 | actualCommand.sort = { [expectedKey]: actualCommand.sort.get(expectedKey) }; |
| 475 | } |
| 476 | |
| 477 | if (expectedCommand.createIndexes) { |
| 478 | // TODO(NODE-3235): This is a workaround that works because all indexes in the specs |
| 479 | // are objects with one key; ideally we'd want to adjust the spec definitions |
| 480 | // to indicate whether order matters for any given key and set general |
| 481 | // expectations accordingly |
| 482 | for (const [i, dbIndex] of actualCommand.indexes.entries()) { |
| 483 | expect(Object.keys(expectedCommand.indexes[i].key)).to.have.lengthOf(1); |
| 484 | expect(dbIndex.key).to.be.instanceOf(Map); |
| 485 | expect(dbIndex.key.size).to.equal(1); |
| 486 | } |
| 487 | actualCommand.indexes = actualCommand.indexes.map(dbIndex => ({ |
| 488 | ...dbIndex, |
| 489 | key: Object.fromEntries(dbIndex.key) |
| 490 | })); |
| 491 | } |
| 492 | |
| 493 | expect(actualCommand).withSessionData(savedSessionData).to.matchMongoSpec(expectedCommand); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | function normalizeCommandShapes(commands) { |
no test coverage detected