-
Notifications
You must be signed in to change notification settings - Fork 109
chore: add integration tests for list-databases #74
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 1 commit
Commits
Show all changes
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
4 changes: 2 additions & 2 deletions
4
src/tools/mongodb/createIndex.ts → src/tools/mongodb/create/createIndex.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
8 changes: 4 additions & 4 deletions
8
src/tools/mongodb/connect.ts → src/tools/mongodb/metadata/connect.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
4 changes: 2 additions & 2 deletions
4
src/tools/mongodb/collectionIndexes.ts → src/tools/mongodb/read/collectionIndexes.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
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
74 changes: 74 additions & 0 deletions
74
tests/integration/tools/mongodb/metadata/listDatabases.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,74 @@ | ||
import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
import { runMongoDB, setupIntegrationTest, getResponseElements, connect } from "../../../helpers.js"; | ||
import runner from "mongodb-runner"; | ||
import { MongoClient } from "mongodb"; | ||
import { toIncludeSameMembers } from "jest-extended"; | ||
|
||
describe("listDatabases tool", () => { | ||
let client: Client; | ||
let serverClientTeardown: () => Promise<void>; | ||
|
||
let cluster: runner.MongoCluster; | ||
|
||
beforeAll(async () => { | ||
cluster = await runMongoDB(); | ||
}, 60_000); | ||
|
||
beforeEach(async () => { | ||
({ client, teardown: serverClientTeardown } = await setupIntegrationTest()); | ||
}); | ||
|
||
afterEach(async () => { | ||
await serverClientTeardown?.(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cluster.close(); | ||
}); | ||
|
||
it("should have correct metadata", async () => { | ||
const tools = await client.listTools(); | ||
const listDatabases = tools.tools.find((tool) => tool.name === "list-databases"); | ||
expect(listDatabases).toBeDefined(); | ||
expect(listDatabases!.description).toBe("List all databases for a MongoDB connection"); | ||
expect(listDatabases!.inputSchema.type).toBe("object"); | ||
expect(listDatabases!.inputSchema.properties).toBeDefined(); | ||
|
||
const propertyNames = Object.keys(listDatabases!.inputSchema.properties!); | ||
expect(propertyNames).toHaveLength(0); | ||
}); | ||
|
||
describe("with no preexisting databases", () => { | ||
it("returns only the system databases", async () => { | ||
await connect(client, cluster); | ||
const response = await client.callTool({ name: "list-databases", arguments: {} }); | ||
const dbNames = getDbNames(response.content); | ||
|
||
expect(dbNames).toIncludeSameMembers(["admin", "config", "local"]); | ||
}); | ||
}); | ||
|
||
describe("with preexisting databases", () => { | ||
it("returns their names and sizes", async () => { | ||
const mongoClient = new MongoClient(cluster.connectionString); | ||
await mongoClient.db("foo").collection("bar").insertOne({ test: "test" }); | ||
await mongoClient.db("baz").collection("qux").insertOne({ test: "test" }); | ||
await mongoClient.close(); | ||
|
||
await connect(client, cluster); | ||
|
||
const response = await client.callTool({ name: "list-databases", arguments: {} }); | ||
const dbNames = getDbNames(response.content); | ||
expect(dbNames).toIncludeSameMembers(["admin", "config", "local", "foo", "baz"]); | ||
}); | ||
}); | ||
}); | ||
|
||
function getDbNames(content: unknown): (string | null)[] { | ||
const responseItems = getResponseElements(content); | ||
|
||
return responseItems.map((item) => { | ||
const match = item.text.match(/Name: (.*), Size: \d+ bytes/); | ||
return match ? match[1] : null; | ||
}); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.