Skip to content

Commit

Permalink
d68a8bf: adding tests, error handling
Browse files Browse the repository at this point in the history
catdad/grandma#9 (Kiril Vatev)
  • Loading branch information
catdad authored and New Test User committed Apr 12, 2016
1 parent 858e86e commit cebbd67
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
21 changes: 19 additions & 2 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function Reporter() {
}

function readJsonLines(stream, onLine, done) {
var readLines = false;
readStream(stream, function eachLine(line) {
var jsonLine;

Expand All @@ -49,9 +50,21 @@ function Reporter() {
// skip this line
return;
}

readLines = true;

onLine(jsonLine);
}, done);
}, function(err) {
if (err) {
return done(err);
}

if (!readLines) {
return done(new Error('no data provided'));
}

done();
});
}

function jsonStats(data) {
Expand Down Expand Up @@ -143,7 +156,7 @@ function Reporter() {

// add headers
tableArr.push([
'Summary',
'Summary:',
'duration', 'rate', 'total'
]);
tableArr.push([
Expand Down Expand Up @@ -296,6 +309,10 @@ function Reporter() {
}

function validateOpts(opts) {
if (opts.type === undefined) {
opts.type = 'text';
}

if (!opts.input || !opts.input.pipe || !opts.input.readable) {
return new Error('no results input stream defined');
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"devDependencies": {
"chai": "^3.5.0",
"event-stream": "^3.3.2",
"istanbul": "^0.4.3",
"mocha": "^2.4.5"
},
Expand Down
53 changes: 49 additions & 4 deletions test/report.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
/* jshint node: true, mocha: true */
/* jshint node: true, mocha: true, expr: true */

var expect = require('chai').expect;
var through = require('through2');
var es = require('event-stream');

var TESTDATA = {};
var report = require('../lib/report.js');

var TESTDATA = [
{"type":"header","epoch":1460127721611,"duration":30000,"rate":20,"targetCount":600},
{"type":"report","report":{"fullTest":{"start":0,"end":19.801774,"duration":19.801774,"status":"success"},"one":{"start":0.14821399999999585,"end":2.897864999999996,"duration":2.749651,"status":"success"},"two":{"start":0.45399899999999604,"end":6.853753000000005,"duration":6.399754000000009,"status":"success"}},"id":0},
{"type":"report","report":{"fullTest":{"start":47.191123999999995,"end":61.882996999999996,"duration":14.691873000000001,"status":"success"},"one":{"start":47.213159999999995,"end":49.951642,"duration":2.7384820000000047,"status":"success"},"two":{"start":47.56996,"end":51.722057,"duration":4.152096999999998,"status":"success"}},"id":0},
{"type":"report","report":{"fullTest":{"start":97.46002200000001,"end":111.861504,"duration":14.401481999999987,"status":"success"},"one":{"start":97.46877600000002,"end":99.933471,"duration":2.4646949999999777,"status":"success"},"two":{"start":97.493529,"end":101.74916300000001,"duration":4.255634000000015,"status":"success"}},"id":0},
];

describe('[report]', function() {
it('takes an input and output stream');
it('reads data from the input stream');
it('takes an input and output stream', function(done) {
var input = through();
var output = through();

report({
input: input,
output: output
}, function(err) {
expect(err).to.be.instanceof(Error);
expect(err).to.have.property('message').and.to.equal('no data provided');

done();
});

input.end();
});
it('reads data from the input stream', function(done) {
var input = through();
var output = through();

report({
input: input,
output: output
}, function(err) {
expect(err).to.not.be.ok;

done();
});

output.pipe(es.wait(function(err, content) {
expect(err).to.not.be.ok;

expect(content).to.be.ok;
expect(content.toString()).to.match(/Summary:/);
expect(content.toString()).to.match(/Latencies:/);
}));

input.end(TESTDATA.map(JSON.stringify).join('\n'));
});
it('writes results to the output stream');

describe('#json', function() {
Expand Down

0 comments on commit cebbd67

Please sign in to comment.