@@ -6,48 +6,6 @@ const Fastify = require('fastify')
66const fastifyPostgres = require ( '../index' )
77const { connectionString } = require ( './helpers' )
88
9- // // A failed set of queries with transactions on, on test, NONE of these entries should be visible in the DB
10- // fastify.get('/fail', { useTransaction: true }, async (req, reply) => {
11- // console.log('in fail registration')
12-
13- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in-q1'])
14- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in-q2'])
15- // await req.pg.query('INSERT INTO nope(username) VALUES($1) RETURNING id', ['fail-opt-in-q3'])
16-
17- // reply.send('Fail example')
18- // })
19-
20- // // A passing set of queries with transactions on, on test, ALL of these entries should be visible in the DB
21- // fastify.get('/pass', { useTransaction: true }, async (req, reply) => {
22- // console.log('in pass registration')
23-
24- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in-q1'])
25- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in-q2'])
26-
27- // reply.send('Pass example')
28- // })
29-
30- // // A failed set of queries with transactions off, on test, THE FIRST TWO of these entries should be visible in the DB
31- // fastify.get('/fail-opt-out', { useTransaction: false }, async (req, reply) => {
32- // console.log('in fail registration')
33-
34- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-out-q1'])
35- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-out-q2'])
36- // await req.pg.query('INSERT INTO nope(username) VALUES($1) RETURNING id', ['fail-opt-out-q3'])
37-
38- // reply.send('Fail example')
39- // })
40-
41- // // A passing set of queries with transactions off, on test, ALL of these entries should be visible in the DB
42- // fastify.get('/pass-opt-out', { useTransaction: false }, async (req, reply) => {
43- // console.log('in pass registration')
44-
45- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-out-q1'])
46- // await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-out-q2'])
47-
48- // reply.send('Pass example')
49- // })
50-
519const extractUserCount = response => parseInt ( JSON . parse ( response . payload ) . rows [ 0 ] . userCount )
5210
5311test ( 'fastify postgress useTransaction route option - ' , t => {
@@ -68,7 +26,7 @@ test('fastify postgress useTransaction route option - ', t => {
6826 reply . send ( result )
6927 } )
7028
71- await fastify . get ( '/pass' , { useTransaction : true } , async ( req , reply ) => {
29+ await fastify . get ( '/pass' , { pg : { transact : true } } , async ( req , reply ) => {
7230 await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
7331 await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
7432 reply . send ( 'complete' )
@@ -102,7 +60,7 @@ test('fastify postgress useTransaction route option - ', t => {
10260 reply . send ( result )
10361 } )
10462
105- await fastify . get ( '/fail' , { useTransaction : true } , async ( req , reply ) => {
63+ await fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
10664 await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
10765 await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
10866 await req . pg . query ( 'INSERT INTO nope(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
@@ -124,79 +82,6 @@ test('fastify postgress useTransaction route option - ', t => {
12482
12583 t . end ( )
12684 } )
127- test ( 'set to false - ' , t => {
128- test ( 'passing queries provided' , async t => {
129- const fastify = Fastify ( )
130- t . teardown ( ( ) => fastify . close ( ) )
131-
132- await fastify . register ( fastifyPostgres , {
133- connectionString
134- } )
135-
136- await fastify . pg . query ( 'TRUNCATE users' )
137-
138- await fastify . get ( '/count-users' , async ( req , reply ) => {
139- const result = await req . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-out\'' )
140-
141- reply . send ( result )
142- } )
143-
144- await fastify . get ( '/pass-opt-out' , { useTransaction : false } , async ( req , reply ) => {
145- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-out' ] )
146- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-out' ] )
147- reply . send ( 'complete' )
148- } )
149-
150- await fastify . inject ( {
151- method : 'GET' ,
152- url : '/pass-opt-out'
153- } )
154-
155- const response = await fastify . inject ( {
156- method : 'GET' ,
157- url : '/count-users'
158- } )
159-
160- t . is ( extractUserCount ( response ) , 2 )
161- } )
162- test ( 'failing queries provided' , async t => {
163- const fastify = Fastify ( )
164- t . teardown ( ( ) => fastify . close ( ) )
165-
166- await fastify . register ( fastifyPostgres , {
167- connectionString
168- } )
169-
170- await fastify . pg . query ( 'TRUNCATE users' )
171-
172- await fastify . get ( '/count-users' , async ( req , reply ) => {
173- const result = await req . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-out\'' )
174-
175- reply . send ( result )
176- } )
177-
178- await fastify . get ( '/fail-opt-out' , { useTransaction : false } , async ( req , reply ) => {
179- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-out' ] )
180- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-out' ] )
181- await req . pg . query ( 'INSERT INTO nope(username) VALUES($1) RETURNING id' , [ 'fail-opt-out' ] )
182- reply . send ( 'complete' )
183- } )
184-
185- await fastify . inject ( {
186- method : 'GET' ,
187- url : '/fail-opt-out'
188- } )
189-
190- const response = await fastify . inject ( {
191- method : 'GET' ,
192- url : '/count-users'
193- } )
194-
195- t . is ( extractUserCount ( response ) , 2 )
196- } )
197-
198- t . end ( )
199- } )
20085
20186 t . end ( )
20287} )
0 commit comments