Skip to content

Commit 48926a7

Browse files
committed
Adds test for update and findone
1 parent f212ac8 commit 48926a7

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

lib/sequence.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,24 @@ Sequence.prototype._setHooks = function() {
175175

176176
// jscs:enable
177177

178-
this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
179-
180178
this.model.findOne(_thisquery._conditions, function(err, obj) {
181179
if (err) return next();
182180
if (obj) return next();
183181
else {
184182
referenceValue = _this._getCounterReferenceField(_thisquery._update['$setOnInsert']);
185183
_this._setNextCounter(_this._options.reference_fields, referenceValue, function(err, seq) {
186184
if (err) return next();
185+
_thisquery._update['$setOnInsert'] = _thisquery._update['$setOnInsert'] || {};
187186
_thisquery._update['$setOnInsert'][_this._options.inc_field] = seq;
188187
next();
189188
});
190189
}
191190
});
192191
});
193192

194-
this._schema.post('findOneAndUpdate', function(next) {
195-
// console.dir(this);
196-
});
193+
// this._schema.post('findOneAndUpdate', function(next) {
194+
// console.dir(this);
195+
// });
197196

198197
this._schema.pre('update', function(next, done) {
199198
// jscs:disable
@@ -202,7 +201,6 @@ Sequence.prototype._setHooks = function() {
202201

203202
// jscs:enable
204203

205-
this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
206204

207205
this.model.findOne(_thisquery._conditions, function(err, obj) {
208206
if (err) return next();
@@ -211,6 +209,7 @@ Sequence.prototype._setHooks = function() {
211209
referenceValue = _this._getCounterReferenceField(_thisquery._update['$setOnInsert']);
212210
_this._setNextCounter(_this._options.reference_fields, referenceValue, function(err, seq) {
213211
if (err) return next();
212+
_thisquery._update['$setOnInsert'] = _thisquery._update['$setOnInsert'] || {};
214213
_thisquery._update['$setOnInsert'][_this._options.inc_field] = seq;
215214
next();
216215
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"grunt-mocha-cov": "^0.4.0",
3737
"load-grunt-tasks": "^3.4.1",
3838
"mocha": "^2.3.3",
39-
"mongoose": "^4.1.11"
39+
"mongoose": "^4.1.11",
40+
"sinon": "^1.17.5"
4041
},
4142
"config": {
4243
"blanket": {

test/base.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var chai = require('chai'),
44
async = require('async'),
55
mongoose = require('mongoose'),
66
Schema = mongoose.Schema,
7-
AutoIncrement = require('../index');
7+
AutoIncrement = require('../index'),
8+
sinon = require('sinon');
89

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

@@ -191,6 +192,76 @@ describe('Basic => ', function() {
191192

192193
});
193194

195+
describe('hook', function(){
196+
before(function(done) {
197+
var SimpleFieldSchema = new Schema({
198+
id: Number,
199+
val: String,
200+
tag: String
201+
});
202+
var wrapper = function(schema, options) {
203+
var instance = AutoIncrement(schema, options);
204+
this.setNextCounterSpy = sinon.spy(instance, '_setNextCounter');
205+
return instance;
206+
}.bind(this);
207+
SimpleFieldSchema.plugin(wrapper, {id: 'id_hook_test', inc_field: 'id'});
208+
this.SimpleField = mongoose.model('SimpleFieldHookTest', SimpleFieldSchema);
209+
this.SimpleField.create({val: 'existing'}, function(err){
210+
this.setNextCounterSpy.reset();
211+
done(err);
212+
}.bind(this));
213+
});
214+
215+
afterEach(function(){
216+
this.setNextCounterSpy.reset();
217+
});
218+
219+
it('is called when saving a new document', function(done){
220+
var t = new this.SimpleField({val: 'a'});
221+
t.save(function(err){
222+
sinon.assert.calledOnce(this.setNextCounterSpy);
223+
done(err);
224+
}.bind(this));
225+
});
226+
227+
it('is not called when saving an existing document', function(done){
228+
var t = new this.SimpleField({val: 'a'});
229+
t.isNew = false;
230+
t.save(function(err){
231+
sinon.assert.notCalled(this.setNextCounterSpy);
232+
done(err);
233+
}.bind(this));
234+
});
235+
236+
it('is called when upserting in an update and result in an insert', function(done){
237+
this.SimpleField.update({val: '1234'}, {tag: 'nothing'}, {upsert: true}, function(err, doc){
238+
sinon.assert.calledOnce(this.setNextCounterSpy);
239+
done(err);
240+
}.bind(this));
241+
});
242+
243+
it('is not called when upserting in an update and not result in an insert', function(done){
244+
this.SimpleField.update({val: 'existing'}, {tag: 'update'}, {upsert: true}, function(err, doc){
245+
sinon.assert.notCalled(this.setNextCounterSpy);
246+
done(err);
247+
}.bind(this));
248+
});
249+
250+
it('is called when upserting in an findOneAndUpdate and result in an insert', function(done){
251+
this.SimpleField.findOneAndUpdate({val: '4567'}, {tag: 'nothing'}, {upsert: true}, function(err, doc){
252+
sinon.assert.calledOnce(this.setNextCounterSpy);
253+
done(err);
254+
}.bind(this));
255+
});
256+
257+
it('is not called when upserting in an findOneAndUpdate and not result in an insert', function(done){
258+
this.SimpleField.findOneAndUpdate({val: '1234'}, {tag: 'findOneAndUpdate'}, {upsert: true}, function(err, doc){
259+
sinon.assert.notCalled(this.setNextCounterSpy);
260+
done(err);
261+
}.bind(this));
262+
});
263+
});
264+
194265
describe('a manual increment field => ', function() {
195266

196267
before(function(done) {
@@ -305,7 +376,7 @@ describe('Basic => ', function() {
305376
assert.throws(function(){
306377
UnusedSchema.plugin(AutoIncrement, {inc_field: 'inhabitant', reference_fields: ['country', 'city'], disable_hooks: true});
307378
}, Error);
308-
379+
309380
});
310381
});
311382

@@ -342,7 +413,7 @@ describe('Basic => ', function() {
342413

343414
});
344415

345-
});
416+
});
346417

347418
});
348419
});

0 commit comments

Comments
 (0)