loopback-datasource-juggler/test/persistence-hooks.suite.js

3377 lines
113 KiB
JavaScript
Raw Normal View History

2016-04-01 22:25:16 +00:00
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
var ValidationError = require('../').ValidationError;
2016-04-12 11:42:57 +00:00
var contextTestHelpers = require('./helpers/context-test-helpers');
var ContextRecorder = contextTestHelpers.ContextRecorder;
var deepCloneToObject = contextTestHelpers.deepCloneToObject;
var aCtxForModel = contextTestHelpers.aCtxForModel;
2016-04-12 12:32:34 +00:00
var uid = require('./helpers/uid-generator');
var getLastGeneratedUid = uid.last;
2016-04-12 13:06:42 +00:00
var HookMonitor = require('./helpers/hook-monitor');
module.exports = function(dataSource, should, connectorCapabilities) {
if (!connectorCapabilities) connectorCapabilities = {};
if (connectorCapabilities.replaceOrCreateReportsNewInstance === undefined) {
2016-04-01 13:23:42 +00:00
console.warn('The connector does not support a recently added feature:' +
' replaceOrCreateReportsNewInstance');
}
describe('Persistence hooks', function() {
2016-04-12 13:06:42 +00:00
var ctxRecorder, hookMonitor, expectedError;
var TestModel, existingInstance;
2016-04-12 12:32:34 +00:00
var migrated = false;
var undefinedValue = undefined;
beforeEach(function setupDatabase(done) {
2016-04-12 11:42:57 +00:00
ctxRecorder = new ContextRecorder('hook not called');
2016-08-19 17:46:59 +00:00
hookMonitor = new HookMonitor({includeModelName: false});
expectedError = new Error('test error');
TestModel = dataSource.createModel('TestModel', {
2015-06-16 20:35:35 +00:00
// Set id.generated to false to honor client side values
2016-08-19 17:46:59 +00:00
id: {type: String, id: true, generated: false, default: uid.next},
name: {type: String, required: true},
extra: {type: String, required: false},
});
2016-04-12 12:32:34 +00:00
uid.reset();
if (migrated) {
TestModel.deleteAll(done);
} else {
dataSource.automigrate(TestModel.modelName, function(err) {
migrated = true;
done(err);
});
}
});
beforeEach(function createTestData(done) {
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'first'}, function(err, instance) {
if (err) return done(err);
// Look it up from DB so that default values are retrieved
TestModel.findById(instance.id, function(err, instance) {
existingInstance = instance;
undefinedValue = existingInstance.extra;
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'second'}, function(err) {
if (err) return done(err);
done();
});
});
});
});
describe('PersistedModel.find', function() {
2015-07-21 09:05:55 +00:00
it('triggers hooks in the correct order', function(done) {
monitorHookExecution();
TestModel.find(
2016-08-19 17:46:59 +00:00
{where: {id: '1'}},
2015-07-21 09:05:55 +00:00
function(err, list) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
2015-07-21 09:05:55 +00:00
'access',
2016-04-01 11:48:17 +00:00
'loaded',
2015-07-21 09:05:55 +00:00
]);
done();
});
});
2016-04-09 14:59:09 +00:00
it('should not trigger hooks, if notify is false', function(done) {
monitorHookExecution();
TestModel.find(
2016-08-19 17:46:59 +00:00
{where: {id: '1'}},
{notify: false},
2016-04-09 14:59:09 +00:00
function(err, list) {
if (err) return done(err);
hookMonitor.names.should.be.empty();
done();
});
});
it('should not trigger hooks for geo queries, if notify is false',
function(done) {
monitorHookExecution();
TestModel.find(
2016-08-19 17:46:59 +00:00
{where: {geo: {near: '10,20'}}},
{notify: false},
2016-04-09 14:59:09 +00:00
function(err, list) {
if (err) return done(err);
hookMonitor.names.should.be.empty();
done();
});
});
it('should apply updates from `access` hook', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {name: 'second'}};
2016-04-09 14:59:09 +00:00
next();
});
2016-08-19 17:46:59 +00:00
TestModel.find({name: 'first'}, function(err, list) {
2016-04-09 14:59:09 +00:00
if (err) return done(err);
list.map(get('name')).should.eql(['second']);
done();
});
});
it('triggers `access` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.find({where: {id: '1'}}, function(err, list) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
query: {where: {id: '1'}},
}));
done();
});
});
it('aborts when `access` hook fails', function(done) {
TestModel.observe('access', nextWithError(expectedError));
TestModel.find(function(err, list) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `access` hook', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: existingInstance.id}};
next();
});
TestModel.find(function(err, list) {
if (err) return done(err);
list.map(get('name')).should.eql([existingInstance.name]);
done();
});
});
it('triggers `access` hook for geo queries', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.find({where: {geo: {near: '10,20'}}}, function(err, list) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
query: {where: {geo: {near: '10,20'}}},
}));
done();
});
});
it('applies updates from `access` hook for geo queries', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: existingInstance.id}};
next();
});
2016-08-19 17:46:59 +00:00
TestModel.find({where: {geo: {near: '10,20'}}}, function(err, list) {
if (err) return done(err);
list.map(get('name')).should.eql([existingInstance.name]);
done();
});
});
2015-07-21 09:05:55 +00:00
it('applies updates from `loaded` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext(function(ctx) {
ctx.data.extra = 'hook data';
2015-07-21 09:05:55 +00:00
}));
TestModel.find(
2016-08-19 17:46:59 +00:00
{where: {id: 1}},
2016-04-01 11:48:17 +00:00
function(err, list) {
2015-07-21 09:05:55 +00:00
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
data: {
2016-04-01 11:48:17 +00:00
id: '1',
name: 'first',
2016-04-01 13:23:42 +00:00
extra: 'hook data',
2015-07-21 09:05:55 +00:00
},
isNewInstance: false,
2016-04-01 11:48:17 +00:00
options: {},
2015-07-21 09:05:55 +00:00
}));
list[0].should.have.property('extra', 'hook data');
2015-07-21 09:05:55 +00:00
done();
});
2016-04-01 11:48:17 +00:00
});
2015-07-21 09:05:55 +00:00
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
TestModel.find(
2016-08-19 17:46:59 +00:00
{where: {id: 1}},
2015-07-21 09:05:55 +00:00
function(err, list) {
[err].should.eql([expectedError]);
done();
});
});
});
describe('PersistedModel.create', function() {
2015-05-21 11:51:30 +00:00
it('triggers hooks in the correct order', function(done) {
monitorHookExecution();
TestModel.create(
2016-08-19 17:46:59 +00:00
{name: 'created'},
2015-05-21 11:51:30 +00:00
function(err, record, created) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
2015-05-21 11:51:30 +00:00
'before save',
'persist',
'loaded',
2016-04-01 11:48:17 +00:00
'after save',
2015-05-21 11:51:30 +00:00
]);
done();
});
});
it('triggers `before save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'created'}, function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: instance.id,
name: 'created',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
done();
});
});
it('aborts when `before save` hook fails', function(done) {
TestModel.observe('before save', nextWithError(expectedError));
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'created'}, function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `before save` hook', function(done) {
TestModel.observe('before save', function(ctx, next) {
ctx.instance.should.be.instanceOf(TestModel);
ctx.instance.extra = 'hook data';
next();
});
2016-08-19 17:46:59 +00:00
TestModel.create({id: uid.next(), name: 'a-name'}, function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('sends `before save` for each model in an array', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.create(
2016-08-19 17:46:59 +00:00
[{name: '1'}, {name: '2'}],
function(err, list) {
if (err) return done(err);
// Creation of multiple instances is executed in parallel
2016-04-12 11:42:57 +00:00
ctxRecorder.records.sort(function(c1, c2) {
return c1.instance.name - c2.instance.name;
});
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql([
aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
instance: {id: list[0].id, name: '1', extra: undefined},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}),
2016-04-12 11:42:57 +00:00
aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
instance: {id: list[1].id, name: '2', extra: undefined},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}),
]);
done();
});
});
it('validates model after `before save` hook', function(done) {
TestModel.observe('before save', invalidateTestModel());
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'created'}, function(err) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
2015-05-13 23:14:40 +00:00
it('triggers `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2015-05-13 23:14:40 +00:00
TestModel.create(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-05-13 23:14:40 +00:00
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
data: {id: 'new-id', name: 'a name'},
2015-05-13 23:14:40 +00:00
isNewInstance: true,
2016-08-19 17:46:59 +00:00
currentInstance: {extra: null, id: 'new-id', name: 'a name'},
2015-05-13 23:14:40 +00:00
}));
done();
});
});
it('applies updates from `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2015-05-13 23:14:40 +00:00
ctx.data.extra = 'hook data';
}));
2015-07-02 21:56:46 +00:00
// By default, the instance passed to create callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
2015-05-21 11:51:30 +00:00
// which if set, will apply these changes to the model instance too.
TestModel.settings.updateOnLoad = true;
2015-05-13 23:14:40 +00:00
TestModel.create(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-05-13 23:14:40 +00:00
function(err, instance) {
if (err) return done(err);
2015-05-21 11:51:30 +00:00
instance.should.have.property('extra', 'hook data');
2015-05-13 23:14:40 +00:00
2015-07-02 21:56:46 +00:00
// Also query the database here to verify that, on `create`
2015-05-13 23:14:40 +00:00
// updates from `persist` hook are reflected into database
TestModel.findById('new-id', function(err, dbInstance) {
if (err) return done(err);
2015-07-02 21:56:46 +00:00
should.exists(dbInstance);
2015-05-13 23:14:40 +00:00
dbInstance.toObject(true).should.eql({
id: 'new-id',
name: 'a name',
2016-04-01 11:48:17 +00:00
extra: 'hook data',
2015-05-13 23:14:40 +00:00
});
done();
2015-05-13 23:14:40 +00:00
});
});
});
2015-05-21 11:51:30 +00:00
it('triggers `loaded` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2015-05-21 11:51:30 +00:00
2015-07-02 21:56:46 +00:00
// By default, the instance passed to create callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
2015-05-21 11:51:30 +00:00
// which if set, will apply these changes to the model instance too.
TestModel.settings.updateOnLoad = true;
TestModel.create(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-05-21 11:51:30 +00:00
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
data: {id: 'new-id', name: 'a name'},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
2015-05-21 11:51:30 +00:00
}));
done();
});
});
2015-07-21 09:05:55 +00:00
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
TestModel.create(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-07-21 09:05:55 +00:00
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
2015-05-21 11:51:30 +00:00
it('applies updates from `loaded` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext(function(ctx) {
2015-05-21 11:51:30 +00:00
ctx.data.extra = 'hook data';
}));
2015-07-02 21:56:46 +00:00
// By default, the instance passed to create callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
2015-05-21 11:51:30 +00:00
// which if set, will apply these changes to the model instance too.
TestModel.settings.updateOnLoad = true;
TestModel.create(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-05-21 11:51:30 +00:00
function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('triggers `after save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'created'}, function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: instance.id,
name: 'created',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
done();
});
});
it('aborts when `after save` hook fails', function(done) {
TestModel.observe('after save', nextWithError(expectedError));
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'created'}, function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `after save` hook', function(done) {
TestModel.observe('after save', function(ctx, next) {
ctx.instance.should.be.instanceOf(TestModel);
ctx.instance.extra = 'hook data';
next();
});
2016-08-19 17:46:59 +00:00
TestModel.create({name: 'a-name'}, function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('sends `after save` for each model in an array', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.create(
2016-08-19 17:46:59 +00:00
[{name: '1'}, {name: '2'}],
function(err, list) {
if (err) return done(err);
// Creation of multiple instances is executed in parallel
2016-04-12 11:42:57 +00:00
ctxRecorder.records.sort(function(c1, c2) {
return c1.instance.name - c2.instance.name;
});
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql([
aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
instance: {id: list[0].id, name: '1', extra: undefined},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}),
2016-04-12 11:42:57 +00:00
aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
instance: {id: list[1].id, name: '2', extra: undefined},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}),
]);
done();
});
});
it('emits `after save` when some models were not saved', function(done) {
TestModel.observe('before save', function(ctx, next) {
if (ctx.instance.name === 'fail')
next(expectedError);
else
next();
});
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.create(
2016-08-19 17:46:59 +00:00
[{name: 'ok'}, {name: 'fail'}],
function(err, list) {
(err || []).should.have.length(2);
err[1].should.eql(expectedError);
// NOTE(bajtos) The current implementation of `Model.create(array)`
// passes all models in the second callback argument, including
// the models that were not created due to an error.
list.map(get('name')).should.eql(['ok', 'fail']);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
instance: {id: list[0].id, name: 'ok', extra: undefined},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
done();
});
});
});
describe('PersistedModel.findOrCreate', function() {
it('triggers `access` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
function(err, record, created) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {name: 'new-record'},
limit: 1,
offset: 0,
2016-04-01 11:48:17 +00:00
skip: 0,
}}));
done();
});
});
if (dataSource.connector.findOrCreate) {
it('triggers `before save` hook when found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: existingInstance.name}},
{name: existingInstance.name},
function(err, record, created) {
if (err) return done(err);
record.id.should.eql(existingInstance.id);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: getLastGeneratedUid(),
name: existingInstance.name,
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
done();
});
});
}
it('triggers `before save` hook when not found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
function(err, record, created) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: record.id,
name: 'new-record',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
done();
});
});
it('validates model after `before save` hook', function(done) {
TestModel.observe('before save', invalidateTestModel());
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
function(err) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
it('triggers hooks in the correct order when not found', function(done) {
2015-05-21 11:51:30 +00:00
monitorHookExecution();
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
function(err, record, created) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
'access',
'before save',
2015-05-13 23:14:40 +00:00
'persist',
2015-05-21 11:51:30 +00:00
'loaded',
2016-04-01 11:48:17 +00:00
'after save',
]);
done();
});
});
2015-05-21 11:51:30 +00:00
it('triggers hooks in the correct order when found', function(done) {
monitorHookExecution();
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: existingInstance.name}},
{name: existingInstance.name},
2015-05-21 11:51:30 +00:00
function(err, record, created) {
if (err) return done(err);
if (dataSource.connector.findOrCreate) {
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
2015-05-21 11:51:30 +00:00
'access',
'before save',
'persist',
2016-04-01 11:48:17 +00:00
'loaded',
2015-05-21 11:51:30 +00:00
]);
} else {
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
2015-05-21 11:51:30 +00:00
'access',
2016-04-01 11:48:17 +00:00
'loaded',
2015-05-21 11:51:30 +00:00
]);
}
done();
});
});
it('aborts when `access` hook fails', function(done) {
TestModel.observe('access', nextWithError(expectedError));
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {id: 'does-not-exist'}},
{name: 'does-not-exist'},
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('aborts when `before save` hook fails', function(done) {
TestModel.observe('before save', nextWithError(expectedError));
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {id: 'does-not-exist'}},
{name: 'does-not-exist'},
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
2015-05-13 23:14:40 +00:00
if (dataSource.connector.findOrCreate) {
it('triggers `persist` hook when found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2015-05-13 23:14:40 +00:00
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: existingInstance.name}},
{name: existingInstance.name},
2015-05-13 23:14:40 +00:00
function(err, record, created) {
if (err) return done(err);
record.id.should.eql(existingInstance.id);
// `findOrCreate` creates a new instance of the object everytime.
// So, `data.id` as well as `currentInstance.id` always matches
2015-06-16 20:35:35 +00:00
// the newly generated UID.
// Hence, the test below asserts both `data.id` and
2015-05-13 23:14:40 +00:00
// `currentInstance.id` to match getLastGeneratedUid().
// On same lines, it also asserts `isNewInstance` to be true.
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-13 23:14:40 +00:00
data: {
id: getLastGeneratedUid(),
2016-04-01 11:48:17 +00:00
name: existingInstance.name,
2015-05-13 23:14:40 +00:00
},
isNewInstance: true,
currentInstance: {
id: getLastGeneratedUid(),
name: record.name,
2016-04-01 11:48:17 +00:00
extra: null,
2015-05-13 23:14:40 +00:00
},
2016-08-19 17:46:59 +00:00
where: {name: existingInstance.name},
2015-05-13 23:14:40 +00:00
}));
done();
});
});
}
it('triggers `persist` hook when not found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2015-05-13 23:14:40 +00:00
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
2015-05-13 23:14:40 +00:00
function(err, record, created) {
if (err) return done(err);
// `context.where` is present in Optimized connector context,
// but, unoptimized connector does NOT have it.
if (dataSource.connector.findOrCreate) {
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-13 23:14:40 +00:00
data: {
id: record.id,
2016-04-01 11:48:17 +00:00
name: 'new-record',
2015-05-13 23:14:40 +00:00
},
isNewInstance: true,
currentInstance: {
id: record.id,
name: record.name,
2016-04-01 11:48:17 +00:00
extra: null,
2015-05-13 23:14:40 +00:00
},
2016-08-19 17:46:59 +00:00
where: {name: 'new-record'},
2015-05-13 23:14:40 +00:00
}));
} else {
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-13 23:14:40 +00:00
data: {
id: record.id,
2016-04-01 11:48:17 +00:00
name: 'new-record',
2015-05-13 23:14:40 +00:00
},
isNewInstance: true,
2016-08-19 17:46:59 +00:00
currentInstance: {id: record.id, name: record.name, extra: null},
2015-05-13 23:14:40 +00:00
}));
}
done();
});
});
if (dataSource.connector.findOrCreate) {
it('applies updates from `persist` hook when found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2015-05-13 23:14:40 +00:00
ctx.data.extra = 'hook data';
}));
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: existingInstance.name}},
{name: existingInstance.name},
2015-05-13 23:14:40 +00:00
function(err, instance) {
if (err) return done(err);
// instance returned by `findOrCreate` context does not
// have the values updated from `persist` hook
instance.should.not.have.property('extra', 'hook data');
2015-06-16 20:35:35 +00:00
// Query the database. Here, since record already exists
2015-05-13 23:14:40 +00:00
// `findOrCreate`, does not update database for
// updates from `persist` hook
TestModel.findById(existingInstance.id, function(err, dbInstance) {
if (err) return done(err);
2015-07-02 21:56:46 +00:00
should.exists(dbInstance);
2015-05-13 23:14:40 +00:00
dbInstance.toObject(true).should.eql({
id: existingInstance.id,
name: existingInstance.name,
2016-04-01 11:48:17 +00:00
extra: undefined,
2015-05-13 23:14:40 +00:00
});
});
done();
});
});
}
it('applies updates from `persist` hook when not found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2015-05-13 23:14:40 +00:00
ctx.data.extra = 'hook data';
}));
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
2015-05-13 23:14:40 +00:00
function(err, instance) {
if (err) return done(err);
if (dataSource.connector.findOrCreate) {
instance.should.have.property('extra', 'hook data');
} else {
// Unoptimized connector gives a call to `create. And during
2015-06-16 20:35:35 +00:00
// create the updates applied through persist hook are
// reflected into the database, but the same updates are
// NOT reflected in the instance object obtained in callback
2015-05-13 23:14:40 +00:00
// of create.
2015-06-16 20:35:35 +00:00
// So, this test asserts unoptimized connector to
// NOT have `extra` property. And then verifes that the
2015-05-13 23:14:40 +00:00
// property `extra` is actually updated in DB
instance.should.not.have.property('extra', 'hook data');
TestModel.findById(instance.id, function(err, dbInstance) {
if (err) return done(err);
2015-07-02 21:56:46 +00:00
should.exists(dbInstance);
2015-05-13 23:14:40 +00:00
dbInstance.toObject(true).should.eql({
id: instance.id,
name: instance.name,
2016-04-01 11:48:17 +00:00
extra: 'hook data',
2015-05-13 23:14:40 +00:00
});
});
}
done();
});
});
2015-05-21 11:51:30 +00:00
if (dataSource.connector.findOrCreate) {
it('triggers `loaded` hook when found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2015-05-21 11:51:30 +00:00
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: existingInstance.name}},
{name: existingInstance.name},
2015-05-21 11:51:30 +00:00
function(err, record, created) {
if (err) return done(err);
record.id.should.eql(existingInstance.id);
2015-07-02 21:56:46 +00:00
// After the call to `connector.findOrCreate`, since the record
// already exists, `data.id` matches `existingInstance.id`
// as against the behaviour noted for `persist` hook
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-21 11:51:30 +00:00
data: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: existingInstance.name,
2015-05-21 11:51:30 +00:00
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
2015-05-21 11:51:30 +00:00
}));
done();
});
});
}
it('triggers `loaded` hook when not found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2015-05-21 11:51:30 +00:00
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
2015-05-21 11:51:30 +00:00
function(err, record, created) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-21 11:51:30 +00:00
data: {
id: record.id,
2016-04-01 11:48:17 +00:00
name: 'new-record',
2015-05-21 11:51:30 +00:00
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
2015-05-21 11:51:30 +00:00
}));
done();
});
});
2015-07-21 09:05:55 +00:00
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
2015-07-21 09:05:55 +00:00
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
2015-05-21 11:51:30 +00:00
if (dataSource.connector.findOrCreate) {
it('applies updates from `loaded` hook when found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext(function(ctx) {
2015-05-21 11:51:30 +00:00
ctx.data.extra = 'hook data';
}));
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: existingInstance.name}},
{name: existingInstance.name},
2015-05-21 11:51:30 +00:00
function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
}
it('applies updates from `loaded` hook when not found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext(function(ctx) {
2015-05-21 11:51:30 +00:00
ctx.data.extra = 'hook data';
}));
// Unoptimized connector gives a call to `create. But,
2015-07-02 21:56:46 +00:00
// by default, the instance passed to create callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
2015-05-21 11:51:30 +00:00
// which if set, will apply these changes to the model instance too.
// Note - in case of findOrCreate, this setting is needed ONLY for
// unoptimized connector.
TestModel.settings.updateOnLoad = true;
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new-record'}},
{name: 'new-record'},
2015-05-21 11:51:30 +00:00
function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('triggers `after save` hook when not found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {name: 'new name'}},
{name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: instance.id,
name: 'new name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
done();
});
});
it('does not trigger `after save` hook when found', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.findOrCreate(
2016-08-19 17:46:59 +00:00
{where: {id: existingInstance.id}},
{name: existingInstance.name},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql('hook not called');
done();
});
});
});
describe('PersistedModel.count', function(done) {
it('triggers `access` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.count({id: existingInstance.id}, function(err, count) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {id: existingInstance.id},
}}));
done();
});
});
it('applies updates from `access` hook', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query.where = {id: existingInstance.id};
next();
});
TestModel.count(function(err, count) {
if (err) return done(err);
count.should.equal(1);
done();
});
});
});
describe('PersistedModel.prototype.save', function() {
2015-05-21 11:51:30 +00:00
it('triggers hooks in the correct order', function(done) {
monitorHookExecution();
existingInstance.save(
function(err, record, created) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
2015-05-21 11:51:30 +00:00
'before save',
'persist',
'loaded',
2016-04-01 11:48:17 +00:00
'after save',
2015-05-21 11:51:30 +00:00
]);
done();
});
});
it('triggers `before save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
existingInstance.name = 'changed';
existingInstance.save(function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {instance: {
id: existingInstance.id,
name: 'changed',
extra: undefined,
2016-08-19 17:46:59 +00:00
}, options: {throws: false, validate: true}}));
done();
});
});
it('aborts when `before save` hook fails', function(done) {
TestModel.observe('before save', nextWithError(expectedError));
existingInstance.save(function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `before save` hook', function(done) {
TestModel.observe('before save', function(ctx, next) {
ctx.instance.should.be.instanceOf(TestModel);
ctx.instance.extra = 'hook data';
next();
});
existingInstance.save(function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('validates model after `before save` hook', function(done) {
TestModel.observe('before save', invalidateTestModel());
existingInstance.save(function(err) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
2015-05-13 23:14:40 +00:00
it('triggers `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2015-05-13 23:14:40 +00:00
existingInstance.name = 'changed';
existingInstance.save(function(err, instance) {
if (err) return done(err);
2015-06-16 20:35:35 +00:00
// HACK: extra is undefined for NoSQL and null for SQL
2016-04-12 11:42:57 +00:00
delete ctxRecorder.records.data.extra;
delete ctxRecorder.records.currentInstance.extra;
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-13 23:14:40 +00:00
data: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: 'changed',
2015-05-13 23:14:40 +00:00
},
currentInstance: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: 'changed',
2015-05-13 23:14:40 +00:00
},
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
options: {throws: false, validate: true},
2015-05-13 23:14:40 +00:00
}));
done();
});
});
it('applies updates from `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2015-05-13 23:14:40 +00:00
ctx.data.extra = 'hook data';
}));
existingInstance.save(function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
2015-05-21 11:51:30 +00:00
it('triggers `loaded` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2015-05-21 11:51:30 +00:00
existingInstance.extra = 'changed';
2015-05-21 11:51:30 +00:00
existingInstance.save(function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-21 11:51:30 +00:00
data: {
id: existingInstance.id,
name: existingInstance.name,
extra: 'changed',
2015-05-21 11:51:30 +00:00
},
isNewInstance: false,
2016-08-19 17:46:59 +00:00
options: {throws: false, validate: true},
2015-05-21 11:51:30 +00:00
}));
done();
});
});
2015-07-21 09:05:55 +00:00
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
existingInstance.save(
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
2015-05-21 11:51:30 +00:00
it('applies updates from `loaded` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext(function(ctx) {
2015-05-21 11:51:30 +00:00
ctx.data.extra = 'hook data';
}));
existingInstance.save(function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('triggers `after save` hook on update', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
existingInstance.name = 'changed';
existingInstance.save(function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: existingInstance.id,
name: 'changed',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
isNewInstance: false,
2016-08-19 17:46:59 +00:00
options: {throws: false, validate: true},
}));
done();
});
});
it('triggers `after save` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
2016-05-31 15:13:28 +00:00
// The rationale behind passing { persisted: true } is to bypass the check
// made by DAO to determine whether the instance should be saved via
// PersistedModel.create and force it to call connector.save()
var instance = new TestModel(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'created'},
{persisted: true});
instance.save(function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: instance.id,
name: 'created',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
isNewInstance: true,
2016-08-19 17:46:59 +00:00
options: {throws: false, validate: true},
}));
done();
});
});
it('aborts when `after save` hook fails', function(done) {
TestModel.observe('after save', nextWithError(expectedError));
existingInstance.save(function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `after save` hook', function(done) {
TestModel.observe('after save', function(ctx, next) {
ctx.instance.should.be.instanceOf(TestModel);
ctx.instance.extra = 'hook data';
next();
});
existingInstance.save(function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
});
describe('PersistedModel.prototype.updateAttributes', function() {
2015-05-21 11:51:30 +00:00
it('triggers hooks in the correct order', function(done) {
monitorHookExecution();
existingInstance.updateAttributes(
2016-08-19 17:46:59 +00:00
{name: 'changed'},
2015-05-21 11:51:30 +00:00
function(err, record, created) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
2015-05-21 11:51:30 +00:00
'before save',
'persist',
'loaded',
2016-04-01 11:48:17 +00:00
'after save',
2015-05-21 11:51:30 +00:00
]);
done();
});
});
it('triggers `before save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
var currentInstance = deepCloneToObject(existingInstance);
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'changed'}, function(err) {
if (err) return done(err);
existingInstance.name.should.equal('changed');
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {name: 'changed'},
2016-04-01 11:48:17 +00:00
currentInstance: currentInstance,
}));
done();
});
});
it('aborts when `before save` hook fails', function(done) {
TestModel.observe('before save', nextWithError(expectedError));
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'updated'}, function(err) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `before save` hook', function(done) {
TestModel.observe('before save', function(ctx, next) {
ctx.data.extra = 'extra data';
ctx.data.name = 'hooked name';
next();
});
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'updated'}, function(err) {
if (err) return done(err);
// We must query the database here because `updateAttributes`
// returns effectively `this`, not the data from the datasource
TestModel.findById(existingInstance.id, function(err, instance) {
if (err) return done(err);
2015-07-02 21:56:46 +00:00
should.exists(instance);
instance.toObject(true).should.eql({
id: existingInstance.id,
name: 'hooked name',
2016-04-01 11:48:17 +00:00
extra: 'extra data',
});
done();
});
});
});
it('validates model after `before save` hook', function(done) {
TestModel.observe('before save', invalidateTestModel());
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'updated'}, function(err) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
2015-05-13 23:14:40 +00:00
it('triggers `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'changed'}, function(err) {
2015-05-13 23:14:40 +00:00
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {name: 'changed'},
2015-05-13 23:14:40 +00:00
currentInstance: {
id: existingInstance.id,
name: 'changed',
2016-04-01 11:48:17 +00:00
extra: null,
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
2015-05-13 23:14:40 +00:00
}));
done();
});
});
it('applies updates from `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2015-05-13 23:14:40 +00:00
ctx.data.extra = 'hook data';
}));
2015-07-02 21:56:46 +00:00
// By default, the instance passed to updateAttributes callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
2015-05-21 11:51:30 +00:00
// which if set, will apply these changes to the model instance too.
TestModel.settings.updateOnLoad = true;
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'changed'}, function(err, instance) {
2015-05-21 11:51:30 +00:00
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('applies updates from `persist` hook - for nested model instance', function(done) {
2015-07-30 18:24:47 +00:00
var Address = dataSource.createModel('NestedAddress', {
2016-08-19 17:46:59 +00:00
id: {type: String, id: true, default: 1},
city: {type: String, required: true},
country: {type: String, required: true},
});
2015-07-30 18:24:47 +00:00
var User = dataSource.createModel('UserWithAddress', {
2016-08-19 17:46:59 +00:00
id: {type: String, id: true, default: uid.next},
name: {type: String, required: true},
address: {type: Address, required: false},
extra: {type: String},
});
2015-07-30 18:24:47 +00:00
dataSource.automigrate(['UserWithAddress', 'NestedAddress'], function(err) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
User.create({name: 'Joe'}, function(err, instance) {
2015-07-30 18:24:47 +00:00
if (err) return done(err);
2015-07-30 18:24:47 +00:00
var existingUser = instance;
2016-04-12 11:42:57 +00:00
User.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2016-04-01 11:48:17 +00:00
should.exist(ctx.data.address);
2015-07-30 18:24:47 +00:00
ctx.data.address.should.be.type('object');
ctx.data.address.should.not.be.instanceOf(Address);
2015-07-30 18:24:47 +00:00
ctx.data.extra = 'hook data';
}));
2015-07-30 18:24:47 +00:00
// By default, the instance passed to updateAttributes callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
// which if set, will apply these changes to the model instance too.
User.settings.updateOnLoad = true;
existingUser.updateAttributes(
2016-08-19 17:46:59 +00:00
{address: new Address({city: 'Springfield', country: 'USA'})},
2015-07-30 18:24:47 +00:00
function(err, inst) {
if (err) return done(err);
2015-07-30 18:24:47 +00:00
inst.should.have.property('extra', 'hook data');
User.findById(existingUser.id, function(err, dbInstance) {
if (err) return done(err);
dbInstance.toObject(true).should.eql({
id: existingUser.id,
name: existingUser.name,
2016-08-19 17:46:59 +00:00
address: {id: '1', city: 'Springfield', country: 'USA'},
2016-04-01 11:48:17 +00:00
extra: 'hook data',
2015-07-30 18:24:47 +00:00
});
done();
});
});
2015-07-30 18:24:47 +00:00
});
});
});
2015-05-21 11:51:30 +00:00
it('triggers `loaded` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'changed'}, function(err) {
2015-05-21 11:51:30 +00:00
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
data: {name: 'changed'},
2015-05-21 11:51:30 +00:00
}));
done();
});
});
2015-07-21 09:05:55 +00:00
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
existingInstance.updateAttributes(
2016-08-19 17:46:59 +00:00
{name: 'changed'},
2015-07-21 09:05:55 +00:00
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
2015-05-21 11:51:30 +00:00
it('applies updates from `loaded` hook updateAttributes', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext(function(ctx) {
2015-05-21 11:51:30 +00:00
ctx.data.extra = 'hook data';
}));
2015-07-02 21:56:46 +00:00
// By default, the instance passed to updateAttributes callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
2015-05-21 11:51:30 +00:00
// which if set, will apply these changes to the model instance too.
TestModel.settings.updateOnLoad = true;
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'changed'}, function(err, instance) {
2015-05-13 23:14:40 +00:00
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
it('triggers `after save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
existingInstance.name = 'changed';
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'changed'}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: existingInstance.id,
name: 'changed',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
}));
done();
});
});
it('aborts when `after save` hook fails', function(done) {
TestModel.observe('after save', nextWithError(expectedError));
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'updated'}, function(err) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `after save` hook', function(done) {
TestModel.observe('after save', function(ctx, next) {
ctx.instance.should.be.instanceOf(TestModel);
ctx.instance.extra = 'hook data';
next();
});
2016-08-19 17:46:59 +00:00
existingInstance.updateAttributes({name: 'updated'}, function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
});
});
2016-04-01 11:48:17 +00:00
if (!getSchema().connector.replaceById) {
2016-04-01 11:48:17 +00:00
describe.skip('replaceById - not implemented', function() {});
} else {
describe('PersistedModel.prototype.replaceAttributes', function() {
it('triggers hooks in the correct order', function(done) {
monitorHookExecution();
existingInstance.replaceAttributes(
2016-08-19 17:46:59 +00:00
{name: 'replaced'},
function(err, record, created) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
'before save',
'persist',
'loaded',
2016-04-01 11:48:17 +00:00
'after save',
]);
done();
});
});
it('triggers `before save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'changed'}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: existingInstance.id,
name: 'changed',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
}));
done();
});
});
it('aborts when `before save` hook fails', function(done) {
TestModel.observe('before save', nextWithError(expectedError));
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'replaced'}, function(err) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `before save` hook', function(done) {
TestModel.observe('before save', function(ctx, next) {
ctx.instance.extra = 'extra data';
ctx.instance.name = 'hooked name';
next();
});
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'updated'}, function(err) {
if (err) return done(err);
TestModel.findById(existingInstance.id, function(err, instance) {
if (err) return done(err);
should.exists(instance);
instance.toObject(true).should.eql({
id: existingInstance.id,
name: 'hooked name',
2016-04-01 11:48:17 +00:00
extra: 'extra data',
});
done();
});
});
});
it('validates model after `before save` hook', function(done) {
TestModel.observe('before save', invalidateTestModel());
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'updated'}, function(err) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
it('triggers `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'replacedName'}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {
name: 'replacedName',
2016-04-01 11:48:17 +00:00
id: existingInstance.id,
},
currentInstance: {
id: existingInstance.id,
name: 'replacedName',
2016-04-01 11:48:17 +00:00
extra: null,
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
}));
done();
});
});
it('applies delete from `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
delete ctx.data.extra;
}));
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'changed'}, function(err, instance) {
if (err) return done(err);
instance.should.not.have.property('extra', 'hook data');
done();
});
2016-04-01 11:48:17 +00:00
});
it('applies updates from `persist` hook - for nested model instance', function(done) {
var Address = dataSource.createModel('NestedAddress', {
2016-08-19 17:46:59 +00:00
id: {type: String, id: true, default: 1},
city: {type: String, required: true},
country: {type: String, required: true},
});
var User = dataSource.createModel('UserWithAddress', {
2016-08-19 17:46:59 +00:00
id: {type: String, id: true, default: uid.next},
name: {type: String, required: true},
address: {type: Address, required: false},
extra: {type: String},
});
dataSource.automigrate(['UserWithAddress', 'NestedAddress'], function(err) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
User.create({name: 'Joe'}, function(err, instance) {
if (err) return done(err);
var existingUser = instance;
2016-04-12 11:42:57 +00:00
User.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2016-04-01 11:48:17 +00:00
should.exist(ctx.data.address);
ctx.data.address.should.be.type('object');
ctx.data.address.should.not.be.instanceOf(Address);
ctx.data.extra = 'hook data';
}));
existingUser.replaceAttributes(
2016-08-19 17:46:59 +00:00
{name: 'John', address: new Address({city: 'Springfield', country: 'USA'})},
function(err, inst) {
if (err) return done(err);
inst.should.have.property('extra', 'hook data');
User.findById(existingUser.id, function(err, dbInstance) {
if (err) return done(err);
dbInstance.toObject(true).should.eql({
id: existingUser.id,
name: 'John',
2016-08-19 17:46:59 +00:00
address: {id: '1', city: 'Springfield', country: 'USA'},
2016-04-01 11:48:17 +00:00
extra: 'hook data',
});
done();
});
});
});
});
});
it('triggers `loaded` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'changed'}, function(err, data) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-04-01 11:48:17 +00:00
data: {
name: 'changed',
2016-04-01 11:48:17 +00:00
id: data.id,
},
2016-04-01 13:23:42 +00:00
isNewInstance: false,
}));
done();
});
});
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
existingInstance.replaceAttributes(
2016-08-19 17:46:59 +00:00
{name: 'replaced'},
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `loaded` hook replaceAttributes', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext(function(ctx) {
ctx.data.name = 'changed in hook';
}));
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'changed'}, function(err, instance) {
if (err) return done(err);
instance.should.have.property('name', 'changed in hook');
done();
});
2016-04-01 11:48:17 +00:00
});
it('triggers `after save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
existingInstance.name = 'replaced';
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'replaced'}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: existingInstance.id,
name: 'replaced',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
}));
done();
});
});
it('aborts when `after save` hook fails', function(done) {
TestModel.observe('after save', nextWithError(expectedError));
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'replaced'}, function(err) {
[err].should.eql([expectedError]);
done();
});
});
it('applies updates from `after save` hook', function(done) {
TestModel.observe('after save', function(ctx, next) {
ctx.instance.should.be.instanceOf(TestModel);
ctx.instance.extra = 'hook data';
next();
});
2016-08-19 17:46:59 +00:00
existingInstance.replaceAttributes({name: 'updated'}, function(err, instance) {
if (err) return done(err);
instance.should.have.property('extra', 'hook data');
done();
});
2016-04-01 11:48:17 +00:00
});
});
}
describe('PersistedModel.updateOrCreate', function() {
2015-05-21 11:51:30 +00:00
it('triggers hooks in the correct order on create', function(done) {
monitorHookExecution();
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'not-found', name: 'not found'},
2015-05-21 11:51:30 +00:00
function(err, record, created) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
2015-05-21 11:51:30 +00:00
'access',
'before save',
'persist',
'loaded',
2016-04-01 11:48:17 +00:00
'after save',
2015-05-21 11:51:30 +00:00
]);
done();
});
});
it('triggers hooks in the correct order on update', function(done) {
monitorHookExecution();
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
2015-05-21 11:51:30 +00:00
function(err, record, created) {
if (err) return done(err);
2016-04-09 14:59:09 +00:00
hookMonitor.names.should.eql([
'access',
'before save',
'persist',
'loaded',
'after save',
]);
2015-05-21 11:51:30 +00:00
done();
});
});
it('triggers `access` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'not-found', name: 'not found'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {id: 'not-found'},
}}));
done();
});
});
it('triggers `access` hook on update', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {id: existingInstance.id},
}}));
done();
});
});
it('does not trigger `access` on missing id', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.equal('hook not called');
done();
});
});
it('applies updates from `access` hook when found', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
findTestModels({fields: ['id', 'name']}, function(err, list) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
(list || []).map(toObject).should.eql([
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: existingInstance.name, extra: undefined},
{id: instance.id, name: 'new name', extra: undefined},
]);
done();
});
2016-04-01 11:48:17 +00:00
});
});
it('applies updates from `access` hook when not found', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: 'not-found'}};
next();
});
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
findTestModels({fields: ['id', 'name']}, function(err, list) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
(list || []).map(toObject).should.eql([
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: existingInstance.name, extra: undefined},
{id: list[1].id, name: 'second', extra: undefined},
{id: instance.id, name: 'new name', extra: undefined},
]);
done();
});
2016-04-01 11:48:17 +00:00
});
});
it('triggers hooks only once', function(done) {
2016-04-12 13:06:42 +00:00
monitorHookExecution(['access', 'before save']);
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'ignored', name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql(['access', 'before save']);
done();
});
});
it('triggers `before save` hook on update', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
if (err) return done(err);
if (dataSource.connector.updateOrCreate) {
// Atomic implementations of `updateOrCreate` cannot
// provide full instance as that depends on whether
// UPDATE or CREATE will be triggered
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {id: existingInstance.id, name: 'updated name'},
}));
} else {
// currentInstance is set, because a non-atomic `updateOrCreate`
// will use `prototype.updateAttributes` internally, which
// exposes this to the context
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {id: existingInstance.id, name: 'updated name'},
2016-04-01 11:48:17 +00:00
currentInstance: existingInstance,
}));
}
done();
});
});
it('triggers `before save` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
if (dataSource.connector.updateOrCreate) {
// Atomic implementations of `updateOrCreate` cannot
// provide full instance as that depends on whether
// UPDATE or CREATE will be triggered
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: 'new-id'},
data: {id: 'new-id', name: 'a name'},
}));
} else {
// The default unoptimized implementation runs
// `instance.save` and thus a full instance is availalbe
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
instance: {id: 'new-id', name: 'a name', extra: undefined},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
}
done();
});
});
it('applies updates from `before save` hook on update', function(done) {
TestModel.observe('before save', function(ctx, next) {
ctx.data.name = 'hooked';
next();
});
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
if (err) return done(err);
instance.name.should.equal('hooked');
done();
});
});
it('applies updates from `before save` hook on create', function(done) {
TestModel.observe('before save', function(ctx, next) {
if (ctx.instance) {
ctx.instance.name = 'hooked';
} else {
ctx.data.name = 'hooked';
}
next();
});
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'new name'},
function(err, instance) {
if (err) return done(err);
instance.name.should.equal('hooked');
done();
});
});
// FIXME(bajtos) this fails with connector-specific updateOrCreate
// implementations, see the comment inside lib/dao.js (updateOrCreate)
it.skip('validates model after `before save` hook on update', function(done) {
TestModel.observe('before save', invalidateTestModel());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
// FIXME(bajtos) this fails with connector-specific updateOrCreate
// implementations, see the comment inside lib/dao.js (updateOrCreate)
it.skip('validates model after `before save` hook on create', function(done) {
TestModel.observe('before save', invalidateTestModel());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'new name'},
function(err, instance) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
2015-05-13 23:14:40 +00:00
it('triggers `persist` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2015-05-13 23:14:40 +00:00
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-05-13 23:14:40 +00:00
function(err, instance) {
if (err) return done(err);
if (dataSource.connector.updateOrCreate) {
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: 'new-id'},
data: {id: 'new-id', name: 'a name'},
2015-05-13 23:14:40 +00:00
currentInstance: {
id: 'new-id',
name: 'a name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2015-05-13 23:14:40 +00:00
}));
} else {
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-13 23:14:40 +00:00
data: {
id: 'new-id',
2016-04-01 11:48:17 +00:00
name: 'a name',
2015-05-13 23:14:40 +00:00
},
isNewInstance: true,
currentInstance: {
id: 'new-id',
name: 'a name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2015-05-13 23:14:40 +00:00
}));
}
done();
});
});
it('triggers `persist` hook on update', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2015-05-13 23:14:40 +00:00
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'updated name'},
2015-05-13 23:14:40 +00:00
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
var expectedContext = aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
2015-05-13 23:14:40 +00:00
data: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: 'updated name',
2015-05-13 23:14:40 +00:00
},
currentInstance: {
id: existingInstance.id,
name: 'updated name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
});
if (!dataSource.connector.updateOrCreate) {
// When the connector does not provide updateOrCreate,
// DAO falls back to updateAttributes which sets this flag
expectedContext.isNewInstance = false;
}
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(expectedContext);
2015-05-13 23:14:40 +00:00
done();
});
});
2015-05-21 11:51:30 +00:00
it('triggers `loaded` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2015-05-21 11:51:30 +00:00
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-05-21 11:51:30 +00:00
function(err, instance) {
if (err) return done(err);
if (dataSource.connector.updateOrCreate) {
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
data: {id: 'new-id', name: 'a name'},
isNewInstance: true,
2015-05-21 11:51:30 +00:00
}));
} else {
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-21 11:51:30 +00:00
data: {
id: 'new-id',
2016-04-01 11:48:17 +00:00
name: 'a name',
2015-05-21 11:51:30 +00:00
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
2015-05-21 11:51:30 +00:00
}));
}
done();
});
});
it('triggers `loaded` hook on update', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2015-05-21 11:51:30 +00:00
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'updated name'},
2015-05-21 11:51:30 +00:00
function(err, instance) {
if (err) return done(err);
if (dataSource.connector.updateOrCreate) {
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2015-05-21 11:51:30 +00:00
data: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: 'updated name',
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
2015-05-21 11:51:30 +00:00
}));
} else {
2016-04-09 14:59:09 +00:00
ctxRecorder.records.should.eql(
2016-04-12 11:42:57 +00:00
aCtxForModel(TestModel, {
2015-05-21 11:51:30 +00:00
data: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: 'updated name',
},
2016-04-09 14:59:09 +00:00
})
);
2015-05-21 11:51:30 +00:00
}
done();
});
});
2015-07-21 09:05:55 +00:00
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
2015-07-21 09:05:55 +00:00
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('triggers `after save` hook on update', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: existingInstance.id,
name: 'updated name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: false,
}));
done();
});
});
it('triggers `after save` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.updateOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: instance.id,
name: 'a name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
2016-04-01 11:48:17 +00:00
isNewInstance: true,
}));
done();
});
});
});
if (!getSchema().connector.replaceById) {
2016-04-01 11:48:17 +00:00
describe.skip('replaceById - not implemented', function() {});
} else {
describe('PersistedModel.replaceOrCreate', function() {
it('triggers hooks in the correct order on create', function(done) {
monitorHookExecution();
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'not-found', name: 'not found'},
function(err, record, created) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql([
'access',
'before save',
'persist',
'loaded',
2016-04-01 11:48:17 +00:00
'after save',
]);
done();
});
});
it('triggers hooks in the correct order on replace', function(done) {
monitorHookExecution();
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
function(err, record, created) {
if (err) return done(err);
2016-04-09 14:59:09 +00:00
hookMonitor.names.should.eql([
'access',
'before save',
'persist',
'loaded',
'after save',
]);
done();
});
});
it('triggers `access` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'not-found', name: 'not found'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {id: 'not-found'},
}}));
done();
});
});
it('triggers `access` hook on replace', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {id: existingInstance.id},
}}));
done();
});
});
it('does not trigger `access` on missing id', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.equal('hook not called');
done();
});
});
it('applies updates from `access` hook when found', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
findTestModels({fields: ['id', 'name']}, function(err, list) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
(list || []).map(toObject).should.eql([
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: existingInstance.name, extra: undefined},
{id: instance.id, name: 'new name', extra: undefined},
]);
done();
});
2016-04-01 11:48:17 +00:00
});
});
it('applies updates from `access` hook when not found', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: 'not-found'}};
next();
});
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
findTestModels({fields: ['id', 'name']}, function(err, list) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
(list || []).map(toObject).should.eql([
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: existingInstance.name, extra: undefined},
{id: list[1].id, name: 'second', extra: undefined},
{id: instance.id, name: 'new name', extra: undefined},
]);
done();
});
2016-04-01 11:48:17 +00:00
});
});
it('triggers hooks only once', function(done) {
2016-04-12 13:06:42 +00:00
monitorHookExecution(['access', 'before save']);
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'ignored', name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql(['access', 'before save']);
done();
});
});
it('triggers `before save` hookon create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.replaceOrCreate({id: existingInstance.id, name: 'new name'},
function(err, instance) {
if (err)
return done(err);
2016-04-12 11:42:57 +00:00
var expectedContext = aCtxForModel(TestModel, {
2016-04-01 11:48:17 +00:00
instance: instance,
});
if (!dataSource.connector.replaceOrCreate) {
expectedContext.isNewInstance = false;
}
done();
});
2016-04-01 11:48:17 +00:00
});
it('triggers `before save` hook on replace', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'replaced name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
var expectedContext = aCtxForModel(TestModel, {
instance: {
id: existingInstance.id,
name: 'replaced name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
});
if (!dataSource.connector.replaceOrCreate) {
expectedContext.isNewInstance = false;
}
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('triggers `before save` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
var expectedContext = aCtxForModel(TestModel, {
instance: {
id: 'new-id',
name: 'a name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
});
if (!dataSource.connector.replaceOrCreate) {
expectedContext.isNewInstance = true;
}
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('applies updates from `before save` hook on create', function(done) {
TestModel.observe('before save', function(ctx, next) {
ctx.instance.name = 'hooked';
next();
});
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'new name'},
function(err, instance) {
if (err) return done(err);
instance.name.should.equal('hooked');
done();
});
});
it('validates model after `before save` hook on create', function(done) {
TestModel.observe('before save', invalidateTestModel());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'new name'},
function(err, instance) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
it('triggers `persist` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
var expectedContext = aCtxForModel(TestModel, {
2016-04-01 13:23:42 +00:00
currentInstance: {
id: 'new-id',
name: 'a name',
extra: undefined,
},
data: {
id: 'new-id',
name: 'a name',
},
});
if (dataSource.connector.replaceOrCreate) {
2016-08-19 17:46:59 +00:00
expectedContext.where = {id: 'new-id'};
} else {
// non-atomic implementation does not provide ctx.where
// because a new instance is being created, so there
// are not records to match where filter.
expectedContext.isNewInstance = true;
}
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('triggers `persist` hook on replace', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'replaced name'},
function(err, instance) {
if (err) return done(err);
var expected = {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: 'replaced name',
},
currentInstance: {
id: existingInstance.id,
name: 'replaced name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
};
2016-04-12 11:42:57 +00:00
var expectedContext = aCtxForModel(TestModel, expected);
var expectedContext;
if (!dataSource.connector.replaceOrCreate) {
expectedContext.isNewInstance = false;
}
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('triggers `loaded` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
var expected = {
data: {
id: 'new-id',
2016-04-01 11:48:17 +00:00
name: 'a name',
},
};
2016-04-01 11:48:17 +00:00
expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
2016-04-01 11:48:17 +00:00
true : undefined;
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, expected));
done();
});
});
it('triggers `loaded` hook on replace', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'replaced name'},
function(err, instance) {
if (err) return done(err);
var expected = {
data: {
id: existingInstance.id,
2016-04-01 11:48:17 +00:00
name: 'replaced name',
},
};
2016-04-01 11:48:17 +00:00
expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
false : undefined;
2016-04-09 14:59:09 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, expected));
done();
});
});
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
2016-04-01 11:48:17 +00:00
});
it('triggers `after save` hook on replace', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: 'replaced name'},
function(err, instance) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
var expected = {
instance: {
id: existingInstance.id,
name: 'replaced name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
};
2016-04-01 11:48:17 +00:00
expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
false : undefined;
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, expected));
done();
});
});
it('triggers `after save` hook on create', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.replaceOrCreate(
2016-08-19 17:46:59 +00:00
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
var expected = {
instance: {
id: instance.id,
name: 'a name',
2016-04-01 11:48:17 +00:00
extra: undefined,
},
};
2016-04-01 11:48:17 +00:00
expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
true : undefined;
2016-04-01 11:48:17 +00:00
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, expected));
done();
});
});
});
}
describe('PersistedModel.deleteAll', function() {
it('triggers `access` hook with query', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.deleteAll({name: existingInstance.name}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
query: {where: {name: existingInstance.name}},
}));
done();
});
});
it('triggers `access` hook without query', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.deleteAll(function(err) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {where: {}}}));
done();
});
});
it('applies updates from `access` hook', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
TestModel.deleteAll(function(err) {
if (err) return done(err);
findTestModels(function(err, list) {
if (err) return done(err);
(list || []).map(get('id')).should.eql([existingInstance.id]);
done();
});
});
});
it('triggers `before delete` hook with query', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before delete', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.deleteAll({name: existingInstance.name}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {name: existingInstance.name},
}));
done();
});
});
it('triggers `before delete` hook without query', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before delete', ctxRecorder.recordAndNext());
TestModel.deleteAll(function(err) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {where: {}}));
done();
});
});
it('applies updates from `before delete` hook', function(done) {
TestModel.observe('before delete', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.where = {id: {neq: existingInstance.id}};
next();
});
TestModel.deleteAll(function(err) {
if (err) return done(err);
findTestModels(function(err, list) {
if (err) return done(err);
(list || []).map(get('id')).should.eql([existingInstance.id]);
done();
});
});
});
it('aborts when `before delete` hook fails', function(done) {
TestModel.observe('before delete', nextWithError(expectedError));
TestModel.deleteAll(function(err, list) {
[err].should.eql([expectedError]);
TestModel.findById(existingInstance.id, function(err, inst) {
if (err) return done(err);
(inst ? inst.toObject() : 'null').should.
eql(existingInstance.toObject());
done();
});
});
});
it('triggers `after delete` hook without query', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after delete', ctxRecorder.recordAndNext());
TestModel.deleteAll(function(err) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {where: {}}));
done();
});
});
it('triggers `after delete` hook without query', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after delete', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.deleteAll({name: existingInstance.name}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {name: existingInstance.name},
}));
done();
});
});
it('aborts when `after delete` hook fails', function(done) {
TestModel.observe('after delete', nextWithError(expectedError));
TestModel.deleteAll(function(err) {
[err].should.eql([expectedError]);
done();
});
});
});
describe('PersistedModel.prototype.delete', function() {
it('triggers `access` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
existingInstance.delete(function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
query: {where: {id: existingInstance.id}},
}));
done();
});
});
it('applies updated from `access` hook', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
existingInstance.delete(function(err) {
if (err) return done(err);
findTestModels(function(err, list) {
if (err) return done(err);
(list || []).map(get('id')).should.eql([existingInstance.id]);
done();
});
});
});
it('triggers `before delete` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before delete', ctxRecorder.recordAndNext());
existingInstance.delete(function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
2016-04-01 11:48:17 +00:00
instance: existingInstance,
}));
done();
});
});
it('applies updated from `before delete` hook', function(done) {
TestModel.observe('before delete', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.where = {id: {neq: existingInstance.id}};
next();
});
existingInstance.delete(function(err) {
if (err) return done(err);
findTestModels(function(err, list) {
if (err) return done(err);
(list || []).map(get('id')).should.eql([existingInstance.id]);
done();
});
});
});
it('aborts when `before delete` hook fails', function(done) {
TestModel.observe('before delete', nextWithError(expectedError));
existingInstance.delete(function(err, list) {
[err].should.eql([expectedError]);
TestModel.findById(existingInstance.id, function(err, inst) {
if (err) return done(err);
(inst ? inst.toObject() : 'null').should.eql(
existingInstance.toObject());
done();
});
});
});
it('triggers `after delete` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after delete', ctxRecorder.recordAndNext());
existingInstance.delete(function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
2016-04-01 11:48:17 +00:00
instance: existingInstance,
}));
done();
});
});
it('triggers `after delete` hook without query', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after delete', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.deleteAll({name: existingInstance.name}, function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {name: existingInstance.name},
}));
done();
});
});
it('aborts when `after delete` hook fails', function(done) {
TestModel.observe('after delete', nextWithError(expectedError));
TestModel.deleteAll(function(err) {
[err].should.eql([expectedError]);
done();
});
});
it('propagates hookState from `before delete` to `after delete`', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before delete', ctxRecorder.recordAndNext(function(ctx) {
2015-03-05 10:55:04 +00:00
ctx.hookState.foo = 'bar';
}));
2016-04-12 11:42:57 +00:00
TestModel.observe('after delete', ctxRecorder.recordAndNext(function(ctx) {
2015-03-05 10:55:04 +00:00
ctx.hookState.foo = ctx.hookState.foo.toUpperCase();
}));
existingInstance.delete(function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql([
aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
hookState: {foo: 'bar'},
where: {id: '1'},
2016-04-01 11:48:17 +00:00
instance: existingInstance,
2015-03-05 10:55:04 +00:00
}),
2016-04-12 11:42:57 +00:00
aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
hookState: {foo: 'BAR'},
where: {id: '1'},
2016-04-01 11:48:17 +00:00
instance: existingInstance,
}),
2015-03-05 10:55:04 +00:00
]);
done();
});
});
it('triggers hooks only once', function(done) {
2016-04-12 13:06:42 +00:00
monitorHookExecution();
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
existingInstance.delete(function(err) {
if (err) return done(err);
2016-04-12 13:06:42 +00:00
hookMonitor.names.should.eql(['access', 'before delete', 'after delete']);
done();
});
});
});
describe('PersistedModel.updateAll', function() {
it('triggers `access` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('access', ctxRecorder.recordAndNext());
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{name: 'searched'},
{name: 'updated'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {name: 'searched'},
}}));
done();
});
});
it('applies updates from `access` hook', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id},
{name: 'new name'},
function(err) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
findTestModels({fields: ['id', 'name']}, function(err, list) {
if (err) return done(err);
2016-04-01 11:48:17 +00:00
(list || []).map(toObject).should.eql([
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: existingInstance.name, extra: undefined},
{id: '2', name: 'new name', extra: undefined},
]);
done();
});
2016-04-01 11:48:17 +00:00
});
});
it('triggers `before save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('before save', ctxRecorder.recordAndNext());
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{name: 'searched'},
{name: 'updated'},
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {name: 'searched'},
data: {name: 'updated'},
}));
done();
});
});
it('applies updates from `before save` hook', function(done) {
TestModel.observe('before save', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.data = {name: 'hooked', extra: 'added'};
next();
});
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id},
{name: 'updated name'},
function(err) {
if (err) return done(err);
loadTestModel(existingInstance.id, function(err, instance) {
if (err) return done(err);
instance.should.have.property('name', 'hooked');
instance.should.have.property('extra', 'added');
done();
});
});
});
2015-05-13 23:14:40 +00:00
it('triggers `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext());
2015-05-13 23:14:40 +00:00
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{name: existingInstance.name},
{name: 'changed'},
2015-05-13 23:14:40 +00:00
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
data: {name: 'changed'},
where: {name: existingInstance.name},
2015-05-13 23:14:40 +00:00
}));
done();
});
});
it('applies updates from `persist` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('persist', ctxRecorder.recordAndNext(function(ctx) {
2015-05-13 23:14:40 +00:00
ctx.data.extra = 'hook data';
}));
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id},
{name: 'changed'},
2015-05-13 23:14:40 +00:00
function(err) {
if (err) return done(err);
loadTestModel(existingInstance.id, function(err, instance) {
instance.should.have.property('extra', 'hook data');
done();
});
});
});
2015-05-21 11:51:30 +00:00
it('does not trigger `loaded`', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2015-05-21 11:51:30 +00:00
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id},
{name: 'changed'},
2015-05-21 11:51:30 +00:00
function(err, instance) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql('hook not called');
2015-05-21 11:51:30 +00:00
done();
});
});
it('triggers `after save` hook', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id},
{name: 'updated name'},
function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {name: 'updated name'},
}));
done();
});
});
it('accepts hookState from options', function(done) {
2016-04-12 11:42:57 +00:00
TestModel.observe('after save', ctxRecorder.recordAndNext());
TestModel.updateAll(
2016-08-19 17:46:59 +00:00
{id: existingInstance.id},
{name: 'updated name'},
{foo: 'bar'},
function(err) {
if (err) return done(err);
2016-04-12 11:42:57 +00:00
ctxRecorder.records.options.should.eql({
2016-04-01 11:48:17 +00:00
foo: 'bar',
});
done();
});
});
});
describe('PersistedModel.upsertWithWhere', function() {
it('triggers hooks in the correct order on create', function(done) {
monitorHookExecution();
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({extra: 'not-found'},
{id: 'not-found', name: 'not found', extra: 'not-found'},
function(err, record, created) {
if (err) return done(err);
hookMonitor.names.should.eql([
'access',
'before save',
'persist',
'loaded',
'after save',
]);
TestModel.findById('not-found', function(err, data) {
if (err) return done(err);
data.name.should.equal('not found');
data.extra.should.equal('not-found');
done();
});
});
});
it('triggers hooks in the correct order on update', function(done) {
monitorHookExecution();
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{name: 'new name', extra: 'new extra'},
function(err, record, created) {
if (err) return done(err);
hookMonitor.names.should.eql([
'access',
'before save',
'persist',
'loaded',
'after save',
]);
TestModel.findById(existingInstance.id, function(err, data) {
if (err) return done(err);
data.name.should.equal('new name');
data.extra.should.equal('new extra');
done();
});
});
});
it('triggers `access` hook on create', function(done) {
TestModel.observe('access', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({extra: 'not-found'},
{id: 'not-found', name: 'not found'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {extra: 'not-found'},
}}));
done();
});
});
it('triggers `access` hook on update', function(done) {
TestModel.observe('access', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{name: 'new name', extra: 'new extra'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {query: {
where: {id: existingInstance.id},
}}));
done();
});
});
it('triggers hooks only once', function(done) {
monitorHookExecution(['access', 'before save']);
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{name: 'new name'},
function(err, instance) {
if (err) return done(err);
hookMonitor.names.should.eql(['access', 'before save']);
done();
});
});
it('applies updates from `access` hook when found', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: {neq: existingInstance.id}}};
next();
});
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
findTestModels({fields: ['id', 'name']}, function(err, list) {
if (err) return done(err);
(list || []).map(toObject).should.eql([
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: existingInstance.name, extra: undefined},
{id: instance.id, name: 'new name', extra: undefined},
]);
done();
});
});
});
it('applies updates from `access` hook when not found', function(done) {
TestModel.observe('access', function(ctx, next) {
2016-08-19 17:46:59 +00:00
ctx.query = {where: {id: 'not-found'}};
next();
});
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{name: 'new name'},
function(err, instance) {
if (err) return done(err);
2016-08-19 17:46:59 +00:00
findTestModels({fields: ['id', 'name']}, function(err, list) {
if (err) return done(err);
(list || []).map(toObject).should.eql([
2016-08-19 17:46:59 +00:00
{id: existingInstance.id, name: existingInstance.name, extra: undefined},
{id: list[1].id, name: 'second', extra: undefined},
{id: instance.id, name: 'new name', extra: undefined},
]);
done();
});
});
});
it('triggers `before save` hook on update', function(done) {
TestModel.observe('before save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
if (err) return done(err);
var expectedContext = aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {
id: existingInstance.id,
name: 'updated name',
},
});
if (!dataSource.connector.upsertWithWhere) {
// the difference between `existingInstance` and the following
// plain-data object is `currentInstance` the missing fields are
// null in `currentInstance`, wehere as in `existingInstance` they
// are undefined; please see other tests for example see:
// test for "PersistedModel.create triggers `persist` hook"
expectedContext.currentInstance = {id: existingInstance.id, name: 'first', extra: null};
}
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('triggers `before save` hook on create', function(done) {
TestModel.observe('before save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: 'new-id'},
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
var expectedContext = aCtxForModel(TestModel, {});
if (dataSource.connector.upsertWithWhere) {
expectedContext.data = {id: 'new-id', name: 'a name'};
expectedContext.where = {id: 'new-id'};
} else {
expectedContext.instance = {id: 'new-id', name: 'a name', extra: null};
expectedContext.isNewInstance = true;
}
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('applies updates from `before save` hook on update', function(done) {
TestModel.observe('before save', function(ctx, next) {
ctx.data.name = 'hooked';
next();
});
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{name: 'updated name'},
function(err, instance) {
if (err) return done(err);
instance.name.should.equal('hooked');
done();
});
});
it('applies updates from `before save` hook on create', function(done) {
TestModel.observe('before save', function(ctx, next) {
if (ctx.instance) {
ctx.instance.name = 'hooked';
} else {
ctx.data.name = 'hooked';
}
next();
});
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: 'new-id'},
{id: 'new-id', name: 'new name'},
function(err, instance) {
if (err) return done(err);
instance.name.should.equal('hooked');
done();
});
});
it('validates model after `before save` hook on create', function(done) {
TestModel.observe('before save', invalidateTestModel());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: 'new-id'},
{id: 'new-id', name: 'new name'},
function(err, instance) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
it('validates model after `before save` hook on update', function(done) {
TestModel.observe('before save', invalidateTestModel());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
(err || {}).should.be.instanceOf(ValidationError);
2016-08-19 17:46:59 +00:00
(err.details.codes || {}).should.eql({name: ['presence']});
done();
});
});
it('triggers `persist` hook on create', function(done) {
TestModel.observe('persist', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: 'new-id'},
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
var expectedContext = aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
data: {id: 'new-id', name: 'a name'},
currentInstance: {
id: 'new-id',
name: 'a name',
extra: undefined,
},
});
if (dataSource.connector.upsertWithWhere) {
expectedContext.where = {id: 'new-id'};
} else {
expectedContext.isNewInstance = true;
}
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('triggers persist hook on update', function(done) {
TestModel.observe('persist', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
if (err) return done(err);
var expectedContext = aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
where: {id: existingInstance.id},
data: {
id: existingInstance.id,
name: 'updated name',
},
currentInstance: {
id: existingInstance.id,
name: 'updated name',
extra: undefined,
},
});
if (!dataSource.connector.upsertWithWhere) {
expectedContext.isNewInstance = false;
}
ctxRecorder.records.should.eql(expectedContext);
done();
});
});
it('triggers `loaded` hook on create', function(done) {
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: 'new-id'},
{id: 'new-id', name: 'a name'},
function(err, instance) {
if (err) return done(err);
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
2016-08-19 17:46:59 +00:00
data: {id: 'new-id', name: 'a name'},
isNewInstance: true,
}));
done();
});
});
it('triggers `loaded` hook on update', function(done) {
TestModel.observe('loaded', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
if (err) return done(err);
var expectedContext = aCtxForModel(TestModel, {
data: {
id: existingInstance.id,
name: 'updated name',
},
});
// For non-atomic implementation of upsertWithWhere on update, it calls
// updateAttributes. loaded hook of updateAttributes does not provide
// isNewInstance.
if (dataSource.connector.upsertWithWhere) {
expectedContext.isNewInstance = false;
}
ctxRecorder.records.should.eql(aCtxForModel(TestModel, expectedContext));
done();
});
});
it('emits error when `loaded` hook fails', function(done) {
TestModel.observe('loaded', nextWithError(expectedError));
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: 'new-id'},
{id: 'new-id', name: 'a name'},
function(err, instance) {
[err].should.eql([expectedError]);
done();
});
});
it('triggers `after save` hook on update', function(done) {
TestModel.observe('after save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: existingInstance.id},
{id: existingInstance.id, name: 'updated name'},
function(err, instance) {
if (err) return done(err);
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: existingInstance.id,
name: 'updated name',
extra: undefined,
},
isNewInstance: false,
}));
done();
});
});
it('triggers `after save` hook on create', function(done) {
TestModel.observe('after save', ctxRecorder.recordAndNext());
2016-08-19 17:46:59 +00:00
TestModel.upsertWithWhere({id: 'new-id'},
{id: 'new-id', name: 'a name'}, function(err, instance) {
if (err) return done(err);
ctxRecorder.records.should.eql(aCtxForModel(TestModel, {
instance: {
id: instance.id,
name: 'a name',
extra: undefined,
},
isNewInstance: true,
}));
done();
});
});
});
function nextWithError(err) {
return function(context, next) {
next(err);
};
}
function invalidateTestModel() {
return function(context, next) {
if (context.instance) {
context.instance.name = '';
} else {
context.data.name = '';
}
next();
};
}
function findTestModels(query, cb) {
if (cb === undefined && typeof query === 'function') {
cb = query;
query = null;
}
2016-08-19 17:46:59 +00:00
TestModel.find(query, {notify: false}, cb);
}
function loadTestModel(id, cb) {
2016-08-19 17:46:59 +00:00
TestModel.findOne({where: {id: id}}, {notify: false}, cb);
}
2016-04-12 13:06:42 +00:00
function monitorHookExecution(hookNames) {
hookMonitor.install(TestModel, hookNames);
2015-05-21 11:51:30 +00:00
}
require('./operation-hooks.suite')(dataSource, should, connectorCapabilities);
});
function get(propertyName) {
return function(obj) {
return obj[propertyName];
};
}
function toObject(obj) {
return obj.toObject ? obj.toObject() : obj;
}
};