@@ -5,47 +5,140 @@ const test = t.test
5
5
const Fastify = require ( 'fastify' )
6
6
const fastifyPostgres = require ( './index' )
7
7
8
- test ( 'fastify.pg.test namespace should exist' , t => {
9
- t . plan ( 6 )
8
+ test ( 'fastify.pg namespace should exist' , t => {
9
+ t . plan ( 5 )
10
10
11
11
const fastify = Fastify ( )
12
12
13
13
fastify . register ( fastifyPostgres , {
14
- name : 'test' ,
15
14
connectionString : 'postgres://postgres@localhost/postgres'
16
15
} )
17
16
18
17
fastify . ready ( err => {
19
18
t . error ( err )
20
19
t . ok ( fastify . pg )
21
- t . ok ( fastify . pg . test )
22
- t . ok ( fastify . pg . test . connect )
23
- t . ok ( fastify . pg . test . pool )
24
- t . ok ( fastify . pg . test . Client )
20
+ t . ok ( fastify . pg . connect )
21
+ t . ok ( fastify . pg . pool )
22
+ t . ok ( fastify . pg . Client )
25
23
fastify . close ( )
26
24
} )
27
25
} )
28
26
29
- test ( 'fastify.pg namespace should exist when name option not configure' , t => {
30
- t . plan ( 5 )
27
+ test ( 'should be able to connect and perform a query' , t => {
28
+ t . plan ( 4 )
29
+
30
+ const fastify = Fastify ( )
31
+
32
+ fastify . register ( fastifyPostgres , {
33
+ connectionString : 'postgres://postgres@localhost/postgres'
34
+ } )
35
+
36
+ fastify . ready ( err => {
37
+ t . error ( err )
38
+ fastify . pg . connect ( onConnect )
39
+ } )
40
+
41
+ function onConnect ( err , client , done ) {
42
+ t . error ( err )
43
+ client . query ( 'SELECT NOW()' , ( err , result ) => {
44
+ done ( )
45
+ t . error ( err )
46
+ t . ok ( result . rows )
47
+ fastify . close ( )
48
+ } )
49
+ }
50
+ } )
51
+
52
+ test ( 'use query util' , t => {
53
+ t . plan ( 3 )
31
54
32
55
const fastify = Fastify ( )
33
56
34
57
fastify . register ( fastifyPostgres , {
35
58
connectionString : 'postgres://postgres@localhost/postgres'
36
59
} )
37
60
61
+ fastify . ready ( err => {
62
+ t . error ( err )
63
+ fastify . pg . query ( 'SELECT NOW()' , ( err , result ) => {
64
+ t . error ( err )
65
+ t . ok ( result . rows )
66
+ fastify . close ( )
67
+ } )
68
+ } )
69
+ } )
70
+
71
+ test ( 'use query util with promises' , t => {
72
+ t . plan ( 2 )
73
+
74
+ const fastify = Fastify ( )
75
+
76
+ fastify . register ( fastifyPostgres , {
77
+ connectionString : 'postgres://postgres@localhost/postgres'
78
+ } )
79
+
80
+ fastify . ready ( err => {
81
+ t . error ( err )
82
+ fastify . pg
83
+ . query ( 'SELECT NOW()' )
84
+ . then ( result => {
85
+ t . ok ( result . rows )
86
+ fastify . close ( )
87
+ } )
88
+ . catch ( err => {
89
+ t . fail ( err )
90
+ fastify . close ( )
91
+ } )
92
+ } )
93
+ } )
94
+
95
+ test ( 'use native module' , t => {
96
+ t . plan ( 2 )
97
+
98
+ const fastify = Fastify ( )
99
+
100
+ fastify . register ( fastifyPostgres , {
101
+ connectionString : 'postgres://postgres@localhost/postgres' ,
102
+ native : true
103
+ } )
104
+
105
+ fastify . ready ( err => {
106
+ t . error ( err )
107
+ fastify . pg
108
+ . query ( 'SELECT 1 AS one' )
109
+ . then ( result => {
110
+ t . ok ( result . rows [ 0 ] . one === 1 )
111
+ fastify . close ( )
112
+ } )
113
+ . catch ( err => {
114
+ t . fail ( err )
115
+ fastify . close ( )
116
+ } )
117
+ } )
118
+ } )
119
+
120
+ test ( 'fastify.pg.test namespace should exist' , t => {
121
+ t . plan ( 6 )
122
+
123
+ const fastify = Fastify ( )
124
+
125
+ fastify . register ( fastifyPostgres , {
126
+ name : 'test' ,
127
+ connectionString : 'postgres://postgres@localhost/postgres'
128
+ } )
129
+
38
130
fastify . ready ( err => {
39
131
t . error ( err )
40
132
t . ok ( fastify . pg )
41
- t . ok ( fastify . pg . connect )
42
- t . ok ( fastify . pg . pool )
43
- t . ok ( fastify . pg . Client )
133
+ t . ok ( fastify . pg . test )
134
+ t . ok ( fastify . pg . test . connect )
135
+ t . ok ( fastify . pg . test . pool )
136
+ t . ok ( fastify . pg . test . Client )
44
137
fastify . close ( )
45
138
} )
46
139
} )
47
140
48
- test ( 'should be able to connect and perform a query' , t => {
141
+ test ( 'fastify.pg.test should be able to connect and perform a query' , t => {
49
142
t . plan ( 4 )
50
143
51
144
const fastify = Fastify ( )
@@ -71,7 +164,7 @@ test('should be able to connect and perform a query', t => {
71
164
}
72
165
} )
73
166
74
- test ( 'use query util' , t => {
167
+ test ( 'fastify.pg.test use query util' , t => {
75
168
t . plan ( 3 )
76
169
77
170
const fastify = Fastify ( )
@@ -91,7 +184,7 @@ test('use query util', t => {
91
184
} )
92
185
} )
93
186
94
- test ( 'use query util with promises' , t => {
187
+ test ( 'fastify.pg.test use query util with promises' , t => {
95
188
t . plan ( 2 )
96
189
97
190
const fastify = Fastify ( )
@@ -116,7 +209,7 @@ test('use query util with promises', t => {
116
209
} )
117
210
} )
118
211
119
- test ( 'use native module' , t => {
212
+ test ( 'fastify.pg.test use native module' , t => {
120
213
t . plan ( 2 )
121
214
122
215
const fastify = Fastify ( )
0 commit comments