Skip to main content
Glama
test-database.ts4.66 kB
/** * Test Database Utilities * * Provides utilities for managing PostgreSQL test databases: * - Creating isolated test databases * - Setting up schema and indexes * - Seeding test data * - Cleaning up after tests */ import { testEnv } from "../setup/test-environment"; export interface TestDatabase { id: string; name: string; connectionString: string; isConnected: boolean; } export interface TestDatabaseOptions { name?: string; setupSchema?: boolean; seedData?: boolean; } /** * Create an isolated test database * * @param options - Configuration options * @returns Test database instance */ export async function createTestDatabase(options: TestDatabaseOptions = {}): Promise<TestDatabase> { const dbId = generateDatabaseId(); const dbName = options.name ?? `test_${dbId}`; const testDb: TestDatabase = { id: dbId, name: dbName, connectionString: buildConnectionString(dbName), isConnected: false, }; // Note: Actual database creation will be implemented when PostgreSQL // connection manager is available (Phase 1) // For now, this is a placeholder that returns the configuration console.log(`📦 Test database created: ${dbName}`); return testDb; } /** * Cleanup test database * * @param testDb - Test database to cleanup */ export async function cleanupTestDatabase(testDb: TestDatabase): Promise<void> { // Note: Actual cleanup will be implemented when PostgreSQL // connection manager is available (Phase 1) console.log(`🧹 Test database cleaned up: ${testDb.name}`); } /** * Setup database schema for testing * * @param testDb - Test database instance */ export async function setupTestSchema(testDb: TestDatabase): Promise<void> { // Note: Schema setup will be implemented when migration system // is available (Phase 1) console.log(`📋 Test schema setup: ${testDb.name}`); } /** * Seed test database with data * * @param testDb - Test database instance * @param seedData - Data to seed */ export async function seedTestDatabase(testDb: TestDatabase, _seedData: unknown): Promise<void> { // Note: Seeding will be implemented when data models // are available (Phase 1) console.log(`🌱 Test database seeded: ${testDb.name}`); } /** * Clear all data from test database * * @param testDb - Test database instance */ export async function clearTestDatabase(testDb: TestDatabase): Promise<void> { // Note: Clearing will be implemented when database operations // are available (Phase 1) console.log(`🗑️ Test database cleared: ${testDb.name}`); } /** * Execute query in test database * * @param testDb - Test database instance * @param query - SQL query to execute * @param params - Query parameters * @returns Query result */ export async function executeTestQuery<T = unknown>( _testDb: TestDatabase, _query: string, _params?: unknown[] ): Promise<T> { // Note: Query execution will be implemented when database // connection manager is available (Phase 1) throw new Error("Query execution not yet implemented"); } /** * Begin transaction in test database * * @param testDb - Test database instance * @returns Transaction handle */ export async function beginTestTransaction(_testDb: TestDatabase): Promise<TestTransaction> { // Note: Transaction support will be implemented when database // connection manager is available (Phase 1) return { commit: async () => {}, rollback: async () => {}, }; } export interface TestTransaction { commit: () => Promise<void>; rollback: () => Promise<void>; } // Helper functions function generateDatabaseId(): string { return `${Date.now()}_${Math.random().toString(36).substring(7)}`; } function buildConnectionString(dbName: string): string { const { host, port, user, password } = testEnv.database; return `postgresql://${user}:${password}@${host}:${port}/${dbName}`; } /** * Wait for database to be ready * * @param testDb - Test database instance * @param timeoutMs - Timeout in milliseconds */ export async function waitForDatabase( _testDb: TestDatabase, timeoutMs: number = 5000 ): Promise<void> { const startTime = Date.now(); // Note: Health check will be implemented when database // connection manager is available (Phase 1) // For now, we just wait a bit to simulate readiness check while (Date.now() - startTime < timeoutMs) { // Simulate health check - will be replaced with actual check in Phase 1 await new Promise((resolve) => setTimeout(resolve, 100)); // Assume ready after first check for now return; } throw new Error(`Database not ready after ${timeoutMs}ms`); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/keyurgolani/ThoughtMcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server