Add test case using updateAttributes
This commit is contained in:
parent
f1d10b47ce
commit
3c19beacbb
|
@ -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() {});
|
||||
|
|
Loading…
Reference in New Issue