@@ -9,78 +9,110 @@ 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
- } )
16
+ await fastify . register ( fastifyPostgres , {
17
+ connectionString
18
+ } )
19
+
20
+ await fastify . pg . query ( 'TRUNCATE users' )
20
21
21
- await fastify . pg . query ( 'TRUNCATE users' )
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\'' )
22
24
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\'' )
25
+ reply . send ( result )
26
+ } )
25
27
26
- reply . send ( result )
27
- } )
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
+ } )
28
33
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
- } )
34
+ await fastify . inject ( {
35
+ method : 'GET' ,
36
+ url : '/pass'
37
+ } )
34
38
35
- await fastify . inject ( {
36
- method : 'GET' ,
37
- url : '/pass '
38
- } )
39
+ const response = await fastify . inject ( {
40
+ method : 'GET' ,
41
+ url : '/count-users '
42
+ } )
39
43
40
- const response = await fastify . inject ( {
41
- method : 'GET' ,
42
- url : '/count-users'
43
- } )
44
+ t . is ( extractUserCount ( response ) , 2 )
45
+ } )
46
+ test ( 'queries that succeed provided to a namespace' , async t => {
47
+ const fastify = Fastify ( )
48
+ t . teardown ( ( ) => fastify . close ( ) )
44
49
45
- t . is ( extractUserCount ( response ) , 2 )
50
+ await fastify . register ( fastifyPostgres , {
51
+ connectionString,
52
+ name : 'test'
46
53
} )
47
- test ( 'failing queries provided' , async t => {
48
- const fastify = Fastify ( )
49
- t . teardown ( ( ) => fastify . close ( ) )
50
54
51
- await fastify . register ( fastifyPostgres , {
52
- connectionString
53
- } )
55
+ await fastify . pg . test . query ( 'TRUNCATE users' )
56
+
57
+ await fastify . get ( '/count-users' , async ( req , reply ) => {
58
+ const result = await fastify . pg . test . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'' )
54
59
55
- await fastify . pg . query ( 'TRUNCATE users' )
60
+ reply . send ( result )
61
+ } )
56
62
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\'' )
63
+ await fastify . get ( '/pass' , { pg : { transact : 'test' } } , async ( req , reply ) => {
64
+ await req . pg . test . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
65
+ await req . pg . test . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'pass-opt-in' ] )
59
66
60
- reply . send ( result )
61
- } )
67
+ reply . send ( 'complete' )
68
+ } )
62
69
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
- } )
70
+ await fastify . inject ( {
71
+ method : 'GET' ,
72
+ url : '/pass'
73
+ } )
69
74
70
- await fastify . inject ( {
71
- method : 'GET' ,
72
- url : '/fail '
73
- } )
75
+ const response = await fastify . inject ( {
76
+ method : 'GET' ,
77
+ url : '/count-users '
78
+ } )
74
79
75
- const response = await fastify . inject ( {
76
- method : 'GET' ,
77
- url : '/count-users'
78
- } )
80
+ t . is ( extractUserCount ( response ) , 2 )
81
+ } )
82
+ test ( 'queries that fail provided' , async t => {
83
+ const fastify = Fastify ( )
84
+ t . teardown ( ( ) => fastify . close ( ) )
85
+
86
+ await fastify . register ( fastifyPostgres , {
87
+ connectionString
88
+ } )
89
+
90
+ await fastify . pg . query ( 'TRUNCATE users' )
91
+
92
+ await fastify . get ( '/count-users' , async ( req , reply ) => {
93
+ const result = await fastify . pg . query ( 'SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'' )
94
+
95
+ reply . send ( result )
96
+ } )
97
+
98
+ await fastify . get ( '/fail' , { pg : { transact : true } } , async ( req , reply ) => {
99
+ await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
100
+ await req . pg . query ( 'INSERT INTO users(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
101
+ await req . pg . query ( 'INSERT INTO nope(username) VALUES($1) RETURNING id' , [ 'fail-opt-in' ] )
102
+ reply . send ( 'complete' )
103
+ } )
104
+
105
+ await fastify . inject ( {
106
+ method : 'GET' ,
107
+ url : '/fail'
108
+ } )
79
109
80
- t . is ( extractUserCount ( response ) , 0 )
110
+ const response = await fastify . inject ( {
111
+ method : 'GET' ,
112
+ url : '/count-users'
81
113
} )
82
114
83
- t . end ( )
115
+ t . is ( extractUserCount ( response ) , 0 )
84
116
} )
85
117
86
118
t . end ( )
0 commit comments