Fix `forceId` check for `replaceById`

This commit is contained in:
Amir Jafarian 2016-04-07 18:22:46 -04:00
parent 281f554365
commit 4e6351b856
2 changed files with 35 additions and 5 deletions

View File

@ -2657,7 +2657,7 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
if (!data[pkName]) data[pkName] = id;
var Model = this;
var inst = new Model(data);
var inst = new Model(data, { persisted: true });
var enforced = {};
this.applyProperties(enforced, inst);
inst.setAttributes(enforced);

View File

@ -4,8 +4,9 @@
// License text available at https://opensource.org/licenses/MIT
// This test written in mocha+should.js
var async = require('async');
var should = require('./init.js');
var db, User, options, whereCount = 0;
var db, User, options, ModelWithForceId, whereCount = 0;
var j = require('../');
var ValidationError = j.ValidationError;
@ -18,6 +19,10 @@ describe('optional-validation', function() {
before(function(done) {
db = getSchema();
ModelWithForceId = db.createModel(
'ModelWithForceId',
{ name: String },
{ forceId: true });
User = db.define('User', {
seq: { type: Number, index: true },
name: { type: String, index: true, sort: true },
@ -27,9 +32,7 @@ describe('optional-validation', function() {
order: { type: Number, index: true, sort: true },
vip: { type: Boolean },
}, { forceId: true, strict: true });
db.automigrate(['User'], done);
db.automigrate(['ModelWithForceId', 'User'], done);
});
beforeEach(function(done) {
@ -107,6 +110,33 @@ describe('optional-validation', function() {
return { name: 'DoesNotExist' + (whereCount++) };
}
describe('forceId', function() {
context('replaceAttributes', function() {
it('should not fail if you do not pass the Primary key in data object',
function(done) {
ModelWithForceId.create({ name: 'foo' }, function(err, created) {
if (err) return done(err);
created.replaceAttributes({ name: 'bar' }, function(err, data) {
done(err);
});
});
});
it('should fail if you pass the Primary key in data object',
function(done) {
ModelWithForceId.create({ name: 'foo' }, function(err, created) {
if (err) return done(err);
created.replaceAttributes({ name: 'bar', id: 999 },
function(err, data) {
should.exist(err);
done();
});
});
});
});
});
describe('no model setting', function() {
describe('method create', function() {