-
Notifications
You must be signed in to change notification settings - Fork 41
chore: add integration tests for count #78
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,118 @@ | ||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||
connect, | ||||||||||||||||||||||||||||
jestTestCluster, | ||||||||||||||||||||||||||||
jestTestMCPClient, | ||||||||||||||||||||||||||||
getResponseContent, | ||||||||||||||||||||||||||||
validateParameters, | ||||||||||||||||||||||||||||
dbOperationParameters, | ||||||||||||||||||||||||||||
} from "../../../helpers.js"; | ||||||||||||||||||||||||||||
import { toIncludeSameMembers } from "jest-extended"; | ||||||||||||||||||||||||||||
import { McpError } from "@modelcontextprotocol/sdk/types.js"; | ||||||||||||||||||||||||||||
import { ObjectId } from "mongodb"; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
describe("count tool", () => { | ||||||||||||||||||||||||||||
const client = jestTestMCPClient(); | ||||||||||||||||||||||||||||
const cluster = jestTestCluster(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let randomDbName: string; | ||||||||||||||||||||||||||||
beforeEach(() => { | ||||||||||||||||||||||||||||
randomDbName = new ObjectId().toString(); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
it("should have correct metadata", async () => { | ||||||||||||||||||||||||||||
const { tools } = await client().listTools(); | ||||||||||||||||||||||||||||
const listCollections = tools.find((tool) => tool.name === "count")!; | ||||||||||||||||||||||||||||
expect(listCollections).toBeDefined(); | ||||||||||||||||||||||||||||
expect(listCollections.description).toBe("Gets the number of documents in a MongoDB collection"); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
validateParameters(listCollections, [ | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
name: "query", | ||||||||||||||||||||||||||||
description: | ||||||||||||||||||||||||||||
"The query filter to count documents. Matches the syntax of the filter argument of db.collection.count()", | ||||||||||||||||||||||||||||
type: "object", | ||||||||||||||||||||||||||||
required: false, | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
...dbOperationParameters, | ||||||||||||||||||||||||||||
]); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
describe("with invalid arguments", () => { | ||||||||||||||||||||||||||||
const args = [ | ||||||||||||||||||||||||||||
{}, | ||||||||||||||||||||||||||||
{ database: 123, collection: "bar" }, | ||||||||||||||||||||||||||||
{ foo: "bar", database: "test", collection: "bar" }, | ||||||||||||||||||||||||||||
{ collection: [], database: "test" }, | ||||||||||||||||||||||||||||
{ collection: "bar", database: "test", query: "{ $gt: { foo: 5 } }" }, | ||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||
for (const arg of args) { | ||||||||||||||||||||||||||||
it(`throws a schema error for: ${JSON.stringify(arg)}`, async () => { | ||||||||||||||||||||||||||||
await connect(client(), cluster()); | ||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||
await client().callTool({ name: "count", arguments: arg }); | ||||||||||||||||||||||||||||
expect.fail("Expected an error to be thrown"); | ||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||
expect(error).toBeInstanceOf(McpError); | ||||||||||||||||||||||||||||
const mcpError = error as McpError; | ||||||||||||||||||||||||||||
expect(mcpError.code).toEqual(-32602); | ||||||||||||||||||||||||||||
expect(mcpError.message).toContain("Invalid arguments for tool count"); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
it("returns 0 when database doesn't exist", async () => { | ||||||||||||||||||||||||||||
await connect(client(), cluster()); | ||||||||||||||||||||||||||||
const response = await client().callTool({ | ||||||||||||||||||||||||||||
name: "count", | ||||||||||||||||||||||||||||
arguments: { database: "non-existent", collection: "foos" }, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
const content = getResponseContent(response.content); | ||||||||||||||||||||||||||||
expect(content).toEqual('Found 0 documents in the collection "foos"'); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
it("returns 0 when collection doesn't exist", async () => { | ||||||||||||||||||||||||||||
await connect(client(), cluster()); | ||||||||||||||||||||||||||||
const mongoClient = cluster().getClient(); | ||||||||||||||||||||||||||||
await mongoClient.db(randomDbName).collection("bar").insertOne({}); | ||||||||||||||||||||||||||||
const response = await client().callTool({ | ||||||||||||||||||||||||||||
name: "count", | ||||||||||||||||||||||||||||
arguments: { database: randomDbName, collection: "non-existent" }, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
const content = getResponseContent(response.content); | ||||||||||||||||||||||||||||
expect(content).toEqual('Found 0 documents in the collection "non-existent"'); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
describe("with existing database", () => { | ||||||||||||||||||||||||||||
beforeEach(async () => { | ||||||||||||||||||||||||||||
const mongoClient = cluster().getClient(); | ||||||||||||||||||||||||||||
await mongoClient | ||||||||||||||||||||||||||||
.db(randomDbName) | ||||||||||||||||||||||||||||
.collection("foo") | ||||||||||||||||||||||||||||
.insertMany([ | ||||||||||||||||||||||||||||
{ name: "Peter", age: 5 }, | ||||||||||||||||||||||||||||
{ name: "Parker", age: 10 }, | ||||||||||||||||||||||||||||
{ name: "George", age: 15 }, | ||||||||||||||||||||||||||||
]); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const testCases = [ | ||||||||||||||||||||||||||||
{ filter: undefined, expectedCount: 3 }, | ||||||||||||||||||||||||||||
{ filter: {}, expectedCount: 3 }, | ||||||||||||||||||||||||||||
{ filter: { age: { $lt: 15 } }, expectedCount: 2 }, | ||||||||||||||||||||||||||||
{ filter: { age: { $gt: 5 }, name: { $regex: "^P" } }, expectedCount: 1 }, | ||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||
for (const testCase of testCases) { | ||||||||||||||||||||||||||||
Comment on lines
+99
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit: I think this is quicker to read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I initially had it like that, but then moved it out because I thought it's getting cluttered with the test case definition inlined into the for loop. |
||||||||||||||||||||||||||||
it(`returns ${testCase.expectedCount} documents for filter ${JSON.stringify(testCase.filter)}`, async () => { | ||||||||||||||||||||||||||||
await connect(client(), cluster()); | ||||||||||||||||||||||||||||
const response = await client().callTool({ | ||||||||||||||||||||||||||||
name: "count", | ||||||||||||||||||||||||||||
arguments: { database: randomDbName, collection: "foo", query: testCase.filter }, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const content = getResponseContent(response.content); | ||||||||||||||||||||||||||||
expect(content).toEqual(`Found ${testCase.expectedCount} documents in the collection "foo"`); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.