Skip to content
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

Commit or rollback? #8

Open
yallie opened this issue Dec 7, 2016 · 10 comments
Open

Commit or rollback? #8

yallie opened this issue Dec 7, 2016 · 10 comments

Comments

@yallie
Copy link
Contributor

yallie commented Dec 7, 2016

Hello Adrian,

just noticed that test_autonomous(p_statement) function performs COMMIT when the p_statement executed correctly. That of course means that running unit tests may leave their traces such as inserted/deleted records, etc.

I believe that the autonomous transactions of the unit tests should be rolled back in any case. What do you think?

@adrianandrei-ca
Copy link
Owner

adrianandrei-ca commented Dec 7, 2016 via email

@yallie
Copy link
Contributor Author

yallie commented Dec 7, 2016

The rollback guarantees that the database will be left exact in the same state as before executing the test case. On the contrary, data cleanup performed by tear down function, can be flawed and we cannot guarantee anything about it.

What that means is that the broken test suite will probably damage the database which is something that's not supposed to happen. I believe that one should be able to run

select * from test_run_suite('business_logic_tests_by_junior_developer')

without expecting bad side effects from it.

@adrianandrei-ca
Copy link
Owner

adrianandrei-ca commented Dec 7, 2016 via email

@yallie
Copy link
Contributor Author

yallie commented Dec 7, 2016

Commit should be part of the test

Can you please explain why? I think that individual test cases should be transparent to the transactions. I mean that a test case shouldn't be aware of the results of other test cases in the same suite.

Tell the junior developer to write the tests this way. ☺

It's just like to say him to write good software and avoid bugs. No matter how we teach our developers they will write flawed code every now and then, unintentionally.

By the way, I've checked the behavior of other Postgres unit testing frameworks concerning transactions. Both PGUnit and pgTAP mentioned in Postgres documentation stick with rolling back the test transactions.

@yallie
Copy link
Contributor Author

yallie commented Dec 7, 2016

P.S. Same with Epic test framework (the website is now defunkt, but the code is mirrored here):

-- ALWAYS RAISE EXCEPTION at the end of test procs to rollback!
RAISE EXCEPTION '[OK]';

@adrianandrei-ca
Copy link
Owner

adrianandrei-ca commented Dec 7, 2016 via email

@yallie
Copy link
Contributor Author

yallie commented Dec 7, 2016

Thanks for the detailed reply Adrian! I see your point.

I chose your testing framework for another reason though. I like that test cases don't have to follow some strict protocol, such as returning specific data types, raising exceptions at the end, etc. I like this no-fuss minimalistic approach and hierarchical test suite grouping. Great stuff, very easy to use! 👍

One can have a trigger on commit that will not be tested if there is a rollback.

That's true, but the trigger logic can be extracted to a function and tested separately.

Space and performance is also impacted.

Either way, whether you commit or roll back the transaction, you have that transactional MVCC overhead.

Also I was looking at the fact that I want the data in when a test fails so further debugging can be done.

But then why do you roll back the autonomous transaction on exceptions?

Another aspect was test chaining as I want the ability to split complex tests into smaller one and group them in suites without changing the actual code.

Well, this particular problem can be addressed by running a transaction per test suite, not per individual test case.

Thanks, Alexey

@yallie
Copy link
Contributor Author

yallie commented Dec 8, 2016

Hello Adrian,

can we just make this behavior adjustable? I'd really like to continue using this framework, but this potential damage issue keeps troubling me. Also, I found that cleanup logic doubles the amount of the test code to be maintained. It can get quite complex for non-trivial tests, which isn't good, too (caught myself thinking about unit testing the cleanup logic).

Having added a little option we could keep best of two worlds by choosing either bulletproof-safe auto-rollback or not-so-safe but close to production auto-commit mode. Would you accept such a PR?

Regards, Alexey

@adrianandrei-ca
Copy link
Owner

adrianandrei-ca commented Dec 8, 2016 via email

@yallie
Copy link
Contributor Author

yallie commented Dec 8, 2016

Cool, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants