Testing Approach
DBHub uses a comprehensive testing strategy combining unit tests and integration tests to ensure reliability across all supported databases.Testing Stack
- Test Framework: Vitest - Fast, modern test runner
- Integration Testing: Testcontainers - Real database instances in Docker
- Type Safety: TypeScript strict mode for compile-time validation
Running Tests
All Tests
Unit Tests Only
Integration Tests
Watch Mode
Specific Database Tests
Test individual database connectors:Test Configuration
Location:vitest.config.ts
The configuration defines two test projects:
Integration Testing with Testcontainers
Prerequisites
Before running integration tests, ensure:- Docker is installed and running:
- Sufficient Docker memory (4GB+ recommended for SQL Server)
- Network access to pull Docker images
How Testcontainers Work
Testcontainers automatically:- Pulls the specified database Docker image
- Starts a container with random ports
- Waits for the database to be ready
- Provides connection details to your tests
- Cleans up containers after tests complete
Database Images Used
Integration Test Architecture
Shared Test Base
All connector integration tests extendIntegrationTestBase for consistency.
Location: src/connectors/__tests__/shared/integration-test-base.ts
Benefits:
- Consistent test structure across all databases
- Reusable test setup and teardown
- Common test scenarios
- Reduced code duplication
Test Lifecycle
Example Integration Test
Common Test Scenarios
Each connector integration test covers:1. Connection Tests
- Successful connection with valid DSN
- Connection failure with invalid credentials
- SSL/TLS connection modes
- Connection timeout handling
2. Schema Discovery
- List all schemas
- List tables in a schema
- Get table column information
- Get table indexes
- List stored procedures/functions (if supported)
3. Query Execution
- Simple SELECT queries
- Parameterized queries
- Multi-statement execution (transactions)
- Query timeout enforcement
- Row limiting (maxRows option)
4. Data Types
- String/varchar types
- Numeric types (integers, decimals)
- Date/time types
- Boolean types
- NULL handling
- Binary/blob types (if supported)
5. Error Handling
- Invalid SQL syntax
- Non-existent tables
- Permission errors
- Connection errors
- Query timeout errors
6. Database-Specific Features
PostgreSQL:- Array types
- JSON/JSONB types
- User-defined types
- Nested multi-line comments
- Dollar-quoted strings
- search_path configuration
- Backtick identifiers
- AUTO_INCREMENT columns
- MySQL-specific data types
- Bracket identifiers
- Named instances
- NTLM authentication
- IDENTITY columns
- In-memory databases (
:memory:) - File-based databases
- Simplified type system
- Read-only mode
SSH Tunnel Tests
Location:src/connectors/__tests__/postgres-ssh.integration.test.ts
Tests SSH tunneling through a bastion host:
- Start SSH server container (openssh-server)
- Start PostgreSQL container (not exposed to host)
- Configure SSH tunnel through bastion
- Connect to PostgreSQL via tunnel
- Verify queries work through tunnel
- Password-based SSH authentication
- Key-based SSH authentication
- SSH config file parsing
- ProxyJump support
- Keepalive settings
- Error handling for SSH failures
Pre-Commit Hooks
DBHub uses Git pre-commit hooks to run related tests automatically: Location:.git/hooks/pre-commit (auto-generated)
Behavior:
- Detects changed files
- Runs unit tests for modified utilities
- Runs integration tests for modified connectors
- Fast feedback loop (only runs relevant tests)
Writing New Tests
Unit Test Example
Location:src/utils/__tests__/sql-parser.test.ts
Integration Test for New Connector
Test Coverage Expectations
Minimum Coverage Goals
- Utilities: 80%+ line coverage
- Connectors: All core methods tested
- Tools: Happy path + error cases
- Integration: All supported databases
Critical Paths Requiring Tests
- Connection handling - connect/disconnect lifecycle
- SQL execution - executeSQL with various options
- Schema discovery - getSchemas, getTables, getTableSchema
- DSN parsing - all supported DSN formats
- Error handling - network errors, SQL errors, timeouts
Troubleshooting
Container Startup Issues
Problem: Containers fail to start Solutions:SQL Server Timeout Issues
Problem: SQL Server tests timeout Cause: SQL Server requires 3-5 minutes to start and additional memory Solutions:- Increase Docker memory allocation to 4GB+
- Run SQL Server tests separately:
- Increase test timeout in
vitest.config.ts
Port Conflicts
Problem: Container fails due to port already in use Solution: Testcontainers automatically selects random ports, but if issues persist:Memory Issues
Problem: Tests crash or containers fail to start Solution:- Increase Docker memory limit (Docker Desktop → Settings → Resources)
- Run fewer tests in parallel
- Run database tests sequentially:
Debug Failed Tests
Continuous Integration (CI/CD)
GitHub Actions Workflow
Location:.github/workflows/test.yml
Triggers:
- Push to main branch
- Pull request creation/update
- Manual workflow dispatch
-
Unit Tests: Fast tests without Docker
-
Integration Tests: Full database testing
-
Type Checking: TypeScript compilation
CI Environment
- Node.js: Latest LTS version
- pnpm: Latest version
- Docker: Available in GitHub Actions runners
- Timeout: 20 minutes for integration tests
Best Practices
1. Test Isolation
- Each test should be independent
- Use
beforeEachfor test-specific setup - Clean up resources in
afterEach/afterAll
2. Meaningful Assertions
3. Test Names
4. Async Handling
5. Error Testing
Next Steps
- Building Connectors - Implement and test new database support
- Architecture - Understand the testing infrastructure
- Development Overview - Contributing guidelines