Skip to content

Commit e02b85a

Browse files
committed
chore: add integration tests for pg, mysql, mariadb, sqlserver
1 parent 886932e commit e02b85a

File tree

8 files changed

+2256
-11
lines changed

8 files changed

+2256
-11
lines changed

README.md

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ https://demo.dbhub.ai/message connects a [sample employee database](https://gith
5454

5555
### Database Tools
5656

57-
| Tool | Command Name | Description | PostgreSQL | MySQL | MariaDB | SQL Server | SQLite | Oracle |
58-
| --------------- | ----------------- | ------------------------------------------------------------------- | :--------: | :---: | :-----: | :--------: | ------ | :----: |
59-
| Execute SQL | `execute_sql` | Execute single or multiple SQL statements (separated by semicolons) |||||||
57+
| Tool | Command Name | Description | PostgreSQL | MySQL | MariaDB | SQL Server | SQLite | Oracle |
58+
| ----------- | ------------- | ------------------------------------------------------------------- | :--------: | :---: | :-----: | :--------: | ------ | :----: |
59+
| Execute SQL | `execute_sql` | Execute single or multiple SQL statements (separated by semicolons) |||||||
6060

6161
### Prompt Capabilities
6262

@@ -236,7 +236,7 @@ npx @bytebase/dbhub --demo
236236
```
237237

238238
> [!WARNING]
239-
If your user/password contains special characters, you need to escape them first. (e.g. `pass#word` should be escaped as `pass%23word`)
239+
> If your user/password contains special characters, you need to escape them first. (e.g. `pass#word` should be escaped as `pass%23word`)
240240
241241
For real databases, a Database Source Name (DSN) is required. You can provide this in several ways:
242242

@@ -318,13 +318,13 @@ Extra query parameters:
318318

319319
### Command line options
320320

321-
| Option | Environment Variable | Description | Default |
322-
| --------- | -------------------- | --------------------------------------------------------------- | ---------------------------- |
323-
| dsn | `DSN` | Database connection string | Required if not in demo mode |
324-
| transport | `TRANSPORT` | Transport mode: `stdio` or `http` | `stdio` |
321+
| Option | Environment Variable | Description | Default |
322+
| --------- | -------------------- | ---------------------------------------------------------------- | ---------------------------- |
323+
| dsn | `DSN` | Database connection string | Required if not in demo mode |
324+
| transport | `TRANSPORT` | Transport mode: `stdio` or `http` | `stdio` |
325325
| port | `PORT` | HTTP server port (only applicable when using `--transport=http`) | `8080` |
326-
| readonly | `READONLY` | Restrict SQL execution to read-only operations | `false` |
327-
| demo | N/A | Run in demo mode with sample employee database | `false` |
326+
| readonly | `READONLY` | Restrict SQL execution to read-only operations | `false` |
327+
| demo | N/A | Run in demo mode with sample employee database | `false` |
328328

329329
The demo mode uses an in-memory SQLite database loaded with the [sample employee database](https://github.com/bytebase/dbhub/tree/main/resources/employee-sqlite) that includes tables for employees, departments, titles, salaries, department employees, and department managers. The sample database includes SQL scripts for table creation, data loading, and testing.
330330

@@ -350,10 +350,85 @@ The demo mode uses an in-memory SQLite database loaded with the [sample employee
350350

351351
### Testing
352352

353-
The project uses Vitest for comprehensive unit testing:
353+
The project uses Vitest for comprehensive unit and integration testing:
354354

355355
- **Run all tests**: `pnpm test`
356356
- **Run tests in watch mode**: `pnpm test:watch`
357+
- **Run integration tests**: `pnpm test:integration`
358+
359+
#### Integration Tests
360+
361+
DBHub includes comprehensive integration tests for all supported database connectors using [Testcontainers](https://testcontainers.com/). These tests run against real database instances in Docker containers, ensuring full compatibility and feature coverage.
362+
363+
##### Prerequisites
364+
365+
- **Docker**: Ensure Docker is installed and running on your machine
366+
- **Docker Resources**: Allocate sufficient memory (recommended: 4GB+) for multiple database containers
367+
- **Network Access**: Ability to pull Docker images from registries
368+
369+
##### Running Integration Tests
370+
371+
**Note**: This command runs all integration tests in parallel, which may take 5-15 minutes depending on your system resources and network speed.
372+
373+
```bash
374+
# Run all database integration tests (PostgreSQL, MySQL, MariaDB, SQL Server)
375+
pnpm test:integration
376+
```
377+
378+
```bash
379+
# Run only PostgreSQL integration tests
380+
pnpm test:integration -- --testNamePattern="PostgreSQL"
381+
# Run only MySQL integration tests
382+
pnpm test:integration -- --testNamePattern="MySQL"
383+
# Run only MariaDB integration tests
384+
pnpm test:integration -- --testNamePattern="MariaDB"
385+
# Run only SQL Server integration tests
386+
pnpm test:integration -- --testNamePattern="SQL Server"
387+
```
388+
389+
All integration tests follow these patterns:
390+
391+
1. **Container Lifecycle**: Start database container → Connect → Setup test data → Run tests → Cleanup
392+
2. **Shared Test Utilities**: Common test patterns implemented in `IntegrationTestBase` class
393+
3. **Database-Specific Features**: Each database includes tests for unique features and capabilities
394+
4. **Error Handling**: Comprehensive testing of connection errors, invalid SQL, and edge cases
395+
396+
##### Troubleshooting Integration Tests
397+
398+
**Container Startup Issues:**
399+
400+
```bash
401+
# Check Docker is running
402+
docker ps
403+
404+
# Check available memory
405+
docker system df
406+
407+
# Pull images manually if needed
408+
docker pull postgres:15-alpine
409+
docker pull mysql:8.0
410+
docker pull mariadb:10.11
411+
docker pull mcr.microsoft.com/mssql/server:2019-latest
412+
```
413+
414+
**SQL Server Timeout Issues:**
415+
416+
- SQL Server containers require significant startup time (3-5 minutes)
417+
- Ensure Docker has sufficient memory allocated (4GB+ recommended)
418+
- Consider running SQL Server tests separately if experiencing timeouts
419+
420+
**Network/Resource Issues:**
421+
422+
```bash
423+
# Run tests with verbose output
424+
pnpm test:integration --reporter=verbose
425+
426+
# Run single database test to isolate issues
427+
pnpm test:integration -- --testNamePattern="PostgreSQL"
428+
429+
# Check Docker container logs if tests fail
430+
docker logs <container_id>
431+
```
357432

358433
#### Pre-commit Hooks (for Developers)
359434

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"crossdev": "cross-env NODE_ENV=development tsx src/index.ts",
2020
"test": "vitest run",
2121
"test:watch": "vitest",
22+
"test:integration": "vitest run --testNamePattern='Integration Tests'",
2223
"prepare": "[[ \"$NODE_ENV\" != \"production\" ]] && husky || echo \"Skipping husky in production\"",
2324
"pre-commit": "lint-staged"
2425
},
@@ -39,6 +40,10 @@
3940
"zod": "^3.24.2"
4041
},
4142
"devDependencies": {
43+
"@testcontainers/mariadb": "^11.0.3",
44+
"@testcontainers/mssqlserver": "^11.0.3",
45+
"@testcontainers/mysql": "^11.0.3",
46+
"@testcontainers/postgresql": "^11.0.3",
4247
"@types/better-sqlite3": "^7.6.12",
4348
"@types/express": "^4.17.21",
4449
"@types/mssql": "^9.1.7",
@@ -49,6 +54,7 @@
4954
"husky": "^9.0.11",
5055
"lint-staged": "^15.2.2",
5156
"prettier": "^3.5.3",
57+
"testcontainers": "^11.0.3",
5258
"ts-node": "^10.9.2",
5359
"tsup": "^8.4.0",
5460
"tsx": "^4.19.3",

0 commit comments

Comments
 (0)