From 2a06dde50b51431152868472dc8c9ea893616a57 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Tue, 7 Apr 2015 11:58:49 +0100 Subject: [PATCH] adding tests to cover the events. --- test/dbSpec.js | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/test/dbSpec.js b/test/dbSpec.js index ee4f2d34..abeda514 100644 --- a/test/dbSpec.js +++ b/test/dbSpec.js @@ -465,6 +465,114 @@ describe("Queries must not allow invalid QRM (Query Request Mask) combinations", }); +describe("Connect/Disconnect notification events", function () { + it("must execute each once during any query", function () { + var result, connected = 0, disconnected = 0; + options.connect = function () { + connected++; + }; + options.disconnect = function () { + disconnected++; + }; + db.query("select 'test'") + .then(function () { + result = null; + }); + waitsFor(function () { + return result !== undefined; + }, "Query timed out", 5000); + runs(function () { + expect(result).toBe(null); + expect(connected).toBe(1); + expect(disconnected).toBe(1); + }); + }); +}); + +describe("Query event", function () { + + it("must pass query and parameters correctly", function () { + var result, p, counter = 0; + options.query = function (e) { + counter++; + p = e; + }; + + db.query("select * from users where id > $1", [0]) + .then(function () { + result = null; + }, function () { + result = null; + }); + waitsFor(function () { + return result !== undefined; + }, "Query timed out", 5000); + runs(function () { + expect(counter).toBe(1); + expect(p.query).toBe('select * from users where id > 0'); + }); + }); +}); + +describe("Start/Finish transaction events", function () { + + it("must execute each once for every transaction, with the right properties", function () { + var result, tag, start = 0, finish = 0, finishContext; + options.transact = function (e) { + if (e.ctx.finish) { + finish++; + finishContext = e.ctx; + } else { + start++; + tag = e.ctx.tag; + } + }; + db.tx("myTransaction", function () { + return promise.resolve('SUCCESS'); + }).then(function (data) { + result = data; + }); + waitsFor(function () { + return result !== undefined; + }, "Query timed out", 5000); + runs(function () { + expect(result).toBe('SUCCESS'); + expect(start).toBe(1); + expect(finish).toBe(1); + expect(tag).toBe("myTransaction"); + expect(finishContext.success).toBe(true); + }); + }); +}); + +describe("Error event", function () { + + it("must report errors from transaction callbacks", function () { + var result, errTxt, context, r, counter = 0; + options.error = function (err, e) { + counter++; + errTxt = err; + context = e.ctx; + }; + db.tx("Error Transaction", function () { + throw new Error("Test Error"); + }).then(function () { + }, function (reason) { + r = reason; + result = null; + }); + waitsFor(function () { + return result !== undefined; + }, "Query timed out", 5000); + runs(function () { + expect(r).toBe('Test Error'); + expect(errTxt).toBe('Test Error'); + expect(counter).toBe(1); + expect(context.tag).toBe("Error Transaction"); + }); + }); +}); + var _finishCallback = jasmine.Runner.prototype.finishCallback; jasmine.Runner.prototype.finishCallback = function () { // Run the old finishCallback: