|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var expect = require('chai').expect |
| 4 | +var wrap = require('./pg') |
| 5 | +var Shimmer = require('../utils/shimmer') |
| 6 | + |
| 7 | +function fakeAgent (sandbox) { |
| 8 | + return { |
| 9 | + generateSpanId: function () { return 'fakeSpanId' }, |
| 10 | + getMicrotime: function () { return 42 }, |
| 11 | + getTransactionId: function () { return 'fakeTransactionId' }, |
| 12 | + clientSend: sandbox.spy(), |
| 13 | + clientReceive: sandbox.spy(), |
| 14 | + CLIENT_SEND: 'fakeSend' |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe('pg module wrapper', function () { |
| 19 | + beforeEach(function () { |
| 20 | + Shimmer.unwrapAll() |
| 21 | + }) |
| 22 | + |
| 23 | + it('should instrument JS query', function (done) { |
| 24 | + var agent = fakeAgent(this.sandbox) |
| 25 | + var pg = wrap(require('pg'), agent) |
| 26 | + |
| 27 | + var conString = 'postgres://localhost/postgres' |
| 28 | + var qryString = 'SELECT 1 AS "one"' |
| 29 | + |
| 30 | + var client = new pg.Client(conString) |
| 31 | + client.connect(function (err) { |
| 32 | + if (err) { |
| 33 | + console.error(err) |
| 34 | + throw err |
| 35 | + } |
| 36 | + client.query(qryString, function (err, result) { |
| 37 | + if (err) { |
| 38 | + console.error(err) |
| 39 | + throw err |
| 40 | + } |
| 41 | + expect(result.rows[0].one).to.eql(1) |
| 42 | + expect(agent.clientReceive).to.have.been.calledWith({ |
| 43 | + host: 'localhost', |
| 44 | + id: 'fakeTransactionId', |
| 45 | + method: 'SELECT', |
| 46 | + mustCollect: undefined, |
| 47 | + protocol: 'pg', |
| 48 | + responseTime: 0, |
| 49 | + spanId: 'fakeSpanId', |
| 50 | + status: 0, |
| 51 | + statusCode: 200, |
| 52 | + time: 42, |
| 53 | + url: 'postgres' |
| 54 | + }) |
| 55 | + client.end() |
| 56 | + done() |
| 57 | + }) |
| 58 | + |
| 59 | + expect(agent.clientSend).to.have.been.called |
| 60 | + expect(agent.clientSend).to.have.been.calledWith({ |
| 61 | + id: 'fakeTransactionId', |
| 62 | + spanId: 'fakeSpanId', |
| 63 | + host: 'localhost', |
| 64 | + time: 42, |
| 65 | + method: 'SELECT', |
| 66 | + type: 'fakeSend', |
| 67 | + url: 'postgres' |
| 68 | + }) |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should instrument JS query w/o callback', function (done) { |
| 73 | + var agent = fakeAgent(this.sandbox) |
| 74 | + agent.clientReceive = this.sandbox.spy(function (options) { |
| 75 | + done() |
| 76 | + }) |
| 77 | + var pg = wrap(require('pg'), agent) |
| 78 | + |
| 79 | + var conString = 'postgres://localhost/postgres' |
| 80 | + var qryString = 'SELECT 1 AS "one"' |
| 81 | + |
| 82 | + var client = new pg.Client(conString) |
| 83 | + client.connect(function (err) { |
| 84 | + if (err) { |
| 85 | + console.error(err) |
| 86 | + throw err |
| 87 | + } |
| 88 | + client.query(qryString) |
| 89 | + |
| 90 | + expect(agent.clientSend).to.have.been.called |
| 91 | + expect(agent.clientSend).to.have.been.calledWith({ |
| 92 | + id: 'fakeTransactionId', |
| 93 | + spanId: 'fakeSpanId', |
| 94 | + host: 'localhost', |
| 95 | + time: 42, |
| 96 | + method: 'SELECT', |
| 97 | + type: 'fakeSend', |
| 98 | + url: 'postgres' |
| 99 | + }) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + it('should instrument native query', function (done) { |
| 104 | + var agent = fakeAgent(this.sandbox) |
| 105 | + var pkg = require('pg/package.json') |
| 106 | + var pg = wrap(require('pg'), agent, pkg).native |
| 107 | + |
| 108 | + var conString = 'postgres://localhost/postgres' |
| 109 | + var qryString = 'SELECT 1 AS "one"' |
| 110 | + |
| 111 | + var client = new pg.Client(conString) |
| 112 | + client.connect(function (err) { |
| 113 | + if (err) { |
| 114 | + console.error(err) |
| 115 | + throw err |
| 116 | + } |
| 117 | + client.query(qryString, function (err, result) { |
| 118 | + if (err) { |
| 119 | + console.error(err) |
| 120 | + throw err |
| 121 | + } |
| 122 | + expect(result.rows[0].one).to.eql(1) |
| 123 | + expect(agent.clientReceive).to.have.been.calledWith({ |
| 124 | + host: 'localhost', |
| 125 | + id: 'fakeTransactionId', |
| 126 | + method: 'SELECT', |
| 127 | + mustCollect: undefined, |
| 128 | + protocol: 'pg', |
| 129 | + responseTime: 0, |
| 130 | + spanId: 'fakeSpanId', |
| 131 | + status: 0, |
| 132 | + statusCode: 200, |
| 133 | + time: 42, |
| 134 | + url: 'postgres' |
| 135 | + }) |
| 136 | + client.end() |
| 137 | + done() |
| 138 | + }) |
| 139 | + |
| 140 | + expect(agent.clientSend).to.have.been.called |
| 141 | + expect(agent.clientSend).to.have.been.calledWith({ |
| 142 | + id: 'fakeTransactionId', |
| 143 | + spanId: 'fakeSpanId', |
| 144 | + host: 'localhost', |
| 145 | + time: 42, |
| 146 | + method: 'SELECT', |
| 147 | + type: 'fakeSend', |
| 148 | + url: 'postgres' |
| 149 | + }) |
| 150 | + }) |
| 151 | + }) |
| 152 | +}) |
0 commit comments