@@ -72,3 +72,107 @@ test('fastify request should have the correct decoration at the custom namespace
7272    t . ok ( fastify . hasRequestDecorator ( 'pg' ) ) 
7373  } ) 
7474} ) 
75+ 
76+ test ( 'when useTransaction is set to true - ' ,  t  =>  { 
77+   test ( 'and the transaction is successful - ' ,  t  =>  { 
78+     test ( 'fastify request should start a transaction' ,  t  =>  { 
79+       t . plan ( 2 ) 
80+ 
81+       const  fastify  =  Fastify ( ) 
82+       t . teardown ( ( )  =>  fastify . close ( ) ) 
83+ 
84+       fastify . register ( fastifyPostgres ,  { 
85+         connectionString : process . env . DATABASE_TEST_URL  ||  'postgres://postgres:postgres@localhost/postgres' 
86+       } ) 
87+ 
88+       fastify . route ( { 
89+         method : 'GET' , 
90+         url : '/users' , 
91+         options : {  useTransaction : true  } , 
92+         handler : ( req ,  reply )  =>  { 
93+           reply . send ( 'response!' ) 
94+           t . ok ( req . pg ) 
95+ 
96+           const  result  =  req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' ,  [ 
97+             'root-with-callback' 
98+           ] ) 
99+ 
100+           const  userId  =  result . rows [ 0 ] . id 
101+ 
102+           req . pg 
103+             . query ( 'SELECT * FROM users WHERE id = $1' ,  [ userId ] ) 
104+             . then ( ( result )  =>  { 
105+               t . is ( result . rows [ 0 ] . username ,  'root-with-callback' ) 
106+             } ) 
107+             . catch ( ( err )  =>  { 
108+               t . fail ( err ) 
109+             } ) 
110+         } 
111+       } ) 
112+ 
113+       fastify . inject ( { 
114+         method : 'GET' , 
115+         url : '/users' 
116+       } ,  ( err ,  res )  =>  { 
117+         t . error ( err ) 
118+       } ) 
119+     } ) 
120+ 
121+     test ( 'queries should run inside the handler' ,  t  =>  { 
122+       t . plan ( 2 ) 
123+ 
124+       const  fastify  =  Fastify ( ) 
125+       t . teardown ( ( )  =>  fastify . close ( ) ) 
126+ 
127+       fastify . register ( fastifyPostgres ,  { 
128+         connectionString : process . env . DATABASE_TEST_URL  ||  'postgres://postgres:postgres@localhost/postgres' 
129+       } ) 
130+ 
131+       fastify . route ( { 
132+         method : 'GET' , 
133+         url : '/users' , 
134+         options : {  useTransaction : true  } , 
135+         handler : ( req ,  reply )  =>  { 
136+           reply . send ( 'response!' ) 
137+           t . ok ( req . pg ) 
138+ 
139+           const  result  =  req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' ,  [ 
140+             'root-with-callback' 
141+           ] ) 
142+ 
143+           const  userId  =  result . rows [ 0 ] . id 
144+ 
145+           req . pg 
146+             . query ( 'SELECT * FROM users WHERE id = $1' ,  [ userId ] ) 
147+             . then ( ( result )  =>  { 
148+               t . is ( result . rows [ 0 ] . username ,  'root-with-callback' ) 
149+             } ) 
150+             . catch ( ( err )  =>  { 
151+               t . fail ( err ) 
152+             } ) 
153+         } 
154+       } ) 
155+ 
156+       fastify . inject ( { 
157+         method : 'GET' , 
158+         url : '/users' 
159+       } ,  ( err ,  res )  =>  { 
160+         t . error ( err ) 
161+       } ) 
162+     } ) 
163+ 
164+     test ( 'fastify request should commit the transaction' ) 
165+ 
166+     t . end ( ) 
167+   } ) 
168+ 
169+   test ( 'and the transaction fails - ' ,  t  =>  { 
170+     test ( 'throws the correct error' ) 
171+ 
172+     test ( 'rolls back the transaction' ) 
173+ 
174+     t . end ( ) 
175+   } ) 
176+ 
177+   t . end ( ) 
178+ } ) 
0 commit comments