Add test for updateOrCreate
* Add test for updateOrCreate when id is not autogenerated Id Backport loopback-datasource-juggler#989
This commit is contained in:
parent
f1a70094fa
commit
6efd00698b
|
@ -654,6 +654,17 @@ describe('manipulation', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('updateOrCreate', 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() {
|
it('has an alias "patchOrCreate"', function() {
|
||||||
StubUser.updateOrCreate.should.equal(StubUser.patchOrCreate);
|
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) {
|
it('should allow save() of the created instance', function(done) {
|
||||||
Person.updateOrCreate(
|
Person.updateOrCreate(
|
||||||
{ id: 999 /* a new id */, name: 'a-name' },
|
{ id: 999 /* a new id */, name: 'a-name' },
|
||||||
|
|
Loading…
Reference in New Issue