forked from mozilla-sensorweb/sensorthings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue mozilla-sensorweb#228] consumers to add model hooks
- Loading branch information
Russ Nicoletti
committed
Jan 26, 2017
1 parent
adf2fd8
commit 9d653f2
Showing
4 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import db from '../src/models/db'; | ||
import * as CONST from './constants'; | ||
|
||
db().then(models => { | ||
describe('db hooks', () => { | ||
beforeEach(() => { | ||
return models.sequelize.transaction(transaction => { | ||
return Promise.all(Object.keys(CONST.entities).map(name => { | ||
return models[name].destroy({ transaction, where: {} }); | ||
})); | ||
}); | ||
}); | ||
|
||
Object.keys(CONST.entities).forEach(entity => { | ||
it(entity + ' db "create" hooks', done => { | ||
models[entity].create(CONST[entity + 'Entity']).then(instance => { | ||
[ | ||
'beforeValidate', | ||
'afterValidate', | ||
'beforeCreate', | ||
'afterCreate' | ||
].forEach(hook => { | ||
hook.should.be.equal(instance[hook]); | ||
}); | ||
}); | ||
done(); | ||
}) | ||
}); | ||
|
||
// This test does not cause the before/afterDestroy hooks to be run. | ||
// From Sequelize documentation: | ||
// (http://docs.sequelizejs.com/en/latest/docs/hooks/) | ||
// "The only way to call beforeDestroy/afterDestroy hooks are on | ||
// associations with `onDelete: 'cascade'` and the option `hooks: true`" | ||
// TBD It seems our ST API implementation does not exercise these hooks. | ||
// Perhaps we should not implement them. | ||
xit('db "destroy" hooks', done => { | ||
models[CONST.things].create(CONST.ThingsEntity).then(instance => { | ||
const id = instance.id; | ||
models[CONST.things].destroy({ where: { id } }).then(() => { | ||
done(); | ||
}); | ||
}); | ||
}) | ||
|
||
xit('db "update" hooks', done => { | ||
// TODO The 'before/afterUpdate' hooks are not being run during this | ||
// test. Why not? | ||
models[CONST.datastreams].create(CONST.DatastreamsEntity).then( | ||
instance => { | ||
const id = instance.id; | ||
const entity = Object.assign({}, CONST.DatastreamsEntity); | ||
entity.name = 'russ'; | ||
models[CONST.datastreams].update(entity, { where: { id: id } }).then( | ||
() => { | ||
done(); | ||
}); | ||
}); | ||
}) | ||
}); | ||
}); | ||
|