Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.x fix/upsert #129

Merged
merged 4 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/cloudant.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@ Cloudant.prototype.updateOrCreate = function(model, data, cb) {
var id = data[idName];
if (id) {
self.getCurrentRevision(model, id, function(err, rev) {
if (err) return cb(err);
if (err && err.statusCode !== 404) {
return cb(err);
}
if (rev) data._rev = rev;
self.create(model, data, {}, function(err) {
if (err) return cb(err);
Expand Down
31 changes: 31 additions & 0 deletions test/cloudant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ describe('cloudant connector', function() {
};
});
});

describe('updateOrCreate', function() {
afterEach(function(done) {
Product.destroyById(1, function(err) {
if (err) return done(err);
done();
Copy link

@Amir-61 Amir-61 May 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of:

function(err) {
  if (err) return done(err);
  done();
}

Just the following line is enough:

done(err)

});
});

it('can call upsert with an id and have it create a document without error',
Copy link

@Amir-61 Amir-61 May 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the following is what you are achieving here:

can call upsert with a primary key and create/update a document without error

Also it is always a good idea if you try primary keys which are not id as well.

function(done) {
// scenario: upsert does a create
Product.upsert({
id: '1',
name: 'bread',
}, function(err, res) {
if (err) return done(err);
res.id.should.equal('1');
res.name.should.equal('bread');
// scenario: upsert does a update
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an update

Product.upsert({
id: '1',
name: 'newBread',
}, function(err, res) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check err here as well.

res.id.should.equal('1');
res.name.should.equal('newBread');
done();
});
});
});
});
});

describe('cloudant constructor', function() {
Expand Down