Skip to content

Commit

Permalink
Tests error situations
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiel committed Aug 22, 2016
1 parent 2ab01a2 commit 727be42
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"istanbul": "^0.4.2",
"load-grunt-tasks": "^3.4.1",
"mocha": "^2.4.5",
"mongoose": "^4.1.11"
"mongoose": "^4.1.11",
"sinon": "^1.17.5"
},
"config": {
"blanket": {
Expand Down
55 changes: 53 additions & 2 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var chai = require('chai'),
async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
AutoIncrement = require('../index');
AutoIncrement = require('../index'),
sinon = require('sinon');

describe('Basic => ', function() {

Expand All @@ -21,6 +22,8 @@ describe('Basic => ', function() {
});
});



describe('Global sequences => ', function() {

describe('a simple id field => ', function() {
Expand Down Expand Up @@ -376,7 +379,55 @@ describe('Basic => ', function() {

});

});
});

describe('Error on hook', function(){
before(function(done) {
var SimpleFieldSchema = new Schema({
id: Number,
val: String
});

SimpleFieldSchema.plugin(function(schema, options) {
var sequence = AutoIncrement(schema, options);
sinon.stub(sequence._counterModel, 'findOneAndUpdate').yields(new Error('Incrementing error'));
return sequence;
}, {id: 'simple_with_error_counter', inc_field: 'id'});
this.SimpleField = mongoose.model('SimpleFieldWithError', SimpleFieldSchema);

var ManualSchema = new Schema({
name: String,
membercount: Number
});
ManualSchema.plugin(function(schema, options) {
var sequence = AutoIncrement(schema, options);
sinon.stub(sequence, '_setNextCounter').yields(new Error('Incrementing error'));
return sequence;
}, {id:'errored_manual_counter', inc_field: 'membercount', disable_hooks: true});
this.Manual = mongoose.model('ManualWithError', ManualSchema);
this.Manual.create([{name: 't1'},{name: 't2'}], done);
});

it('do not save the document if an error happens in the plugin', function(done) {
var t = new this.SimpleField();
t.save(function(err) {
assert.isOk(err);
assert.instanceOf(err, Error);
done();
});
});

it('do not save the document after a manual incrementation if an error happens in the plugin', function(done) {
this.Manual.findOne({name: 't1'}, function(err, entity) {
if(err) return done(err);
entity.setNext('errored_manual_counter', function(err, entity) {
assert.isOk(err);
assert.instanceOf(err, Error);
done();
});
});
});
});

});
});

0 comments on commit 727be42

Please sign in to comment.