feat(mongodb): complete collection method option completion keys
This commit is contained in:
1 parent
21a47de988
commit
dd82d948a9
3 files changed
+178
-13
No files matched your search
+78
-2
@@ -1,7 +1,8 @@
|
||||
import { strict as assert } from "node:assert";
|
||||
import { test } from "vitest";
|
||||
import { buildMongoCompletionItems, getMongoCompletionContext, getMongoCompletionResultValidFor, inferMongoCompletionFields, shouldAutoOpenMongoCompletion } from "../../apps/desktop/src/lib/mongo/mongoCompletion.ts";
|
||||
import { ACCUMULATORS, EXPRESSION_OPERATORS, EXTENDED_JSON_VALUES, PIPELINE_STAGES, PUSH_MODIFIERS, QUERY_OPERATORS, STAGE_OPTION_KEYS, UPDATE_OPERATORS, VALUE_SNIPPETS } from "../../apps/desktop/src/lib/mongo/mongoCompletionTables.ts";
|
||||
import { ACCUMULATORS, EXPRESSION_OPERATORS, EXTENDED_JSON_VALUES, METHOD_OPTION_KEYS, PIPELINE_STAGES, PUSH_MODIFIERS, QUERY_OPERATORS, STAGE_OPTION_KEYS, UPDATE_OPERATORS, VALUE_SNIPPETS } from "../../apps/desktop/src/lib/mongo/mongoCompletionTables.ts";
|
||||
import { parseMongoCommand } from "../../apps/desktop/src/lib/mongo/mongoShellCommand.ts";
|
||||
|
||||
const collections = ["users", "user_events", "order-items", "audit.logs"];
|
||||
const fields = [
|
||||
@@ -492,7 +493,8 @@ test("stays quiet inside string values, comments and unmodelled arguments", () =
|
||||
assert.deepEqual(labels('db.users.find({ name: "Ad', { fields }), []);
|
||||
assert.deepEqual(labels("// db.users.fi", { fields }), []);
|
||||
assert.deepEqual(labels('db.users.find({ _id: ObjectId("6a04', { fields }), []);
|
||||
assert.deepEqual(labels("db.users.updateOne({}, { $set: {} }, { up", { fields }), []);
|
||||
// bulkWrite operation entries are still unmodelled; its options argument is not.
|
||||
assert.deepEqual(labels("db.users.bulkWrite([{ insertOne: { do", { fields }), []);
|
||||
});
|
||||
|
||||
test("suggests only helpers the shell parser accepts", () => {
|
||||
@@ -513,6 +515,80 @@ test("suggests only helpers the shell parser accepts", () => {
|
||||
assert.equal(methodLabels.includes("limit"), false);
|
||||
});
|
||||
|
||||
test("suggests the option keys of methods that take an options argument", () => {
|
||||
assert.deepEqual(labels("db.users.updateOne({}, {$set:{a:1}}, { "), ["arrayFilters", "upsert"]);
|
||||
assert.deepEqual(labels("db.users.updateOne({}, {$set:{a:1}}, { ups"), ["upsert"]);
|
||||
assert.deepEqual(labels("db.users.replaceOne({}, {}, { "), ["upsert"]);
|
||||
assert.deepEqual(labels("db.users.findOneAndDelete({}, { "), ["projection", "sort"]);
|
||||
assert.deepEqual(labels("db.users.bulkWrite([], { "), ["ordered"]);
|
||||
assert.deepEqual(labels("db.users.findOne({}, {}, { "), ["sort"]);
|
||||
assert.ok(labels("db.users.findOneAndUpdate({}, {$set:{a:1}}, { ").includes("returnDocument"));
|
||||
assert.ok(labels("db.users.createIndex({a:1}, { ").includes("expireAfterSeconds"));
|
||||
assert.ok(labels("db.users.aggregate([], { ").includes("allowDiskUse"));
|
||||
|
||||
// A `sort` or `projection` option holds field names.
|
||||
assert.deepEqual(labels("db.users.findOneAndUpdate({}, {$set:{a:1}}, { sort: { na", { fields }), ["name"]);
|
||||
assert.deepEqual(labels("db.users.findOneAndDelete({}, { projection: { na", { fields }), ["name"]);
|
||||
});
|
||||
|
||||
test("stays quiet in the trailing argument of methods that take no options", () => {
|
||||
// Suggesting an option key here would hand the user a command the parser rejects.
|
||||
for (const text of ["db.users.find({}, {}, { ", "db.users.insertOne({}, { ", "db.users.insertMany([], { ", "db.users.deleteOne({}, { ", "db.users.deleteMany({}, { ", "db.users.countDocuments({}, { "]) {
|
||||
assert.deepEqual(labels(text, { fields, collections }), [], text);
|
||||
}
|
||||
});
|
||||
|
||||
test("every suggested option key parses on the method that offers it", () => {
|
||||
// The option sets mirror the driver's own structs, most of which reject unknown fields, so a
|
||||
// key that only exists in this table would complete into a command that fails at Run.
|
||||
const values: Record<string, string> = {
|
||||
upsert: "true",
|
||||
arrayFilters: '[{ "e.f": 1 }]',
|
||||
returnDocument: '"after"',
|
||||
returnNewDocument: "true",
|
||||
new: "true",
|
||||
projection: "{ name: 1 }",
|
||||
sort: "{ name: -1 }",
|
||||
ordered: "false",
|
||||
name: '"idx"',
|
||||
unique: "true",
|
||||
sparse: "true",
|
||||
expireAfterSeconds: "3600",
|
||||
partialFilterExpression: "{ name: { $exists: true } }",
|
||||
collation: '{ locale: "en" }',
|
||||
hidden: "true",
|
||||
allowDiskUse: "true",
|
||||
maxTimeMS: "5000",
|
||||
hint: '"idx"',
|
||||
comment: '"why"',
|
||||
let: "{ n: 1 }",
|
||||
explain: "true",
|
||||
};
|
||||
const callArgs: Record<string, string> = {
|
||||
findOne: "{}, {}",
|
||||
updateOne: "{}, {$set:{a:1}}",
|
||||
updateMany: "{}, {$set:{a:1}}",
|
||||
replaceOne: "{}, {b:1}",
|
||||
findOneAndUpdate: "{}, {$set:{a:1}}",
|
||||
findOneAndReplace: "{}, {b:1}",
|
||||
findOneAndDelete: "{}",
|
||||
bulkWrite: "[{ insertOne: { document: { a: 1 } } }]",
|
||||
createIndex: "{a:1}",
|
||||
aggregate: "[]",
|
||||
};
|
||||
|
||||
for (const [method, options] of Object.entries(METHOD_OPTION_KEYS)) {
|
||||
const args = callArgs[method];
|
||||
assert.ok(args !== undefined, `${method} needs sample arguments in this test`);
|
||||
for (const option of options) {
|
||||
const value = values[option.label];
|
||||
assert.ok(value !== undefined, `${option.label} needs a sample value in this test`);
|
||||
const command = `db.users.${method}(${args}, { ${option.label}: ${value} })`;
|
||||
assert.ok(parseMongoCommand(command), `${command} must parse`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("completes both arguments of distinct", () => {
|
||||
const method = buildMongoCompletionItems("db.users.dist", "db.users.dist".length).find((item) => item.label === "distinct");
|
||||
assert.equal(method?.apply, 'users.distinct("${field}")');
|
||||
|
||||
Reference in new issue
Block a user