From 3c19beacbbaf04914df737f565dd59a3012b2f34 Mon Sep 17 00:00:00 2001 From: Loay Date: Mon, 17 Apr 2017 18:18:20 -0400 Subject: [PATCH] Add test case using updateAttributes --- test/manipulation.test.js | 107 +++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index bee6cba7..e55a3cdf 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -913,60 +913,71 @@ describe('manipulation', function() { }); }); - describe('updateOrCreate when forceId is true', function() { - var Post; - before(function definePostModel(done) { - var ds = getSchema(); - Post = ds.define('Post', { - title: {type: String, length: 255}, - content: {type: String}, - }, {forceId: true}); - ds.automigrate('Post', done); - }); - - it('fails when id does not exist in db & validate is true', function(done) { - var unknownId = uid.fromConnector(db) || 123; - var post = {id: unknownId, title: 'a', content: 'AAA'}; - Post.updateOrCreate(post, {validate: true}, (err) => { - err.statusCode.should.equal(404); - done(); + bdd.describeIf(connectorCapabilities.supportForceId !== false, + 'updateOrCreate when forceId is true', function() { + var Post; + before(function definePostModel(done) { + var ds = getSchema(); + Post = ds.define('Post', { + title: {type: String, length: 255}, + content: {type: String}, + }, {forceId: true}); + ds.automigrate('Post', done); }); - }); - it('fails when id does not exist in db & validate is false', function(done) { - var unknownId = uid.fromConnector(db) || 123; - var post = {id: unknownId, title: 'a', content: 'AAA'}; - Post.updateOrCreate(post, {validate: false}, (err) => { - err.statusCode.should.equal(404); - done(); - }); - }); - - it('works on create if the request does not include an id', function(done) { - var post = {title: 'a', content: 'AAA'}; - Post.updateOrCreate(post, (err, p) => { - if (err) return done(err); - p.title.should.equal(post.title); - p.content.should.equal(post.content); - done(); - }); - }); - - it('works on update if the request includes an existing id in db', function(done) { - Post.create({title: 'a', content: 'AAA'}, (err, post) => { - if (err) return done(err); - post = post.toObject(); - delete post.content; - post.title = 'b'; - Post.updateOrCreate(post, function(err, p) { - if (err) return done(err); - p.id.should.equal(post.id); - p.title.should.equal('b'); + it('fails when id does not exist in db & validate is true', function(done) { + var unknownId = uid.fromConnector(db) || 123; + var post = {id: unknownId, title: 'a', content: 'AAA'}; + Post.updateOrCreate(post, {validate: true}, (err) => { + err.statusCode.should.equal(404); done(); }); }); + + it('fails when id does not exist in db & validate is false', function(done) { + var unknownId = uid.fromConnector(db) || 123; + var post = {id: unknownId, title: 'a', content: 'AAA'}; + Post.updateOrCreate(post, {validate: false}, (err) => { + err.statusCode.should.equal(404); + done(); + }); + }); + + it('fails when id does not exist in db & validate is false when using updateAttributes', + function(done) { + var unknownId = uid.fromConnector(db) || 123; + var post = new Post({id: unknownId}); + post.updateAttributes({title: 'updated title', content: 'AAA'}, {validate: false}, (err) => { + err.statusCode.should.equal(404); + done(); + }); + }); + + it('works on create if the request does not include an id', function(done) { + var post = {title: 'a', content: 'AAA'}; + Post.updateOrCreate(post, (err, p) => { + if (err) return done(err); + p.title.should.equal(post.title); + p.content.should.equal(post.content); + done(); + }); + }); + + it('works on update if the request includes an existing id in db', function(done) { + Post.create({title: 'a', content: 'AAA'}, (err, post) => { + if (err) return done(err); + post = post.toObject(); + delete post.content; + post.title = 'b'; + Post.updateOrCreate(post, function(err, p) { + if (err) return done(err); + p.id.should.equal(post.id); + p.title.should.equal('b'); + done(); + }); + }); + }); }); - }); if (!getSchema().connector.replaceById) { describe.skip('replaceById - not implemented', function() {});