Repository navigation
feat: add PostgreSQL database isolation tests and enhance connection handling in PostgresDriverAdapter #2754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+234
−149
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/services/database/__tests__/postgres-database.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { execFileSync } from 'node:child_process' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { Client } from 'pg' | ||
| import { PostgresDriverAdapter } from '../drivers/postgres-driver' | ||
| import type { ResolvedDbCredential } from '../types' | ||
|
|
||
| // Opt in against a local Docker container; never uses an application database for fixtures. | ||
| describe.skipIf(!process.env.PG_TEST_CONTAINER)('PostgreSQL database isolation', () => { | ||
| it('loads each database independently with and without a configured database', async () => { | ||
| const details = JSON.parse(execFileSync('docker', ['inspect', process.env.PG_TEST_CONTAINER!], { encoding: 'utf8' })) | ||
| const env = Object.fromEntries( | ||
| details[0].Config.Env.map((entry: string) => { | ||
| const index = entry.indexOf('=') | ||
| return [entry.slice(0, index), entry.slice(index + 1)] | ||
| }) | ||
| ) | ||
| const config = { | ||
| host: '127.0.0.1', | ||
| port: Number(process.env.PG_TEST_PORT || 5432), | ||
| user: env.POSTGRES_USER || 'postgres', | ||
| password: env.POSTGRES_PASSWORD, | ||
| database: 'postgres' | ||
| } | ||
| const admin = new Client(config) | ||
| const databases = [`chaterm_test_a_${process.pid}`, `chaterm_test_b_${process.pid}`] | ||
| const adapter = new PostgresDriverAdapter() | ||
| await admin.connect() | ||
| try { | ||
| for (const [index, database] of databases.entries()) { | ||
| await admin.query(`CREATE DATABASE "${database}"`) | ||
| const client = new Client({ ...config, database }) | ||
| await client.connect() | ||
| try { | ||
| await client.query(`CREATE TABLE public.fixture_${index} (column_${index} integer PRIMARY KEY)`) | ||
| await client.query(`CREATE SCHEMA schema_${index}`) | ||
| } finally { | ||
| await client.end() | ||
| } | ||
| } | ||
| for (const database of [null, databases[0]]) { | ||
| const handle = await adapter.connect({ | ||
| host: config.host, | ||
| port: config.port, | ||
| username: config.user, | ||
| password: config.password, | ||
| database | ||
| } as ResolvedDbCredential) | ||
| try { | ||
| for (const [index, target] of databases.entries()) { | ||
| expect(await adapter.listTables(handle, target, 'public')).toEqual([`fixture_${index}`]) | ||
| expect(await adapter.listObjects(handle, target, 'public', 'tables')).toEqual([`fixture_${index}`]) | ||
| expect(await adapter.listColumns(handle, target, `fixture_${index}`, 'public')).toEqual([`column_${index}`]) | ||
| expect(await adapter.detectPrimaryKey(handle, target, `fixture_${index}`, 'public')).toEqual([`column_${index}`]) | ||
| expect((await adapter.listSchemas(handle, target)).map((schema) => schema.name)).toContain(`schema_${index}`) | ||
| } | ||
| const results = await Promise.all(databases.map((target) => adapter.listTables(handle, target, 'public'))) | ||
| expect(results).toEqual([['fixture_0'], ['fixture_1']]) | ||
| await expect(adapter.listTables(handle, `chaterm_missing_${process.pid}`, 'public')).rejects.toThrow() | ||
| } finally { | ||
| await adapter.disconnect(handle) | ||
| } | ||
| } | ||
| } finally { | ||
| for (const database of databases) await admin.query(`DROP DATABASE IF EXISTS "${database}"`) | ||
| await admin.end() | ||
| } | ||
| }, 30000) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isolate the default database used by the null-database case.
If the test process has
PGDATABASEset to a database absent from the container,adapter.connectfails before any routing assertions. The admin connection still succeeds because it explicitly selectspostgres. An omitted node-postgres database usesPGDATABASE, then the configured user. (github.com)Set a known default database for this test and restore the previous environment afterward. Keep the
database: nullcase so it still tests routing without an explicit database.