Skip to content

Commit 3fb43bd

Browse files
committed
test: use TransactionTestCase._post_teardown in _django_db_fixture_helper
Let's see where this blows up.
1 parent 55f5ddd commit 3fb43bd

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

pytest_django/fixtures.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,44 @@ def _django_db_fixture_helper(transactional, request, _django_cursor_wrapper):
6464
if is_django_unittest(request):
6565
return
6666

67+
if not transactional and 'live_server' in request.funcargnames:
68+
# Do nothing, we get called with transactional=True, too.
69+
return
70+
71+
django_case = None
72+
73+
_django_cursor_wrapper.enable()
74+
request.addfinalizer(_django_cursor_wrapper.disable)
75+
6776
if transactional:
68-
_django_cursor_wrapper.enable()
69-
70-
def flushdb():
71-
"""Flush the database and close database connections"""
72-
# Django does this by default *before* each test
73-
# instead of after.
74-
from django.db import connections
75-
from django.core.management import call_command
76-
77-
for db in connections:
78-
call_command('flush', verbosity=0,
79-
interactive=False, database=db)
80-
for conn in connections.all():
81-
conn.close()
82-
83-
request.addfinalizer(_django_cursor_wrapper.disable)
84-
request.addfinalizer(flushdb)
77+
from django import get_version
78+
79+
if get_version() >= '1.5':
80+
from django.test import TransactionTestCase as django_case
81+
82+
else:
83+
# Django before 1.5 flushed the DB during setUp.
84+
# Use pytest-django's old behavior with it.
85+
def flushdb():
86+
"""Flush the database and close database connections"""
87+
# Django does this by default *before* each test
88+
# instead of after.
89+
from django.db import connections
90+
from django.core.management import call_command
91+
92+
for db in connections:
93+
call_command('flush', verbosity=0,
94+
interactive=False, database=db)
95+
for conn in connections.all():
96+
conn.close()
97+
request.addfinalizer(flushdb)
98+
8599
else:
86-
if 'live_server' in request.funcargnames:
87-
return
88-
from django.test import TestCase
100+
from django.test import TestCase as django_case
89101

90-
_django_cursor_wrapper.enable()
91-
_django_cursor_wrapper._is_transactional = False
92-
case = TestCase(methodName='__init__')
102+
if django_case:
103+
case = django_case(methodName='__init__')
93104
case._pre_setup()
94-
request.addfinalizer(_django_cursor_wrapper.disable)
95105
request.addfinalizer(case._post_teardown)
96106

97107

0 commit comments

Comments
 (0)