Skip to content

Commit 1e806ce

Browse files
committed
Refactor previous tests, cov 100
1 parent b189dae commit 1e806ce

File tree

5 files changed

+199
-256
lines changed

5 files changed

+199
-256
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A plugin for Fastify to provide Oracle DB connections",
55
"main": "plugin.js",
66
"scripts": {
7-
"test": "tap --cov 'test/**/*.test.js'",
7+
"test": "standard && tap --cov test.js",
88
"lint": "standard | snazzy"
99
},
1010
"precommit": [

plugin.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@
33
const fp = require('fastify-plugin')
44
const oracledb = require('oracledb')
55

6-
const close = function (fastify, done) {
7-
Object.keys(fastify.oracle)
8-
.forEach(key => {
9-
if (fastify.oracle[key].pool) {
10-
fastify.oracle[key].pool.close(done)
11-
}
12-
})
13-
14-
if (fastify.oracle.pool) {
15-
fastify.oracle.pool.close(done)
16-
}
17-
}
18-
196
function decorateFastifyInstance (pool, fastify, options, next) {
207
const oracle = {
218
getConnection: pool.getConnection.bind(pool),
@@ -40,7 +27,7 @@ function decorateFastifyInstance (pool, fastify, options, next) {
4027
}
4128
}
4229

43-
fastify.addHook('onClose', close)
30+
fastify.addHook('onClose', (fastify, done) => pool.close(done))
4431

4532
return next()
4633
}

test.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const plugin = require('./plugin')
5+
const Fastify = require('fastify')
6+
const oracledb = require('oracledb')
7+
const poolOptions = {
8+
user: 'travis',
9+
password: 'travis',
10+
connectString: 'localhost/xe'
11+
}
12+
13+
test('creates pool from config', (t) => {
14+
t.plan(9)
15+
16+
const fastify = Fastify()
17+
fastify.register(plugin, { pool: poolOptions })
18+
19+
fastify.ready(err => {
20+
t.error(err)
21+
t.ok(fastify.oracle.pool)
22+
23+
fastify.oracle.getConnection((err, conn) => {
24+
t.error(err)
25+
conn.execute('SELECT 1 AS FOO FROM DUAL', {}, { outFormat: fastify.oracle.db.OBJECT }, (err, result) => {
26+
t.error(err)
27+
t.is(result.rows.length, 1)
28+
t.is(result.rows[0].FOO, 1)
29+
conn.close(err => {
30+
t.error(err)
31+
32+
fastify.close(err => {
33+
t.error(err)
34+
t.is(fastify.oracle.pool.status, fastify.oracle.db.POOL_STATUS_CLOSED)
35+
})
36+
})
37+
})
38+
})
39+
})
40+
})
41+
42+
test('creates named pool from config', (t) => {
43+
t.plan(11)
44+
45+
const fastify = Fastify()
46+
fastify.register(plugin, { pool: poolOptions, name: 'testdb' })
47+
48+
fastify.ready(err => {
49+
t.error(err)
50+
t.ok(fastify.oracle.pool)
51+
t.ok(fastify.oracle.testdb.pool)
52+
53+
fastify.oracle.testdb.getConnection((err, conn) => {
54+
t.error(err)
55+
56+
conn.execute('SELECT 1 AS FOO FROM DUAL', {}, { outFormat: fastify.oracle.db.OBJECT }, (err, result) => {
57+
t.error(err)
58+
t.is(result.rows.length, 1)
59+
t.is(result.rows[0].FOO, 1)
60+
conn.close(err => {
61+
t.error(err)
62+
63+
fastify.close(err => {
64+
t.error(err)
65+
t.is(fastify.oracle.pool.status, fastify.oracle.db.POOL_STATUS_CLOSED)
66+
t.is(fastify.oracle.testdb.pool.status, fastify.oracle.db.POOL_STATUS_CLOSED)
67+
})
68+
})
69+
})
70+
})
71+
})
72+
})
73+
74+
test('accepts singleton client', (t) => {
75+
t.plan(7)
76+
oracledb.createPool(poolOptions, (err, pool) => {
77+
t.error(err)
78+
79+
const fastify = Fastify()
80+
fastify.register(plugin, { client: pool })
81+
82+
fastify.ready(err => {
83+
t.error(err)
84+
t.is(fastify.oracle.pool.status, fastify.oracle.db.POOL_STATUS_OPEN)
85+
t.is(fastify.oracle.db, oracledb)
86+
t.is(fastify.oracle.pool, pool)
87+
fastify.close(err => {
88+
t.error(err)
89+
t.is(fastify.oracle.pool.status, fastify.oracle.db.POOL_STATUS_CLOSED)
90+
})
91+
})
92+
})
93+
})
94+
95+
test('retrieves a cached pool', (t) => {
96+
t.plan(7)
97+
98+
const opts = Object.assign({}, poolOptions)
99+
oracledb.createPool(Object.assign(opts, { poolAlias: 'foo' }), (err, pool) => {
100+
t.error(err)
101+
102+
const fastify = Fastify()
103+
fastify.register(plugin, { poolAlias: 'foo' })
104+
105+
fastify.ready(err => {
106+
t.error(err)
107+
t.is(fastify.oracle.pool.status, fastify.oracle.db.POOL_STATUS_OPEN)
108+
t.is(fastify.oracle.db, oracledb)
109+
t.is(fastify.oracle.pool, pool)
110+
fastify.close(err => {
111+
t.error(err)
112+
t.is(fastify.oracle.pool.status, fastify.oracle.db.POOL_STATUS_CLOSED)
113+
})
114+
})
115+
})
116+
})
117+
118+
test('client must be instance of oracledb.pool', (t) => {
119+
t.plan(1)
120+
121+
const fastify = Fastify()
122+
123+
fastify.register(plugin, { client: 'hello world' })
124+
125+
fastify.ready(err => {
126+
t.is(err.message, 'fastify-oracle: supplied client must be an instance of oracledb.pool')
127+
fastify.close()
128+
})
129+
})
130+
131+
test('duplicate connection names should throw', (t) => {
132+
t.plan(1)
133+
134+
const fastify = Fastify()
135+
136+
fastify
137+
.register(plugin, { pool: poolOptions, name: 'testdb' })
138+
.register(plugin, { pool: poolOptions, name: 'testdb' })
139+
140+
fastify.ready(err => {
141+
t.is(err.message, 'fastify-oracle: connection name "testdb" has already been registered')
142+
fastify.close()
143+
})
144+
})
145+
146+
test('duplicate plugin registration should throw', (t) => {
147+
t.plan(1)
148+
149+
const fastify = Fastify()
150+
151+
fastify
152+
.register(plugin, { pool: poolOptions })
153+
.register(plugin, { pool: poolOptions })
154+
155+
fastify.ready(err => {
156+
t.is(err.message, 'fastify-oracle has already been registered')
157+
fastify.close()
158+
})
159+
})
160+
161+
test('should throw if no pool option is provided', (t) => {
162+
t.plan(1)
163+
164+
const fastify = Fastify()
165+
166+
fastify.register(plugin, {})
167+
fastify.ready(err => {
168+
t.is(err.message, 'fastify-oracle: must supply options.pool oracledb pool options')
169+
fastify.close()
170+
})
171+
})
172+
173+
test('should throw if could not get pool alias', (t) => {
174+
t.plan(1)
175+
176+
const fastify = Fastify()
177+
178+
fastify.register(plugin, { poolAlias: 'test' })
179+
180+
fastify.ready(err => {
181+
t.match(err.message, 'fastify-oracle: could not get pool alias')
182+
fastify.close()
183+
})
184+
})
185+
186+
test('should throw if pool cannot be created', (t) => {
187+
t.plan(1)
188+
189+
const fastify = Fastify()
190+
191+
fastify.register(plugin, { pool: { poolMin: -5 } })
192+
193+
fastify.ready(err => {
194+
t.match(err.message, 'fastify-oracle: failed to create pool')
195+
fastify.close()
196+
})
197+
})

test/client.test.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)