@@ -8,7 +8,7 @@ const { connectionString } = require('./helpers')
8
8
9
9
const extractUserCount = response => parseInt ( JSON . parse ( response . payload ) . rows [ 0 ] . userCount )
10
10
11
- test ( 'fastify postgress useTransaction route option - ' , t => {
11
+ test ( 'fastify postgress useTransaction route option' , t => {
12
12
test ( 'queries that succeed provided' , async t => {
13
13
const fastify = Fastify ( )
14
14
t . teardown ( ( ) => fastify . close ( ) )
@@ -19,13 +19,13 @@ test('fastify postgress useTransaction route option - ', t => {
19
19
20
20
await fastify . pg . query ( 'TRUNCATE users' )
21
21
22
- await fastify . get ( '/count-users' , async ( req , reply ) => {
22
+ fastify . get ( '/count-users' , async ( req , reply ) => {
23
23
const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'' )
24
24
25
25
reply . send ( result )
26
26
} )
27
27
28
- await fastify . get ( '/pass' , { pg : { transact : true } } , async ( req , reply ) => {
28
+ fastify . get ( '/pass' , { pg : { transact : true } } , async ( req , reply ) => {
29
29
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
30
30
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
31
31
reply . send ( 'complete' )
@@ -54,13 +54,13 @@ test('fastify postgress useTransaction route option - ', t => {
54
54
55
55
await fastify . pg . test . query ( 'TRUNCATE users' )
56
56
57
- await fastify . get ( '/count-users' , async ( req , reply ) => {
57
+ fastify . get ( '/count-users' , async ( req , reply ) => {
58
58
const result = await fastify . pg . test . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'' )
59
59
60
60
reply . send ( result )
61
61
} )
62
62
63
- await fastify . get ( '/pass' , { pg : { transact : 'test' } } , async ( req , reply ) => {
63
+ fastify . get ( '/pass' , { pg : { transact : 'test' } } , async ( req , reply ) => {
64
64
await req . pg . test . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
65
65
await req . pg . test . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
66
66
@@ -89,13 +89,13 @@ test('fastify postgress useTransaction route option - ', t => {
89
89
90
90
await fastify . pg . query ( 'TRUNCATE users' )
91
91
92
- await fastify . get ( '/count-users' , async ( req , reply ) => {
92
+ fastify . get ( '/count-users' , async ( req , reply ) => {
93
93
const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'' )
94
94
95
95
reply . send ( result )
96
96
} )
97
97
98
- await fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
98
+ fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
99
99
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
100
100
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
101
101
await req . pg . query ( 'INSERT INTO nope(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
@@ -117,3 +117,105 @@ test('fastify postgress useTransaction route option - ', t => {
117
117
118
118
t . end ( )
119
119
} )
120
+
121
+ test ( 'combinations of registrationOptions.name and routeOptions.pg.transact that should not add hooks' , t => {
122
+ test ( 'transact not set' , t => {
123
+ t . plan ( 1 )
124
+
125
+ const fastify = Fastify ( )
126
+ t . teardown ( ( ) => fastify . close ( ) )
127
+
128
+ fastify . register ( fastifyPostgres , {
129
+ connectionString
130
+ } )
131
+
132
+ fastify . get ( '/' , ( req , reply ) => {
133
+ t . is ( req . pg , null )
134
+ } )
135
+
136
+ fastify . inject ( {
137
+ method : 'GET' ,
138
+ url : '/'
139
+ } )
140
+ } )
141
+ test ( 'name set and transact not set' , t => {
142
+ t . plan ( 1 )
143
+
144
+ const fastify = Fastify ( )
145
+ t . teardown ( ( ) => fastify . close ( ) )
146
+
147
+ fastify . register ( fastifyPostgres , {
148
+ connectionString,
149
+ name : 'test'
150
+ } )
151
+
152
+ fastify . get ( '/' , ( req , reply ) => {
153
+ t . is ( req . pg , null )
154
+ } )
155
+
156
+ fastify . inject ( {
157
+ method : 'GET' ,
158
+ url : '/'
159
+ } )
160
+ } )
161
+ test ( 'name set and transact set to true' , t => {
162
+ t . plan ( 1 )
163
+
164
+ const fastify = Fastify ( )
165
+ t . teardown ( ( ) => fastify . close ( ) )
166
+
167
+ fastify . register ( fastifyPostgres , {
168
+ connectionString,
169
+ name : 'test'
170
+ } )
171
+
172
+ fastify . get ( '/' , { pg : { transact : true } } , ( req , reply ) => {
173
+ t . is ( req . pg , null )
174
+ } )
175
+
176
+ fastify . inject ( {
177
+ method : 'GET' ,
178
+ url : '/'
179
+ } )
180
+ } )
181
+ test ( 'name not set and transact set to string' , t => {
182
+ t . plan ( 1 )
183
+
184
+ const fastify = Fastify ( )
185
+ t . teardown ( ( ) => fastify . close ( ) )
186
+
187
+ fastify . register ( fastifyPostgres , {
188
+ connectionString
189
+ } )
190
+
191
+ fastify . get ( '/' , { pg : { transact : 'test' } } , ( req , reply ) => {
192
+ t . is ( req . pg , null )
193
+ } )
194
+
195
+ fastify . inject ( {
196
+ method : 'GET' ,
197
+ url : '/'
198
+ } )
199
+ } )
200
+ test ( 'name and transact set to different strings' , t => {
201
+ t . plan ( 1 )
202
+
203
+ const fastify = Fastify ( )
204
+ t . teardown ( ( ) => fastify . close ( ) )
205
+
206
+ fastify . register ( fastifyPostgres , {
207
+ connectionString,
208
+ name : 'test'
209
+ } )
210
+
211
+ fastify . get ( '/' , { pg : { transact : 'different' } } , ( req , reply ) => {
212
+ t . is ( req . pg , null )
213
+ } )
214
+
215
+ fastify . inject ( {
216
+ method : 'GET' ,
217
+ url : '/'
218
+ } )
219
+ } )
220
+ t . end ( )
221
+ } )
0 commit comments