-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (62 loc) · 2.2 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
67
68
69
70
71
72
73
// Richard Wen
// rrwen.dev@gmail.com
const { Client } = require('pg')
var pgtools = require('pgtools');
module.exports = function(options, callback) {
var options = options || {};
// (defaults_testdb) Default testdb options
options.tests = options.tests || [];
options.testdb = options.testdb || process.env.PGTESTDB || 'testdb';
options.messages = typeof options.messages !== 'undefined' ? options.messages : true;
// (defaults_connection) Default postgres connection options
options.connection = options.connection || process.env.PGCONNECTION || {};
if (Object.prototype.toString.call(options.connection) == '[object Object]') {
options.connection.host = options.connection.host || process.env.PGHOST || 'localhost';
options.connection.port = options.connection.port || process.env.PGPORT || 5432;
options.connection.user = options.connection.user || process.env.PGUSER || 'postgres';
options.connection.password = options.connection.password || process.env.PGPASSWORD || '';
}
// (test) Test a node function inside a database
pgtools.createdb(options.connection, options.testdb, function (err, res) {
// (test_create_error) Exit on database creation error
if (err) {
callback(err);
}
if (!err && options.messages) {
console.log('CREATE ' + options.testdb);
}
// (test_client) Create pg client
const client = new Client({
host: options.connection.host,
port: options.connection.port,
database: options.testdb,
user: options.connection.user,
password: options.connection.password
});
// (test_call) Call tests functions in order
if (options.tests.length > 0) {
var chain = options.tests.shift()(client);
// (test_call_chain) Chain promises in order
if (options.tests.length > 1) {
options.tests.forEach(function(test) {
chain = chain.then(() => {
return test(client);
});
});
}
// (test_call_end) Drop database after tests
chain
.catch(err => {
if (options.messages) {
console.error('Error: ' + err.message);
}
})
.then(() => {
client.end();
pgtools.dropdb(options.connection, options.testdb, callback);
});
} else {
pgtools.dropdb(options.connection, options.testdb, callback);
};
});
};