2015-01-21 16:57:47 +00:00
|
|
|
var ValidationError = require('../').ValidationError;
|
|
|
|
var traverse = require('traverse');
|
|
|
|
|
|
|
|
module.exports = function(dataSource, should) {
|
|
|
|
describe('Persistence hooks', function() {
|
|
|
|
var observedContexts, expectedError, observersCalled;
|
|
|
|
var TestModel, existingInstance;
|
|
|
|
var migrated = false, lastId;
|
|
|
|
|
2015-02-21 00:10:25 +00:00
|
|
|
var undefinedValue = undefined;
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
beforeEach(function setupDatabase(done) {
|
|
|
|
observedContexts = "hook not called";
|
|
|
|
expectedError = new Error('test error');
|
|
|
|
observersCalled = [];
|
|
|
|
|
|
|
|
TestModel = dataSource.createModel('TestModel', {
|
|
|
|
id: { type: String, id: true, default: uid },
|
|
|
|
name: { type: String, required: true },
|
|
|
|
extra: { type: String, required: false }
|
|
|
|
});
|
|
|
|
|
|
|
|
lastId = 0;
|
|
|
|
|
|
|
|
if (migrated) {
|
|
|
|
TestModel.deleteAll(done);
|
|
|
|
} else {
|
|
|
|
dataSource.automigrate(TestModel.modelName, function(err) {
|
|
|
|
migrated = true;
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function createTestData(done) {
|
|
|
|
TestModel.create({ name: 'first' }, function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
2015-02-21 00:10:25 +00:00
|
|
|
// Look it up from DB so that default values are retrieved
|
|
|
|
TestModel.findById(instance.id, function(err, instance) {
|
|
|
|
existingInstance = instance;
|
|
|
|
undefinedValue = existingInstance.extra;
|
|
|
|
|
|
|
|
TestModel.create({ name: 'second' }, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
done();
|
|
|
|
});
|
2015-01-21 16:57:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PersistedModel.find', function() {
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.find({ where: { id: '1' } }, function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
query: { where: { id: '1' } }
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('aborts when `access` hook fails', function(done) {
|
|
|
|
TestModel.observe('access', nextWithError(expectedError));
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.find(function(err, list) {
|
|
|
|
[err].should.eql([expectedError]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updates from `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook for geo queries', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.find({ where: { geo: { near: '10,20' }}}, function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
query: { where: { geo: { near: '10,20' } } }
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updates from `access` hook for geo queries', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +00:00
|
|
|
ctx.query = { where: { id: existingInstance.id } };
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.find({ where: { geo: { near: '10,20' } } }, function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
list.map(get('name')).should.eql([existingInstance.name]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PersistedModel.create', function() {
|
|
|
|
it('triggers `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.create({ name: 'created' }, function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: instance.id,
|
|
|
|
name: 'created',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: true
|
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('aborts when `before save` hook fails', function(done) {
|
|
|
|
TestModel.observe('before save', nextWithError(expectedError));
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.create({ id: uid(), 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) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.create(
|
2015-02-21 00:10:25 +00:00
|
|
|
[{ name: '1' }, { name: '2' }],
|
2015-01-21 16:57:47 +00:00
|
|
|
function(err, list) {
|
|
|
|
if (err) return done(err);
|
2015-02-21 00:10:25 +00:00
|
|
|
// Creation of multiple instances is executed in parallel
|
|
|
|
observedContexts.sort(function(c1, c2) {
|
|
|
|
return c1.instance.name - c2.instance.name;
|
|
|
|
});
|
2015-01-21 16:57:47 +00:00
|
|
|
observedContexts.should.eql([
|
|
|
|
aTestModelCtx({
|
2015-03-19 12:25:03 +00:00
|
|
|
instance: { id: list[0].id, name: '1', extra: undefined },
|
|
|
|
isNewInstance: true
|
2015-01-21 16:57:47 +00:00
|
|
|
}),
|
|
|
|
aTestModelCtx({
|
2015-03-19 12:25:03 +00:00
|
|
|
instance: { id: list[1].id, name: '2', extra: undefined },
|
|
|
|
isNewInstance: true
|
2015-01-21 16:57:47 +00:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('validates model after `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', invalidateTestModel());
|
|
|
|
|
|
|
|
TestModel.create({ name: 'created' }, function(err) {
|
|
|
|
(err || {}).should.be.instanceOf(ValidationError);
|
|
|
|
(err.details.codes || {}).should.eql({ name: ['presence'] });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 23:14:40 +00:00
|
|
|
it('triggers `persist` hook', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.create(
|
|
|
|
{ id: 'new-id', name: 'a name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
data: { id: 'new-id', name: 'a name' },
|
|
|
|
isNewInstance: true,
|
|
|
|
currentInstance: { extra: null, id: 'new-id', name: 'a name' }
|
|
|
|
}));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updates from `persist` hook', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext(function(ctx){
|
|
|
|
ctx.data.extra = 'hook data';
|
|
|
|
}));
|
|
|
|
|
|
|
|
TestModel.create(
|
|
|
|
{ id: 'new-id', name: 'a name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
// the, instance returned by `create` context does not have the
|
|
|
|
// values updated from `persist` hook
|
|
|
|
instance.should.not.have.property('extra', 'hook data');
|
|
|
|
|
|
|
|
// So, we must query the database here because on `create`
|
|
|
|
// updates from `persist` hook are reflected into database
|
|
|
|
TestModel.findById('new-id', function(err, dbInstance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
dbInstance.toObject(true).should.eql({
|
|
|
|
id: 'new-id',
|
|
|
|
name: 'a name',
|
|
|
|
extra: 'hook data'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
it('triggers `after save` hook', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.create({ name: 'created' }, function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: instance.id,
|
|
|
|
name: 'created',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: true
|
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('aborts when `after save` hook fails', function(done) {
|
|
|
|
TestModel.observe('after save', nextWithError(expectedError));
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.create(
|
2015-02-21 00:10:25 +00:00
|
|
|
[{ name: '1' }, { name: '2' }],
|
2015-01-21 16:57:47 +00:00
|
|
|
function(err, list) {
|
|
|
|
if (err) return done(err);
|
2015-02-21 00:10:25 +00:00
|
|
|
// Creation of multiple instances is executed in parallel
|
|
|
|
observedContexts.sort(function(c1, c2) {
|
|
|
|
return c1.instance.name - c2.instance.name;
|
|
|
|
});
|
2015-01-21 16:57:47 +00:00
|
|
|
observedContexts.should.eql([
|
|
|
|
aTestModelCtx({
|
2015-03-19 12:25:03 +00:00
|
|
|
instance: { id: list[0].id, name: '1', extra: undefined },
|
|
|
|
isNewInstance: true
|
2015-01-21 16:57:47 +00:00
|
|
|
}),
|
|
|
|
aTestModelCtx({
|
2015-03-19 12:25:03 +00:00
|
|
|
instance: { id: list[1].id, name: '2', extra: undefined },
|
|
|
|
isNewInstance: true
|
2015-01-21 16:57:47 +00:00
|
|
|
}),
|
|
|
|
]);
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.create(
|
|
|
|
[{ 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']);
|
|
|
|
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
2015-03-19 12:25:03 +00:00
|
|
|
instance: { id: list[0].id, name: 'ok', extra: undefined },
|
|
|
|
isNewInstance: true
|
2015-01-21 16:57:47 +00:00
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PersistedModel.findOrCreate', function() {
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: 'new-record' } },
|
|
|
|
{ name: 'new-record' },
|
|
|
|
function(err, record, created) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ query: {
|
|
|
|
where: { name: 'new-record' },
|
|
|
|
limit: 1,
|
|
|
|
offset: 0,
|
|
|
|
skip: 0
|
|
|
|
}}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-03 07:34:49 +00:00
|
|
|
if (dataSource.connector.findOrCreate) {
|
|
|
|
it('triggers `before save` hook when found', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: existingInstance.name } },
|
|
|
|
{ name: existingInstance.name },
|
|
|
|
function(err, record, created) {
|
|
|
|
if (err) return done(err);
|
|
|
|
record.id.should.eql(existingInstance.id);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: getLastGeneratedUid(),
|
|
|
|
name: existingInstance.name,
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: true
|
|
|
|
}));
|
2015-02-03 07:34:49 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
it('triggers `before save` hook when not found', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: 'new-record' } },
|
|
|
|
{ name: 'new-record' },
|
|
|
|
function(err, record, created) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: record.id,
|
|
|
|
name: 'new-record',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: true
|
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('validates model after `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', invalidateTestModel());
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: 'new-record' } },
|
|
|
|
{ name: 'new-record' },
|
|
|
|
function(err) {
|
|
|
|
(err || {}).should.be.instanceOf(ValidationError);
|
|
|
|
(err.details.codes || {}).should.eql({ name: ['presence'] });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers hooks in the correct order when not found', function(done) {
|
|
|
|
var triggered = [];
|
|
|
|
TestModel._notify = TestModel.notifyObserversOf;
|
|
|
|
TestModel.notifyObserversOf = function(operation, context, callback) {
|
|
|
|
triggered.push(operation);
|
|
|
|
this._notify.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: 'new-record' } },
|
|
|
|
{ name: 'new-record' },
|
|
|
|
function(err, record, created) {
|
|
|
|
if (err) return done(err);
|
|
|
|
triggered.should.eql([
|
2015-02-02 09:31:45 +00:00
|
|
|
'access',
|
2015-01-21 16:57:47 +00:00
|
|
|
'before save',
|
2015-05-13 23:14:40 +00:00
|
|
|
'persist',
|
2015-01-21 16:57:47 +00:00
|
|
|
'after save'
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('aborts when `access` hook fails', function(done) {
|
|
|
|
TestModel.observe('access', nextWithError(expectedError));
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ 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(
|
|
|
|
{ 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) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: existingInstance.name } },
|
|
|
|
{ name: existingInstance.name },
|
|
|
|
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
|
|
|
|
// the newly generated UID.
|
|
|
|
// Hence, the test below asserts both `data.id` and
|
|
|
|
// `currentInstance.id` to match getLastGeneratedUid().
|
|
|
|
// On same lines, it also asserts `isNewInstance` to be true.
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
data: {
|
|
|
|
id: getLastGeneratedUid(),
|
|
|
|
name: existingInstance.name
|
|
|
|
},
|
|
|
|
isNewInstance: true,
|
|
|
|
currentInstance: {
|
|
|
|
id: getLastGeneratedUid(),
|
|
|
|
name: record.name,
|
|
|
|
extra: null
|
|
|
|
},
|
|
|
|
where: { name: existingInstance.name }
|
|
|
|
}));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('triggers `persist` hook when not found', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: 'new-record' } },
|
|
|
|
{ name: 'new-record' },
|
|
|
|
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) {
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
data: {
|
|
|
|
id: record.id,
|
|
|
|
name: 'new-record'
|
|
|
|
},
|
|
|
|
isNewInstance: true,
|
|
|
|
currentInstance: {
|
|
|
|
id: record.id,
|
|
|
|
name: record.name,
|
|
|
|
extra: null
|
|
|
|
},
|
|
|
|
where: { name: 'new-record' }
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
data: {
|
|
|
|
id: record.id,
|
|
|
|
name: 'new-record'
|
|
|
|
},
|
|
|
|
isNewInstance: true,
|
|
|
|
currentInstance: { id: record.id, name: record.name, extra: null }
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (dataSource.connector.findOrCreate) {
|
|
|
|
it('applies updates from `persist` hook when found', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext(function(ctx){
|
|
|
|
ctx.data.extra = 'hook data';
|
|
|
|
}));
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: existingInstance.name } },
|
|
|
|
{ name: existingInstance.name },
|
|
|
|
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');
|
|
|
|
|
|
|
|
// Query the database. Here, since record already exists
|
|
|
|
// `findOrCreate`, does not update database for
|
|
|
|
// updates from `persist` hook
|
|
|
|
TestModel.findById(existingInstance.id, function(err, dbInstance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
dbInstance.toObject(true).should.eql({
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: existingInstance.name,
|
|
|
|
extra: undefined
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('applies updates from `persist` hook when not found', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext(function(ctx){
|
|
|
|
ctx.data.extra = 'hook data';
|
|
|
|
}));
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: 'new-record' } },
|
|
|
|
{ name: 'new-record' },
|
|
|
|
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
|
|
|
|
// 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
|
|
|
|
// of create.
|
|
|
|
// So, this test asserts unoptimized connector to
|
|
|
|
// NOT have `extra` property. And then verifes that the
|
|
|
|
// 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);
|
|
|
|
dbInstance.toObject(true).should.eql({
|
|
|
|
id: instance.id,
|
|
|
|
name: instance.name,
|
|
|
|
extra: 'hook data'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
it('triggers `after save` hook when not found', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { name: 'new name' } },
|
|
|
|
{ name: 'new name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: instance.id,
|
|
|
|
name: 'new name',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: true
|
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not trigger `after save` hook when found', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.findOrCreate(
|
|
|
|
{ where: { id: existingInstance.id } },
|
|
|
|
{ name: existingInstance.name },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql("hook not called");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PersistedModel.count', function(done) {
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.count({ id: existingInstance.id }, function(err, count) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ query: {
|
|
|
|
where: { id: existingInstance.id }
|
|
|
|
}}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updates from `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +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() {
|
|
|
|
it('triggers `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
existingInstance.name = 'changed';
|
|
|
|
existingInstance.save(function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ instance: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'changed',
|
2015-03-30 13:03:45 +00:00
|
|
|
extra: undefined,
|
|
|
|
}, options: { throws: false, validate: true } }));
|
2015-01-21 16:57:47 +00:00
|
|
|
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);
|
|
|
|
(err.details.codes || {}).should.eql({ name: ['presence'] });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 23:14:40 +00:00
|
|
|
it('triggers `persist` hook', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
|
|
|
|
existingInstance.name = 'changed';
|
|
|
|
existingInstance.save(function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
data: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'changed'
|
|
|
|
},
|
|
|
|
currentInstance: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'changed',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
where: { id: existingInstance.id },
|
|
|
|
options: { throws: false, validate: true }
|
|
|
|
}));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updates from `persist` hook', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext(function(ctx){
|
|
|
|
ctx.data.extra = 'hook data';
|
|
|
|
}));
|
|
|
|
|
|
|
|
existingInstance.save(function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
instance.should.have.property('extra', 'hook data');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-19 12:25:03 +00:00
|
|
|
it('triggers `after save` hook on update', function(done) {
|
2015-01-21 16:57:47 +00:00
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
existingInstance.name = 'changed';
|
|
|
|
existingInstance.save(function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'changed',
|
|
|
|
extra: undefined
|
|
|
|
},
|
2015-03-30 13:03:45 +00:00
|
|
|
isNewInstance: false,
|
2015-04-01 16:25:45 +00:00
|
|
|
options: { throws: false, validate: true }
|
|
|
|
}));
|
2015-03-19 12:25:03 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `after save` hook on create', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
2015-04-01 16:25:45 +00:00
|
|
|
var instance = new TestModel(
|
|
|
|
{ id: 'new-id', name: 'created' },
|
|
|
|
{ persisted: true });
|
|
|
|
|
2015-03-19 12:25:03 +00:00
|
|
|
instance.save(function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: instance.id,
|
|
|
|
name: 'created',
|
|
|
|
extra: undefined
|
|
|
|
},
|
2015-04-01 16:25:45 +00:00
|
|
|
isNewInstance: true,
|
|
|
|
options: { throws: false, validate: true }
|
2015-03-19 12:25:03 +00:00
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
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() {
|
|
|
|
it('triggers `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
2015-03-05 17:16:12 +00:00
|
|
|
var currentInstance = deepCloneToObject(existingInstance);
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
existingInstance.updateAttributes({ name: 'changed' }, function(err) {
|
|
|
|
if (err) return done(err);
|
2015-03-05 17:16:12 +00:00
|
|
|
existingInstance.name.should.equal('changed');
|
2015-01-21 16:57:47 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { id: existingInstance.id },
|
2015-03-05 17:16:12 +00:00
|
|
|
data: { name: 'changed' },
|
|
|
|
currentInstance: currentInstance
|
2015-01-21 16:57:47 +00:00
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('aborts when `before save` hook fails', function(done) {
|
|
|
|
TestModel.observe('before save', nextWithError(expectedError));
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
instance.toObject(true).should.eql({
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'hooked name',
|
|
|
|
extra: 'extra data'
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('validates model after `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', invalidateTestModel());
|
|
|
|
|
|
|
|
existingInstance.updateAttributes({ name: 'updated' }, function(err) {
|
|
|
|
(err || {}).should.be.instanceOf(ValidationError);
|
|
|
|
(err.details.codes || {}).should.eql({ name: ['presence'] });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 23:14:40 +00:00
|
|
|
it('triggers `persist` hook', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
existingInstance.updateAttributes({ name: 'changed' }, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { id: existingInstance.id },
|
|
|
|
data: { name: 'changed' },
|
|
|
|
currentInstance: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'changed',
|
|
|
|
extra: null
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updates from `persist` hook', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext(function(ctx){
|
|
|
|
ctx.data.extra = 'hook data';
|
|
|
|
}));
|
|
|
|
|
|
|
|
existingInstance.save(function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
instance.should.have.property('extra', 'hook data');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
it('triggers `after save` hook', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
existingInstance.name = 'changed';
|
|
|
|
existingInstance.updateAttributes({ name: 'changed' }, function(err) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'changed',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: false
|
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('aborts when `after save` hook fails', function(done) {
|
|
|
|
TestModel.observe('after save', nextWithError(expectedError));
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
existingInstance.updateAttributes({ name: 'updated' }, function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
instance.should.have.property('extra', 'hook data');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PersistedModel.updateOrCreate', function() {
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook on create', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: 'not-found', name: 'not found' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ query: {
|
|
|
|
where: { id: 'not-found' }
|
|
|
|
}}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook on update', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: existingInstance.id, name: 'new name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ query: {
|
|
|
|
where: { id: existingInstance.id }
|
|
|
|
}}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('does not trigger `access` on missing id', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ name: 'new name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.equal('hook not called');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updates from `access` hook when found', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +00:00
|
|
|
ctx.query = { where: { id: { neq: existingInstance.id } } };
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: existingInstance.id, name: 'new name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
findTestModels({ fields: ['id', 'name' ] }, function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
(list||[]).map(toObject).should.eql([
|
|
|
|
{ id: existingInstance.id, name: existingInstance.name, extra: undefined },
|
|
|
|
{ id: instance.id, name: 'new name', extra: undefined }
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updates from `access` hook when not found', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +00:00
|
|
|
ctx.query = { where: { id: 'not-found' } };
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: existingInstance.id, name: 'new name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
findTestModels({ fields: ['id', 'name' ] }, function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
(list||[]).map(toObject).should.eql([
|
|
|
|
{ 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 hooks only once', function(done) {
|
2015-02-02 09:31:45 +00:00
|
|
|
TestModel.observe('access', pushNameAndNext('access'));
|
2015-01-21 16:57:47 +00:00
|
|
|
TestModel.observe('before save', pushNameAndNext('before save'));
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +00:00
|
|
|
ctx.query = { where: { id: { neq: existingInstance.id } } };
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: 'ignored', name: 'new name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-02-02 09:31:45 +00:00
|
|
|
observersCalled.should.eql(['access', 'before save']);
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `before save` hook on update', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: existingInstance.id, name: 'updated name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-03-05 17:16:12 +00:00
|
|
|
if (dataSource.connector.updateOrCreate) {
|
|
|
|
// Atomic implementations of `updateOrCreate` cannot
|
|
|
|
// provide full instance as that depends on whether
|
|
|
|
// UPDATE or CREATE will be triggered
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
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
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { id: existingInstance.id },
|
|
|
|
data: { id: existingInstance.id, name: 'updated name' },
|
|
|
|
currentInstance: existingInstance
|
|
|
|
}));
|
|
|
|
}
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `before save` hook on create', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ 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
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
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
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
2015-03-19 12:25:03 +00:00
|
|
|
instance: { id: 'new-id', name: 'a name', extra: undefined },
|
|
|
|
isNewInstance: true
|
2015-01-21 16:57:47 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
{ 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(
|
|
|
|
{ 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(
|
|
|
|
{ id: existingInstance.id, name: 'updated name' },
|
|
|
|
function(err, instance) {
|
|
|
|
(err || {}).should.be.instanceOf(ValidationError);
|
|
|
|
(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(
|
|
|
|
{ id: 'new-id', name: 'new name' },
|
|
|
|
function(err, instance) {
|
|
|
|
(err || {}).should.be.instanceOf(ValidationError);
|
|
|
|
(err.details.codes || {}).should.eql({ name: ['presence'] });
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 23:14:40 +00:00
|
|
|
it('triggers `persist` hook on create', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: 'new-id', name: 'a name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
if (dataSource.connector.updateOrCreate) {
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { id: 'new-id' },
|
|
|
|
data: { id: 'new-id', name: 'a name' },
|
|
|
|
currentInstance: {
|
|
|
|
id: 'new-id',
|
|
|
|
name: 'a name',
|
|
|
|
extra: undefined
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
data: {
|
|
|
|
id: 'new-id',
|
|
|
|
name: 'a name'
|
|
|
|
},
|
|
|
|
isNewInstance: true,
|
|
|
|
currentInstance: {
|
|
|
|
id: 'new-id',
|
|
|
|
name: 'a name',
|
|
|
|
extra: undefined
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `persist` hook on update', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: existingInstance.id, name: 'updated name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { id: existingInstance.id },
|
|
|
|
data: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'updated name'
|
|
|
|
},
|
|
|
|
currentInstance: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'updated name',
|
|
|
|
extra: undefined
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
it('triggers `after save` hook on update', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: existingInstance.id, name: 'updated name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: existingInstance.id,
|
|
|
|
name: 'updated name',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: false
|
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `after save` hook on create', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateOrCreate(
|
|
|
|
{ id: 'new-id', name: 'a name' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
2015-03-19 12:25:03 +00:00
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
instance: {
|
|
|
|
id: instance.id,
|
|
|
|
name: 'a name',
|
|
|
|
extra: undefined
|
|
|
|
},
|
|
|
|
isNewInstance: true
|
|
|
|
}));
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PersistedModel.deleteAll', function() {
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook with query', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.deleteAll({ name: existingInstance.name }, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
query: { where: { name: existingInstance.name } }
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook without query', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.deleteAll(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ query: { where: {} } }));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updates from `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-29 09:09:01 +00:00
|
|
|
it('triggers `before delete` hook with query', function(done) {
|
|
|
|
TestModel.observe('before delete', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.deleteAll({ name: existingInstance.name }, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { name: existingInstance.name }
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `before delete` hook without query', function(done) {
|
|
|
|
TestModel.observe('before delete', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.deleteAll(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ where: {} }));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updates from `before delete` hook', function(done) {
|
|
|
|
TestModel.observe('before delete', function(ctx, next) {
|
|
|
|
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);
|
2015-02-21 00:10:25 +00:00
|
|
|
(inst ? inst.toObject() : 'null').should.
|
|
|
|
eql(existingInstance.toObject());
|
2015-01-29 09:09:01 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
it('triggers `after delete` hook without query', function(done) {
|
|
|
|
TestModel.observe('after delete', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.deleteAll(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ where: {} }));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `after delete` hook without query', function(done) {
|
|
|
|
TestModel.observe('after delete', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.deleteAll({ name: existingInstance.name }, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
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() {
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
existingInstance.delete(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
query: { where: { id: existingInstance.id } }
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updated from `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-29 09:09:01 +00:00
|
|
|
it('triggers `before delete` hook', function(done) {
|
|
|
|
TestModel.observe('before delete', pushContextAndNext());
|
|
|
|
|
|
|
|
existingInstance.delete(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
2015-03-05 17:16:12 +00:00
|
|
|
where: { id: existingInstance.id },
|
|
|
|
instance: existingInstance
|
2015-01-29 09:09:01 +00:00
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updated from `before delete` hook', function(done) {
|
|
|
|
TestModel.observe('before delete', function(ctx, next) {
|
|
|
|
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);
|
2015-02-21 00:10:25 +00:00
|
|
|
(inst ? inst.toObject() : 'null').should.eql(
|
|
|
|
existingInstance.toObject());
|
2015-01-29 09:09:01 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
it('triggers `after delete` hook', function(done) {
|
|
|
|
TestModel.observe('after delete', pushContextAndNext());
|
|
|
|
|
|
|
|
existingInstance.delete(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
2015-03-05 17:16:12 +00:00
|
|
|
where: { id: existingInstance.id },
|
|
|
|
instance: existingInstance
|
2015-01-21 16:57:47 +00:00
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `after delete` hook without query', function(done) {
|
|
|
|
TestModel.observe('after delete', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.deleteAll({ name: existingInstance.name }, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-05 14:53:34 +00:00
|
|
|
it('propagates hookState from `before delete` to `after delete`', function(done) {
|
2015-03-05 10:55:04 +00:00
|
|
|
TestModel.observe('before delete', pushContextAndNext(function(ctx) {
|
|
|
|
ctx.hookState.foo = 'bar';
|
|
|
|
}));
|
|
|
|
|
|
|
|
TestModel.observe('after delete', pushContextAndNext(function(ctx) {
|
|
|
|
ctx.hookState.foo = ctx.hookState.foo.toUpperCase();
|
|
|
|
}));
|
|
|
|
|
|
|
|
existingInstance.delete(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql([
|
2015-03-19 12:25:03 +00:00
|
|
|
aTestModelCtx({
|
2015-03-05 10:55:04 +00:00
|
|
|
hookState: { foo: 'bar', test: true },
|
2015-03-05 17:16:12 +00:00
|
|
|
where: { id: '1' },
|
|
|
|
instance: existingInstance
|
2015-03-05 10:55:04 +00:00
|
|
|
}),
|
2015-03-19 12:25:03 +00:00
|
|
|
aTestModelCtx({
|
2015-03-05 10:55:04 +00:00
|
|
|
hookState: { foo: 'BAR', test: true },
|
2015-03-05 17:16:12 +00:00
|
|
|
where: { id: '1' },
|
|
|
|
instance: existingInstance
|
2015-03-05 10:55:04 +00:00
|
|
|
})
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
it('triggers hooks only once', function(done) {
|
2015-02-02 09:31:45 +00:00
|
|
|
TestModel.observe('access', pushNameAndNext('access'));
|
2015-01-21 16:57:47 +00:00
|
|
|
TestModel.observe('after delete', pushNameAndNext('after delete'));
|
2015-02-02 09:31:45 +00:00
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +00:00
|
|
|
ctx.query = { where: { id: { neq: existingInstance.id } } };
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
existingInstance.delete(function(err) {
|
|
|
|
if (err) return done(err);
|
2015-02-02 09:31:45 +00:00
|
|
|
observersCalled.should.eql(['access', 'after delete']);
|
2015-01-21 16:57:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PersistedModel.updateAll', function() {
|
2015-02-02 09:31:45 +00:00
|
|
|
it('triggers `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', pushContextAndNext());
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
TestModel.updateAll(
|
|
|
|
{ name: 'searched' },
|
|
|
|
{ name: 'updated' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({ query: {
|
|
|
|
where: { name: 'searched' }
|
|
|
|
}}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-02 09:31:45 +00:00
|
|
|
it('applies updates from `access` hook', function(done) {
|
|
|
|
TestModel.observe('access', function(ctx, next) {
|
2015-01-21 16:57:47 +00:00
|
|
|
ctx.query = { where: { id: { neq: existingInstance.id } } };
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.updateAll(
|
|
|
|
{ id: existingInstance.id },
|
|
|
|
{ name: 'new name' },
|
|
|
|
function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
findTestModels({ fields: ['id', 'name' ] }, function(err, list) {
|
|
|
|
if (err) return done(err);
|
|
|
|
(list||[]).map(toObject).should.eql([
|
|
|
|
{ id: existingInstance.id, name: existingInstance.name, extra: undefined },
|
|
|
|
{ id: '2', name: 'new name', extra: undefined }
|
|
|
|
]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('triggers `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateAll(
|
|
|
|
{ name: 'searched' },
|
|
|
|
{ name: 'updated' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { name: 'searched' },
|
|
|
|
data: { name: 'updated' },
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updates from `before save` hook', function(done) {
|
|
|
|
TestModel.observe('before save', function(ctx, next) {
|
|
|
|
ctx.data = { name: 'hooked', extra: 'added' };
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel.updateAll(
|
|
|
|
{ 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) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateAll(
|
2015-06-15 22:17:47 +00:00
|
|
|
{ name: existingInstance.name },
|
2015-05-13 23:14:40 +00:00
|
|
|
{ name: 'changed' },
|
|
|
|
function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
data: { name: 'changed' },
|
2015-06-15 22:17:47 +00:00
|
|
|
where: { name: existingInstance.name }
|
2015-05-13 23:14:40 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies updates from `persist` hook', function(done) {
|
|
|
|
TestModel.observe('persist', pushContextAndNext(function(ctx){
|
|
|
|
ctx.data.extra = 'hook data';
|
|
|
|
}));
|
|
|
|
|
|
|
|
TestModel.updateAll(
|
|
|
|
{ id: existingInstance.id },
|
|
|
|
{ name: 'changed' },
|
|
|
|
function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
loadTestModel(existingInstance.id, function(err, instance) {
|
|
|
|
instance.should.have.property('extra', 'hook data');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
it('triggers `after save` hook', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateAll(
|
|
|
|
{ id: existingInstance.id },
|
|
|
|
{ name: 'updated name' },
|
|
|
|
function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.should.eql(aTestModelCtx({
|
|
|
|
where: { id: existingInstance.id },
|
|
|
|
data: { name: 'updated name' }
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-30 13:03:45 +00:00
|
|
|
|
|
|
|
it('accepts hookState from options', function(done) {
|
|
|
|
TestModel.observe('after save', pushContextAndNext());
|
|
|
|
|
|
|
|
TestModel.updateAll(
|
|
|
|
{ id: existingInstance.id },
|
|
|
|
{ name: 'updated name' },
|
|
|
|
{ foo: 'bar' },
|
|
|
|
function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
observedContexts.options.should.eql({
|
|
|
|
foo: 'bar'
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
});
|
|
|
|
|
2015-03-05 10:55:04 +00:00
|
|
|
function pushContextAndNext(fn) {
|
2015-01-21 16:57:47 +00:00
|
|
|
return function(context, next) {
|
2015-03-05 10:55:04 +00:00
|
|
|
if (typeof fn === 'function') {
|
|
|
|
fn(context);
|
|
|
|
}
|
|
|
|
|
2015-01-21 16:57:47 +00:00
|
|
|
context = deepCloneToObject(context);
|
2015-03-05 10:55:04 +00:00
|
|
|
context.hookState.test = true;
|
2015-01-21 16:57:47 +00:00
|
|
|
|
|
|
|
if (typeof observedContexts === 'string') {
|
|
|
|
observedContexts = context;
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(observedContexts)) {
|
|
|
|
observedContexts = [observedContexts];
|
|
|
|
}
|
|
|
|
|
|
|
|
observedContexts.push(context);
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function pushNameAndNext(name) {
|
|
|
|
return function(context, next) {
|
|
|
|
observersCalled.push(name);
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 aTestModelCtx(ctx) {
|
|
|
|
ctx.Model = TestModel;
|
2015-03-05 10:55:04 +00:00
|
|
|
if (!ctx.hookState) {
|
|
|
|
ctx.hookState = { test: true };
|
|
|
|
}
|
2015-03-30 13:03:45 +00:00
|
|
|
if (!ctx.options) {
|
|
|
|
ctx.options = {};
|
|
|
|
}
|
2015-01-21 16:57:47 +00:00
|
|
|
return deepCloneToObject(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findTestModels(query, cb) {
|
|
|
|
if (cb === undefined && typeof query === 'function') {
|
|
|
|
cb = query;
|
|
|
|
query = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestModel.find(query, { notify: false }, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadTestModel(id, cb) {
|
|
|
|
TestModel.findOne({ where: { id: id } }, { notify: false }, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
function uid() {
|
|
|
|
lastId += 1;
|
|
|
|
return '' + lastId;
|
|
|
|
}
|
2015-02-03 07:34:49 +00:00
|
|
|
|
|
|
|
function getLastGeneratedUid() {
|
|
|
|
return '' + lastId;
|
|
|
|
}
|
2015-01-21 16:57:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function deepCloneToObject(obj) {
|
|
|
|
return traverse(obj).map(function(x) {
|
2015-02-21 00:10:25 +00:00
|
|
|
if (x === undefined) {
|
|
|
|
// RDBMSs return null
|
|
|
|
return null;
|
|
|
|
}
|
2015-01-21 16:57:47 +00:00
|
|
|
if (x && x.toObject)
|
|
|
|
return x.toObject(true);
|
|
|
|
if (x && typeof x === 'function' && x.modelName)
|
|
|
|
return '[ModelCtor ' + x.modelName + ']';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function get(propertyName) {
|
|
|
|
return function(obj) {
|
|
|
|
return obj[propertyName];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function toObject(obj) {
|
|
|
|
return obj.toObject ? obj.toObject() : obj;
|
|
|
|
}
|
|
|
|
};
|