Skip to content

Commit c0d0fe4

Browse files
committed
add subset.js; add tests
1 parent 79fdbba commit c0d0fe4

File tree

8 files changed

+130
-31
lines changed

8 files changed

+130
-31
lines changed

hapi-server

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# https://unix.stackexchange.com/a/76518
44
path=$(exec 2>/dev/null;cd -- $(dirname "$0"); unset PWD; /usr/bin/pwd || /bin/pwd || pwd)
55

6-
#echo $(type $path"/bin/node")
7-
86
if command -v $path"/bin/node" > /dev/null 2>&1; then
97
$path"/bin/node" $path"/server.js" $@
108
elif command -v node > /dev/null 2>&1; then

lib/subset.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var fs = require('fs');
2+
var http = require('http');
3+
var readline = require('readline');
4+
var yargs = require('yargs');
5+
6+
let argv = yargs
7+
.default({
8+
'columns': '',
9+
'start': '0001-01-01T00:00:00.000000000Z',
10+
'stop': '9999-12-31T23:59:59.999999999Z',
11+
'file': '',
12+
'url': '',
13+
'format': 'csv'
14+
})
15+
.option('columns', {type:'string'})
16+
.option('start', {type:'string'})
17+
.option('stop', {type:'string'})
18+
.argv;
19+
20+
function range(start, end) {
21+
var arr = [];
22+
for (var i = start; i <= end; i += 1) {
23+
arr.push(i);
24+
}
25+
return arr;
26+
}
27+
28+
let columnsarr = [];
29+
if (argv.columns !== '') {
30+
// Convert, e.g, "1,2,4-6" to [1,2,4,5,6] - 1
31+
let tmp = argv.columns.split(",");
32+
for (i = 0; i < tmp.length; i++) {
33+
if (/-/.test(tmp[i])) {
34+
tmp2 = tmp[i].split("-");
35+
let expanded = range(parseInt(tmp2[0])-1,parseInt(tmp2[1])-1);
36+
columnsarr = columnsarr.concat(expanded);
37+
} else {
38+
columnsarr.push(tmp[i]-1);
39+
}
40+
}
41+
} else {
42+
columnsarr = null;
43+
}
44+
45+
if (argv.file !== '') {
46+
if (argv.format !== 'csv') {
47+
console.log('Only --format=csv is implemented');
48+
process.exit(1);
49+
}
50+
readlines(fs.createReadStream(argv.file));
51+
} else if (argv.url !== '') {
52+
http.get(argv.url, res => readlines(res));
53+
} else {
54+
readlines(process.stdin);
55+
}
56+
57+
function readlines(readableStream) {
58+
var rl = readline.createInterface({
59+
input: readableStream,
60+
output: process.stdout,
61+
terminal: false
62+
})
63+
.on('line', function(line){
64+
let linea = line.split(',');
65+
let time = linea[0];
66+
let l = time.length;
67+
let m = Math.min(l,argv.start.length,argv.stop.length);
68+
let start = argv.start.slice(0,m);
69+
let stop = argv.stop.slice(0,m);
70+
time = time.slice(0,m);
71+
if (time >= start && time < stop) {
72+
if (columnsarr == null) {
73+
console.log(line);
74+
} else {
75+
line = "";
76+
for (let i = 0;i < columnsarr.length;i++) {
77+
line = line + linea[columnsarr[i]] + ",";
78+
}
79+
console.log(line.slice(0,-1));
80+
}
81+
}
82+
if (time >= stop) {
83+
process.exit(0);
84+
}
85+
})
86+
}

lib/test.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const clc = require('chalk');
44
// Date string for logging.
55
function ds() {return (new Date()).toISOString() + " [server] ";};
66

7-
function commands(commandarr,catalog,force) {
7+
function commands(commandarr,catalog) {
88
for (var i = 0;i < commandarr.length;i++) {
99
command = commandarr[i].command;
1010
// TODO: Substitute paths.
@@ -107,29 +107,28 @@ function testoutput(testobj,body) {
107107
type = "URL";
108108
teststr = " for test URL number " + tn;
109109
}
110-
if ("length" in testobj) {
111-
if (body.length != testobj["length"]) {
110+
111+
function compare(n1,n2,s) {
112+
if (n1 != n2) {
112113
msg = testobj.catalog + " test command " + tn + ": ";
113-
msg = body.length + " bytes found but expected " + testobj["length"] + teststr;
114-
console.log(ds() + clc.red(msg + ". Output tested:"));
114+
msg = n1 + " " + s + " found but expected " + n2 + teststr;
115+
msg = msg + ".\nTest: " + (testobj.command || testobj.url);
116+
console.log(ds() + clc.red(msg + "\nTest output:"));
115117
process.stdout.write(body);
116118
console.log(clc.red("Exiting."));
117119
process.exit(1);
118120
} else {
119-
console.log(ds() + testobj.catalog + " length test on " + type + " " + tn + " passed.");
121+
console.log(ds() + testobj.catalog + " " + s + " test on " + type + " " + tn + " passed.");
120122
}
121123
}
124+
125+
if ("length" in testobj) {
126+
compare(body.length,testobj.length,'bytes');
127+
}
122128
if ("Nlines" in testobj) {
123-
nnl = body.split("\n").length - 1; // Number of newlines
124-
if (nnl != testobj["Nlines"]) {
125-
msg = testobj.catalog + " test command " + tn + ": ";
126-
msg = nnl + " newlines found but expected " + testobj["Nlines"] + teststr;
127-
console.log(ds() + clc.red(msg + ". Output tested:"));
128-
process.stdout.write(body);
129-
console.log(clc.red("Exiting."));
130-
process.exit(1);
131-
} else {
132-
console.log(ds() + testobj.catalog + " # lines test on " + type + " " + tn + " passed.");
133-
}
129+
compare(body.split("\n").length - 1,testobj.Nlines,'# lines');
130+
}
131+
if ("Ncommas" in testobj) {
132+
compare((body.match(/,/g) || []).length,testobj.Ncommas,'# commas');
134133
}
135134
}

metadata/Example8.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"stopDate" : "1970-01-01T01:00:00Z",
2020
"sampleStartDate": "1970-01-01Z",
2121
"sampleStopDate" : "1970-01-01T00:10:00Z",
22-
"cadence": "PT1M",
22+
"cadence": "PT1S",
2323
"parameters":
2424
[
2525
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"scripts": {
2929
"clean": "rm -rf node_modules/; rm -rf public/data/QinDenton/",
30-
"test": "node test/test.js",
30+
"test": "node test/server-test.js; node test/subset-test.js",
3131
"start": "node server.js --port 8999",
3232
"release": "cd pkg; make release VERSION=v$npm_package_version"
3333
},

server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var argv = yargs
5555
.alias('ignore','i')
5656
.describe('open','Open web page on start')
5757
.alias('open','o')
58-
.describe('test','Exit after test URL tests complete')
58+
.describe('test','Exit after URL tests complete')
5959
.alias('test','t')
6060
.describe('verify','Run verification tests')
6161
.alias('verify','v')
@@ -540,7 +540,8 @@ function data(req,res,catalog,header,include) {
540540
subsettime = true;
541541
}
542542
if (subsetcols || subsettime) {
543-
com = com + " | python lib/subset.py";
543+
//com = com + " | " + config.PYTHON_EXE + " " + __dirname + "/lib/subset.py";
544+
com = com + " | " + config.NODE_EXE + " " + __dirname + "/lib/subset.js";
544545
if (subsettime) {
545546
com = com + " --start " + start;
546547
com = com + " --stop " + stop;

test/test.js renamed to test/server-test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ const excludes =
1212
"TestDataBad"
1313
];
1414

15-
var env = process.env;
1615
var exe = process.execPath + " server.js";
17-
if (/server$/.test(process.execPath)) {
18-
//env['PKG_EXECPATH'] = 'PKG_INVOKE_NODEJS';
19-
//var exe = process.execPath;
20-
}
2116

2217
const metadir = __dirname + '/../metadata';
2318
files = [];
@@ -33,13 +28,14 @@ var fails = 0;
3328
for (var i = 0; i < files.length; i++) {
3429
var com = exe + " --test --ignore -f " + metadir + "/" + files[i];
3530
process.stdout.write(clc.blue("Testing: ") + com);
36-
var child = spawnSync('bash', ['-c', com], {stdio: 'pipe', env: env});
31+
var child = spawnSync('bash', ['-c', com], {stdio: 'pipe'});
3732
if (child.status == 0) {
3833
console.log(clc.green(" PASS"));
3934
} else {
4035
fails = fails + 1;
41-
console.log(child.stdout.toString());
42-
console.log(child.stderr.toString());
36+
console.log(clc.red(" FAIL"));
37+
console.log("\n" + child.stdout.toString());
38+
console.log("\n" + child.stderr.toString());
4339
}
4440
}
4541
if (fails == 0) {

test/subset-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var test = require('../lib/test.js');
2+
var testa = [
3+
{
4+
"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",
5+
"Nlines": "7",
6+
"Ncommas": "7"
7+
},
8+
{
9+
"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",
10+
"Nlines": "7",
11+
"Ncommas": "0"
12+
},
13+
{
14+
"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",
15+
"Nlines": "7",
16+
"Ncommas": "7"
17+
}
18+
]
19+
test.commands(testa,"subset.js");

0 commit comments

Comments
 (0)