Skip to content

Commit b189dae

Browse files
committed
Added more tests
1 parent feb4fa6 commit b189dae

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

plugin.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ function decorateFastifyInstance (pool, fastify, options, next) {
2828
}
2929

3030
if (fastify.oracle[options.name]) {
31-
return next(new Error('Connection name has already been registered: ' + options.name))
31+
return next(Error('fastify-oracle: connection name "' + options.name + '" has already been registered'))
3232
}
3333

3434
fastify.oracle[options.name] = oracle
3535
} else {
3636
if (fastify.oracle) {
37-
return next(new Error('fastify-oracle has already been registered'))
37+
return next(Error('fastify-oracle has already been registered'))
3838
} else {
3939
fastify.decorate('oracle', Object.assign(oracle, { db: oracledb }))
4040
}
@@ -47,24 +47,30 @@ function decorateFastifyInstance (pool, fastify, options, next) {
4747

4848
function fastifyOracleDB (fastify, options, next) {
4949
if (options.client) {
50-
if (oracledb.Pool.prototype.isPrototypeOf(options.client) === false) {
51-
return next(Error('supplied client must be an instance of oracledb.pool'))
50+
if (oracledb.Pool.prototype.isPrototypeOf(options.client)) {
51+
return decorateFastifyInstance(options.client, fastify, options, next)
52+
} else {
53+
return next(Error('fastify-oracle: supplied client must be an instance of oracledb.pool'))
5254
}
53-
return decorateFastifyInstance(options.client, fastify, options, next)
5455
}
5556

5657
if (options.poolAlias) {
57-
const pool = oracledb.getPool(options.poolAlias)
58-
if (!pool) return next(Error('could not get default pool from oracledb instance'))
59-
return decorateFastifyInstance(pool, fastify, options, next)
58+
try {
59+
const pool = oracledb.getPool(options.poolAlias) // synchronous, throws error
60+
return decorateFastifyInstance(pool, fastify, options, next)
61+
} catch (err) {
62+
return next(Error('fastify-oracle: could not get pool alias' + '-' + err.message))
63+
}
6064
}
6165

6266
if (!options.pool) {
63-
return next(Error('must supply options.pool oracledb pool options'))
67+
return next(Error('fastify-oracle: must supply options.pool oracledb pool options'))
6468
}
6569

6670
oracledb.createPool(options.pool, (err, pool) => {
67-
if (err) return next(err)
71+
if (err) {
72+
return next(Error('fastify-oracle: failed to create pool' + '-' + err.message))
73+
}
6874
return decorateFastifyInstance(pool, fastify, options, next)
6975
})
7076
}

test/client.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const test = require('tap').test
44
const oracledb = require('oracledb')
55
const plugin = require('../plugin')
6+
const Fastify = require('fastify')
67

78
test('accepts singleton client', (t) => {
89
t.plan(5)
@@ -60,3 +61,15 @@ test('retrieves a cached pool', (t) => {
6061
})
6162
})
6263
})
64+
65+
test('client must be instance of oracledb.pool', (t) => {
66+
t.plan(1)
67+
68+
const fastify = Fastify()
69+
70+
fastify.register(plugin, { client: 'hello world' })
71+
72+
fastify.ready(err => {
73+
t.is(err.message, 'fastify-oracle: supplied client must be an instance of oracledb.pool')
74+
})
75+
})

test/pool.test.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,61 @@ test('duplicate connection names should throw', (t) => {
106106
.register(plugin, { pool: opts, name: 'testdb' })
107107

108108
fastify.ready(err => {
109-
t.is(err.message, 'Connection name has already been registered: testdb')
109+
t.is(err.message, 'fastify-oracle: connection name "testdb" has already been registered')
110+
})
111+
})
112+
113+
test('duplicate plugin registration should throw', (t) => {
114+
t.plan(1)
115+
116+
const fastify = Fastify()
117+
118+
const opts = {
119+
user: 'travis',
120+
password: 'travis',
121+
connectString: 'localhost/xe'
122+
}
123+
124+
fastify
125+
.register(plugin, { pool: opts })
126+
.register(plugin, { pool: opts })
127+
128+
fastify.ready(err => {
129+
t.is(err.message, 'fastify-oracle has already been registered')
130+
})
131+
})
132+
133+
test('should throw if no pool option is provided', (t) => {
134+
t.plan(1)
135+
136+
const fastify = Fastify()
137+
138+
fastify.register(plugin, {})
139+
fastify.ready(err => {
140+
t.is(err.message, 'fastify-oracle: must supply options.pool oracledb pool options')
141+
})
142+
})
143+
144+
test('should throw if could not get pool alias', (t) => {
145+
t.plan(1)
146+
147+
const fastify = Fastify()
148+
149+
fastify.register(plugin, { poolAlias: 'test' })
150+
151+
fastify.ready(err => {
152+
t.match(err.message, 'fastify-oracle: could not get pool alias')
153+
})
154+
})
155+
156+
test('should throw if pool cannot be created', (t) => {
157+
t.plan(1)
158+
159+
const fastify = Fastify()
160+
161+
fastify.register(plugin, { pool: { poolMin: -5 } })
162+
163+
fastify.ready(err => {
164+
t.match(err.message, 'fastify-oracle: failed to create pool')
110165
})
111166
})

0 commit comments

Comments
 (0)