-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Tests that logging and transaction options are being passed internally #3834
Description
I just submitted a PR #3833 concerning some places where options.logging was not being passed around internally within Sequelize.
I'm not completely confident, however, that I've found all the places where this might be happening, and it's now got me worrying about something else...
The problem
More critically, the same may be happening with options.transaction.
I found by accident one place where the transaction was getting lost (lib/model.js line 1553), due to the recent change in the signature of Model#findAll(). My recent PR fixes that case, but I'd bet there are more of these bugs lurking in the code base.
It feels to me like this might need a systematic approach to test coverage.
Here's a possible approach:
Testing for options.logging
new Sequelize()at the start of the tests is provided with anoptions.loggingfunction. That function throws an error if it is called.
var sequelize = new Sequelize( database, username, password, {
logging: function() {
throw new Error('options.logging not passed correctly');
}
} );- All Sequelize methods are shimmed within the tests to insert an
options.loggingfunction which does nothing, but serves to override the throwing logging function.
Model.prototype.testDestroy = function(options) {
options = options || {};
if (options.logging === undefined) options.logging = function() {};
return this.destroy(options);
}So now any place where options.logging is not passed correctly, it will fall back to the function defined in new Sequelize() which will throw an error causing the test to fail.
The difficult part is making sure that the shim functions are used when called by the tests but not within the Sequelize codebase itself, without re-writing all the tests.
Testing for options.transaction
- Start a transaction before each test
- Each sequelize call in the tests passes this transaction
options.loggingfunction defined universally checks each time it is called that the message it gets indicates that the SQL was run within a transaction
(obviously tests specifically dealing with transactions would need a separate treatment)
Conclusion
Transactions not behaving as expected could have pretty dire consequences, and a slip-up where a transaction doesn't get passed around within Sequelize is a pretty easy coding mistake to make.
Therefore it feels to me that this should have extensive test coverage.
There may well be better ways to do it than what I've outlined about, though.
Any thoughts?