@@ -9,78 +9,74 @@ const { connectionString } = require('./helpers')
9
9
const extractUserCount = response => parseInt ( JSON . parse ( response . payload ) . rows [ 0 ] . userCount )
10
10
11
11
test ( 'fastify postgress useTransaction route option - ' , t => {
12
- test ( 'set to true - ' , t => {
13
- test ( 'passing queries provided' , async t => {
14
- const fastify = Fastify ( )
15
- t . teardown ( ( ) => fastify . close ( ) )
12
+ test ( 'queries that succeed provided' , async t => {
13
+ const fastify = Fastify ( )
14
+ t . teardown ( ( ) => fastify . close ( ) )
16
15
17
- await fastify . register ( fastifyPostgres , {
18
- connectionString
19
- } )
20
-
21
- await fastify . pg . query ( 'TRUNCATE users' )
16
+ await fastify . register ( fastifyPostgres , {
17
+ connectionString
18
+ } )
22
19
23
- await fastify . get ( '/count-users' , async ( req , reply ) => {
24
- const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'' )
20
+ await fastify . pg . query ( 'TRUNCATE users' )
25
21
26
- reply . send ( result )
27
- } )
22
+ await fastify . get ( '/count-users' , async ( req , reply ) => {
23
+ const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'' )
28
24
29
- await fastify . get ( '/pass' , { pg : { transact : true } } , async ( req , reply ) => {
30
- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
31
- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
32
- reply . send ( 'complete' )
33
- } )
25
+ reply . send ( result )
26
+ } )
34
27
35
- await fastify . inject ( {
36
- method : 'GET' ,
37
- url : '/pass'
38
- } )
28
+ await fastify . get ( '/pass' , { pg : { transact : true } } , async ( req , reply ) => {
29
+ await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
30
+ await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
31
+ reply . send ( 'complete' )
32
+ } )
39
33
40
- const response = await fastify . inject ( {
41
- method : 'GET' ,
42
- url : '/count-users '
43
- } )
34
+ await fastify . inject ( {
35
+ method : 'GET' ,
36
+ url : '/pass '
37
+ } )
44
38
45
- t . is ( extractUserCount ( response ) , 2 )
39
+ const response = await fastify . inject ( {
40
+ method : 'GET' ,
41
+ url : '/count-users'
46
42
} )
47
- test ( 'failing queries provided' , async t => {
48
- const fastify = Fastify ( )
49
- t . teardown ( ( ) => fastify . close ( ) )
50
43
51
- await fastify . register ( fastifyPostgres , {
52
- connectionString
53
- } )
44
+ t . is ( extractUserCount ( response ) , 2 )
45
+ } )
46
+ test ( 'queries that fail provided' , async t => {
47
+ const fastify = Fastify ( )
48
+ t . teardown ( ( ) => fastify . close ( ) )
54
49
55
- await fastify . pg . query ( 'TRUNCATE users' )
50
+ await fastify . register ( fastifyPostgres , {
51
+ connectionString
52
+ } )
56
53
57
- await fastify . get ( '/count-users' , async ( req , reply ) => {
58
- const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'' )
54
+ await fastify . pg . query ( 'TRUNCATE users' )
59
55
60
- reply . send ( result )
61
- } )
56
+ await fastify . get ( '/count-users' , async ( req , reply ) => {
57
+ const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'' )
62
58
63
- await fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
64
- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
65
- await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
66
- await req . pg . query ( 'INSERT INTO nope(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
67
- reply . send ( 'complete' )
68
- } )
59
+ reply . send ( result )
60
+ } )
69
61
70
- await fastify . inject ( {
71
- method : 'GET' ,
72
- url : '/fail'
73
- } )
62
+ await fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
63
+ await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
64
+ await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
65
+ await req . pg . query ( 'INSERT INTO nope(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
66
+ reply . send ( 'complete' )
67
+ } )
74
68
75
- const response = await fastify . inject ( {
76
- method : 'GET' ,
77
- url : '/count-users '
78
- } )
69
+ await fastify . inject ( {
70
+ method : 'GET' ,
71
+ url : '/fail '
72
+ } )
79
73
80
- t . is ( extractUserCount ( response ) , 0 )
74
+ const response = await fastify . inject ( {
75
+ method : 'GET' ,
76
+ url : '/count-users'
81
77
} )
82
78
83
- t . end ( )
79
+ t . is ( extractUserCount ( response ) , 0 )
84
80
} )
85
81
86
82
t . end ( )
0 commit comments