-
Notifications
You must be signed in to change notification settings - Fork 17
chore: add tests for read operations #102
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
databaseCollectionParameters, | ||
validateToolMetadata, | ||
validateThrowsForInvalidArguments, | ||
getResponseElements, | ||
} from "../../../helpers.js"; | ||
import { describeWithMongoDB, validateAutoConnectBehavior } from "../mongodbHelpers.js"; | ||
|
||
describeWithMongoDB("aggregate tool", (integration) => { | ||
validateToolMetadata(integration, "aggregate", "Run an aggregation against a MongoDB collection", [ | ||
...databaseCollectionParameters, | ||
{ | ||
name: "pipeline", | ||
description: "An array of aggregation stages to execute", | ||
type: "array", | ||
required: true, | ||
}, | ||
]); | ||
|
||
validateThrowsForInvalidArguments(integration, "aggregate", [ | ||
{}, | ||
{ database: "test", collection: "foo" }, | ||
{ database: test, pipeline: [] }, | ||
{ database: "test", collection: "foo", pipeline: {} }, | ||
{ database: "test", collection: "foo", pipeline: [], extra: "extra" }, | ||
{ database: "test", collection: [], pipeline: [] }, | ||
{ database: 123, collection: "foo", pipeline: [] }, | ||
]); | ||
|
||
it("can run aggragation on non-existent database", async () => { | ||
await integration.connectMcpClient(); | ||
const response = await integration.mcpClient().callTool({ | ||
name: "aggregate", | ||
arguments: { database: "non-existent", collection: "people", pipeline: [{ $match: { name: "Peter" } }] }, | ||
}); | ||
|
||
const elements = getResponseElements(response.content); | ||
expect(elements).toHaveLength(1); | ||
expect(elements[0].text).toEqual('Found 0 documents in the collection "people":'); | ||
}); | ||
|
||
it("can run aggragation on an empty collection", async () => { | ||
await integration.mongoClient().db(integration.randomDbName()).createCollection("people"); | ||
|
||
await integration.connectMcpClient(); | ||
const response = await integration.mcpClient().callTool({ | ||
name: "aggregate", | ||
arguments: { | ||
database: integration.randomDbName(), | ||
collection: "people", | ||
pipeline: [{ $match: { name: "Peter" } }], | ||
}, | ||
}); | ||
|
||
const elements = getResponseElements(response.content); | ||
expect(elements).toHaveLength(1); | ||
expect(elements[0].text).toEqual('Found 0 documents in the collection "people":'); | ||
}); | ||
|
||
it("can run aggragation on an existing collection", async () => { | ||
const mongoClient = integration.mongoClient(); | ||
await mongoClient | ||
.db(integration.randomDbName()) | ||
.collection("people") | ||
.insertMany([ | ||
{ name: "Peter", age: 5 }, | ||
{ name: "Laura", age: 10 }, | ||
{ name: "Søren", age: 15 }, | ||
]); | ||
|
||
await integration.connectMcpClient(); | ||
const response = await integration.mcpClient().callTool({ | ||
name: "aggregate", | ||
arguments: { | ||
database: integration.randomDbName(), | ||
collection: "people", | ||
pipeline: [{ $match: { age: { $gt: 8 } } }, { $sort: { name: -1 } }], | ||
}, | ||
}); | ||
|
||
const elements = getResponseElements(response.content); | ||
expect(elements).toHaveLength(3); | ||
expect(elements[0].text).toEqual('Found 2 documents in the collection "people":'); | ||
expect(JSON.parse(elements[1].text)).toEqual({ _id: expect.any(Object), name: "Søren", age: 15 }); | ||
expect(JSON.parse(elements[2].text)).toEqual({ _id: expect.any(Object), name: "Laura", age: 10 }); | ||
}); | ||
|
||
validateAutoConnectBehavior(integration, "aggregate", () => { | ||
return { | ||
args: { | ||
database: integration.randomDbName(), | ||
collection: "coll1", | ||
pipeline: [{ $match: { name: "Liva" } }], | ||
}, | ||
expectedResponse: 'Found 0 documents in the collection "coll1"', | ||
}; | ||
}); | ||
}); |
98 changes: 98 additions & 0 deletions
98
tests/integration/tools/mongodb/read/collectionIndexes.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { IndexDirection } from "mongodb"; | ||
import { | ||
databaseCollectionParameters, | ||
validateToolMetadata, | ||
validateThrowsForInvalidArguments, | ||
getResponseElements, | ||
databaseCollectionInvalidArgs, | ||
} from "../../../helpers.js"; | ||
import { describeWithMongoDB, validateAutoConnectBehavior } from "../mongodbHelpers.js"; | ||
|
||
describeWithMongoDB("collectionIndexes tool", (integration) => { | ||
validateToolMetadata( | ||
integration, | ||
"collection-indexes", | ||
"Describe the indexes for a collection", | ||
databaseCollectionParameters | ||
); | ||
|
||
validateThrowsForInvalidArguments(integration, "collection-indexes", databaseCollectionInvalidArgs); | ||
|
||
it("can inspect indexes on non-existent database", async () => { | ||
await integration.connectMcpClient(); | ||
const response = await integration.mcpClient().callTool({ | ||
name: "collection-indexes", | ||
arguments: { database: "non-existent", collection: "people" }, | ||
}); | ||
|
||
const elements = getResponseElements(response.content); | ||
expect(elements).toHaveLength(1); | ||
expect(elements[0].text).toEqual( | ||
'The indexes for "non-existent.people" cannot be determined because the collection does not exist.' | ||
); | ||
}); | ||
|
||
it("returns the _id index for a new collection", async () => { | ||
await integration.mongoClient().db(integration.randomDbName()).createCollection("people"); | ||
|
||
await integration.connectMcpClient(); | ||
const response = await integration.mcpClient().callTool({ | ||
name: "collection-indexes", | ||
arguments: { | ||
database: integration.randomDbName(), | ||
collection: "people", | ||
}, | ||
}); | ||
|
||
const elements = getResponseElements(response.content); | ||
expect(elements).toHaveLength(2); | ||
expect(elements[0].text).toEqual('Found 1 indexes in the collection "people":'); | ||
expect(elements[1].text).toEqual('Name "_id_", definition: {"_id":1}'); | ||
}); | ||
|
||
it("returns all indexes for a collection", async () => { | ||
await integration.mongoClient().db(integration.randomDbName()).createCollection("people"); | ||
|
||
const indexTypes: IndexDirection[] = [-1, 1, "2d", "2dsphere", "text", "hashed"]; | ||
for (const indexType of indexTypes) { | ||
await integration | ||
.mongoClient() | ||
.db(integration.randomDbName()) | ||
.collection("people") | ||
.createIndex({ [`prop_${indexType}`]: indexType }); | ||
} | ||
|
||
await integration.connectMcpClient(); | ||
const response = await integration.mcpClient().callTool({ | ||
name: "collection-indexes", | ||
arguments: { | ||
database: integration.randomDbName(), | ||
collection: "people", | ||
}, | ||
}); | ||
|
||
const elements = getResponseElements(response.content); | ||
expect(elements).toHaveLength(indexTypes.length + 2); | ||
expect(elements[0].text).toEqual(`Found ${indexTypes.length + 1} indexes in the collection "people":`); | ||
expect(elements[1].text).toEqual('Name "_id_", definition: {"_id":1}'); | ||
|
||
for (const indexType of indexTypes) { | ||
const index = elements.find((element) => element.text.includes(`prop_${indexType}`)); | ||
expect(index).toBeDefined(); | ||
|
||
let expectedDefinition = JSON.stringify({ [`prop_${indexType}`]: indexType }); | ||
if (indexType === "text") { | ||
expectedDefinition = '{"_fts":"text"'; | ||
} | ||
|
||
expect(index!.text).toContain(`definition: ${expectedDefinition}`); | ||
} | ||
}); | ||
|
||
validateAutoConnectBehavior(integration, "collection-indexes", () => { | ||
return { | ||
args: { database: integration.randomDbName(), collection: "coll1" }, | ||
expectedResponse: `The indexes for "${integration.randomDbName()}.coll1" cannot be determined because the collection does not exist.`, | ||
}; | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm a bit worried about this, we should run the MCP against a very very large collection and ask for all documents: #108
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.
That wasn't previously used, so figured it's better to remove it rather than lie to the model. Agree that we should investigate mechanisms to mitigate that risk post initial release.