Skip to content

Commit 269e6d3

Browse files
chore: tests for insert-many tool
1 parent 81c07e3 commit 269e6d3

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

src/tools/mongodb/create/insertMany.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
33
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
44
import { ToolArgs, OperationType } from "../../tool.js";
55

6+
export function insertManyResponse(collection: string, insertedCount: number, insertedIds: unknown[]): CallToolResult {
7+
return {
8+
content: [
9+
{
10+
text: `Inserted \`${insertedCount}\` document(s) into collection "${collection}"`,
11+
type: "text",
12+
},
13+
{
14+
text: `Inserted IDs: ${insertedIds.join(", ")}`,
15+
type: "text",
16+
},
17+
],
18+
};
19+
}
20+
621
export class InsertManyTool extends MongoDBToolBase {
722
protected name = "insert-many";
823
protected description = "Insert an array of documents into a MongoDB collection";
@@ -24,17 +39,6 @@ export class InsertManyTool extends MongoDBToolBase {
2439
const provider = await this.ensureConnected();
2540
const result = await provider.insertMany(database, collection, documents);
2641

27-
return {
28-
content: [
29-
{
30-
text: `Inserted \`${result.insertedCount}\` document(s) into collection "${collection}"`,
31-
type: "text",
32-
},
33-
{
34-
text: `Inserted IDs: ${Object.values(result.insertedIds).join(", ")}`,
35-
type: "text",
36-
},
37-
],
38-
};
42+
return insertManyResponse(collection, result.insertedCount, Object.values(result.insertedIds));
3943
}
4044
}

tests/accuracy/insert-many.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describeAccuracyTests } from "./sdk/describe-accuracy-tests.js";
2+
import { getAvailableModels } from "./sdk/models.js";
3+
import { AccuracyTestConfig } from "./sdk/describe-accuracy-tests.js";
4+
import { insertManyResponse } from "../../src/tools/mongodb/create/insertMany.js";
5+
6+
function callsInsertMany(prompt: string): AccuracyTestConfig {
7+
return {
8+
injectConnectedAssumption: true,
9+
prompt: prompt,
10+
mockedTools: {
11+
"insert-many": function listDatabases() {
12+
return insertManyResponse("coll1", 3, ["1FOO", "2BAR", "3BAZ"]);
13+
},
14+
},
15+
expectedToolCalls: [
16+
{
17+
toolName: "insert-many",
18+
parameters: {
19+
database: "db1",
20+
collection: "coll1",
21+
documents: [
22+
{
23+
id: 1,
24+
name: "name1",
25+
},
26+
{
27+
id: 2,
28+
name: "name2",
29+
},
30+
{
31+
id: 3,
32+
name: "name3",
33+
},
34+
],
35+
},
36+
},
37+
],
38+
};
39+
}
40+
41+
function callsEmptyInsertMany(prompt: string) {
42+
return {
43+
injectConnectedAssumption: true,
44+
prompt: prompt,
45+
mockedTools: {
46+
"insert-many": function listDatabases() {
47+
return insertManyResponse("coll1", 3, ["1FOO", "2BAR", "3BAZ"]);
48+
},
49+
},
50+
expectedToolCalls: [
51+
{
52+
toolName: "insert-many",
53+
parameters: {
54+
database: "db1",
55+
collection: "coll1",
56+
documents: [{}, {}, {}],
57+
},
58+
},
59+
],
60+
};
61+
}
62+
63+
describeAccuracyTests("insert-many", getAvailableModels(), [
64+
callsInsertMany(
65+
[
66+
"In my namespace 'db1.coll1', insert 3 documents each with the following fields:",
67+
"- id: an incremental number starting from 1",
68+
"- name: a string of format 'name<id>'",
69+
].join("\n")
70+
),
71+
callsEmptyInsertMany("Add three empty documents in collection 'coll1' inside database 'db1'"),
72+
]);

0 commit comments

Comments
 (0)