Skip to content

Commit 9f0c10f

Browse files
committed
Add test for updateOrCreate
* Add test for updateOrCreate when id is not autogenerated Id
1 parent 45e14af commit 9f0c10f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/manipulation.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,17 @@ describe('manipulation', function() {
658658
});
659659

660660
describe('updateOrCreate', function() {
661+
var ds = getSchema();
662+
var Post;
663+
664+
before('prepare "Post" model', function(done) {
665+
Post = ds.define('Post', {
666+
title: { type: String, id: true },
667+
content: { type: String },
668+
});
669+
ds.automigrate('Post', done);
670+
});
671+
661672
it('has an alias "patchOrCreate"', function() {
662673
StubUser.updateOrCreate.should.equal(StubUser.patchOrCreate);
663674
});
@@ -716,6 +727,37 @@ describe('manipulation', function() {
716727
});
717728
});
718729

730+
it('updates specific instances when PK is not an auto-generated id', function(done) {
731+
Post.create([
732+
{ title: 'postA', content: 'contentA' },
733+
{ title: 'postB', content: 'contentB' },
734+
], function(err, instance) {
735+
if (err) return done(err);
736+
737+
Post.updateOrCreate({
738+
title: 'postA', content: 'newContent',
739+
}, function(err, instance) {
740+
if (err) return done(err);
741+
742+
var result = instance.toObject();
743+
result.should.have.properties({
744+
title: 'postA',
745+
content: 'newContent',
746+
});
747+
Post.find(function(err, posts) {
748+
if (err) return done(err);
749+
750+
posts.should.have.length(2);
751+
posts[0].title.should.equal('postA');
752+
posts[0].content.should.equal('newContent');
753+
posts[1].title.should.equal('postB');
754+
posts[1].content.should.equal('contentB');
755+
done();
756+
});
757+
});
758+
});
759+
});
760+
719761
it('should allow save() of the created instance', function(done) {
720762
Person.updateOrCreate(
721763
{ id: 999 /* a new id */, name: 'a-name' },

0 commit comments

Comments
 (0)