|
| 1 | +process.env.LOG_SCOPE = 'pgsql-test'; |
| 2 | + |
| 3 | +import { DeferredConstraintsMode } from '@pgpmjs/types'; |
| 4 | + |
| 5 | +import { getConnections } from '../src/connect'; |
| 6 | +import { PgTestClient } from '../src/test-client'; |
| 7 | + |
| 8 | +const SCHEMA = ` |
| 9 | + CREATE TABLE parents ( |
| 10 | + id int PRIMARY KEY |
| 11 | + ); |
| 12 | + CREATE TABLE children ( |
| 13 | + id int PRIMARY KEY, |
| 14 | + parent_id int NOT NULL |
| 15 | + REFERENCES parents(id) DEFERRABLE INITIALLY DEFERRED |
| 16 | + ); |
| 17 | + CREATE TABLE slots ( |
| 18 | + id int PRIMARY KEY, |
| 19 | + position int NOT NULL, |
| 20 | + CONSTRAINT slots_position_key UNIQUE (position) DEFERRABLE INITIALLY DEFERRED |
| 21 | + ); |
| 22 | + INSERT INTO slots (id, position) VALUES (1, 1), (2, 2); |
| 23 | +`; |
| 24 | + |
| 25 | +const connect = async (deferredConstraints: DeferredConstraintsMode) => { |
| 26 | + const conn = await getConnections({ db: { deferredConstraints } }, []); |
| 27 | + await conn.pg.query(SCHEMA); |
| 28 | + return conn; |
| 29 | +}; |
| 30 | + |
| 31 | +const insertDanglingChild = (client: PgTestClient) => |
| 32 | + client.query(`INSERT INTO children (id, parent_id) VALUES (1, 999)`); |
| 33 | + |
| 34 | +const insertChildThenParent = async (client: PgTestClient) => { |
| 35 | + await client.query(`INSERT INTO children (id, parent_id) VALUES (1, 1)`); |
| 36 | + await client.query(`INSERT INTO parents (id) VALUES (1)`); |
| 37 | +}; |
| 38 | + |
| 39 | +const swapPositions = async (client: PgTestClient) => { |
| 40 | + await client.query(`UPDATE slots SET position = 2 WHERE id = 1`); |
| 41 | + await client.query(`UPDATE slots SET position = 1 WHERE id = 2`); |
| 42 | +}; |
| 43 | + |
| 44 | +let teardown: () => Promise<void>; |
| 45 | +let checkPg: PgTestClient; |
| 46 | +let immediatePg: PgTestClient; |
| 47 | +let offPg: PgTestClient; |
| 48 | + |
| 49 | +beforeAll(async () => { |
| 50 | + ({ pg: checkPg } = await connect('check')); |
| 51 | + ({ pg: immediatePg } = await connect('immediate')); |
| 52 | + ({ pg: offPg, teardown } = await connect('off')); |
| 53 | +}); |
| 54 | + |
| 55 | +afterAll(async () => { |
| 56 | + await teardown(); |
| 57 | +}); |
| 58 | + |
| 59 | +describe("deferredConstraints: 'check'", () => { |
| 60 | + it('keeps deferral inside the test: child-before-parent and unique swap pass', async () => { |
| 61 | + await checkPg.beforeEach(); |
| 62 | + await insertChildThenParent(checkPg); |
| 63 | + await swapPositions(checkPg); |
| 64 | + await expect(checkPg.afterEach()).resolves.toBeUndefined(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('fails afterEach for a dangling deferred FK, then rolls back so the client is reusable', async () => { |
| 68 | + await checkPg.beforeEach(); |
| 69 | + await insertDanglingChild(checkPg); |
| 70 | + |
| 71 | + await expect(checkPg.afterEach()).rejects.toThrow( |
| 72 | + /\[pgsql-test\] deferred constraint violated at end of test[\s\S]*children_parent_id_fkey/ |
| 73 | + ); |
| 74 | + |
| 75 | + const res = await checkPg.query(`SELECT count(*)::int AS n FROM children`); |
| 76 | + expect(res.rows[0].n).toBe(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('does not mask an error the test body already raised (aborted transaction)', async () => { |
| 80 | + await checkPg.beforeEach(); |
| 81 | + await insertDanglingChild(checkPg); |
| 82 | + await expect(checkPg.query(`SELECT 1/0`)).rejects.toThrow(/division by zero/); |
| 83 | + |
| 84 | + await expect(checkPg.afterEach()).resolves.toBeUndefined(); |
| 85 | + |
| 86 | + const res = await checkPg.query(`SELECT 1 AS ok`); |
| 87 | + expect(res.rows[0].ok).toBe(1); |
| 88 | + }); |
| 89 | + |
| 90 | + it('checkConstraints() lets a test assert enforcement and leaves afterEach clean', async () => { |
| 91 | + await checkPg.beforeEach(); |
| 92 | + await insertDanglingChild(checkPg); |
| 93 | + |
| 94 | + await expect(checkPg.checkConstraints()).rejects.toThrow(/children_parent_id_fkey/); |
| 95 | + |
| 96 | + await expect(checkPg.afterEach()).resolves.toBeUndefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('checkConstraints() is a no-op when nothing is pending', async () => { |
| 100 | + await checkPg.beforeEach(); |
| 101 | + await insertChildThenParent(checkPg); |
| 102 | + await expect(checkPg.checkConstraints()).resolves.toBeUndefined(); |
| 103 | + await expect(checkPg.afterEach()).resolves.toBeUndefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('survives publish(): violations after a publish are still caught', async () => { |
| 107 | + await checkPg.beforeEach(); |
| 108 | + await checkPg.query(`INSERT INTO parents (id) VALUES (42)`); |
| 109 | + await checkPg.publish(); |
| 110 | + await insertDanglingChild(checkPg); |
| 111 | + await expect(checkPg.afterEach()).rejects.toThrow(/children_parent_id_fkey/); |
| 112 | + await checkPg.query(`DELETE FROM parents WHERE id = 42`); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe("deferredConstraints: 'immediate'", () => { |
| 117 | + afterEach(async () => { |
| 118 | + await immediatePg.afterEach(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('fails on the offending statement', async () => { |
| 122 | + await immediatePg.beforeEach(); |
| 123 | + await expect(insertDanglingChild(immediatePg)).rejects.toThrow(/children_parent_id_fkey/); |
| 124 | + }); |
| 125 | + |
| 126 | + it('disables deferral: child-before-parent fails', async () => { |
| 127 | + await immediatePg.beforeEach(); |
| 128 | + await expect(insertChildThenParent(immediatePg)).rejects.toThrow(/children_parent_id_fkey/); |
| 129 | + }); |
| 130 | + |
| 131 | + it('disables deferral: unique swap fails', async () => { |
| 132 | + await immediatePg.beforeEach(); |
| 133 | + await expect(swapPositions(immediatePg)).rejects.toThrow(/slots_position_key/); |
| 134 | + }); |
| 135 | + |
| 136 | + it('is re-applied after publish()', async () => { |
| 137 | + await immediatePg.beforeEach(); |
| 138 | + await immediatePg.publish(); |
| 139 | + await expect(insertDanglingChild(immediatePg)).rejects.toThrow(/children_parent_id_fkey/); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe("deferredConstraints: 'off' (default)", () => { |
| 144 | + it('a dangling deferred FK passes silently and is rolled back', async () => { |
| 145 | + await offPg.beforeEach(); |
| 146 | + await insertDanglingChild(offPg); |
| 147 | + await expect(offPg.afterEach()).resolves.toBeUndefined(); |
| 148 | + |
| 149 | + const res = await offPg.query(`SELECT count(*)::int AS n FROM children`); |
| 150 | + expect(res.rows[0].n).toBe(0); |
| 151 | + }); |
| 152 | + |
| 153 | + it('is the default when no mode is given', async () => { |
| 154 | + const { pg } = await getConnections({}, []); |
| 155 | + await pg.query(SCHEMA); |
| 156 | + await pg.beforeEach(); |
| 157 | + await insertDanglingChild(pg); |
| 158 | + await expect(pg.afterEach()).resolves.toBeUndefined(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments