1
1
'use strict'
2
2
3
- const t = require ( 'tap' )
4
- const test = t . test
3
+ const { test } = require ( 'tap' )
5
4
const Fastify = require ( 'fastify' )
6
5
const fastifyPostgres = require ( '../index' )
7
6
const { connectionString } = require ( './helpers' )
8
7
9
8
const extractUserCount = response => parseInt ( JSON . parse ( response . payload ) . rows [ 0 ] . userCount )
10
9
11
- test ( 'fastify postgress useTransaction route option' , t => {
12
- test ( 'queries that succeed provided' , async t => {
10
+ test ( 'When we use the fastify-postgres transaction route option' , t => {
11
+ t . test ( 'Should be able to execute queries provided to the request pg decorator ' , async t => {
13
12
const fastify = Fastify ( )
14
13
t . teardown ( ( ) => fastify . close ( ) )
15
14
@@ -40,7 +39,8 @@ test('fastify postgress useTransaction route option', t => {
40
39
41
40
t . equal ( extractUserCount ( response ) , 2 )
42
41
} )
43
- test ( 'queries that succeed provided to a namespace' , async t => {
42
+
43
+ t . test ( 'Should be able to execute queries provided to a namespaced request pg decorator' , async t => {
44
44
const fastify = Fastify ( )
45
45
t . teardown ( ( ) => fastify . close ( ) )
46
46
@@ -73,7 +73,8 @@ test('fastify postgress useTransaction route option', t => {
73
73
74
74
t . equal ( extractUserCount ( response ) , 2 )
75
75
} )
76
- test ( 'queries that fail provided' , async t => {
76
+
77
+ t . test ( 'Should trigger a rollback when failing to execute a query provided to the request pg decorator' , async t => {
77
78
const fastify = Fastify ( )
78
79
t . teardown ( ( ) => fastify . close ( ) )
79
80
@@ -92,7 +93,43 @@ test('fastify postgress useTransaction route option', t => {
92
93
fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
93
94
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
94
95
await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
95
- await req . pg . query ( 'INSERT INTO nope(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
96
+ // This one should fail (unknown_table does not exist) and trigger a rollback
97
+ await req . pg . query ( 'INSERT INTO unknown_table(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
98
+ reply . send ( 'complete' )
99
+ } )
100
+
101
+ await fastify . inject ( { url : '/fail' } )
102
+
103
+ const response = await fastify . inject ( {
104
+ method : 'GET' ,
105
+ url : '/count-users'
106
+ } )
107
+
108
+ t . equal ( extractUserCount ( response ) , 0 )
109
+ } )
110
+
111
+ t . test ( 'Should trigger a rollback when failing to execute a query provided to a namespaced request pg decorator' , async t => {
112
+ const fastify = Fastify ( )
113
+ t . teardown ( ( ) => fastify . close ( ) )
114
+
115
+ await fastify . register ( fastifyPostgres , {
116
+ connectionString,
117
+ name : 'test'
118
+ } )
119
+
120
+ await fastify . pg . test . query ( 'TRUNCATE users' )
121
+
122
+ fastify . get ( '/count-users' , async ( req , reply ) => {
123
+ const result = await fastify . pg . test . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'' )
124
+
125
+ reply . send ( result )
126
+ } )
127
+
128
+ fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
129
+ await req . pg . test . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
130
+ await req . pg . test . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
131
+ // This one should fail (unknown_table does not exist) and trigger a rollback
132
+ await req . pg . test . query ( 'INSERT INTO unknown_table(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
96
133
reply . send ( 'complete' )
97
134
} )
98
135
@@ -109,8 +146,8 @@ test('fastify postgress useTransaction route option', t => {
109
146
t . end ( )
110
147
} )
111
148
112
- test ( 'combinations of registrationOptions .name and routeOptions. pg.transact that should not add hooks ' , t => {
113
- test ( 'transact not set' , t => {
149
+ test ( 'Should not add hooks with combinations of registration `options .name` and route options ` pg.transact` ' , t => {
150
+ t . test ( 'Should not add hooks when ` transact` is not set' , t => {
114
151
t . plan ( 1 )
115
152
116
153
const fastify = Fastify ( )
@@ -126,7 +163,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
126
163
127
164
fastify . inject ( { url : '/' } )
128
165
} )
129
- test ( 'name set and transact not set' , t => {
166
+
167
+ t . test ( 'Should not add hooks when `name` is set and `transact` is not set' , t => {
130
168
t . plan ( 1 )
131
169
132
170
const fastify = Fastify ( )
@@ -143,7 +181,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
143
181
144
182
fastify . inject ( { url : '/' } )
145
183
} )
146
- test ( 'name set and transact set to true' , t => {
184
+
185
+ t . test ( 'Should not add hooks when `name` is set and `transact` is set to `true`' , t => {
147
186
t . plan ( 1 )
148
187
149
188
const fastify = Fastify ( )
@@ -160,7 +199,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
160
199
161
200
fastify . inject ( { url : '/' } )
162
201
} )
163
- test ( 'name not set and transact set to string' , t => {
202
+
203
+ t . test ( 'Should not add hooks when `name` is not set and `transact` is set and is a string' , t => {
164
204
t . plan ( 1 )
165
205
166
206
const fastify = Fastify ( )
@@ -176,7 +216,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
176
216
177
217
fastify . inject ( { url : '/' } )
178
218
} )
179
- test ( 'name and transact set to different strings' , t => {
219
+
220
+ t . test ( 'Should not add hooks when `name` and `transact` are set to different strings' , t => {
180
221
t . plan ( 1 )
181
222
182
223
const fastify = Fastify ( )
@@ -193,11 +234,12 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
193
234
194
235
fastify . inject ( { url : '/' } )
195
236
} )
237
+
196
238
t . end ( )
197
239
} )
198
240
199
- test ( 'incorrect combinations of registrationOptions .name and routeOptions. pg.transact should throw errors ' , t => {
200
- t . test ( 'name set as reserved keyword' , t => {
241
+ test ( 'Should throw errors with incorrect combinations of registration `options .name` and route options ` pg.transact` ' , t => {
242
+ t . test ( 'Should throw an error when ` name` is set as reserved keyword' , t => {
201
243
t . plan ( 2 )
202
244
203
245
const fastify = Fastify ( )
@@ -222,7 +264,7 @@ test('incorrect combinations of registrationOptions.name and routeOptions.pg.tra
222
264
} )
223
265
} )
224
266
225
- t . test ( 'named pg client has already registered' , t => {
267
+ t . test ( 'Should throw an error when pg client has already been registered with the same name ' , t => {
226
268
t . plan ( 2 )
227
269
228
270
const fastify = Fastify ( )
@@ -249,7 +291,7 @@ test('incorrect combinations of registrationOptions.name and routeOptions.pg.tra
249
291
} )
250
292
} )
251
293
252
- t . test ( 'pg client has already registered' , t => {
294
+ t . test ( 'Should throw an error when pg client has already been registered' , t => {
253
295
t . plan ( 2 )
254
296
255
297
const fastify = Fastify ( )
@@ -272,5 +314,6 @@ test('incorrect combinations of registrationOptions.name and routeOptions.pg.tra
272
314
} )
273
315
} )
274
316
} )
317
+
275
318
t . end ( )
276
319
} )
0 commit comments