-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (52 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Decorator functions to provide extra functionality to the tape test object
*/
'use strict';
/**
* Provides postgres connection in each test. Disconnects client after test.
* @param {Object} opts :: Options object to configure wrapper
* @param {tape.Test} tape :: The tape test function
* @return {Function} :: Test function w/ tape compatible API
*/
module.exports = function (opts, tape) {
var pg = opts.pg || require('pg');
var model = opts.model || '';
var pool = new pg.Pool(opts.connection);
var _client, _release;
tape.onFinish(() => pool.end());
function wrapper (name, expect, fn) {
if (!fn) {
fn = expect; // eslint-disable-line no-param-reassign
expect = null; // eslint-disable-line no-param-reassign
}
tape(name + ' :: connect', (t) => {
pool.connect((connErr, client, release) => {
if (connErr) {
t.fail(connErr);
return t.end();
}
_client = client;
_release = release;
// Need to drop ?
_client.query(model)
.then(() => t.end())
.catch((err) => {
t.fail(err);
t.end();
});
});
});
tape(name, (t) => {
if (expect !== null)
t.plan(expect);
fn.call(t, t, _client);
});
tape(name + ' :: disconnect', (t) => {
// Need to drop ?
_release();
t.end();
});
}
wrapper.onFinish = tape.onFinish;
return wrapper;
};