From 6efd00698baa90c0da34a902aa3cb89d5a61b29d Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Fri, 1 Jul 2016 08:25:45 -0400 Subject: [PATCH] Add test for updateOrCreate * Add test for updateOrCreate when id is not autogenerated Id Backport loopback-datasource-juggler#989 --- test/manipulation.test.js | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 29afab09..d5793423 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -654,6 +654,17 @@ describe('manipulation', function() { }); describe('updateOrCreate', function() { + var ds = getSchema(); + var Post; + + before('prepare "Post" model', function(done) { + Post = ds.define('Post', { + title: { type: String, id: true }, + content: { type: String }, + }); + ds.automigrate('Post', done); + }); + it('has an alias "patchOrCreate"', function() { StubUser.updateOrCreate.should.equal(StubUser.patchOrCreate); }); @@ -712,6 +723,37 @@ describe('manipulation', function() { }); }); + it('updates specific instances when PK is not an auto-generated id', function(done) { + Post.create([ + { title: 'postA', content: 'contentA' }, + { title: 'postB', content: 'contentB' }, + ], function(err, instance) { + if (err) return done(err); + + Post.updateOrCreate({ + title: 'postA', content: 'newContent', + }, function(err, instance) { + if (err) return done(err); + + var result = instance.toObject(); + result.should.have.properties({ + title: 'postA', + content: 'newContent', + }); + Post.find(function(err, posts) { + if (err) return done(err); + + posts.should.have.length(2); + posts[0].title.should.equal('postA'); + posts[0].content.should.equal('newContent'); + posts[1].title.should.equal('postB'); + posts[1].content.should.equal('contentB'); + done(); + }); + }); + }); + }); + it('should allow save() of the created instance', function(done) { Person.updateOrCreate( { id: 999 /* a new id */, name: 'a-name' },