11'use strict'
22
3- const t = require ( 'tap' )
4- const test = t . test
3+ const { test } = require ( 'tap' )
54const Fastify = require ( 'fastify' )
65const fastifyPostgres = require ( '../index' )
76const { connectionString } = require ( './helpers' )
87
98const extractUserCount = response => parseInt ( JSON . parse ( response . payload ) . rows [ 0 ] . userCount )
109
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 => {
1312 const fastify = Fastify ( )
1413 t . teardown ( ( ) => fastify . close ( ) )
1514
@@ -40,7 +39,8 @@ test('fastify postgress useTransaction route option', t => {
4039
4140 t . equal ( extractUserCount ( response ) , 2 )
4241 } )
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 => {
4444 const fastify = Fastify ( )
4545 t . teardown ( ( ) => fastify . close ( ) )
4646
@@ -73,7 +73,8 @@ test('fastify postgress useTransaction route option', t => {
7373
7474 t . equal ( extractUserCount ( response ) , 2 )
7575 } )
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 => {
7778 const fastify = Fastify ( )
7879 t . teardown ( ( ) => fastify . close ( ) )
7980
@@ -92,7 +93,43 @@ test('fastify postgress useTransaction route option', t => {
9293 fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
9394 await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
9495 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' ] )
96133 reply . send ( 'complete' )
97134 } )
98135
@@ -109,8 +146,8 @@ test('fastify postgress useTransaction route option', t => {
109146 t . end ( )
110147} )
111148
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 => {
114151 t . plan ( 1 )
115152
116153 const fastify = Fastify ( )
@@ -126,7 +163,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
126163
127164 fastify . inject ( { url : '/' } )
128165 } )
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 => {
130168 t . plan ( 1 )
131169
132170 const fastify = Fastify ( )
@@ -143,7 +181,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
143181
144182 fastify . inject ( { url : '/' } )
145183 } )
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 => {
147186 t . plan ( 1 )
148187
149188 const fastify = Fastify ( )
@@ -160,7 +199,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
160199
161200 fastify . inject ( { url : '/' } )
162201 } )
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 => {
164204 t . plan ( 1 )
165205
166206 const fastify = Fastify ( )
@@ -176,7 +216,8 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
176216
177217 fastify . inject ( { url : '/' } )
178218 } )
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 => {
180221 t . plan ( 1 )
181222
182223 const fastify = Fastify ( )
@@ -193,11 +234,12 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
193234
194235 fastify . inject ( { url : '/' } )
195236 } )
237+
196238 t . end ( )
197239} )
198240
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 => {
201243 t . plan ( 2 )
202244
203245 const fastify = Fastify ( )
@@ -222,7 +264,7 @@ test('incorrect combinations of registrationOptions.name and routeOptions.pg.tra
222264 } )
223265 } )
224266
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 => {
226268 t . plan ( 2 )
227269
228270 const fastify = Fastify ( )
@@ -249,7 +291,7 @@ test('incorrect combinations of registrationOptions.name and routeOptions.pg.tra
249291 } )
250292 } )
251293
252- t . test ( 'pg client has already registered' , t => {
294+ t . test ( 'Should throw an error when pg client has already been registered' , t => {
253295 t . plan ( 2 )
254296
255297 const fastify = Fastify ( )
@@ -272,5 +314,6 @@ test('incorrect combinations of registrationOptions.name and routeOptions.pg.tra
272314 } )
273315 } )
274316 } )
317+
275318 t . end ( )
276319} )
0 commit comments