Skip to content
This repository has been archived by the owner on Aug 9, 2018. It is now read-only.

Commit

Permalink
[test] add test with revision mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
andrenarchy committed May 24, 2015
1 parent 7459785 commit 2249084
Showing 1 changed file with 53 additions and 15 deletions.
68 changes: 53 additions & 15 deletions test/concurrentPlugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*jshint expr: true*/
'use strict';

var mongoose = require('mongoose');
Expand Down Expand Up @@ -31,22 +32,59 @@ describe('concurrentPlugin()', function() {
});
});

it('should allow saving if revision matches', function(done) {
var User = registerUserSchema();

async.waterfall([
function(cb) {
User.create({name: 'Darth', age: 42}, cb);
},
function(darth, cb) {
darth.name = 'Darth Vader';
darth.concurrentUpdate(darth._revision, cb);
}
], function(err, darth) {
if (err) {return done(err);}
darth.should.have.property('_revision').which.is.exactly(1);
done();
describe('concurrentUpdate()', function() {

it('should allow saving if revision matches', function(done) {
var User = registerUserSchema();

async.waterfall([
function(cb) {
User.create({name: 'Darth', age: 42}, cb);
},
function(darth, cb) {
darth.name = 'Darth Vader';
darth.concurrentUpdate(darth._revision, cb);
},
function(darth, cb) {
darth.should.have.properties({
_revision: 1,
name: 'Darth Vader',
age: 42
});
cb();
}
], done);
});

it('should deny saving if revision does not match', function(done) {
var User = registerUserSchema();
var darthOrig;

async.waterfall([
function(cb) {
User.create({name: 'Darth', age: 42}, cb);
},
function(darth, cb) {
darthOrig = darth;
darth.name = 'Darth Vader';
darth.concurrentUpdate(darth._revision - 1, cb);
}
], function(err, darth) {
should(err).be.Error;
should.not.exist(darth);
// check if document is unchanged in db
User.findById(darthOrig._id, function(err, darth) {
should.not.exist(err);
darth.should.have.properties({
_revision: 0,
name: 'Darth',
age: 42
});
done();
});
});
});

});

});

0 comments on commit 2249084

Please sign in to comment.