Add test case using updateAttributes

This commit is contained in:
Loay 2017-04-17 18:18:20 -04:00
parent f1d10b47ce
commit 3c19beacbb
1 changed files with 59 additions and 48 deletions

View File

@ -913,60 +913,71 @@ describe('manipulation', function() {
}); });
}); });
describe('updateOrCreate when forceId is true', function() { bdd.describeIf(connectorCapabilities.supportForceId !== false,
var Post; 'updateOrCreate when forceId is true', function() {
before(function definePostModel(done) { var Post;
var ds = getSchema(); before(function definePostModel(done) {
Post = ds.define('Post', { var ds = getSchema();
title: {type: String, length: 255}, Post = ds.define('Post', {
content: {type: String}, title: {type: String, length: 255},
}, {forceId: true}); content: {type: String},
ds.automigrate('Post', done); }, {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();
}); });
});
it('fails when id does not exist in db & validate is false', function(done) { it('fails when id does not exist in db & validate is true', function(done) {
var unknownId = uid.fromConnector(db) || 123; var unknownId = uid.fromConnector(db) || 123;
var post = {id: unknownId, title: 'a', content: 'AAA'}; var post = {id: unknownId, title: 'a', content: 'AAA'};
Post.updateOrCreate(post, {validate: false}, (err) => { Post.updateOrCreate(post, {validate: true}, (err) => {
err.statusCode.should.equal(404); 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(); 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) { if (!getSchema().connector.replaceById) {
describe.skip('replaceById - not implemented', function() {}); describe.skip('replaceById - not implemented', function() {});