@@ -17,6 +17,7 @@ import { PgTransactionManager } from '../pg-transaction-manager.js';
1717import { testTable2 } from './test.table.js' ;
1818import { InvalidOperationException } from '@ddd-framework/core' ;
1919import { expectEntries } from './assertions.js' ;
20+ import { Pool } from 'pg' ;
2021
2122describe ( 'PgTransactionManager' , ( ) => {
2223 let module : TestingModule ;
@@ -25,7 +26,7 @@ describe('PgTransactionManager', () => {
2526
2627 beforeAll ( async ( ) => {
2728 module = await Test . createTestingModule ( {
28- imports : [ DrizzleOrmModule . forRoot ( { client : globalThis . __pgClient } ) ]
29+ imports : [ DrizzleOrmModule . forRoot ( { pg : globalThis . __pgClient } ) ]
2930 } ) . compile ( ) ;
3031
3132 await module . init ( ) ;
@@ -170,3 +171,83 @@ describe('PgTransactionManager', () => {
170171 await expectEntries ( db , testTable2 , [ { id : firstId } , { id : secondId } ] ) ;
171172 } ) ;
172173} ) ;
174+
175+ describe ( 'PgTransactionManager with a pool' , ( ) => {
176+ let module : TestingModule ;
177+ let db : NodePgDatabase ;
178+ let manager : PgTransactionManager ;
179+
180+ beforeAll ( async ( ) => {
181+ module = await Test . createTestingModule ( {
182+ imports : [
183+ DrizzleOrmModule . forRoot ( { pg : new Pool ( globalThis . __dbCredentials ) } )
184+ ]
185+ } ) . compile ( ) ;
186+
187+ await module . init ( ) ;
188+
189+ db = module . get ( PgDatabase ) ;
190+
191+ // This would be returned by a dependency injection container.
192+ manager = new PgTransactionManager ( db ) ;
193+ } ) ;
194+
195+ beforeEach ( async ( ) => {
196+ await db . delete ( testTable2 ) ;
197+ } ) ;
198+
199+ afterEach ( async ( ) => {
200+ vi . clearAllMocks ( ) ;
201+ } ) ;
202+
203+ afterAll ( async ( ) => {
204+ await module ?. close ( ) ;
205+ } ) ;
206+
207+ test ( 'starts transaction and implicitly commits' , async ( ) => {
208+ const firstId = Uuid . generate ( ) ;
209+ const secondId = Uuid . generate ( ) ;
210+
211+ await manager . startTransaction ( async ( transaction ) => {
212+ await transaction . context
213+ . insert ( testTable2 )
214+ . values ( [ { id : firstId } , { id : secondId } ] ) ;
215+ } ) ;
216+
217+ await expectEntries ( db , testTable2 , [ { id : firstId } , { id : secondId } ] ) ;
218+ } ) ;
219+
220+ test ( 'returns result from transaction' , async ( ) => {
221+ const firstId = Uuid . generate ( ) ;
222+ const secondId = Uuid . generate ( ) ;
223+
224+ const result = await manager . startTransaction ( async ( transaction ) => {
225+ await transaction . context
226+ . insert ( testTable2 )
227+ . values ( [ { id : firstId } , { id : secondId } ] ) ;
228+
229+ return true ;
230+ } ) ;
231+
232+ expect ( result ) . toBe ( true ) ;
233+
234+ await expectEntries ( db , testTable2 , [ { id : firstId } , { id : secondId } ] ) ;
235+ } ) ;
236+
237+ test ( 'transactions are automatically rolled back when an error occurs' , async ( ) => {
238+ const firstId = Uuid . generate ( ) ;
239+ const secondId = Uuid . generate ( ) ;
240+
241+ await expect ( ( ) =>
242+ manager . startTransaction ( async ( transaction ) => {
243+ await transaction . context
244+ . insert ( testTable2 )
245+ . values ( [ { id : firstId } , { id : secondId } ] ) ;
246+
247+ throw new InvalidOperationException ( 'An error occurred' ) ;
248+ } )
249+ ) . rejects . toThrow ( 'An error occurred' ) ;
250+
251+ await expectEntries ( db , testTable2 , [ ] ) ;
252+ } ) ;
253+ } ) ;
0 commit comments