Skip to content

Commit

Permalink
Fix update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ssh24 committed Jun 8, 2017
1 parent 5f5c717 commit 6458a69
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,26 @@ Let's say we have an instance in the database:

- update/updateAll

```javascript
Model.updateAll({
_rev:'2-abcedf',
name:'Bar4'
}, {name: 'Bar4-updated', _rev: '2-abcedf'}, function(err, result) {
if (err) throw err;
console.log('Update an existing instance: ' + JSON.stringify(result));
});
```
- with `_rev` property
```javascript
Model.updateAll({
_rev:'2-abcedf',
name:'Bar4'
}, {name: 'Bar4-updated', _rev: '2-abcedf'}, function(err, result) {
if (err) throw err;
console.log('Update an existing instance: ' + JSON.stringify(result));
});
```

- without `_rev` property
```javascript
Model.updateAll({
name:'Bar4'
}, {name: 'Bar4-updated'}, function(err, result) {
if (err) throw err;
console.log('Update an existing instance: ' + JSON.stringify(result));
});
```

# Setup Cloudant Instance

Expand Down
2 changes: 1 addition & 1 deletion lib/cloudant.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ Cloudant.prototype.updateAll = function(model, where, data, options, cb) {
var errorArray = _.filter(result, 'error');
if (errorArray.length > 0) {
err = new Error(g.f(util.format('Unable to update 1 or more ' +
'document(s): %s', util.inspect(errorArray, 2))));
'document(s): %s', util.inspect(result, 2))));
return cb(err);
} else {
return cb(err, {count: result.length});
Expand Down
2 changes: 2 additions & 0 deletions test/cloudant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ describe('cloudant connector', function() {
}, function(err, product) {
if (err) return done(err);
prod2 = product;
delete prod1._rev;
delete prod2._rev;
done();
});
});
Expand Down
51 changes: 46 additions & 5 deletions test/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,52 @@ describe('updateAll', function() {
}, {forceId: false});

db.once('connected', function() {
db.automigrate(function(err) {
Product.create(bread, done);
});
db.automigrate(done);
});
});

after(cleanUpData);
beforeEach(function(done) {
Product.create([{
name: 'bread',
price: 100,
}, {
name: 'bread-x',
price: 110,
}], done);
});

afterEach(cleanUpData);

it('updates a model instance without `_rev` property', function(done) {
var newData = {
name: 'bread2',
price: 250,
};
Product.find(function(err, result) {
err = testUtil.refinedError(err, result);
if (err) return done(err);
testUtil.hasResult(err, result).should.be.ok();
var id = result[0].id;
Product.update({id: id}, newData, function(err, result) {
err = testUtil.refinedError(err, result);
if (err) return done(err);
testUtil.hasResult(err, result).should.be.ok();
result.should.have.property('count');
result.count.should.equal(1);
Product.find(function(err, result) {
err = testUtil.refinedError(err, result);
if (err) return done(err);
testUtil.hasResult(err, result).should.be.ok();
result.length.should.equal(2);
newData.name.should.be.oneOf(result[0].name, result[1].name);
newData.price.should.be.oneOf(result[0].price, result[1].price);
done();
});
});
});
});

it('updates a model instance', function(done) {
it('updates a model instance with `_rev` property', function(done) {
var newData = {
name: 'bread2',
price: 250,
Expand All @@ -147,6 +184,7 @@ describe('updateAll', function() {
if (err) return done(err);
testUtil.hasResult(err, result).should.be.ok();
var id = result[0].id;
newData._rev = result[0]._rev;
Product.update({id: id}, newData, function(err, result) {
err = testUtil.refinedError(err, result);
if (err) return done(err);
Expand All @@ -157,6 +195,9 @@ describe('updateAll', function() {
err = testUtil.refinedError(err, result);
if (err) return done(err);
testUtil.hasResult(err, result).should.be.ok();
result.length.should.equal(2);
newData.name.should.be.oneOf(result[0].name, result[1].name);
newData.price.should.be.oneOf(result[0].price, result[1].price);
done();
});
});
Expand Down

0 comments on commit 6458a69

Please sign in to comment.