Skip to content

Commit

Permalink
[test] improve query tests. fix rows option for File transport.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed May 15, 2012
1 parent 3bec167 commit 0ccbc4c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
10 changes: 8 additions & 2 deletions lib/winston/transports/file.js
Expand Up @@ -213,7 +213,12 @@ File.prototype.query = function (options, callback) {

function push(log) {
if (options.rows && results.length >= options.rows) {
return stream.destroy();
try {
stream.destroy();
} catch (e) {
;
}
return;
}

if (options.fields) {
Expand All @@ -233,7 +238,8 @@ File.prototype.query = function (options, callback) {
if (typeof log !== 'object') return;

var time = new Date(log.timestamp);
if (time < options.from || time > options.until) {
if ((options.from && time < options.from)
|| (options.until && time > options.until)) {
return;
}

Expand Down
29 changes: 21 additions & 8 deletions test/transports/transport.js
Expand Up @@ -11,6 +11,13 @@ module.exports = function (transport, options) {
]
});

// hack to fix transports that don't log
// any unit of time smaller than seconds
var common = require('../../lib/winston/common');
common.timestamp = function() {
return new Date().toISOString();
};

var transport = logger.transports[logger._names[0]];

var out = {
Expand Down Expand Up @@ -46,7 +53,7 @@ module.exports = function (transport, options) {
if (!transport.query) return;
var cb = this.callback;
logger.log('info', 'hello world', {}, function () {
logger.query({}, cb);
logger.query(cb);
});
},
'should return matching results': function (err, results) {
Expand Down Expand Up @@ -74,26 +81,32 @@ module.exports = function (transport, options) {
while (!Array.isArray(results)) {
results = results[Object.keys(results).pop()];
}
//assert.equal(results.length, 1);
assert.equal(results.length, 1);
}
},
'using fields and order option': {
'topic': function (logger) {
if (!transport.query) return;
var cb = this.callback;
logger.log('info', 'hello world', {}, function () {
logger.query({ order: 'asc', fields: ['timestamp'] }, cb);
});
// wait a second to increase timestamp
// some transports may not be logging
// a timestamp in smaller units of time
// than seconds.
setTimeout(function () {
logger.log('info', 'hello world', {}, function () {
logger.query({ order: 'asc', fields: ['timestamp'] }, cb);
});
}, 2000);
},
'should return matching results': function (err, results) {
if (!transport.query) return;
results = results[transport.name];
while (!Array.isArray(results)) {
results = results[Object.keys(results).pop()];
}
//assert.equal(Object.keys(results[0]).length, 1);
//assert.ok(new Date(results.shift().timestamp)
// < new Date(results.pop().timestamp));
assert.equal(Object.keys(results[0]).length, 1);
assert.ok(new Date(results.shift().timestamp)
< new Date(results.pop().timestamp));
}
}
},
Expand Down

0 comments on commit 0ccbc4c

Please sign in to comment.