Skip to content

Commit

Permalink
Added queues to check()
Browse files Browse the repository at this point in the history
  • Loading branch information
thepeak99 committed May 30, 2015
1 parent 1c025e3 commit 48d557c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 85 deletions.
105 changes: 58 additions & 47 deletions lib/pgtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,60 +60,71 @@ Query.prototype.run = function (args) {
};

function PgMock() {
this.queue = [];
}

PgMock.prototype.connect = function (db, cb) {
var client, done, self;

self = this;
this.doneCalled = false;
var self = this;

client = {
query: function (query) {
var retVal;

if (self.queue.length === 0) {
throw new AssertionError('Unexpected query: ' + query);
self.expectQueue = [];
self.queryQueue = [];

self.connect = function (db, cb) {
var client, done;

self.doneCalled = false;
self.checkCalled = false;

client = {
query: function (query) {
self.queryQueue.push(arguments);
}
self.queue.shift().run(arguments);
}
};

done = function () {
self.doneCalled = true;
};

done = function () {
self.doneCalled = true;
};

cb(null, client, done);
};

cb(null, client, done);
};

PgMock.prototype.check = function () {
if (!this.doneCalled) {
throw new AssertionError('Done was not called');
}

if (this.queue.length !== 0) {
throw new AssertionError('Not all queries were executed');
}
};
self.check = function () {
self.checkCalled = true;
self.queryQueue.forEach(function (query) {
if (self.expectQueue.length === 0) {
throw new AssertionError('Unexpected query: ' + query[0]);
}

PgMock.prototype.expect = function () {
var match = new Query(arguments);
this.queue.push(match);

return {
returning: function (err, data) {
match.retVal = {
err: err,
data: data
};
self.expectQueue.shift().run(query);
});

if (!self.doneCalled) {
throw new AssertionError('Done was not called');
}

if (self.expectQueue.length !== 0) {
throw new AssertionError('Not all queries were executed');
}
};
};

PgMock.prototype.reset = function () {
this.queue = [];
};
self.expect = function () {
var match = new Query(arguments);
self.expectQueue.push(match);

return {
returning: function (err, data) {
match.retVal = {
err: err,
data: data
};
}
};
};

self.reset = function (checkCheck) {
self.expectQueue = [];
self.queryQueue = [];

if (checkCheck && !self.checkCalled) {
throw new AssertionError('Check not called');
}
};
}

module.exports = new PgMock();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"lodash": "^3.9.3"
},
"devDependencies": {
"async": "^1.0.0",
"chai": "^2.3.0",
"mocha": "^2.2.5",
"sinon": "^1.14.1"
"mocha": "^2.2.5"
}
}
105 changes: 69 additions & 36 deletions test/test-pgtest.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
/*jslint node: true nomen: true*/
/*global describe, it, before, beforeEach*/
/*global describe, it, before, beforeEach, afterEach*/
'use strict';

var expect = require('chai').expect;
var sinon = require('sinon');
var async = require('async');

var pgtest = require('../lib/pgtest');

describe('pgtest', function () {
beforeEach(function () {
pgtest.reset();
afterEach(function () {
pgtest.reset(true);
});

describe('connect', function () {
it('should call its callback with a client and a done function', function () {
it('should call its callback with a client and a done function', function (testDone) {
pgtest.connect('foo', function (err, client, done) {
expect(err).to.be.equal(null);
expect(client).to.be.not.equal(null);
expect(done).to.be.not.equal(null);

done();
pgtest.check();
testDone();
});
});
});
Expand All @@ -29,50 +34,54 @@ describe('pgtest', function () {
pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM vegetables', function () {});
client.query('SELECT * FROM fruits', function () {});
done();
});

pgtest.check();
});

it('should reject queries in different order', function () {
pgtest.expect('SELECT * FROM vegetables');
pgtest.expect('SELECT * FROM fruits');

function test() {
pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM fruits', function () {});
client.query('SELECT * FROM vegetables', function () {});
});
}
expect(test).to.Throw(/Unexpected query/);
pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM fruits', function () {});
client.query('SELECT * FROM vegetables', function () {});
});

expect(pgtest.check).to.Throw(/Unexpected query/);
});

it('should reject queries with unexpected parameters', function () {
pgtest.expect('SELECT * FROM vegetables');

function test() {
pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM vegetables', ['potato'], function () {});
});
}
expect(test).to.Throw('Unexpected params: potato');
pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM vegetables', ['potato'], function () {});
});

expect(pgtest.check).to.Throw('Unexpected params: potato');
});

it('should reject queries that do not contain requested params', function () {
pgtest.expect('SELECT * FROM vegetables WHERE name = $1', ['potato']);

function test() {
pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM vegetables WHERE name = $1', function () {});
});
}
expect(test).to.Throw('Missing expected params');
pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM vegetables WHERE name = $1', function () {});
done();
});

expect(pgtest.check).to.Throw('Missing expected params');
});

it('should accept expected queries with params', function () {
pgtest.expect('SELECT * FROM vegetables WHERE name = $1', ['potato']);

pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM vegetables WHERE name = $1', ['potato'], function () { });
done();
});

pgtest.check();
});
});

Expand All @@ -86,7 +95,10 @@ describe('pgtest', function () {
expect(err).to.be.equal(null);
expect(data.rows).to.be.deep.equal(rows);
});
done();
});

pgtest.check();
});

it('should let the query return errors', function () {
Expand All @@ -97,42 +109,63 @@ describe('pgtest', function () {
expect(err).to.be.equal('error');
expect(data).to.be.deep.equal(null);
});
done();
});
pgtest.check();
});

it('should work with async properly', function () {
pgtest.expect('SELECT * FROM vegetables WHERE name = $1', ['potato']).returning('potato err', null);
pgtest.expect('SELECT * FROM fruits WHERE name = $1', ['banana']).returning(null, []);

pgtest.connect('foo', function (err, client, done) {
async.parallel({
first: function (cb) {
client.query('SELECT * FROM vegetables WHERE name = $1', ['potato'], cb);
},
second: function (cb) {
client.query('SELECT * FROM fruits WHERE name = $1', ['banana'], cb);
}
}, function (err) {
done();
});
});

pgtest.check();
});

});

describe('check', function () {
it('should fail if done() was not called', function () {
pgtest.connect('foo', function (err, client, done) { });
function test() {
pgtest.check();
}
expect(test).to.Throw('Done was not called');
expect(pgtest.check).to.Throw('Done was not called');
});

it('should fail if not all queries were run', function () {
pgtest.expect('SELECT * FROM vegetables');
pgtest.connect('foo', function (err, client, done) {
done();
});
function test() {
pgtest.check();
}
expect(test).to.Throw('Not all queries were executed');
expect(pgtest.check).to.Throw('Not all queries were executed');
});
});

describe('client', function () {
it('should call callback after a query', function () {
var spy;
spy = sinon.spy();
var calls = 0;
pgtest.expect('SELECT * FROM vegetables').returning('error');

pgtest.connect('foo', function (err, client, done) {
client.query('SELECT * FROM vegetables', spy);
client.query('SELECT * FROM vegetables', function () {
calls += 1;
});
done();
});

pgtest.check();

expect(spy.calledOnce).to.be.equal(true);
expect(calls).to.be.equal(1);
});
});
});

0 comments on commit 48d557c

Please sign in to comment.