|
| 1 | +import dataclasses |
| 2 | +import re |
| 3 | +import shlex |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +from cratedb_toolkit.util import DatabaseAdapter |
| 8 | + |
| 9 | + |
| 10 | +@dataclasses.dataclass |
| 11 | +class Process: |
| 12 | + """ |
| 13 | + Manage outputs of a process. |
| 14 | + """ |
| 15 | + proc: subprocess.Popen |
| 16 | + stdout: bytes |
| 17 | + stderr: bytes |
| 18 | + |
| 19 | + @property |
| 20 | + def returncode(self) -> int: |
| 21 | + return self.proc.returncode |
| 22 | + |
| 23 | + |
| 24 | +def run(command: str, timeout: int = 30) -> Process: |
| 25 | + """ |
| 26 | + Invoke a command in a subprocess. |
| 27 | + """ |
| 28 | + proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 29 | + stdout, stderr = proc.communicate(timeout=timeout) |
| 30 | + return Process(proc, stdout, stderr) |
| 31 | + |
| 32 | + |
| 33 | +def test_builtin(): |
| 34 | + """ |
| 35 | + Validate the basic built-in MCP server for PostgreSQL works well. |
| 36 | +
|
| 37 | + It is written in TypeScript. |
| 38 | + https://www.npmjs.com/package/@modelcontextprotocol/server-postgres |
| 39 | + """ |
| 40 | + p = run(f"{sys.executable} example_builtin.py") |
| 41 | + assert p.returncode == 0 |
| 42 | + |
| 43 | + # Validate output specific to the MCP server. |
| 44 | + assert b"Could not roll back transaction: error: line 1:1: mismatched input 'ROLLBACK' expecting" in p.stderr |
| 45 | + |
| 46 | + # Validate output specific to CrateDB. |
| 47 | + assert b"Calling tool: read_query" in p.stdout |
| 48 | + assert b'"mountain": "Mont Blanc"' in p.stdout |
| 49 | + |
| 50 | + |
| 51 | +def test_jdbc(): |
| 52 | + """ |
| 53 | + Validate the Quarkus MCP server for JDBC works well. |
| 54 | +
|
| 55 | + It is written in Java. |
| 56 | + https://github.com/quarkiverse/quarkus-mcp-servers/tree/main/jdbc#readme |
| 57 | + """ |
| 58 | + p = run(f"{sys.executable} example_jdbc.py") |
| 59 | + assert p.returncode == 0 |
| 60 | + |
| 61 | + # Validate output specific to the MCP server. |
| 62 | + assert re.match(br".*\[io.quarkus\] \(main\) mcp-server-jdbc 999-SNAPSHOT on JVM.*", p.stderr, re.DOTALL) |
| 63 | + |
| 64 | + # Validate output specific to CrateDB. |
| 65 | + assert b"Calling tool: database_info" in p.stdout |
| 66 | + assert b'{"database_product_name":"PostgreSQL","driver_name":"PostgreSQL JDBC Driver"' in p.stdout |
| 67 | + assert b"Calling tool: describe_table" in p.stdout |
| 68 | + assert b"Failed to describe table: The column name current_database was not found in this ResultSet." in p.stdout |
| 69 | + assert b"Calling tool: read_query" in p.stdout |
| 70 | + assert b'"mountain":"Mont Blanc"' in p.stdout |
| 71 | + |
| 72 | + # Validate database content. |
| 73 | + db = DatabaseAdapter("crate://crate@localhost:4200/") |
| 74 | + records = db.refresh_table("doc.testdrive") |
| 75 | + records = db.run_sql("SELECT * FROM doc.testdrive", records=True) |
| 76 | + assert len(records) >= 1 |
| 77 | + assert records[0] == {"id": 42, "data": "foobar"} |
| 78 | + |
| 79 | + |
| 80 | +def test_dbhub(): |
| 81 | + """ |
| 82 | + Validate the DBHub MCP server works well. |
| 83 | +
|
| 84 | + DBHub is a universal database gateway implementing the Model Context |
| 85 | + Protocol (MCP) server interface. This gateway allows MCP-compatible |
| 86 | + clients to connect to and explore different databases. |
| 87 | +
|
| 88 | + It is written in TypeScript. |
| 89 | + https://github.com/bytebase/dbhub |
| 90 | + """ |
| 91 | + p = run(f"{sys.executable} example_dbhub.py") |
| 92 | + assert p.returncode == 0 |
| 93 | + |
| 94 | + # Validate output specific to the MCP server. |
| 95 | + assert b"Successfully connected to PostgreSQL database" in p.stderr |
| 96 | + assert b"Universal Database MCP Server" in p.stderr |
| 97 | + |
| 98 | + # Validate output specific to CrateDB. |
| 99 | + assert b"Calling tool: run_query" in p.stdout |
| 100 | + assert b'"mountain": "Mont Blanc"' in p.stdout |
| 101 | + |
| 102 | + assert b"Calling tool: list_connectors" in p.stdout |
| 103 | + assert b' "dsn": "postgres://' in p.stdout |
0 commit comments