rochejul / sequelize-mocking

Sequelize extension to deal with data-mocking for testing
MIT License
63 stars 26 forks source link

Getting error on aftereach restore #4

Closed justechn closed 6 years ago

justechn commented 7 years ago

I am getting the following error from this library when the tests are done ER_ROW_IS_REFERENCED: Cannot delete or update a parent row: a foreign key constraint fails

This is obviously because I have foreign keys in my database, so I looked to see how sequelize handles this internally and I found in the query-interface.js on line 238

return self.sequelize.query('PRAGMA foreign_keys = OFF', options).then(function() {
    return dropAllTables(tableNames).then(function() {
        return self.sequelize.query('PRAGMA foreign_keys = ON', options);

So I tried to modify the restore function in your module to do the same

return mockedSequelize
    .query('PRAGMA foreign_keys = OFF', {raw: true})
    .then(function () {
        return mockedSequelize
            .drop({ 'logging': logging })
            .then(function () {
                logging && console.log('SequelizeMocking - Context is restored');
            });
    });

but I still get the same error, so something is not working. Do you have any idea what I am doing wrong?

justechn commented 7 years ago

I think I found a solution. The native sequelize dropAllTables works great.

return mockedSequelize
    .getQueryInterface()
    .dropAllTables({ 'logging': logging })
    .then(function () {
        logging && console.log('SequelizeMocking - Context is restored');
   });

should I submit a PR to get this changed?

rochejul commented 7 years ago

Sure, this is a good idea

Maybe try to add some tests to check your needs into the pull request

Many thanks for your work

Cheers

anibali commented 7 years ago

Further to this (and perhaps scarier), I find that the existing version of restore drops all of the tables in my actual database. The dropAllTables solution does not have this issue.

rochejul commented 7 years ago

Hi,

Have you got a pull-request ?

Shall I only change this line https://github.com/rochejul/sequelize-mocking/blob/master/lib/sequelize-mocking.js#L226 with:

return mockedSequelize
    .getQueryInterface()
    .dropAllTables({ 'logging': logging })
    .then(function () {
        logging && console.log('SequelizeMocking - Context is restored');
   });

Many thanks for your feedback

Cheers

justechn commented 7 years ago

Yes, that is the change I made. https://github.com/justechn/sequelize-mocking/commit/73f9e9b65312ac1b529235ba971de09a9aa0dc2c

I forked the repo and made the changes, but it caused the tests to fail. I have not had time to dig into why.

rochejul commented 7 years ago

Copy that

If I have some times, I will look on it

Many thanks

Cheers