test: seed test DB with example

Only run if the environment defines a test database to connect to.
This commit is contained in:
Ryan Graham 2016-08-04 19:40:08 -07:00
parent ebb6e68f0e
commit acaa6b0bb9
No known key found for this signature in database
GPG Key ID: F15A82CDEFD85858
2 changed files with 29 additions and 0 deletions

View File

@ -4,6 +4,7 @@
"description": "MySQL connector for loopback-datasource-juggler", "description": "MySQL connector for loopback-datasource-juggler",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"pretest": "node pretest.js",
"test": "mocha" "test": "mocha"
}, },
"dependencies": { "dependencies": {

28
pretest.js Normal file
View File

@ -0,0 +1,28 @@
if (!process.env.TEST_MYSQL_USER) {
console.log('not seeding DB with example db');
return;
}
var fs = require('fs');
var cp = require('child_process');
var sql = fs.createReadStream(require.resolve('./example/table.sql'));
var stdio = ['pipe', process.stdout, process.stderr];
var args = ['--user=' + process.env.TEST_MYSQL_USER];
if (process.env.TEST_MYSQL_HOST) {
args.push('--host=' + process.env.TEST_MYSQL_HOST);
}
if (process.env.TEST_MYSQL_PORT) {
args.push('--port=' + process.env.TEST_MYSQL_PORT);
}
if (process.env.TEST_MYSQL_PASSWORD) {
args.push('--password=' + process.env.TEST_MYSQL_PASSWORD);
}
console.log('seeding DB with example db...');
var mysql = cp.spawn('mysql', args, {stdio: stdio});
sql.pipe(mysql.stdin);
mysql.on('exit', function() {
console.log('done seeding DB');
});