Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#24652 closed Cleanup/optimization (fixed)

SimpleTestCase should prevent execution of database queries

Reported by: Simon Charette Owned by: Simon Charette
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

SimpleTestCase is a simple subclass of unittest.TestCase with added assertion methods to deal with Django related stuff such as the TestClient.

While it's documented that SimpleTestCase shouldn't be used to test anything related to the ORM, nothing prevent users from doing it in unexpected ways. This can result in hard to detect test isolation issues as we have experienced with the Django test suite.

I suggest to prevent the usage of the ORM during the execution of a SimpleTestCase by raising an exception pointing to the usage of TestCase (or TransactionTestCase) on query execution attempts.

This will also be a good safenet for our internal usage, we should feel confident asking contributors to use SimpleTestCase where appropriate to avoid useless transaction wrapping.

Change History (16)

comment:1 by Simon Charette, 10 years ago

Owner: changed from nobody to Simon Charette
Status: newassigned

comment:2 by Simon Charette, 10 years ago

Has patch: set
Patch needs improvement: set

comment:3 by Anssi Kääriäinen, 10 years ago

I'm not sure full prevention of database queries is a good idea. There are cases where you do want to run outside transaction control but prevent database flushing. The most important reasons for the latter part are that you want to preserve some of your dataset between test runs, and performance of database flushing.

Maybe we could go with an approach where SimpleTestCase disables database queries unless the test class sets attribute allow_database_queries to True. Then the exception could tell why it isn't a good idea to allow database queries, but also allow users to execute queries if they want to do so.

Another approach could be to support TransactionTestCase with "disable_db_flush" attribute. If you use that class, then your database isn't flushed pre or post run. This one is more explicit in opting in to the unsafe behavior. The hard part might be that all flushing must be disabled until all TransactionTestCases with disable_db_flush set to True are ran.

comment:4 by Simon Charette, 10 years ago

Maybe we could go with an approach where SimpleTestCase disables database queries unless the test class sets attribute allow_database_queries to True. Then the exception could tell why it isn't a good idea to allow database queries, but also allow users to execute queries if they want to do so.

This is what the actual implementation does. I'll alter the message error to suggest setting the allow_database_queries attribute to True with a big warning.

comment:5 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:6 by Simon Charette, 9 years ago

Patch needs improvement: unset

comment:7 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Simon Charette <charette.s@…>, 9 years ago

In 3db21c35:

Refs #24652 -- Enforced test isolation in file_storage tests.

comment:9 by Simon Charette <charette.s@…>, 9 years ago

In 8bf1449:

Refs #24652 -- Converted a template test to avoid executing queries.

comment:10 by Simon Charette <charette.s@…>, 9 years ago

In ead36e8:

Refs #24652 -- Made sure template backend tests call their super setUpClass.

comment:11 by Simon Charette <charette.s@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In c15b0c2:

Fixed #24652 -- Disallowed query execution in SimpleTestCase subclasses.

Thanks to Tim and Anssi for the review.

comment:12 by Simon Charette <charette.s@…>, 9 years ago

In e846ea06:

[1.8.x] Refs #24652 -- Enforced test isolation in file_storage tests.

Backport of 3db21c351b9b1108954c388799d35c8dad7dfc19 from master

comment:13 by Simon Charette <charette.s@…>, 9 years ago

In 3ab3be4b:

[1.8.x] Refs #24652 -- Converted a template test to avoid executing queries.

Backport of 8bf1449edb4139451643e3823b194b4c02ca7633 from master

comment:14 by Simon Charette <charette.s@…>, 9 years ago

In 2b2a2157:

[1.8.x] Refs #24652 -- Made sure template backend tests call their super setUpClass.

Backport of ead36e8a471389a6032d825c8245245ebb89ea5d from master

comment:15 by Simon Charette <charette.s@…>, 9 years ago

In be67400b:

Refs #24652 -- Used SimpleTestCase where appropriate.

comment:16 by Simon Charette <charette.s@…>, 9 years ago

In 4ccfc44:

Refs #24652 -- Fixed a test failure in file_uploads tests on Windows.

Thanks to Tim Graham for the report.

Note: See TracTickets for help on using tickets.
Back to Top