|
| 1 | +import { describe, it, expect, beforeAll } from '@jest/globals'; |
| 2 | +import { query } from '../db/connection.js'; |
| 3 | + |
| 4 | +let databaseAvailable = false; |
| 5 | + |
| 6 | +beforeAll(async () => { |
| 7 | + try { |
| 8 | + await query('SELECT 1'); |
| 9 | + databaseAvailable = true; |
| 10 | + } catch { |
| 11 | + databaseAvailable = false; |
| 12 | + } |
| 13 | +}); |
| 14 | + |
| 15 | +const describeIf = (name: string, fn: () => void) => { |
| 16 | + if (databaseAvailable) { |
| 17 | + describe(name, fn); |
| 18 | + } else { |
| 19 | + describe.skip(`${name} (skipped: no database)`, fn); |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +describeIf('loan_disputes schema', () => { |
| 24 | + it('should successfully insert a loan dispute against the schema', async () => { |
| 25 | + const result = await query( |
| 26 | + `INSERT INTO loan_disputes (loan_id, borrower, reason) VALUES ($1, $2, $3) RETURNING id`, |
| 27 | + [100, 'G_DISPUTE_TEST_BORROWER', 'Test dispute reason'], |
| 28 | + ); |
| 29 | + |
| 30 | + expect(result.rows.length).toBe(1); |
| 31 | + expect(result.rows[0].id).toBeDefined(); |
| 32 | + |
| 33 | + const selectResult = await query(`SELECT * FROM loan_disputes WHERE id = $1`, [ |
| 34 | + result.rows[0].id, |
| 35 | + ]); |
| 36 | + expect(selectResult.rows[0].loan_id).toBe(100); |
| 37 | + expect(selectResult.rows[0].borrower).toBe('G_DISPUTE_TEST_BORROWER'); |
| 38 | + expect(selectResult.rows[0].status).toBe('open'); |
| 39 | + |
| 40 | + await query('DELETE FROM loan_disputes WHERE id = $1', [result.rows[0].id]); |
| 41 | + }); |
| 42 | +}); |
0 commit comments