Skip to content

Commit

Permalink
Merge pull request #21 from jimmiehansson/bugfix/ISSUE-19-empty-value…
Browse files Browse the repository at this point in the history
…-time

ISSUE-19 Check value before setting date object
  • Loading branch information
mcollina committed Oct 23, 2018
2 parents 3f61968 + 96d0bfa commit 944daef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
10 changes: 8 additions & 2 deletions pino-elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ function pinoElasticSearch (opts) {
if (typeof value === 'string') {
value = {
data: value,
time: (new Date()).toISOString()
time: setDateTimeString(value)
}
} else {
value.time = (new Date(value.time)).toISOString()
value.time = setDateTimeString(value)
}

function setDateTimeString (value) {
if (typeof value === 'object' && value.hasOwnProperty('time')) {
return (value.time.length > 0) ? new Date(value.time).toISOString() : new Date().toISOString()
}
return new Date().toISOString()
}
return value
})
const client = new elasticsearch.Client({
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'
const ctx = module.exports

ctx.datetime = {
object: new Date('2018-10-22T18:09:48.110Z').toISOString(),
string: '2018-10-22T18:09:48.110Z'
}
module.exports = ctx
15 changes: 12 additions & 3 deletions test/unit.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
'use strict'

const pino = require('pino')

const proxyquire = require('proxyquire')

const test = require('tap').test
const fix = require('./fixtures')

const matchISOString = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/
const options = {
index: 'pinotest',
type: 'log',
consistency: 'one',
host: 'localhost',
port: 9200
}

test('make sure date format is valid', (t) => {
t.type(fix.datetime.object, 'string')
t.equal(fix.datetime.object, fix.datetime.string)
t.end()
})
test('make sure log is a valid json', (t) => {
t.plan(2)
t.plan(4)
const Client = function (config) {
t.equal(config.host, `${options.host}:${options.port}`)
}
Client.prototype.index = (obj, cb) => {
t.ok(obj, true)
t.type(obj.body.time, 'string')
t.match(obj.body.time, matchISOString)
cb(null, {})
}
const elastic = proxyquire('../', {
Expand Down

0 comments on commit 944daef

Please sign in to comment.