Added 3 tests for ScenarioManager.loadIfEmpty function. Refactored loadIfEmpty function to enable testing.

This commit is contained in:
LeeDr 2015-10-28 12:11:48 -05:00 committed by Joe Fleming
parent e9d349bf24
commit e40c7fa7a5
2 changed files with 58 additions and 13 deletions

View file

@ -21,11 +21,11 @@ describe('scenario manager', function () {
it('should be able to load scenarios', function () {
return manager.load('makelogs')
.then(function () {
expect(create.getCall(0).args[0].index).to.be('logstash-2015.09.17');
expect(create.getCall(1).args[0].index).to.be('logstash-2015.09.18');
expect(bulk.called).to.be(true);
});
.then(function () {
expect(create.getCall(0).args[0].index).to.be('logstash-2015.09.17');
expect(create.getCall(1).args[0].index).to.be('logstash-2015.09.18');
expect(bulk.called).to.be(true);
});
});
it('should be able to delete all indices', function () {
@ -55,6 +55,52 @@ describe('scenario manager', function () {
});
});
it('should load if the index does not exist', function () {
var load = sinon.stub(manager, 'load', Promise.resolve);
var throwError = sinon.stub(manager.client, 'count', Promise.reject);
var id = 'makelogs';
return manager.loadIfEmpty(id).then(function () {
expect(load.calledWith(id)).to.be(true);
load.restore();
throwError.restore();
});
});
it('should load if the index is empty', function () {
var load = sinon.stub(manager, 'load', Promise.resolve);
var returnZero = sinon.stub(manager.client, 'count', function () {
return Promise.resolve({
'count': 0
});
});
var id = 'makelogs';
return manager.loadIfEmpty(id).then(function () {
expect(load.calledWith(id)).to.be(true);
load.restore();
returnZero.restore();
});
});
it('should load if the index is empty', function () {
var load = sinon.stub(manager, 'load', Promise.resolve);
var returnOne = sinon.stub(manager.client, 'count', function () {
return Promise.resolve({
'count': 1
});
});
var id = 'makelogs';
return manager.loadIfEmpty(id).then(function () {
expect(load.called).to.be(false);
load.restore();
returnOne.restore();
});
});
afterEach(function () {
bulk.restore();
create.restore();

View file

@ -98,18 +98,17 @@ ScenarioManager.prototype.loadIfEmpty = function (id) {
return self.client.count({
index: bulk.indexName
}, function handleCountResponse(error, response) {
// if (error) {
// console.log('Need to load index. error=' + error);
// } else {
// console.log('index=' + bulk.indexName + ' count=' + response.count);
// }
// if the index is undefined or count ===0 then call the load function above
if (error || response.count === 0) {
})
.then(function handleCountResponse(response) {
if (response.count === 0) {
self.load(id);
}
})
.catch(function (reason) {
self.load(id);
});
}));
};
module.exports = ScenarioManager;