Skip to content

Commit 81c07e3

Browse files
chore: added tests for find tool
1 parent 6ec698a commit 81c07e3

File tree

2 files changed

+176
-17
lines changed

2 files changed

+176
-17
lines changed

src/tools/mongodb/read/find.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const FindArgs = {
1313
.describe("The query filter, matching the syntax of the query argument of db.collection.find()"),
1414
projection: z
1515
.record(z.string(), z.unknown())
16-
.optional()
16+
// .optional()
1717
.describe("The projection, matching the syntax of the projection argument of db.collection.find()"),
1818
limit: z.number().optional().default(10).describe("The maximum number of documents to return"),
1919
sort: z
@@ -22,6 +22,23 @@ export const FindArgs = {
2222
.describe("A document, describing the sort order, matching the syntax of the sort argument of cursor.sort()"),
2323
};
2424

25+
export function findResponse(collection: string, documents: unknown[]): CallToolResult {
26+
return {
27+
content: [
28+
{
29+
text: `Found ${documents.length} documents in the collection "${collection}":`,
30+
type: "text",
31+
},
32+
...documents.map<{ type: "text"; text: string }>((doc) => {
33+
return {
34+
text: EJSON.stringify(doc),
35+
type: "text",
36+
};
37+
}),
38+
],
39+
};
40+
}
41+
2542
export class FindTool extends MongoDBToolBase {
2643
protected name = "find";
2744
protected description = "Run a find query against a MongoDB collection";
@@ -50,21 +67,6 @@ export class FindTool extends MongoDBToolBase {
5067

5168
const documents = await provider.find(database, collection, filter, { projection, limit, sort }).toArray();
5269

53-
const content: Array<{ text: string; type: "text" }> = [
54-
{
55-
text: `Found ${documents.length} documents in the collection "${collection}":`,
56-
type: "text",
57-
},
58-
...documents.map((doc) => {
59-
return {
60-
text: EJSON.stringify(doc),
61-
type: "text",
62-
} as { text: string; type: "text" };
63-
}),
64-
];
65-
66-
return {
67-
content,
68-
};
70+
return findResponse(collection, documents);
6971
}
7072
}

tests/accuracy/find.test.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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 { findResponse } from "../../src/tools/mongodb/read/find.js";
5+
import { MockedTools } from "./sdk/test-tools.js";
6+
import { collectionSchemaResponse } from "../../src/tools/mongodb/metadata/collectionSchema.js";
7+
import { getSimplifiedSchema } from "mongodb-schema";
8+
9+
const documents = [
10+
{
11+
title: "book1",
12+
author: "author1",
13+
date_of_publish: "01.01.1990",
14+
},
15+
{
16+
title: "book2",
17+
author: "author1",
18+
date_of_publish: "01.01.1992",
19+
},
20+
{
21+
title: "book3",
22+
author: "author2",
23+
date_of_publish: "01.01.1990",
24+
},
25+
];
26+
27+
function callsFindNoFilter(prompt: string): AccuracyTestConfig {
28+
return {
29+
injectConnectedAssumption: true,
30+
prompt: prompt,
31+
mockedTools: {
32+
"collection-schema": async () =>
33+
collectionSchemaResponse("db1", "coll1", await getSimplifiedSchema(documents)),
34+
find: () => findResponse("coll1", documents),
35+
},
36+
expectedToolCalls: [
37+
{
38+
toolName: "find",
39+
parameters: {
40+
database: "db1",
41+
collection: "coll1",
42+
},
43+
},
44+
],
45+
};
46+
}
47+
48+
function callsFindWithFilter(prompt: string): AccuracyTestConfig {
49+
return {
50+
injectConnectedAssumption: true,
51+
prompt: prompt,
52+
mockedTools: {
53+
"collection-schema": async () =>
54+
collectionSchemaResponse("db1", "coll1", await getSimplifiedSchema(documents)),
55+
find: () =>
56+
findResponse(
57+
"coll1",
58+
documents.filter((doc) => doc.author === "author1")
59+
),
60+
},
61+
expectedToolCalls: [
62+
{
63+
toolName: "find",
64+
parameters: {
65+
database: "db1",
66+
collection: "coll1",
67+
filter: { author: "author1" },
68+
},
69+
},
70+
],
71+
};
72+
}
73+
74+
function callsFindWithProjection(prompt: string): AccuracyTestConfig {
75+
return {
76+
injectConnectedAssumption: true,
77+
prompt: prompt,
78+
mockedTools: {
79+
"collection-schema": async () =>
80+
collectionSchemaResponse("db1", "coll1", await getSimplifiedSchema(documents)),
81+
find: () => findResponse("coll1", documents),
82+
},
83+
expectedToolCalls: [
84+
{
85+
toolName: "find",
86+
parameters: {
87+
database: "db1",
88+
collection: "coll1",
89+
projection: { title: 1 },
90+
},
91+
},
92+
],
93+
};
94+
}
95+
96+
function callsFindWithProjectionAndFilters(prompt: string): AccuracyTestConfig {
97+
return {
98+
injectConnectedAssumption: true,
99+
prompt: prompt,
100+
mockedTools: {
101+
"collection-schema": async () =>
102+
collectionSchemaResponse("db1", "coll1", await getSimplifiedSchema(documents)),
103+
find: () =>
104+
findResponse(
105+
"coll1",
106+
documents.filter((doc) => doc.date_of_publish === "01.01.1992")
107+
),
108+
},
109+
expectedToolCalls: [
110+
{
111+
toolName: "find",
112+
parameters: {
113+
database: "db1",
114+
collection: "coll1",
115+
filter: { date_of_publish: "01.01.1992" },
116+
projection: { title: 1 },
117+
},
118+
},
119+
],
120+
};
121+
}
122+
123+
function callsFindWithSortAndLimit(prompt: string): AccuracyTestConfig {
124+
return {
125+
injectConnectedAssumption: true,
126+
prompt: prompt,
127+
mockedTools: {
128+
"collection-schema": async () =>
129+
collectionSchemaResponse("db1", "coll1", await getSimplifiedSchema(documents)),
130+
find: () => findResponse("coll1", [documents[0], documents[1]]),
131+
},
132+
expectedToolCalls: [
133+
{
134+
toolName: "find",
135+
parameters: {
136+
database: "db1",
137+
collection: "coll1",
138+
sort: { date_of_publish: 1 },
139+
limit: 2,
140+
},
141+
},
142+
],
143+
};
144+
}
145+
146+
describeAccuracyTests("find", getAvailableModels(), [
147+
callsFindNoFilter("List all the documents in 'db1.coll1' namespace"),
148+
callsFindNoFilter("Find all the documents from collection coll1 in database db1"),
149+
callsFindWithFilter("Find all the books published by author name 'author1' in db1.coll1 namespace"),
150+
callsFindWithFilter("Find all the documents in coll1 collection and db1 database where author is 'author1'"),
151+
callsFindWithProjection("Give me all the title of the books available in 'db1.coll1' namespace"),
152+
callsFindWithProjection("Give me all the title of the books published in available in 'db1.coll1' namespace"),
153+
callsFindWithProjectionAndFilters(
154+
"Find all the book titles from 'db1.coll1' namespace where date_of_publish is '01.01.1992'"
155+
),
156+
callsFindWithSortAndLimit("List first two books sorted by the field date_of_publish in namespace db1.coll1"),
157+
]);

0 commit comments

Comments
 (0)