Skip to content

Commit

Permalink
adding tests to cover the events.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Apr 7, 2015
1 parent 11ea012 commit 2a06dde
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions test/dbSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2a06dde

Please sign in to comment.