@@ -5,47 +5,140 @@ const test = t.test
55const Fastify = require ( 'fastify' )
66const fastifyPostgres = require ( './index' )
77
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 )
1010
1111 const fastify = Fastify ( )
1212
1313 fastify . register ( fastifyPostgres , {
14- name : 'test' ,
1514 connectionString : 'postgres://postgres@localhost/postgres'
1615 } )
1716
1817 fastify . ready ( err => {
1918 t . error ( err )
2019 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 )
2523 fastify . close ( )
2624 } )
2725} )
2826
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 )
3154
3255 const fastify = Fastify ( )
3356
3457 fastify . register ( fastifyPostgres , {
3558 connectionString : 'postgres://postgres@localhost/postgres'
3659 } )
3760
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+
38130 fastify . ready ( err => {
39131 t . error ( err )
40132 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 )
44137 fastify . close ( )
45138 } )
46139} )
47140
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 => {
49142 t . plan ( 4 )
50143
51144 const fastify = Fastify ( )
@@ -71,7 +164,7 @@ test('should be able to connect and perform a query', t => {
71164 }
72165} )
73166
74- test ( 'use query util' , t => {
167+ test ( 'fastify.pg.test use query util' , t => {
75168 t . plan ( 3 )
76169
77170 const fastify = Fastify ( )
@@ -91,7 +184,7 @@ test('use query util', t => {
91184 } )
92185} )
93186
94- test ( 'use query util with promises' , t => {
187+ test ( 'fastify.pg.test use query util with promises' , t => {
95188 t . plan ( 2 )
96189
97190 const fastify = Fastify ( )
@@ -116,7 +209,7 @@ test('use query util with promises', t => {
116209 } )
117210} )
118211
119- test ( 'use native module' , t => {
212+ test ( 'fastify.pg.test use native module' , t => {
120213 t . plan ( 2 )
121214
122215 const fastify = Fastify ( )
0 commit comments