|
| 1 | +import { Client } from 'pg'; |
| 2 | + |
| 3 | +const PG_CONFIG = { |
| 4 | + host: process.env.PGHOST || 'localhost', |
| 5 | + port: Number(process.env.PGPORT) || 5432, |
| 6 | + user: process.env.PGUSER || 'postgres', |
| 7 | + password: process.env.PGPASSWORD || 'postgres123!', |
| 8 | + database: process.env.PGDATABASE || 'constructive', |
| 9 | +}; |
| 10 | + |
| 11 | +const JOB_TIMEOUT = 30000; |
| 12 | + |
| 13 | +describe('sql-example e2e', () => { |
| 14 | + let client: Client; |
| 15 | + let databaseId: string; |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + client = new Client(PG_CONFIG); |
| 19 | + await client.connect(); |
| 20 | + |
| 21 | + const dbResult = await client.query( |
| 22 | + `SELECT id FROM metaschema_public.database LIMIT 1` |
| 23 | + ); |
| 24 | + if (dbResult.rows.length === 0) { |
| 25 | + throw new Error('No database record found in metaschema_public.database'); |
| 26 | + } |
| 27 | + databaseId = dbResult.rows[0].id; |
| 28 | + }); |
| 29 | + |
| 30 | + afterAll(async () => { |
| 31 | + await client.end(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should connect to postgres via pool and complete job', async () => { |
| 35 | + const insertResult = await client.query( |
| 36 | + `SELECT * FROM app_jobs.add_job($1, 'sql-example'::text, $2::json)`, |
| 37 | + [databaseId, JSON.stringify({})] |
| 38 | + ); |
| 39 | + |
| 40 | + const jobId = insertResult.rows[0].id; |
| 41 | + expect(jobId).toBeDefined(); |
| 42 | + |
| 43 | + const startTime = Date.now(); |
| 44 | + while (Date.now() - startTime < JOB_TIMEOUT) { |
| 45 | + const statusResult = await client.query( |
| 46 | + `SELECT * FROM app_jobs.jobs WHERE id = $1`, |
| 47 | + [jobId] |
| 48 | + ); |
| 49 | + |
| 50 | + if (statusResult.rows.length === 0) { |
| 51 | + return; // Job completed successfully |
| 52 | + } |
| 53 | + |
| 54 | + const job = statusResult.rows[0]; |
| 55 | + if (job.last_error) { |
| 56 | + throw new Error(`Job failed: ${job.last_error}`); |
| 57 | + } |
| 58 | + |
| 59 | + await new Promise((r) => setTimeout(r, 500)); |
| 60 | + } |
| 61 | + |
| 62 | + throw new Error('Job did not complete within timeout'); |
| 63 | + }); |
| 64 | +}); |
0 commit comments