@@ -59,6 +59,39 @@ def teardown_database():
59
59
if not request .config .getvalue ('reuse_db' ):
60
60
request .addfinalizer (teardown_database )
61
61
62
+ def _django_db_fixture_helper (transactional , request , _django_cursor_wrapper ):
63
+ if is_django_unittest (request .node ):
64
+ return
65
+
66
+ if transactional :
67
+ _django_cursor_wrapper .enable ()
68
+
69
+ def flushdb ():
70
+ """Flush the database and close database connections"""
71
+ # Django does this by default *before* each test
72
+ # instead of after.
73
+ from django .db import connections
74
+ from django .core .management import call_command
75
+
76
+ for db in connections :
77
+ call_command ('flush' , verbosity = 0 ,
78
+ interactive = False , database = db )
79
+ for conn in connections .all ():
80
+ conn .close ()
81
+
82
+ request .addfinalizer (_django_cursor_wrapper .disable )
83
+ request .addfinalizer (flushdb )
84
+ else :
85
+ if 'live_server' in request .funcargnames :
86
+ return
87
+ from django .test import TestCase
88
+
89
+ _django_cursor_wrapper .enable ()
90
+ _django_cursor_wrapper ._is_transactional = False
91
+ case = TestCase (methodName = '__init__' )
92
+ case ._pre_setup ()
93
+ request .addfinalizer (_django_cursor_wrapper .disable )
94
+ request .addfinalizer (case ._post_teardown )
62
95
63
96
################ User visible fixtures ################
64
97
@@ -70,24 +103,16 @@ def db(request, _django_db_setup, _django_cursor_wrapper):
70
103
This database will be setup with the default fixtures and will
71
104
have the transaction management disabled. At the end of the test
72
105
the transaction will be rolled back to undo any changes to the
73
- database. This is more limited then the ``transaction_db ``
106
+ database. This is more limited then the ``transactional_db ``
74
107
resource but faster.
75
108
76
- If both this and ``transaction_db `` are requested then the
77
- database setup will behave as only ``transaction_db `` was
109
+ If both this and ``transactional_db `` are requested then the
110
+ database setup will behave as only ``transactional_db `` was
78
111
requested.
79
112
"""
80
- if ('transactional_db' not in request .funcargnames and
81
- 'live_server' not in request .funcargnames and
82
- not is_django_unittest (request .node )):
83
-
84
- from django .test import TestCase
85
-
86
- _django_cursor_wrapper .enable ()
87
- case = TestCase (methodName = '__init__' )
88
- case ._pre_setup ()
89
- request .addfinalizer (_django_cursor_wrapper .disable )
90
- request .addfinalizer (case ._post_teardown )
113
+ if 'transactional_db' in request .funcargnames :
114
+ return request .getfuncargvalue ('transactional_db' )
115
+ return _django_db_fixture_helper (False , request , _django_cursor_wrapper )
91
116
92
117
93
118
@pytest .fixture (scope = 'function' )
@@ -99,27 +124,10 @@ def transactional_db(request, _django_db_setup, _django_cursor_wrapper):
99
124
100
125
If you want to use the database with transactions you must request
101
126
this resource. If both this and ``db`` are requested then the
102
- database setup will behave as only ``transaction_db `` was
127
+ database setup will behave as only ``transactional_db `` was
103
128
requested.
104
129
"""
105
- if not is_django_unittest (request .node ):
106
- _django_cursor_wrapper .enable ()
107
-
108
- def flushdb ():
109
- """Flush the database and close database connections"""
110
- # Django does this by default *before* each test
111
- # instead of after.
112
- from django .db import connections
113
- from django .core .management import call_command
114
-
115
- for db in connections :
116
- call_command ('flush' , verbosity = 0 ,
117
- interactive = False , database = db )
118
- for conn in connections .all ():
119
- conn .close ()
120
-
121
- request .addfinalizer (_django_cursor_wrapper .disable )
122
- request .addfinalizer (flushdb )
130
+ return _django_db_fixture_helper (True , request , _django_cursor_wrapper )
123
131
124
132
125
133
@pytest .fixture ()
0 commit comments