@@ -6,48 +6,6 @@ const Fastify = require('fastify')
6
6
const fastifyPostgres = require ( '../index' )
7
7
const { connectionString } = require ( './helpers' )
8
8
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
-
51
9
const extractUserCount = response => parseInt ( JSON . parse ( response . payload ) . rows [ 0 ] . userCount )
52
10
53
11
test ( 'fastify postgress useTransaction route option - ' , t => {
@@ -63,12 +21,12 @@ test('fastify postgress useTransaction route option - ', t => {
63
21
await fastify . pg . query ( 'TRUNCATE users' )
64
22
65
23
await fastify . get ( '/count-users' , async ( req , reply ) => {
66
- const result = await req . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'' )
24
+ const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'' )
67
25
68
26
reply . send ( result )
69
27
} )
70
28
71
- await fastify . get ( '/pass' , { useTransaction : true } , async ( req , reply ) => {
29
+ await fastify . get ( '/pass' , { pg : { transact : true } } , async ( req , reply ) => {
72
30
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
73
31
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
74
32
reply . send ( 'complete' )
@@ -97,12 +55,12 @@ test('fastify postgress useTransaction route option - ', t => {
97
55
await fastify . pg . query ( 'TRUNCATE users' )
98
56
99
57
await fastify . get ( '/count-users' , async ( req , reply ) => {
100
- const result = await req . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'' )
58
+ const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'' )
101
59
102
60
reply . send ( result )
103
61
} )
104
62
105
- await fastify . get ( '/fail' , { useTransaction : true } , async ( req , reply ) => {
63
+ await fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
106
64
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
107
65
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
108
66
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 => {
124
82
125
83
t . end ( )
126
84
} )
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
- } )
200
85
201
86
t . end ( )
202
87
} )
0 commit comments