Created
November 5, 2014 14:13
-
-
Save dwaligora/3c11079fc63b05396e1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var expect = require('chai').expect, | |
should = require('chai').should, | |
chai = require('chai'), | |
sinon = require('sinon'), | |
Q = require('q'), | |
config = require('../../../config.json'), | |
JobManager = require('../../../app/managers/job').JobManager, | |
MySQLStub = require('../../stubs/services/mysql'); | |
chai.use(require('chai-as-promised')); | |
describe('JobManager', function () { | |
describe('construct', function () { | |
it('should throw exception unless MySQL Object client provided', function() { | |
var tableName = 'dbTable'; | |
// Null | |
expect(function() { | |
new JobManager(null, tableName); | |
}).to.throw(Error); | |
// Number | |
expect(function() { | |
new JobManager(1, tableName); | |
}).to.throw(Error); | |
// String | |
expect(function() { | |
new JobManager('string', tableName); | |
}).to.throw(Error); | |
}); | |
it('should throw exception unless tableName String provided', function() { | |
// empty | |
expect(function() { | |
new JobManager(MySQLStub.create()); | |
}).to.throw(Error); | |
// Number | |
expect(function() { | |
new JobManager(MySQLStub.create(), 1); | |
}).to.throw(Error); | |
// Object | |
expect(function() { | |
new JobManager(MySQLStub.create(), {}); | |
}).to.throw(Error); | |
}); | |
it('should instantiate JobManager if tableName String and MySQL Object Client are provided', function() { | |
var tableName = 'dbTable', | |
jobManager = new JobManager(MySQLStub.create(), tableName); | |
expect(jobManager).to.be.an.instanceof(JobManager); | |
}); | |
}); | |
describe('#getTableName', function () { | |
it('should set tableName if provided', function() { | |
var tableName = 'dbName', | |
jobManager = new JobManager(MySQLStub.create(), tableName); | |
expect(jobManager.getTableName()).to.equal(tableName); | |
}); | |
}); | |
describe('#find', function () { | |
it('should throw exception unless criteria Object provided', function() { | |
var tableName = 'dbTable'; | |
// Empty | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.find(); | |
}).to.throw(Error); | |
// String | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.find('not object'); | |
}).to.throw(Error); | |
// Number | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.find(1); | |
}).to.throw(Error); | |
}); | |
it('should execute MySQL SELECT on constructor given DB TABLE and criteria object', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }, | |
jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.find(criteria); | |
expect(mySQLStub.select.calledOnce).to.equal(true); | |
expect(mySQLStub.select.calledWith('*')).to.equal(true); | |
expect(mySQLStub.from.calledOnce).to.equal(true); | |
expect(mySQLStub.from.calledWith(tableName)).to.equal(true); | |
expect(mySQLStub.where.calledOnce).to.equal(true); | |
expect(mySQLStub.where.calledWith(criteria)).to.equal(true); | |
expect(mySQLStub.orderBy.calledOnce).to.equal(false); | |
expect(mySQLStub.limit.calledOnce).to.equal(false); | |
expect(mySQLStub.offset.calledOnce).to.equal(false); | |
}); | |
it('should return Promise if, at least, criteria object provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }, | |
jobManager = new JobManager(mySQLStub, tableName); | |
var result = jobManager.find(criteria); | |
expect(Q.isPromise(result)).to.equal(true); | |
}); | |
it('should reject Promise if, MySQL Object Client fail', function(done) { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }; | |
mySQLStub.then = sinon.stub().callsArgWithAsync(1, new Error('fail')); | |
var jobManager = new JobManager(mySQLStub, tableName); | |
var result = jobManager.find(criteria); | |
expect(result).to.eventually.be.rejected.and.notify(done); | |
}); | |
describe('.orderBy', function() { | |
it('should add to MySQL SELECT if Object with field property provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }, | |
groupBy = { field: 'created_at' }; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.find(criteria, groupBy); | |
expect(mySQLStub.orderBy.calledOnce).to.equal(true); | |
expect(mySQLStub.orderBy.calledWith(groupBy.field, 'asc')).to.equal(true); | |
}); | |
it('should add to MySQL SELECT and set order if as Object with field and order property provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }, | |
groupBy = { field: 'created_at', order: 'desc' }; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.find(criteria, groupBy); | |
expect(mySQLStub.orderBy.calledOnce).to.equal(true); | |
expect(mySQLStub.orderBy.calledWith(groupBy.field, groupBy.order)).to.equal(true); | |
}); | |
it('shouldn\'t add to MySQL SELECT if Number provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.find(criteria, 1); | |
expect(mySQLStub.orderBy.calledOnce).to.equal(false); | |
}); | |
it('shouldn\'t add to MySQL SELECT if String provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.find(criteria, 'string'); | |
expect(mySQLStub.orderBy.calledOnce).to.equal(false); | |
}); | |
}); | |
describe('.limit', function() { | |
it('should add to MySQL SELECT if provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }, | |
limit = 25; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.find(criteria, null, limit); | |
expect(mySQLStub.orderBy.calledOnce).to.equal(false); | |
expect(mySQLStub.offset.calledOnce).to.equal(false); | |
expect(mySQLStub.limit.calledOnce).to.equal(true); | |
expect(mySQLStub.limit.calledWith(limit)).to.equal(true); | |
}); | |
}); | |
describe('.offset', function() { | |
it('should add to MySQL SELECT if provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
criteria = { name: 'test' }, | |
offset = 25; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.find(criteria, null, null, offset); | |
expect(mySQLStub.orderBy.calledOnce).to.equal(false); | |
expect(mySQLStub.limit.calledOnce).to.equal(false); | |
expect(mySQLStub.offset.calledOnce).to.equal(true); | |
expect(mySQLStub.offset.calledWith(offset)).to.equal(true); | |
}); | |
}); | |
}); | |
describe('#update', function () { | |
it('should throw exception unless job ID and job Object provided', function() { | |
var tableName = 'dbTable'; | |
// Both Empty | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.update(); | |
}).to.throw(Error); | |
// Id null | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.update(null, {}); | |
}).to.throw(Error); | |
// Job Object null | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.update(1); | |
}).to.throw(Error); | |
// Job Object Number | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.update(1, 1); | |
}).to.throw(Error); | |
// Job Object String | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.update(1, 'not object'); | |
}).to.throw(Error); | |
}); | |
it('should execute MySQL UPDATE with given properties on row with given ID', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
properties = { name: 'test' }; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.update(1, properties); | |
expect(mySQLStub.getTableName()).to.equal(tableName); | |
expect(mySQLStub.where.calledOnce).to.equal(true); | |
expect(mySQLStub.where.calledWith('id', 1)).to.equal(true); | |
expect(mySQLStub.update.calledOnce).to.equal(true); | |
expect(mySQLStub.update.calledWith(properties)).to.equal(true); | |
}); | |
it('should return Promise if job ID and data to update provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
properties = { name: 'test' }, | |
jobManager = new JobManager(mySQLStub, tableName); | |
var result = jobManager.update(1, properties); | |
expect(Q.isPromise(result)).to.equal(true); | |
}); | |
it('should reject Promise if, MySQL Object Client fail', function(done) { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
properties = { name: 'test' }; | |
mySQLStub.then = sinon.stub().callsArgWithAsync(1, new Error('fail')); | |
var jobManager = new JobManager(mySQLStub, tableName); | |
var result = jobManager.update(1, properties); | |
expect(result).to.eventually.be.rejected.and.notify(done); | |
}); | |
}); | |
describe('#create', function () { | |
it('should throw exception unless job Object provided', function() { | |
var tableName = 'dbTable'; | |
// Empty | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.create(); | |
}).to.throw(Error); | |
// Number | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.create(1); | |
}).to.throw(Error); | |
// String | |
expect(function() { | |
var jobManager = new JobManager(MySQLStub.create(), tableName); | |
jobManager.update('string'); | |
}).to.throw(Error); | |
}); | |
it('should execute MySQL INSERT with given properties', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
properties = { name: 'test' }; | |
var jobManager = new JobManager(mySQLStub, tableName); | |
jobManager.create(properties); | |
expect(mySQLStub.getTableName()).to.equal(tableName); | |
expect(mySQLStub.insert.calledOnce).to.equal(true); | |
expect(mySQLStub.insert.calledWith(properties)).to.equal(true); | |
}); | |
it('should return Promise if job data provided', function() { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
properties = { name: 'test' }, | |
jobManager = new JobManager(mySQLStub, tableName); | |
var result = jobManager.create(properties); | |
expect(Q.isPromise(result)).to.equal(true); | |
}); | |
it('should reject Promise if, MySQL Object Client fail', function(done) { | |
var mySQLStub = MySQLStub.create(), | |
tableName = 'dbTable', | |
properties = { name: 'test' }; | |
mySQLStub.then = sinon.stub().callsArgWithAsync(1, new Error('fail')); | |
var jobManager = new JobManager(mySQLStub, tableName); | |
var result = jobManager.create(properties); | |
expect(result).to.eventually.be.rejected.and.notify(done); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment