More fixes/tests

This commit is contained in:
Fabien Franzen 2014-09-05 17:09:23 +02:00
parent aebf5e9e6b
commit fafe51833b
3 changed files with 34 additions and 5 deletions

View File

@ -172,6 +172,7 @@ DataAccessObject.create = function (data, callback) {
if (err) { if (err) {
return callback(err, obj); return callback(err, obj);
} }
obj.__persisted = true;
saveDone.call(obj, function () { saveDone.call(obj, function () {
createDone.call(obj, function () { createDone.call(obj, function () {
callback(err, obj); callback(err, obj);
@ -961,8 +962,7 @@ DataAccessObject.prototype.save = function (options, callback) {
if (err) { if (err) {
return callback(err, inst); return callback(err, inst);
} }
inst._initProperties(data); inst._initProperties(data, { persisted: true });
inst.__persisted = true;
updateDone.call(inst, function () { updateDone.call(inst, function () {
saveDone.call(inst, function () { saveDone.call(inst, function () {
callback(err, inst); callback(err, inst);

View File

@ -69,7 +69,9 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
strict = ctor.definition.settings.strict; strict = ctor.definition.settings.strict;
} }
this.__persisted = options.persisted === true; if (options.persisted !== undefined) {
this.__persisted = options.persisted === true;
}
if (ctor.hideInternalProperties) { if (ctor.hideInternalProperties) {
// Object.defineProperty() is expensive. We only try to make the internal // Object.defineProperty() is expensive. We only try to make the internal

View File

@ -40,6 +40,18 @@ describe('manipulation', function () {
}); });
}); });
}); });
it('should instantiate an object', function (done) {
var p = new Person({name: 'Anatoliy'});
p.name.should.equal('Anatoliy');
p.isNewRecord().should.be.true;
p.save(function(err, inst) {
should.not.exist(err);
inst.isNewRecord().should.be.false;
inst.should.equal(p);
done();
});
});
it('should return instance of object', function (done) { it('should return instance of object', function (done) {
var person = Person.create(function (err, p) { var person = Person.create(function (err, p) {
@ -51,13 +63,28 @@ describe('manipulation', function () {
should.not.exist(person.id); should.not.exist(person.id);
}); });
it('should not allow user-defined value for the id of object', function (done) { it('should not allow user-defined value for the id of object - create', function (done) {
Person.create({ id: 123456 }, function (err, p) { Person.create({id: 123456}, function (err, p) {
err.should.be.instanceof(ValidationError); err.should.be.instanceof(ValidationError);
err.message.should.equal('The `Person` instance is not valid. Details: `id` can\'t be set.'); err.message.should.equal('The `Person` instance is not valid. Details: `id` can\'t be set.');
err.statusCode.should.equal(422); err.statusCode.should.equal(422);
p.should.be.instanceof(Person); p.should.be.instanceof(Person);
p.id.should.equal(123456); p.id.should.equal(123456);
p.isNewRecord().should.be.true;
done();
});
});
it('should not allow user-defined value for the id of object - save', function (done) {
var p = new Person({id: 123456});
p.isNewRecord().should.be.true;
p.save(function(err, inst) {
err.should.be.instanceof(ValidationError);
err.message.should.equal('The `Person` instance is not valid. Details: `id` can\'t be set.');
err.statusCode.should.equal(422);
inst.isNewRecord().should.be.true;
inst.id.should.equal(123456);
inst.isNewRecord().should.be.true;
done(); done();
}); });
}); });