Skip to content

Commit 730f43a

Browse files
authored
fix(NODE-3173): Preserve sort key order for numeric string keys (#2790)
1 parent 16e7064 commit 730f43a

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

lib/utils.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var formatSortValue = (exports.formatSortValue = function(sortDirection) {
4040
});
4141

4242
var formattedOrderClause = (exports.formattedOrderClause = function(sortValue) {
43-
var orderBy = {};
43+
var orderBy = new Map();
4444
if (sortValue == null) return null;
4545
if (Array.isArray(sortValue)) {
4646
if (sortValue.length === 0) {
@@ -49,15 +49,22 @@ var formattedOrderClause = (exports.formattedOrderClause = function(sortValue) {
4949

5050
for (var i = 0; i < sortValue.length; i++) {
5151
if (sortValue[i].constructor === String) {
52-
orderBy[sortValue[i]] = 1;
52+
orderBy.set(`${sortValue[i]}`, 1);
5353
} else {
54-
orderBy[sortValue[i][0]] = formatSortValue(sortValue[i][1]);
54+
orderBy.set(`${sortValue[i][0]}`, formatSortValue(sortValue[i][1]));
5555
}
5656
}
5757
} else if (sortValue != null && typeof sortValue === 'object') {
58-
orderBy = sortValue;
58+
if (sortValue instanceof Map) {
59+
orderBy = sortValue;
60+
} else {
61+
var sortKeys = Object.keys(sortValue);
62+
for (var k of sortKeys) {
63+
orderBy.set(k, sortValue[k]);
64+
}
65+
}
5966
} else if (typeof sortValue === 'string') {
60-
orderBy[sortValue] = 1;
67+
orderBy.set(`${sortValue}`, 1);
6168
} else {
6269
throw new Error(
6370
'Illegal sort clause, must be of the form ' +

test/functional/spec-runner/index.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ function validateExpectations(commandEvents, spec, savedSessionData) {
384384

385385
const actualCommand = actual.command;
386386
const expectedCommand = expected.command;
387+
if (expectedCommand.sort) {
388+
// TODO: This is a workaround that works because all sorts in the specs
389+
// are objects with one key; ideally we'd want to adjust the spec definitions
390+
// to indicate whether order matters for any given key and set general
391+
// expectations accordingly (see NODE-3235)
392+
expect(Object.keys(expectedCommand.sort)).to.have.lengthOf(1);
393+
expect(actualCommand.sort).to.be.instanceOf(Map);
394+
expect(actualCommand.sort.size).to.equal(1);
395+
const expectedKey = Object.keys(expectedCommand.sort)[0];
396+
expect(actualCommand.sort).to.have.all.keys(expectedKey);
397+
actualCommand.sort = { [expectedKey]: actualCommand.sort.get(expectedKey) };
398+
}
387399

388400
expect(actualCommand)
389401
.withSessionData(savedSessionData)
@@ -392,18 +404,23 @@ function validateExpectations(commandEvents, spec, savedSessionData) {
392404
}
393405

394406
function normalizeCommandShapes(commands) {
395-
return commands.map(command =>
396-
JSON.parse(
407+
return commands.map(def => {
408+
const output = JSON.parse(
397409
EJSON.stringify(
398410
{
399-
command: command.command,
400-
commandName: command.command_name ? command.command_name : command.commandName,
401-
databaseName: command.database_name ? command.database_name : command.databaseName
411+
command: def.command,
412+
commandName: def.command_name ? def.command_name : def.commandName,
413+
databaseName: def.database_name ? def.database_name : def.databaseName
402414
},
403415
{ relaxed: true }
404416
)
405-
)
406-
);
417+
);
418+
// TODO: this is a workaround to preserve sort Map type until NODE-3235 is completed
419+
if (def.command.sort) {
420+
output.command.sort = def.command.sort;
421+
}
422+
return output;
423+
});
407424
}
408425

409426
function extractCrudResult(result, operation) {

0 commit comments

Comments
 (0)