Skip to content

Tests that logging and transaction options are being passed internally #3834

@overlookmotel

Description

@overlookmotel

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

  1. new Sequelize() at the start of the tests is provided with an options.logging function. 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');
    }
} );
  1. All Sequelize methods are shimmed within the tests to insert an options.logging function 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

  1. Start a transaction before each test
  2. Each sequelize call in the tests passes this transaction
  3. options.logging function 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: in discussionFor issues and PRs. Community and maintainers are discussing the applicability of the issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions