@@ -253,3 +253,97 @@ test('fastify.pg.test should throw with duplicate connection names', t => {
253253 t . is ( err . message , 'Connection name has already been registered: test' )
254254 } )
255255} )
256+
257+ test ( 'fastify.pg.test use transact util with promise' , t => {
258+ t . plan ( 3 )
259+
260+ const fastify = Fastify ( )
261+ t . tearDown ( fastify . close . bind ( fastify ) )
262+
263+ fastify . register ( fastifyPostgres , {
264+ name : 'test' ,
265+ connectionString : 'postgres://postgres@localhost/postgres'
266+ } )
267+
268+ fastify . ready ( err => {
269+ t . error ( err )
270+ fastify . pg . test
271+ . transact ( client => client . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'with-promise' ] ) )
272+ . then ( result => {
273+ t . equals ( result . rows . length , 1 )
274+ fastify . pg . test
275+ . query ( `SELECT * FROM users WHERE username = 'with-promise'` )
276+ . then ( result => {
277+ t . ok ( result . rows [ 0 ] . username === 'with-promise' )
278+ } ) . catch ( err => {
279+ t . fail ( err )
280+ } )
281+ } )
282+ . catch ( err => {
283+ t . fail ( err )
284+ } )
285+ } )
286+ } )
287+
288+ test ( 'fastify.pg.test use transact util with callback' , t => {
289+ t . plan ( 4 )
290+
291+ const fastify = Fastify ( )
292+ t . tearDown ( fastify . close . bind ( fastify ) )
293+
294+ fastify . register ( fastifyPostgres , {
295+ name : 'test' ,
296+ connectionString : 'postgres://postgres@localhost/postgres'
297+ } )
298+
299+ fastify . ready ( err => {
300+ t . error ( err )
301+
302+ fastify . pg . test
303+ . transact ( client => client . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'with-callback' ] ) , function ( err , res ) {
304+ t . error ( err )
305+ t . equals ( res . rows . length , 1 )
306+
307+ fastify . pg . test
308+ . query ( `SELECT * FROM users WHERE username = 'with-callback'` )
309+ . then ( result => {
310+ t . ok ( result . rows [ 0 ] . username === 'with-callback' )
311+ } ) . catch ( err => {
312+ t . fail ( err )
313+ } )
314+ } )
315+ } )
316+ } )
317+
318+ test ( 'fastify.pg.test use transact util with commit callback' , t => {
319+ t . plan ( 4 )
320+
321+ const fastify = Fastify ( )
322+ t . tearDown ( fastify . close . bind ( fastify ) )
323+
324+ fastify . register ( fastifyPostgres , {
325+ name : 'test' ,
326+ connectionString : 'postgres://postgres@localhost/postgres'
327+ } )
328+
329+ fastify . ready ( err => {
330+ t . error ( err )
331+
332+ fastify . pg . test . transact ( ( client , commit ) => {
333+ client . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'commit-callback' ] , ( err , id ) => {
334+ commit ( err , id )
335+ } )
336+ } , function ( err , res ) {
337+ t . error ( err )
338+ t . equals ( res . rows . length , 1 )
339+
340+ fastify . pg . test
341+ . query ( `SELECT * FROM users WHERE username = 'commit-callback'` )
342+ . then ( result => {
343+ t . ok ( result . rows [ 0 ] . username === 'commit-callback' )
344+ } ) . catch ( err => {
345+ t . fail ( err )
346+ } )
347+ } )
348+ } )
349+ } )
0 commit comments