mongo/jstests/libs/query_optimization/api_version_helpers.js
Denis Trailin c94577b041 SERVER-120771: Move query-optimization files from jstests/libs to their own directory (#52411)
GitOrigin-RevId: 01afffc3fb070a6a52a5bd0982660fd01dd73272
2026-04-28 18:30:14 +00:00

114 lines
3.6 KiB
JavaScript

/**
* Helper functions that help make assertions on API Version parameters.
*/
export var APIVersionHelpers = (function () {
/**
* Asserts that the pipeline fails with the given code when apiStrict is set to true and
* apiVersion is "1".
*/
function assertAggregateFailsWithAPIStrict(db, pipeline, collName, errorCodes) {
assert.commandFailedWithCode(
db.runCommand({
aggregate: collName,
pipeline: pipeline,
cursor: {},
apiStrict: true,
apiVersion: "1",
}),
errorCodes,
pipeline,
);
}
/**
* Asserts that the pipeline succeeds when apiStrict is set to true and apiVersion is "1".
*/
function assertAggregateSucceedsWithAPIStrict(db, pipeline, collName, errorCodes) {
if (errorCodes) {
assert.commandWorkedOrFailedWithCode(
db.runCommand({
aggregate: collName,
pipeline: pipeline,
cursor: {},
apiStrict: true,
apiVersion: "1",
}),
errorCodes,
pipeline,
);
} else {
assert.commandWorked(
db.runCommand({
aggregate: collName,
pipeline: pipeline,
cursor: {},
apiStrict: true,
apiVersion: "1",
}),
pipeline,
);
}
}
/**
* Asserts that the pipeline succeeds with the given code when apiStrict is set to false and
* apiVersion is "1".
*/
function assertAggregateSucceedsAPIVersionWithoutAPIStrict(db, pipeline, collName) {
assert.commandWorked(
db.runCommand({
aggregate: collName,
pipeline: pipeline,
cursor: {},
apiStrict: false,
apiVersion: "1",
}),
pipeline,
);
}
/**
* Asserts that the given pipeline cannot be used to define a view when apiStrict is set to true
* and apiVersion is "1" on the create command.
*/
function assertViewFailsWithAPIStrict(db, pipeline, viewName, collName) {
assert.commandFailedWithCode(
db.runCommand({
create: viewName,
viewOn: collName,
pipeline: pipeline,
apiStrict: true,
apiVersion: "1",
}),
ErrorCodes.APIStrictError,
pipeline,
);
}
/**
* Asserts that the given pipeline can be used to define a view when apiStrict is set to true
* and apiVersion is "1" on the create command.
*/
function assertViewSucceedsWithAPIStrict(db, pipeline, viewName, collName) {
assert.commandWorked(
db.runCommand({
create: viewName,
viewOn: collName,
pipeline: pipeline,
apiStrict: true,
apiVersion: "1",
}),
);
assert.commandWorked(db.runCommand({drop: viewName}));
}
return {
assertAggregateFailsWithAPIStrict: assertAggregateFailsWithAPIStrict,
assertAggregateSucceedsWithAPIStrict: assertAggregateSucceedsWithAPIStrict,
assertAggregateSucceedsAPIVersionWithoutAPIStrict: assertAggregateSucceedsAPIVersionWithoutAPIStrict,
assertViewFailsWithAPIStrict: assertViewFailsWithAPIStrict,
assertViewSucceedsWithAPIStrict: assertViewSucceedsWithAPIStrict,
};
})();