New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Yii2] Transactions are not rolled back #27
Comments
Why does |
@Naktibalda Look at the call stack: |
We could load and unload fixtures outside the transaction |
@SamMousa In my case |
The method won't work for MySQL because of that. |
Today I basically tested all possibilities of suites and fixture loading places:
and I got strange results:
After running above test, you will get success, but Color model still persists in database.
Test is failing with message: If you want I can prepare ready-to-test package with above models and tests in some time, but I am very busy now. |
There is no generic fix that will work here. Some DBMSes will commit transactions when DDL queries are executed. Or we create some kind of queue for DDL queries that should be executed after rollback (this would be a big undertaking IMO) |
The easiest way would be to forbid loading fixtures when transaction is started. |
Other DBMSes allow DDL inside transaction; for example postgres. (AFAIK) |
I think I found answer for my 2nd "strange" result - \Yii::$app in acceptance tests is different instance than tested application. So following code is failing:
I believe this is expected, am I right? |
And for below code, there is no transaction started, so nothing is rolled back:
Conclusion: clean-up does not work for acceptance tests, no matter if you use _fixture() and which dbms you use. |
In acceptance tests transactions make no sense. Fixtures could be cleaned up though. |
Your OP was about functional tests though |
I have a suggestion that is kind of related. I was expecting that the flow for running a unit test class is like this:
So I expected fixtures to be loaded only once for a test class. But instead I see that fixtures are reloaded for each single test inside the class. I was expecting that the main reason for transactions was to speed up the loading/unloading of fixtures. If this is not the case I wonder if such a feature could be added. Shouldn't this also solve the problem in this issue? |
You could manually load fixtures in the setup / teardown functions. I personally don't use fixtures at all; I have found it's less work to just create helper functions that create any records I need when I need them. |
@SamMousa imo fixtures are more handy than helpers for many-to-many relationship. |
I'm closing this due to its age. If it is still relevant in current versions feel free to create a new issue. |
What are you trying to achieve?
Run functional tests with database cleanup for Yii2 app.
What do you get instead?
Database is not cleaned up after testing.
I checked database logs and it appears ROLLBACK doesn't work due to ALTER TABLE inside transaction. You can check this fiddle to see the same effect.
I figured out this query is result of calling unloadFixtures() in haveFixtures():
I wonder if calling this function is necessary... so as temporary workaround I commented out one line https://github.com/Codeception/Codeception/blob/3.0/src/Codeception/Module/Yii2.php#L458 and now database is cleared after testing.
I hope you can investigate and solve this problem.
Runtime details
The text was updated successfully, but these errors were encountered: