Merge pull request #554 from strongloop/fix/isNewInstance-tests
Fix isNewInstance tests + code cleanup
This commit is contained in:
commit
bc0f3b1d5c
12
lib/dao.js
12
lib/dao.js
|
@ -370,7 +370,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
}
|
}
|
||||||
|
|
||||||
var context = {
|
var context = {
|
||||||
Model: Model,
|
Model: Model,
|
||||||
query: byIdQuery(Model, id),
|
query: byIdQuery(Model, id),
|
||||||
hookState: hookState,
|
hookState: hookState,
|
||||||
options: options
|
options: options
|
||||||
|
@ -410,7 +410,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
self.getDataSource().connector
|
self.getDataSource().connector
|
||||||
.updateOrCreate(Model.modelName, update, done);
|
.updateOrCreate(Model.modelName, update, done);
|
||||||
|
|
||||||
function done(err, data, result) {
|
function done(err, data, info) {
|
||||||
var obj;
|
var obj;
|
||||||
if (data && !(data instanceof Model)) {
|
if (data && !(data instanceof Model)) {
|
||||||
inst._initProperties(data, { persisted: true });
|
inst._initProperties(data, { persisted: true });
|
||||||
|
@ -427,7 +427,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
|
||||||
var context = {
|
var context = {
|
||||||
Model: Model,
|
Model: Model,
|
||||||
instance: obj,
|
instance: obj,
|
||||||
isNewInstance: result ? result.isNewInstance : undefined,
|
isNewInstance: info ? info.isNewInstance : undefined,
|
||||||
hookState: hookState,
|
hookState: hookState,
|
||||||
options: options
|
options: options
|
||||||
};
|
};
|
||||||
|
@ -1356,11 +1356,11 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
|
||||||
cb = cb || utils.createPromiseCallback();
|
cb = cb || utils.createPromiseCallback();
|
||||||
where = where || {};
|
where = where || {};
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
assert(typeof where === 'object', 'The where argument must be an object');
|
assert(typeof where === 'object', 'The where argument must be an object');
|
||||||
assert(typeof options === 'object', 'The options argument must be an object');
|
assert(typeof options === 'object', 'The options argument must be an object');
|
||||||
assert(typeof cb === 'function', 'The cb argument must be a function');
|
assert(typeof cb === 'function', 'The cb argument must be a function');
|
||||||
|
|
||||||
var hookState = {};
|
var hookState = {};
|
||||||
|
|
||||||
var query = { where: where };
|
var query = { where: where };
|
||||||
|
@ -1593,7 +1593,7 @@ DataAccessObject.prototype.save = function (options, cb) {
|
||||||
|
|
||||||
assert(typeof options === 'object', 'The options argument should be an object');
|
assert(typeof options === 'object', 'The options argument should be an object');
|
||||||
assert(typeof cb === 'function', 'The cb argument should be a function');
|
assert(typeof cb === 'function', 'The cb argument should be a function');
|
||||||
|
|
||||||
var hookState = {};
|
var hookState = {};
|
||||||
|
|
||||||
if (options.validate === undefined) {
|
if (options.validate === undefined) {
|
||||||
|
|
|
@ -525,7 +525,8 @@ module.exports = function(dataSource, should) {
|
||||||
extra: undefined
|
extra: undefined
|
||||||
},
|
},
|
||||||
isNewInstance: false,
|
isNewInstance: false,
|
||||||
options: { throws: false, validate: true } }));
|
options: { throws: false, validate: true }
|
||||||
|
}));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -533,7 +534,10 @@ module.exports = function(dataSource, should) {
|
||||||
it('triggers `after save` hook on create', function(done) {
|
it('triggers `after save` hook on create', function(done) {
|
||||||
TestModel.observe('after save', pushContextAndNext());
|
TestModel.observe('after save', pushContextAndNext());
|
||||||
|
|
||||||
var instance = new TestModel({ name: 'created' });
|
var instance = new TestModel(
|
||||||
|
{ id: 'new-id', name: 'created' },
|
||||||
|
{ persisted: true });
|
||||||
|
|
||||||
instance.save(function(err, instance) {
|
instance.save(function(err, instance) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
observedContexts.should.eql(aTestModelCtx({
|
observedContexts.should.eql(aTestModelCtx({
|
||||||
|
@ -542,7 +546,8 @@ module.exports = function(dataSource, should) {
|
||||||
name: 'created',
|
name: 'created',
|
||||||
extra: undefined
|
extra: undefined
|
||||||
},
|
},
|
||||||
isNewInstance: true
|
isNewInstance: true,
|
||||||
|
options: { throws: false, validate: true }
|
||||||
}));
|
}));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue