1515
1616from .lazy_django import skip_if_no_django
1717
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' ,
2020 'client' , 'admin_client' , 'rf' , 'settings' , 'live_server' ,
2121 '_live_server_helper' , 'django_assert_num_queries' ,
2222 'django_assert_max_num_queries' ]
@@ -109,7 +109,8 @@ def teardown_database():
109109 request .addfinalizer (teardown_database )
110110
111111
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 ):
113114 if is_django_unittest (request ):
114115 return
115116
@@ -122,6 +123,11 @@ def _django_db_fixture_helper(transactional, request, django_db_blocker):
122123
123124 if transactional :
124125 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
125131 else :
126132 from django .test import TestCase as django_case
127133
@@ -141,7 +147,7 @@ def _disable_native_migrations():
141147
142148@pytest .fixture (scope = 'function' )
143149def db (request , django_db_setup , django_db_blocker ):
144- """Require a django test database
150+ """Require a django test database.
145151
146152 This database will be setup with the default fixtures and will have
147153 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):
150156 This is more limited than the ``transactional_db`` resource but
151157 faster.
152158
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`` .
156162 """
163+ if 'django_db_reset_sequences' in request .funcargnames :
164+ request .getfixturevalue ('django_db_reset_sequences' )
157165 if 'transactional_db' in request .funcargnames \
158166 or 'live_server' in request .funcargnames :
159167 request .getfixturevalue ('transactional_db' )
160168 else :
161- _django_db_fixture_helper (False , request , django_db_blocker )
169+ _django_db_fixture_helper (request , django_db_blocker , transactional = False )
162170
163171
164172@pytest .fixture (scope = 'function' )
165173def 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.
167175
168176 This will re-initialise the django database for each test and is
169177 thus slower than the normal ``db`` fixture.
170178
171179 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``.
175204 """
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 )
177207
178208
179209@pytest .fixture ()
0 commit comments