15
15
16
16
from .lazy_django import skip_if_no_django
17
17
18
- __all__ = ['django_db_setup' , 'db' , 'transactional_db' , 'admin_user ' ,
19
- 'django_user_model' , 'django_username_field' ,
18
+ __all__ = ['django_db_setup' , 'db' , 'transactional_db' , 'django_db_reset_sequences ' ,
19
+ 'admin_user' , ' django_user_model' , 'django_username_field' ,
20
20
'client' , 'admin_client' , 'rf' , 'settings' , 'live_server' ,
21
21
'_live_server_helper' , 'django_assert_num_queries' ,
22
22
'django_assert_max_num_queries' ]
@@ -109,7 +109,8 @@ def teardown_database():
109
109
request .addfinalizer (teardown_database )
110
110
111
111
112
- def _django_db_fixture_helper (transactional , request , django_db_blocker ):
112
+ def _django_db_fixture_helper (request , django_db_blocker ,
113
+ transactional = False , reset_sequences = False ):
113
114
if is_django_unittest (request ):
114
115
return
115
116
@@ -122,6 +123,11 @@ def _django_db_fixture_helper(transactional, request, django_db_blocker):
122
123
123
124
if transactional :
124
125
from django .test import TransactionTestCase as django_case
126
+
127
+ if reset_sequences :
128
+ class ResetSequenceTestCase (django_case ):
129
+ reset_sequences = True
130
+ django_case = ResetSequenceTestCase
125
131
else :
126
132
from django .test import TestCase as django_case
127
133
@@ -141,7 +147,7 @@ def _disable_native_migrations():
141
147
142
148
@pytest .fixture (scope = 'function' )
143
149
def db (request , django_db_setup , django_db_blocker ):
144
- """Require a django test database
150
+ """Require a django test database.
145
151
146
152
This database will be setup with the default fixtures and will have
147
153
the transaction management disabled. At the end of the test the outer
@@ -150,30 +156,54 @@ def db(request, django_db_setup, django_db_blocker):
150
156
This is more limited than the ``transactional_db`` resource but
151
157
faster.
152
158
153
- If both this and ``transactional_db`` are requested then the
154
- database setup will behave as only ``transactional_db`` was
155
- requested .
159
+ If multiple database fixtures are requested, they take precedence
160
+ over each other in the following order (the last one wins): ``db``,
161
+ ``transactional_db``, ``django_db_reset_sequences`` .
156
162
"""
163
+ if 'django_db_reset_sequences' in request .funcargnames :
164
+ request .getfixturevalue ('django_db_reset_sequences' )
157
165
if 'transactional_db' in request .funcargnames \
158
166
or 'live_server' in request .funcargnames :
159
167
request .getfixturevalue ('transactional_db' )
160
168
else :
161
- _django_db_fixture_helper (False , request , django_db_blocker )
169
+ _django_db_fixture_helper (request , django_db_blocker , transactional = False )
162
170
163
171
164
172
@pytest .fixture (scope = 'function' )
165
173
def transactional_db (request , django_db_setup , django_db_blocker ):
166
- """Require a django test database with transaction support
174
+ """Require a django test database with transaction support.
167
175
168
176
This will re-initialise the django database for each test and is
169
177
thus slower than the normal ``db`` fixture.
170
178
171
179
If you want to use the database with transactions you must request
172
- this resource. If both this and ``db`` are requested then the
173
- database setup will behave as only ``transactional_db`` was
174
- requested.
180
+ this resource.
181
+
182
+ If multiple database fixtures are requested, they take precedence
183
+ over each other in the following order (the last one wins): ``db``,
184
+ ``transactional_db``, ``django_db_reset_sequences``.
185
+ """
186
+ if 'django_db_reset_sequences' in request .funcargnames :
187
+ request .getfuncargvalue ('django_db_reset_sequences' )
188
+ _django_db_fixture_helper (request , django_db_blocker ,
189
+ transactional = True )
190
+
191
+
192
+ @pytest .fixture (scope = 'function' )
193
+ def django_db_reset_sequences (request , django_db_setup , django_db_blocker ):
194
+ """Require a transactional test database with sequence reset support.
195
+
196
+ This behaves like the ``transactional_db`` fixture, with the addition
197
+ of enforcing a reset of all auto increment sequences. If the enquiring
198
+ test relies on such values (e.g. ids as primary keys), you should
199
+ request this resource to ensure they are consistent across tests.
200
+
201
+ If multiple database fixtures are requested, they take precedence
202
+ over each other in the following order (the last one wins): ``db``,
203
+ ``transactional_db``, ``django_db_reset_sequences``.
175
204
"""
176
- _django_db_fixture_helper (True , request , django_db_blocker )
205
+ _django_db_fixture_helper (request , django_db_blocker ,
206
+ transactional = True , reset_sequences = True )
177
207
178
208
179
209
@pytest .fixture ()
0 commit comments