Skip to content

Commit

Permalink
adding tests, documentation + detach support.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Nov 22, 2015
1 parent 2105971 commit 71db491
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 34 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Adds event handlers to object `options` that's used during [pg-promise initializ
var pgp = pgpLib(options);
```

Calling second time (without calling [detach] first) will throw `Repeated attachments not supported, must call detach first.`

#### [events]

Optional array of event names to which to attach. Passing `null`/`undefined` will attach
Expand Down Expand Up @@ -127,6 +129,12 @@ Example of overriding all known event handlers:
monitor.attach(options, null, true);
```

## detach()

Detaches from all the events to which attached after the last successful call to [attach].

Calling it while not attached will throw `Event monitor not attached.`

## connect(client, [detailed])

Monitors and reports event [connect](https://github.com/vitaly-t/pg-promise#connect).
Expand Down Expand Up @@ -305,3 +313,5 @@ that returns the tag name.
[pg-promise]:https://github.com/vitaly-t/pg-promise
[monitor.detailed]:https://github.com/vitaly-t/pg-monitor#detailed-4
[Client]:https://github.com/brianc/node-postgres/blob/master/lib/client.js#L12
[attach]:https://github.com/vitaly-t/pg-monitor#attachoptions-events-override
[detach]:https://github.com/vitaly-t/pg-monitor#detach
52 changes: 40 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ var themes = require("./themes");

var cct = themes.dimmed; // current/default color theme;

// monitor state;
var $state = {};

// supported events;
var $events = ['connect', 'disconnect', 'query', 'error', 'task', 'transact'];

var monitor = {

///////////////////////////////////////////////
Expand Down Expand Up @@ -197,6 +203,10 @@ var monitor = {
// - override - optional, overrides the existing event handlers;
attach: function (options, events, override) {

if ($state.options) {
throw new TypeError("Repeated attachments not supported, must call detach first.");
}

if (!options || typeof options !== 'object') {
throw new TypeError("Initialization object 'options' must be specified.");
}
Expand All @@ -207,14 +217,16 @@ var monitor = {
throw new TypeError("Invalid parameter 'events' passed.");
}

$state.options = options;

var self = this;

// attaching to 'connect' event:
if (!hasFilter || events.indexOf('connect') !== -1) {
$state.connect = options.connect;
if (options.connect instanceof Function && !override) {
var cn = options.connect;
options.connect = function (client) {
cn(client); // call the original handler;
$state.connect(client); // call the original handler;
self.connect(client);
};
} else {
Expand All @@ -224,10 +236,10 @@ var monitor = {

// attaching to 'disconnect' event:
if (!hasFilter || events.indexOf('disconnect') !== -1) {
$state.disconnect = options.disconnect;
if (options.disconnect instanceof Function && !override) {
var dis = options.disconnect;
options.disconnect = function (client) {
dis(client); // call the original handler;
$state.disconnect(client); // call the original handler;
self.disconnect(client);
};
} else {
Expand All @@ -237,10 +249,10 @@ var monitor = {

// attaching to 'query' event:
if (!hasFilter || events.indexOf('query') !== -1) {
$state.query = options.query;
if (options.query instanceof Function && !override) {
var q = options.query;
options.query = function (e) {
q(e); // call the original handler;
$state.query(e); // call the original handler;
self.query(e);
};
} else {
Expand All @@ -250,10 +262,10 @@ var monitor = {

// attaching to 'task' event:
if (!hasFilter || events.indexOf('task') !== -1) {
$state.task = options.task;
if (options.task instanceof Function && !override) {
var task = options.task;
options.task = function (e) {
task(e); // call the original handler;
$state.task(e); // call the original handler;
self.task(e);
};
} else {
Expand All @@ -263,10 +275,10 @@ var monitor = {

// attaching to 'transact' event:
if (!hasFilter || events.indexOf('transact') !== -1) {
$state.transact = options.transact;
if (options.transact instanceof Function && !override) {
var tx = options.transact;
options.transact = function (e) {
tx(e); // call the original handler;
$state.transact(e); // call the original handler;
self.transact(e);
};
} else {
Expand All @@ -276,10 +288,10 @@ var monitor = {

// attaching to 'error' event:
if (!hasFilter || events.indexOf('error') !== -1) {
$state.error = options.error;
if (options.error instanceof Function && !override) {
var er = options.error;
options.error = function (err, e) {
er(err, e); // call the original handler;
$state.error(err, e); // call the original handler;
self.error(err, e);
};
} else {
Expand All @@ -288,6 +300,22 @@ var monitor = {
}
},

/////////////////////////////////////////////////////////
// detaches from all events to which was attached during
// the last `attach` call.
detach: function () {
if (!$state.options) {
throw new Error("Event monitor not attached.");
}
$events.forEach(function (e) {
if (e in $state) {
$state.options[e] = $state[e];
delete $state[e];
}
});
$state.options = null;
},

//////////////////////////////////////////////////////////////////
// sets a new theme either by its name (from the predefined ones),
// or as a new object with all colors specified.
Expand Down
36 changes: 34 additions & 2 deletions test/attachSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,38 @@ describe("Attach - Positive", function () {

describe("without override", function () {
var options = {};
it("must add new handlers without overriding", function () {
beforeEach(function () {
mon.attach(options);
});
it("must add new handlers without overriding", function () {
expect(options.connect instanceof Function).toBe(true);
expect(options.disconnect instanceof Function).toBe(true);
expect(options.query instanceof Function).toBe(true);
expect(options.error instanceof Function).toBe(true);
expect(options.transact instanceof Function).toBe(true);
expect(options.task instanceof Function).toBe(true);
});
afterEach(function () {
mon.detach();
});
});

describe("select events", function () {
var options = {};
it("must set only the events specified", function () {
beforeEach(function () {
mon.attach(options, ['query', 'task']);
});
it("must set only the events specified", function () {
expect(options.connect).toBe(undefined);
expect(options.disconnect).toBe(undefined);
expect(options.query instanceof Function).toBe(true);
expect(options.error).toBe(undefined);
expect(options.transact).toBe(undefined);
expect(options.task instanceof Function).toBe(true);
});
afterEach(function () {
mon.detach();
});
});
});

Expand All @@ -42,4 +52,26 @@ describe("Attach - Negative", function () {
}).toThrow("Invalid parameter 'events' passed.");
});

describe("repeated attachment", function () {
var options = {};
beforeEach(function () {
mon.attach(options);
});
it("must throw an error", function () {
expect(function () {
mon.attach(options);
}).toThrow("Repeated attachments not supported, must call detach first.");
});
afterEach(function () {
mon.detach();
});
});

describe("invalid detachment", function () {
it("must throw an error", function () {
expect(function () {
mon.detach();
}).toThrow("Event monitor not attached.");
});
});
});
12 changes: 10 additions & 2 deletions test/events/connectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("Connect - Positive", function () {
expect(text).toBe('connect');
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -39,7 +40,8 @@ describe("Connect - Positive", function () {
connect: function (c) {
ctx = c;
}
}, text = null;
};
text = null;
mon.attach(options, ['connect']);
mon.log = function (msg, info) {
text = info.text;
Expand All @@ -54,6 +56,7 @@ describe("Connect - Positive", function () {
expect(ctx).toEqual(client);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -62,11 +65,16 @@ describe("Connect - Positive", function () {
describe("Connect - Negative", function () {
describe("invalid parameters", function () {
var options = {};
mon.attach(options, ['connect']);
beforeEach(function () {
mon.attach(options, ['connect']);
});
it("must report event correctly", function () {
expect(function () {
options.connect();
}).toThrow("Invalid event 'connect' redirect parameters.");
});
afterEach(function () {
mon.detach();
});
});
});
12 changes: 10 additions & 2 deletions test/events/disconnectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("Disconnect - Positive", function () {
expect(text).toBe('disconnect');
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -39,7 +40,8 @@ describe("Disconnect - Positive", function () {
disconnect: function (c) {
ctx = c;
}
}, text = null;
};
text = null;
mon.attach(options, ['disconnect']);
mon.log = function (msg, info) {
text = info.text;
Expand All @@ -54,6 +56,7 @@ describe("Disconnect - Positive", function () {
expect(ctx).toEqual(client);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -62,11 +65,16 @@ describe("Disconnect - Positive", function () {
describe("Disconnect - Negative", function () {
describe("invalid parameters", function () {
var options = {};
mon.attach(options, ['disconnect']);
beforeEach(function () {
mon.attach(options, ['disconnect']);
});
it("must report event correctly", function () {
expect(function () {
options.disconnect();
}).toThrow("Invalid event 'disconnect' redirect parameters.");
});
afterEach(function () {
mon.detach();
});
});
});
11 changes: 10 additions & 1 deletion test/events/errorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe("Error - Positive", function () {
expect(text).toEqual(['error: errMsg', 'task(test): hello']);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand Down Expand Up @@ -54,6 +55,7 @@ describe("Error - Positive", function () {
expect(cb.e).toEqual(context);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -75,6 +77,7 @@ describe("Error - Positive", function () {
expect(text).toEqual(['error: errMsg', 'query: 123']);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -96,6 +99,7 @@ describe("Error - Positive", function () {
expect(text).toEqual(['error: errMsg', 'connection: 123']);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -104,11 +108,16 @@ describe("Error - Positive", function () {
describe("Error - Negative", function () {
describe("invalid parameters", function () {
var options = {};
mon.attach(options, ['error']);
beforeEach(function () {
mon.attach(options, ['error']);
});
it("must report event correctly", function () {
expect(function () {
options.error();
}).toThrow("Invalid event 'error' redirect parameters.");
});
afterEach(function () {
mon.detach();
});
});
});
9 changes: 8 additions & 1 deletion test/events/querySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("Query - Positive", function () {
expect(text[1]).toBe("params: [1,2,3]");
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand Down Expand Up @@ -62,6 +63,7 @@ describe("Query - Positive", function () {
expect(cb).toEqual(e);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});
Expand All @@ -70,11 +72,16 @@ describe("Query - Positive", function () {
describe("Query - Negative", function () {
describe("invalid parameters", function () {
var options = {};
mon.attach(options, ['query']);
beforeEach(function () {
mon.attach(options, ['query']);
});
it("must report event correctly", function () {
expect(function () {
options.query();
}).toThrow("Invalid event 'query' redirect parameters.");
});
afterEach(function () {
mon.detach();
});
});
});

0 comments on commit 71db491

Please sign in to comment.