Skip to content

Commit

Permalink
test(pg): cover with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Aug 23, 2017
1 parent a8e2d7a commit 8a110ac
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ cache:
- node_modules
notifications:
email: false
services:
- postgresql
node_js:
- 'stable'
- '8'
env:
- PG_URI=postgres://postgres:@localhost:5432/travis_ci_test
before_script:
- psql -c 'create database travis_ci_test;' -U postgres
- npm prune
branches:
except:
Expand Down
1 change: 1 addition & 0 deletions src/instrumentation/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
module: 'pg',
supportedVersions: ['6.x'],
OPERATION_NAME,
DB_TYPE,
patch,
unpatch
}
68 changes: 68 additions & 0 deletions src/instrumentation/pg.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict'

const pg = require('pg')
const knex = require('knex')
const { expect } = require('chai')
const { Tracer, Tags } = require('opentracing')
const cls = require('../cls')
const instrumentation = require('./pg')

describe('instrumentation: pg', () => {
let tracer
let mockChildSpan
let db

beforeEach(function () {
tracer = new Tracer()
mockChildSpan = {
setTag: this.sandbox.spy(),
log: this.sandbox.spy(),
finish: this.sandbox.spy()
}

this.sandbox.stub(cls, 'startChildSpan').callsFake(() => mockChildSpan)

instrumentation.patch(pg, tracer)

db = knex({
client: 'pg',
connection: process.env.PG_URI
})
})

afterEach(() => {
instrumentation.unpatch(pg)
})

describe('#patch', () => {
it('should start and finish span', async () => {
const query = 'SELECT 1 AS result'
const { rows } = await db.raw(query)

expect(rows).to.be.eql([{ result: 1 }])

expect(cls.startChildSpan).to.be.calledWith(tracer, `${instrumentation.OPERATION_NAME}_query`)
expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_TYPE, instrumentation.DB_TYPE)
expect(mockChildSpan.setTag).to.have.calledWith(Tags.DB_STATEMENT, query)
})

it('should flag error', async () => {
const query = 'SELECT invalid AS result'

try {
await db.raw(query)
} catch (err) {
expect(mockChildSpan.setTag).to.be.calledWith(Tags.ERROR, true)
expect(mockChildSpan.log).to.be.calledWith({
event: 'error',
'error.object': err,
message: 'column "invalid" does not exist',
stack: err.stack
})
return
}

throw new Error('Uncaught exception')
})
})
})
6 changes: 6 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const sinon = require('sinon')
const chai = require('chai')
const sinonChai = require('sinon-chai')

// postgres
const pgUser = process.env.PG_USER || process.env.USER || 'root'
const pgPw = process.env.PG_PASSWORD || ''
const pgDB = process.env.PG_DATABASE || 'test_jaeger'
process.env.PG_URI = process.env.PG_URI || `postgres://${pgUser}:${pgPw}@localhost:5432/${pgDB}`

before(() => {
chai.use(sinonChai)
})
Expand Down

0 comments on commit 8a110ac

Please sign in to comment.