@@ -267,22 +267,15 @@ test('fastify.pg.test use transact util with promise', t => {
267267 fastify . ready ( err => {
268268 t . error ( err )
269269 fastify . pg . test
270- . query ( 'CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL)' )
271- . then ( ( ) => {
270+ . transact ( client => { return client . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'with-promise' ] ) } )
271+ . then ( result => {
272+ t . equals ( result . rows . length , 1 )
272273 fastify . pg . test
273- . transact ( client => { return client . query ( 'INSERT INTO users( username) VALUES($1) RETURNING id' , [ 'brianc' ] ) } )
274+ . query ( `SELECT * FROM users WHERE username = 'with-promise'` )
274275 . then ( result => {
275- t . ok ( result . rows [ 0 ] . id === 1 )
276- fastify . pg . test
277- . query ( 'SELECT * FROM users' )
278- . then ( result => {
279- t . ok ( result . rows [ 0 ] . username === 'brianc' )
280- } ) . catch ( err => {
281- t . fail ( err )
282- fastify . close ( )
283- } )
284- } )
285- . catch ( err => {
276+ t . ok ( result . rows [ 0 ] . username === 'with-promise' )
277+ fastify . close ( )
278+ } ) . catch ( err => {
286279 t . fail ( err )
287280 fastify . close ( )
288281 } )
@@ -295,7 +288,7 @@ test('fastify.pg.test use transact util with promise', t => {
295288} )
296289
297290test ( 'fastify.pg.test use transact util with callback' , t => {
298- t . plan ( 2 )
291+ t . plan ( 4 )
299292
300293 const fastify = Fastify ( )
301294
@@ -306,22 +299,55 @@ test('fastify.pg.test use transact util with callback', t => {
306299
307300 fastify . ready ( err => {
308301 t . error ( err )
302+
309303 fastify . pg . test
310- . query ( 'CREATE TABLE users2(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL)' )
311- . then ( ( ) => {
304+ . transact ( client => { return client . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'with-callback' ] ) } , function ( err , res ) {
305+ t . error ( err )
306+ t . equals ( res . rows . length , 1 )
307+
312308 fastify . pg . test
313- . transact ( client => { return client . query ( 'INSERT INTO users2( username) VALUES($1) RETURNING id' , [ 'brianc' ] ) } , function ( err , res ) {
314- if ( err ) {
315- t . fail ( err )
316- fastify . close ( )
317- } else {
318- t . ok ( res . rows [ 0 ] . id === 1 )
319- }
309+ . query ( `SELECT * FROM users WHERE username = 'with-callback'` )
310+ . then ( result => {
311+ t . ok ( result . rows [ 0 ] . username === 'with-callback' )
312+ fastify . close ( )
313+ } ) . catch ( err => {
314+ t . fail ( err )
315+ fastify . close ( )
320316 } )
321317 } )
322- . catch ( err => {
323- t . fail ( err )
324- fastify . close ( )
318+ } )
319+ } )
320+
321+ test ( 'fastify.pg.test use transact util with commit callback' , t => {
322+ t . plan ( 4 )
323+
324+ const fastify = Fastify ( )
325+
326+ fastify . register ( fastifyPostgres , {
327+ name : 'test' ,
328+ connectionString : 'postgres://postgres@localhost/postgres'
329+ } )
330+
331+ fastify . ready ( err => {
332+ t . error ( err )
333+
334+ fastify . pg . test . transact ( ( client , commit ) => {
335+ client . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'commit-callback' ] , ( err , id ) => {
336+ commit ( err , id )
325337 } )
338+ } , function ( err , res ) {
339+ t . error ( err )
340+ t . equals ( res . rows . length , 1 )
341+
342+ fastify . pg . test
343+ . query ( `SELECT * FROM users WHERE username = 'commit-callback'` )
344+ . then ( result => {
345+ t . ok ( result . rows [ 0 ] . username === 'commit-callback' )
346+ fastify . close ( )
347+ } ) . catch ( err => {
348+ t . fail ( err )
349+ fastify . close ( )
350+ } )
351+ } )
326352 } )
327353} )
0 commit comments