Skip to content

Commit 750d26a

Browse files
authored
fix: improve code coverage to 100% #88 (#105)
1 parent 2755ab6 commit 750d26a

File tree

6 files changed

+262
-62
lines changed

6 files changed

+262
-62
lines changed

.taprc

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,17 @@ function fastifyPostgres (fastify, options, next) {
144144
}
145145

146146
if (client[name]) {
147+
client.release()
147148
throw new Error(`pg client '${name}' is a reserved keyword`)
148149
} else if (req.pg[name]) {
150+
client.release()
149151
throw new Error(`request client '${name}' has already been registered`)
150152
}
151153

152154
req.pg[name] = client
153155
} else {
154156
if (req.pg) {
157+
client.release()
155158
throw new Error('request client has already been registered')
156159
} else {
157160
req.pg = client

test/initialization.test.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const t = require('tap')
44
const test = t.test
55
const Fastify = require('fastify')
6+
const pg = require('pg')
67
const fastifyPostgres = require('../index')
78
const { connectionString } = require('./helpers')
89

@@ -23,7 +24,41 @@ test('Should be able to use native module', (t) => {
2324
fastify.pg
2425
.query('SELECT 1 AS one')
2526
.then((result) => {
26-
t.is(result.rows[0].one, 1)
27+
t.equal(result.rows[0].one, 1)
28+
})
29+
.catch((err) => {
30+
t.fail(err)
31+
})
32+
})
33+
})
34+
35+
test('Should print warning when native module not installed', (t) => {
36+
t.plan(3)
37+
38+
const mockedFastifyPostgres = t.mock('../index', {
39+
pg: { ...pg, native: null }
40+
})
41+
const realConsole = global.console
42+
global.console.warn = (msg) => t.equal(msg, "pg-native not installed, can't use native option - fallback to pg module")
43+
44+
const fastify = Fastify()
45+
t.teardown(() => {
46+
fastify.close()
47+
global.console = realConsole
48+
})
49+
50+
fastify.register(mockedFastifyPostgres, {
51+
connectionString,
52+
native: true
53+
})
54+
55+
fastify.ready((err) => {
56+
t.error(err)
57+
58+
fastify.pg
59+
.query('SELECT 1 AS one')
60+
.then((result) => {
61+
t.equal(result.rows[0].one, 1)
2762
})
2863
.catch((err) => {
2964
t.fail(err)
@@ -49,7 +84,7 @@ test('Should be able to use an alternative pg module', (t) => {
4984
fastify.pg
5085
.query('SELECT 1 AS one')
5186
.then((result) => {
52-
t.is(result.rows[0].one, 1)
87+
t.equal(result.rows[0].one, 1)
5388
})
5489
.catch((err) => {
5590
t.fail(err)
@@ -106,7 +141,7 @@ test('Should throw when trying to register multiple instances without giving a n
106141

107142
fastify.ready((err) => {
108143
t.ok(err)
109-
t.is((err || {}).message, 'fastify-postgres has already been registered')
144+
t.equal((err || {}).message, 'fastify-postgres has already been registered')
110145
})
111146
})
112147

@@ -149,7 +184,7 @@ test('Should throw when trying to register duplicate connection names', (t) => {
149184

150185
fastify.ready((err) => {
151186
t.ok(err)
152-
t.is((err || {}).message, `fastify-postgres '${name}' instance name has already been registered`)
187+
t.equal((err || {}).message, `fastify-postgres '${name}' instance name has already been registered`)
153188
})
154189
})
155190

@@ -167,7 +202,7 @@ test('Should throw when trying to register a named connection with a reserved ke
167202

168203
fastify.ready((err) => {
169204
t.ok(err)
170-
t.is((err || {}).message, `fastify-postgres '${name}' is a reserved keyword`)
205+
t.equal((err || {}).message, `fastify-postgres '${name}' is a reserved keyword`)
171206
})
172207
})
173208

test/query.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ test('When fastify.pg root namespace is used:', (t) => {
9999
t.error(err)
100100

101101
fastify.pg.query('SELECT NOW()', (err, result) => {
102-
t.is(result, undefined)
102+
t.equal(result, undefined)
103103
t.ok(err)
104-
t.is(err.message, `database "${BAD_DB_NAME}" does not exist`)
104+
t.equal(err.message, `database "${BAD_DB_NAME}" does not exist`)
105105
})
106106
})
107107
}
@@ -127,7 +127,7 @@ test('When fastify.pg root namespace is used:', (t) => {
127127
})
128128
.catch((err) => {
129129
t.ok(err)
130-
t.is(err.message, `database "${BAD_DB_NAME}" does not exist`)
130+
t.equal(err.message, `database "${BAD_DB_NAME}" does not exist`)
131131
})
132132
})
133133
})
@@ -225,7 +225,7 @@ test('When fastify.pg custom namespace is used:', (t) => {
225225
fastify.pg.test
226226
.query('SELECT 1 AS one')
227227
.then((result) => {
228-
t.is(result.rows[0].one, 1)
228+
t.equal(result.rows[0].one, 1)
229229
})
230230
.catch((err) => {
231231
t.fail(err)
@@ -254,7 +254,7 @@ test('When fastify.pg custom namespace is used:', (t) => {
254254
})
255255
.catch((err) => {
256256
t.ok(err)
257-
t.is(err.message, `database "${BAD_DB_NAME}" does not exist`)
257+
t.equal(err.message, `database "${BAD_DB_NAME}" does not exist`)
258258
})
259259
})
260260
})

test/req-initialization.test.js

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('fastify postgress useTransaction route option', t => {
3838
url: '/count-users'
3939
})
4040

41-
t.is(extractUserCount(response), 2)
41+
t.equal(extractUserCount(response), 2)
4242
})
4343
test('queries that succeed provided to a namespace', async t => {
4444
const fastify = Fastify()
@@ -71,7 +71,7 @@ test('fastify postgress useTransaction route option', t => {
7171
url: '/count-users'
7272
})
7373

74-
t.is(extractUserCount(response), 2)
74+
t.equal(extractUserCount(response), 2)
7575
})
7676
test('queries that fail provided', async t => {
7777
const fastify = Fastify()
@@ -103,7 +103,7 @@ test('fastify postgress useTransaction route option', t => {
103103
url: '/count-users'
104104
})
105105

106-
t.is(extractUserCount(response), 0)
106+
t.equal(extractUserCount(response), 0)
107107
})
108108

109109
t.end()
@@ -121,7 +121,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
121121
})
122122

123123
fastify.get('/', (req, reply) => {
124-
t.is(req.pg, null)
124+
t.equal(req.pg, null)
125125
})
126126

127127
fastify.inject({ url: '/' })
@@ -138,7 +138,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
138138
})
139139

140140
fastify.get('/', (req, reply) => {
141-
t.is(req.pg, null)
141+
t.equal(req.pg, null)
142142
})
143143

144144
fastify.inject({ url: '/' })
@@ -155,7 +155,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
155155
})
156156

157157
fastify.get('/', { pg: { transact: true } }, (req, reply) => {
158-
t.is(req.pg, null)
158+
t.equal(req.pg, null)
159159
})
160160

161161
fastify.inject({ url: '/' })
@@ -171,7 +171,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
171171
})
172172

173173
fastify.get('/', { pg: { transact: 'test' } }, (req, reply) => {
174-
t.is(req.pg, null)
174+
t.equal(req.pg, null)
175175
})
176176

177177
fastify.inject({ url: '/' })
@@ -188,10 +188,89 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
188188
})
189189

190190
fastify.get('/', { pg: { transact: 'different' } }, (req, reply) => {
191-
t.is(req.pg, null)
191+
t.equal(req.pg, null)
192192
})
193193

194194
fastify.inject({ url: '/' })
195195
})
196196
t.end()
197197
})
198+
199+
test('incorrect combinations of registrationOptions.name and routeOptions.pg.transact should throw errors', t => {
200+
t.test('name set as reserved keyword', t => {
201+
t.plan(2)
202+
203+
const fastify = Fastify()
204+
t.teardown(() => fastify.close())
205+
206+
const name = 'user'
207+
208+
fastify.register(fastifyPostgres, {
209+
connectionString,
210+
name
211+
})
212+
213+
fastify.get('/', { pg: { transact: name } }, (req, reply) => {})
214+
215+
fastify.inject({ url: '/' }, (err, response) => {
216+
t.error(err)
217+
t.same(response.json(), {
218+
statusCode: 500,
219+
error: 'Internal Server Error',
220+
message: `request client '${name}' does not exist`
221+
})
222+
})
223+
})
224+
225+
t.test('named pg client has already registered', t => {
226+
t.plan(2)
227+
228+
const fastify = Fastify()
229+
t.teardown(() => fastify.close())
230+
231+
const name = 'test'
232+
233+
fastify.register(fastifyPostgres, {
234+
connectionString,
235+
name
236+
})
237+
fastify.addHook('onRequest', async (req, reply) => {
238+
req.pg = { [name]: await fastify.pg[name].connect() }
239+
})
240+
fastify.get('/', { pg: { transact: name } }, (req, reply) => {})
241+
242+
fastify.inject({ url: '/' }, (err, response) => {
243+
t.error(err)
244+
t.same(response.json(), {
245+
statusCode: 500,
246+
error: 'Internal Server Error',
247+
message: `request client '${name}' has already been registered`
248+
})
249+
})
250+
})
251+
252+
t.test('pg client has already registered', t => {
253+
t.plan(2)
254+
255+
const fastify = Fastify()
256+
t.teardown(() => fastify.close())
257+
258+
fastify.register(fastifyPostgres, {
259+
connectionString
260+
})
261+
fastify.addHook('onRequest', async (req, reply) => {
262+
req.pg = await fastify.pg.connect()
263+
})
264+
fastify.get('/', { pg: { transact: true } }, (req, reply) => {})
265+
266+
fastify.inject({ url: '/' }, (err, response) => {
267+
t.error(err)
268+
t.same(response.json(), {
269+
statusCode: 500,
270+
error: 'Internal Server Error',
271+
message: 'request client has already been registered'
272+
})
273+
})
274+
})
275+
t.end()
276+
})

0 commit comments

Comments
 (0)