Skip to content

Commit

Permalink
add subset.js; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Weigel committed Jan 13, 2019
1 parent 79fdbba commit c0d0fe4
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 31 deletions.
2 changes: 0 additions & 2 deletions hapi-server
Expand Up @@ -3,8 +3,6 @@
# https://unix.stackexchange.com/a/76518
path=$(exec 2>/dev/null;cd -- $(dirname "$0"); unset PWD; /usr/bin/pwd || /bin/pwd || pwd)

#echo $(type $path"/bin/node")

if command -v $path"/bin/node" > /dev/null 2>&1; then
$path"/bin/node" $path"/server.js" $@
elif command -v node > /dev/null 2>&1; then
Expand Down
86 changes: 86 additions & 0 deletions lib/subset.js
@@ -0,0 +1,86 @@
var fs = require('fs');
var http = require('http');
var readline = require('readline');
var yargs = require('yargs');

let argv = yargs
.default({
'columns': '',
'start': '0001-01-01T00:00:00.000000000Z',
'stop': '9999-12-31T23:59:59.999999999Z',
'file': '',
'url': '',
'format': 'csv'
})
.option('columns', {type:'string'})
.option('start', {type:'string'})
.option('stop', {type:'string'})
.argv;

function range(start, end) {
var arr = [];
for (var i = start; i <= end; i += 1) {
arr.push(i);
}
return arr;
}

let columnsarr = [];
if (argv.columns !== '') {
// Convert, e.g, "1,2,4-6" to [1,2,4,5,6] - 1
let tmp = argv.columns.split(",");
for (i = 0; i < tmp.length; i++) {
if (/-/.test(tmp[i])) {
tmp2 = tmp[i].split("-");
let expanded = range(parseInt(tmp2[0])-1,parseInt(tmp2[1])-1);
columnsarr = columnsarr.concat(expanded);
} else {
columnsarr.push(tmp[i]-1);
}
}
} else {
columnsarr = null;
}

if (argv.file !== '') {
if (argv.format !== 'csv') {
console.log('Only --format=csv is implemented');
process.exit(1);
}
readlines(fs.createReadStream(argv.file));
} else if (argv.url !== '') {
http.get(argv.url, res => readlines(res));
} else {
readlines(process.stdin);
}

function readlines(readableStream) {
var rl = readline.createInterface({
input: readableStream,
output: process.stdout,
terminal: false
})
.on('line', function(line){
let linea = line.split(',');
let time = linea[0];
let l = time.length;
let m = Math.min(l,argv.start.length,argv.stop.length);
let start = argv.start.slice(0,m);
let stop = argv.stop.slice(0,m);
time = time.slice(0,m);
if (time >= start && time < stop) {
if (columnsarr == null) {
console.log(line);
} else {
line = "";
for (let i = 0;i < columnsarr.length;i++) {
line = line + linea[columnsarr[i]] + ",";
}
console.log(line.slice(0,-1));
}
}
if (time >= stop) {
process.exit(0);
}
})
}
33 changes: 16 additions & 17 deletions lib/test.js
Expand Up @@ -4,7 +4,7 @@ const clc = require('chalk');
// Date string for logging.
function ds() {return (new Date()).toISOString() + " [server] ";};

function commands(commandarr,catalog,force) {
function commands(commandarr,catalog) {
for (var i = 0;i < commandarr.length;i++) {
command = commandarr[i].command;
// TODO: Substitute paths.
Expand Down Expand Up @@ -107,29 +107,28 @@ function testoutput(testobj,body) {
type = "URL";
teststr = " for test URL number " + tn;
}
if ("length" in testobj) {
if (body.length != testobj["length"]) {

function compare(n1,n2,s) {
if (n1 != n2) {
msg = testobj.catalog + " test command " + tn + ": ";
msg = body.length + " bytes found but expected " + testobj["length"] + teststr;
console.log(ds() + clc.red(msg + ". Output tested:"));
msg = n1 + " " + s + " found but expected " + n2 + teststr;
msg = msg + ".\nTest: " + (testobj.command || testobj.url);
console.log(ds() + clc.red(msg + "\nTest output:"));
process.stdout.write(body);
console.log(clc.red("Exiting."));
process.exit(1);
} else {
console.log(ds() + testobj.catalog + " length test on " + type + " " + tn + " passed.");
console.log(ds() + testobj.catalog + " " + s + " test on " + type + " " + tn + " passed.");
}
}

if ("length" in testobj) {
compare(body.length,testobj.length,'bytes');
}
if ("Nlines" in testobj) {
nnl = body.split("\n").length - 1; // Number of newlines
if (nnl != testobj["Nlines"]) {
msg = testobj.catalog + " test command " + tn + ": ";
msg = nnl + " newlines found but expected " + testobj["Nlines"] + teststr;
console.log(ds() + clc.red(msg + ". Output tested:"));
process.stdout.write(body);
console.log(clc.red("Exiting."));
process.exit(1);
} else {
console.log(ds() + testobj.catalog + " # lines test on " + type + " " + tn + " passed.");
}
compare(body.split("\n").length - 1,testobj.Nlines,'# lines');
}
if ("Ncommas" in testobj) {
compare((body.match(/,/g) || []).length,testobj.Ncommas,'# commas');
}
}
2 changes: 1 addition & 1 deletion metadata/Example8.json
Expand Up @@ -19,7 +19,7 @@
"stopDate" : "1970-01-01T01:00:00Z",
"sampleStartDate": "1970-01-01Z",
"sampleStopDate" : "1970-01-01T00:10:00Z",
"cadence": "PT1M",
"cadence": "PT1S",
"parameters":
[
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
},
"scripts": {
"clean": "rm -rf node_modules/; rm -rf public/data/QinDenton/",
"test": "node test/test.js",
"test": "node test/server-test.js; node test/subset-test.js",
"start": "node server.js --port 8999",
"release": "cd pkg; make release VERSION=v$npm_package_version"
},
Expand Down
5 changes: 3 additions & 2 deletions server.js
Expand Up @@ -55,7 +55,7 @@ var argv = yargs
.alias('ignore','i')
.describe('open','Open web page on start')
.alias('open','o')
.describe('test','Exit after test URL tests complete')
.describe('test','Exit after URL tests complete')
.alias('test','t')
.describe('verify','Run verification tests')
.alias('verify','v')
Expand Down Expand Up @@ -540,7 +540,8 @@ function data(req,res,catalog,header,include) {
subsettime = true;
}
if (subsetcols || subsettime) {
com = com + " | python lib/subset.py";
//com = com + " | " + config.PYTHON_EXE + " " + __dirname + "/lib/subset.py";
com = com + " | " + config.NODE_EXE + " " + __dirname + "/lib/subset.js";
if (subsettime) {
com = com + " --start " + start;
com = com + " --stop " + stop;
Expand Down
12 changes: 4 additions & 8 deletions test/test.js → test/server-test.js
Expand Up @@ -12,12 +12,7 @@ const excludes =
"TestDataBad"
];

var env = process.env;
var exe = process.execPath + " server.js";
if (/server$/.test(process.execPath)) {
//env['PKG_EXECPATH'] = 'PKG_INVOKE_NODEJS';
//var exe = process.execPath;
}

const metadir = __dirname + '/../metadata';
files = [];
Expand All @@ -33,13 +28,14 @@ var fails = 0;
for (var i = 0; i < files.length; i++) {
var com = exe + " --test --ignore -f " + metadir + "/" + files[i];
process.stdout.write(clc.blue("Testing: ") + com);
var child = spawnSync('bash', ['-c', com], {stdio: 'pipe', env: env});
var child = spawnSync('bash', ['-c', com], {stdio: 'pipe'});
if (child.status == 0) {
console.log(clc.green(" PASS"));
} else {
fails = fails + 1;
console.log(child.stdout.toString());
console.log(child.stderr.toString());
console.log(clc.red(" FAIL"));
console.log("\n" + child.stdout.toString());
console.log("\n" + child.stderr.toString());
}
}
if (fails == 0) {
Expand Down
19 changes: 19 additions & 0 deletions test/subset-test.js
@@ -0,0 +1,19 @@
var test = require('../lib/test.js');
var testa = [
{
"command": "node lib/subset.js --url 'http://hapi-server.org/servers/TestData/hapi/data?id=dataset1&parameters=scalar&time.min=1970-01-01Z&time.max=1970-01-01T00:00:11Z&attach=false' --stop 1970-01-01T00:00:07",
"Nlines": "7",
"Ncommas": "7"
},
{
"command": "node lib/subset.js --columns 1 --url 'http://hapi-server.org/servers/TestData/hapi/data?id=dataset1&parameters=scalar&time.min=1970-01-01Z&time.max=1970-01-01T00:00:11Z&attach=false' --stop 1970-01-01T00:00:07",
"Nlines": "7",
"Ncommas": "0"
},
{
"command": "node lib/subset.js --columns 1-2 --url 'http://hapi-server.org/servers/TestData/hapi/data?id=dataset1&parameters=scalar&time.min=1970-01-01Z&time.max=1970-01-01T00:00:11Z&attach=false' --stop 1970-01-01T00:00:07",
"Nlines": "7",
"Ncommas": "7"
}
]
test.commands(testa,"subset.js");

0 comments on commit c0d0fe4

Please sign in to comment.