NewTestDB creates an isolated PostgreSQL database for each test by copying a template. The template database has migrations pre-applied, so each test is fast. Requires PostgreSQL to be running on localhost:5432 (e.g., via docker-compose).
(t *testing.T)
| 58 | // The template database has migrations pre-applied, so each test is fast. |
| 59 | // Requires PostgreSQL to be running on localhost:5432 (e.g., via docker-compose). |
| 60 | func NewTestDB(t *testing.T) Database { |
| 61 | t.Helper() |
| 62 | |
| 63 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 64 | defer cancel() |
| 65 | |
| 66 | // Connect to postgres database |
| 67 | adminURI := "postgres://mcpregistry:mcpregistry@localhost:5432/postgres?sslmode=disable" //nolint:gosec // G101: test database credentials |
| 68 | adminConn, err := pgx.Connect(ctx, adminURI) |
| 69 | require.NoError(t, err, "Failed to connect to PostgreSQL. Make sure PostgreSQL is running via: docker-compose up -d postgres") |
| 70 | defer adminConn.Close(ctx) |
| 71 | |
| 72 | // Ensure template database exists with migrations |
| 73 | err = ensureTemplateDB(ctx, adminConn) |
| 74 | require.NoError(t, err, "Failed to initialize template database") |
| 75 | |
| 76 | // Generate unique database name for this test |
| 77 | var randomBytes [8]byte |
| 78 | _, err = rand.Read(randomBytes[:]) |
| 79 | require.NoError(t, err, "Failed to generate random database id") |
| 80 | randomInt := binary.BigEndian.Uint64(randomBytes[:]) |
| 81 | dbName := fmt.Sprintf("test_%d", randomInt) |
| 82 | |
| 83 | // Create test database from template (fast - just copies files) |
| 84 | _, err = adminConn.Exec(ctx, fmt.Sprintf("CREATE DATABASE %s TEMPLATE %s", dbName, templateDBName)) |
| 85 | require.NoError(t, err, "Failed to create test database from template") |
| 86 | |
| 87 | // Register cleanup to drop database |
| 88 | t.Cleanup(func() { |
| 89 | cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 90 | defer cleanupCancel() |
| 91 | |
| 92 | // Terminate any remaining connections |
| 93 | _, _ = adminConn.Exec(cleanupCtx, fmt.Sprintf( |
| 94 | "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '%s' AND pid <> pg_backend_pid()", |
| 95 | dbName, |
| 96 | )) |
| 97 | |
| 98 | // Drop database |
| 99 | _, _ = adminConn.Exec(cleanupCtx, fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbName)) |
| 100 | }) |
| 101 | |
| 102 | // Connect to test database (no migrations needed - copied from template) |
| 103 | testURI := fmt.Sprintf("postgres://mcpregistry:mcpregistry@localhost:5432/%s?sslmode=disable", dbName) |
| 104 | |
| 105 | db, err := NewPostgreSQL(ctx, testURI) |
| 106 | require.NoError(t, err, "Failed to connect to test database") |
| 107 | |
| 108 | // Register cleanup to close connection |
| 109 | t.Cleanup(func() { |
| 110 | if err := db.Close(); err != nil { |
| 111 | t.Logf("Warning: failed to close test database connection: %v", err) |
| 112 | } |
| 113 | }) |
| 114 | |
| 115 | return db |
| 116 | } |
searching dependent graphs…