@@ -116,3 +116,121 @@ test('use native module', t => {
116116 } )
117117 } )
118118} )
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+
130+ fastify . ready ( err => {
131+ t . error ( err )
132+ t . ok ( fastify . pg )
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 )
137+ fastify . close ( )
138+ } )
139+ } )
140+
141+ test ( 'fastify.pg.test should be able to connect and perform a query' , t => {
142+ t . plan ( 4 )
143+
144+ const fastify = Fastify ( )
145+
146+ fastify . register ( fastifyPostgres , {
147+ name : 'test' ,
148+ connectionString : 'postgres://postgres@localhost/postgres'
149+ } )
150+
151+ fastify . ready ( err => {
152+ t . error ( err )
153+ fastify . pg . test . connect ( onConnect )
154+ } )
155+
156+ function onConnect ( err , client , done ) {
157+ t . error ( err )
158+ client . query ( 'SELECT NOW()' , ( err , result ) => {
159+ done ( )
160+ t . error ( err )
161+ t . ok ( result . rows )
162+ fastify . close ( )
163+ } )
164+ }
165+ } )
166+
167+ test ( 'fastify.pg.test use query util' , t => {
168+ t . plan ( 3 )
169+
170+ const fastify = Fastify ( )
171+
172+ fastify . register ( fastifyPostgres , {
173+ name : 'test' ,
174+ connectionString : 'postgres://postgres@localhost/postgres'
175+ } )
176+
177+ fastify . ready ( err => {
178+ t . error ( err )
179+ fastify . pg . test . query ( 'SELECT NOW()' , ( err , result ) => {
180+ t . error ( err )
181+ t . ok ( result . rows )
182+ fastify . close ( )
183+ } )
184+ } )
185+ } )
186+
187+ test ( 'fastify.pg.test use query util with promises' , t => {
188+ t . plan ( 2 )
189+
190+ const fastify = Fastify ( )
191+
192+ fastify . register ( fastifyPostgres , {
193+ name : 'test' ,
194+ connectionString : 'postgres://postgres@localhost/postgres'
195+ } )
196+
197+ fastify . ready ( err => {
198+ t . error ( err )
199+ fastify . pg . test
200+ . query ( 'SELECT NOW()' )
201+ . then ( result => {
202+ t . ok ( result . rows )
203+ fastify . close ( )
204+ } )
205+ . catch ( err => {
206+ t . fail ( err )
207+ fastify . close ( )
208+ } )
209+ } )
210+ } )
211+
212+ test ( 'fastify.pg.test use native module' , t => {
213+ t . plan ( 2 )
214+
215+ const fastify = Fastify ( )
216+
217+ fastify . register ( fastifyPostgres , {
218+ name : 'test' ,
219+ connectionString : 'postgres://postgres@localhost/postgres' ,
220+ native : true
221+ } )
222+
223+ fastify . ready ( err => {
224+ t . error ( err )
225+ fastify . pg . test
226+ . query ( 'SELECT 1 AS one' )
227+ . then ( result => {
228+ t . ok ( result . rows [ 0 ] . one === 1 )
229+ fastify . close ( )
230+ } )
231+ . catch ( err => {
232+ t . fail ( err )
233+ fastify . close ( )
234+ } )
235+ } )
236+ } )
0 commit comments