Skip to content

fix(NODE-3173): Preserve sort key order for numeric string keys #2790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var formatSortValue = (exports.formatSortValue = function(sortDirection) {
});

var formattedOrderClause = (exports.formattedOrderClause = function(sortValue) {
var orderBy = {};
var orderBy = new Map();
if (sortValue == null) return null;
if (Array.isArray(sortValue)) {
if (sortValue.length === 0) {
Expand All @@ -49,15 +49,22 @@ var formattedOrderClause = (exports.formattedOrderClause = function(sortValue) {

for (var i = 0; i < sortValue.length; i++) {
if (sortValue[i].constructor === String) {
orderBy[sortValue[i]] = 1;
orderBy.set(`${sortValue[i]}`, 1);
} else {
orderBy[sortValue[i][0]] = formatSortValue(sortValue[i][1]);
orderBy.set(`${sortValue[i][0]}`, formatSortValue(sortValue[i][1]));
}
}
} else if (sortValue != null && typeof sortValue === 'object') {
orderBy = sortValue;
if (sortValue instanceof Map) {
orderBy = sortValue;
} else {
var sortKeys = Object.keys(sortValue);
for (var k of sortKeys) {
orderBy.set(k, sortValue[k]);
}
}
} else if (typeof sortValue === 'string') {
orderBy[sortValue] = 1;
orderBy.set(`${sortValue}`, 1);
} else {
throw new Error(
'Illegal sort clause, must be of the form ' +
Expand Down
31 changes: 24 additions & 7 deletions test/functional/spec-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,18 @@ function validateExpectations(commandEvents, spec, savedSessionData) {

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

expect(actualCommand)
.withSessionData(savedSessionData)
Expand All @@ -392,18 +404,23 @@ function validateExpectations(commandEvents, spec, savedSessionData) {
}

function normalizeCommandShapes(commands) {
return commands.map(command =>
JSON.parse(
return commands.map(def => {
const output = JSON.parse(
EJSON.stringify(
{
command: command.command,
commandName: command.command_name ? command.command_name : command.commandName,
databaseName: command.database_name ? command.database_name : command.databaseName
command: def.command,
commandName: def.command_name ? def.command_name : def.commandName,
databaseName: def.database_name ? def.database_name : def.databaseName
},
{ relaxed: true }
)
)
);
);
// TODO: this is a workaround to preserve sort Map type until NODE-3235 is completed
if (def.command.sort) {
output.command.sort = def.command.sort;
}
return output;
});
}

function extractCrudResult(result, operation) {
Expand Down