Skip to content

Commit d0efc7d

Browse files
committed
Apply reset_sequences fixture where needed to limit slow down
1 parent d8df0f1 commit d0efc7d

11 files changed

Lines changed: 34 additions & 12 deletions

tests/conftest.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,20 @@
77
from django.core import management
88
from django.core.management.color import no_style
99
from django.db import connection
10-
from django.test import TestCase, TransactionTestCase
1110

1211

13-
@pytest.fixture(autouse=True)
14-
def _reset_sequences(request):
15-
"""Reset all database sequences so PKs start from 1 in each test.
12+
@pytest.fixture
13+
def reset_sequences():
14+
"""
15+
Reset all database sequences so PKs start from 1.
1616
1717
PostgreSQL sequences are non-transactional and persist across
18-
TestCase's transaction rollbacks. This fixture ensures every test
19-
gets predictable PKs starting from 1 regardless of execution order.
20-
No-op on SQLite and skipped for tests that don't use the database.
18+
TestCase's transaction rollbacks. Apply this fixture to test
19+
classes that rely on hardcoded PKs to keep them predictable
20+
regardless of execution order. No-op on SQLite.
2121
"""
2222
if connection.vendor != 'postgresql':
2323
return
24-
# Only run for tests that actually have database access.
25-
if not (request.cls and issubclass(request.cls, (TestCase, TransactionTestCase))):
26-
if 'db' not in request.fixturenames and 'transactional_db' not in request.fixturenames:
27-
return
28-
2924
table_names = set(connection.introspection.table_names())
3025
models = [m for m in apps.get_models() if m._meta.db_table in table_names]
3126
sql_list = connection.ops.sequence_reset_sql(no_style(), models)

tests/test_filters.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ class Meta:
459459
fields = '__all__'
460460

461461

462+
@pytest.mark.usefixtures("reset_sequences")
462463
class SearchFilterM2MTests(TestCase):
463464
def setUp(self):
464465
# Sequence of title/text/attributes is:
@@ -657,6 +658,7 @@ class Meta:
657658
fields = '__all__'
658659

659660

661+
@pytest.mark.usefixtures("reset_sequences")
660662
class OrderingFilterTests(TestCase):
661663
def setUp(self):
662664
# Sequence of title/text is:
@@ -974,6 +976,7 @@ class Meta:
974976
fields = ('id', 'user')
975977

976978

979+
@pytest.mark.usefixtures("reset_sequences")
977980
class SensitiveOrderingFilterTests(TestCase):
978981
def setUp(self):
979982
for idx in range(3):

tests/test_generics.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class SlugBasedInstanceView(InstanceView):
7676

7777

7878
# Tests
79+
@pytest.mark.usefixtures("reset_sequences")
7980
class TestRootView(TestCase):
8081
def setUp(self):
8182
"""
@@ -171,6 +172,7 @@ def test_post_error_root_view(self):
171172
EXPECTED_QUERIES_FOR_PUT = 2
172173

173174

175+
@pytest.mark.usefixtures("reset_sequences")
174176
class TestInstanceView(TestCase):
175177
def setUp(self):
176178
"""
@@ -334,6 +336,7 @@ def setUp(self):
334336
self.view = FKInstanceView.as_view()
335337

336338

339+
@pytest.mark.usefixtures("reset_sequences")
337340
class TestOverriddenGetObject(TestCase):
338341
"""
339342
Test cases for a RetrieveUpdateDestroyAPIView that does NOT use the
@@ -477,6 +480,7 @@ class Meta:
477480
return DynamicSerializer
478481

479482

483+
@pytest.mark.usefixtures("reset_sequences")
480484
class TestFilterBackendAppliedToViews(TestCase):
481485
def setUp(self):
482486
"""

tests/test_model_serializer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ class DisplayValueModel(models.Model):
758758
color = models.ForeignKey(DisplayValueTargetModel, on_delete=models.CASCADE)
759759

760760

761+
@pytest.mark.usefixtures("reset_sequences")
761762
class TestRelationalFieldDisplayValue(TestCase):
762763
def setUp(self):
763764
DisplayValueTargetModel.objects.bulk_create([

tests/test_pagination.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ class CursorPaginationModel(models.Model):
10451045
created = models.IntegerField()
10461046

10471047

1048+
@pytest.mark.usefixtures("reset_sequences")
10481049
class TestCursorPaginationWithValueQueryset(CursorPaginationTestsMixin, TestCase):
10491050
"""
10501051
Unit tests for `pagination.CursorPagination` for value querysets.

tests/test_permissions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
from unittest import mock
44

5+
import pytest
56
from django.conf import settings
67
from django.contrib.auth.models import AnonymousUser, Group, Permission, User
78
from django.db import models
@@ -73,6 +74,7 @@ def basic_auth_header(username, password):
7374
return 'Basic %s' % base64_credentials
7475

7576

77+
@pytest.mark.usefixtures("reset_sequences")
7678
class ModelPermissionsIntegrationTests(TestCase):
7779
def setUp(self):
7880
User.objects.create_user('disallowed', 'disallowed@example.com', 'password')
@@ -325,6 +327,7 @@ def get_queryset(self):
325327
get_queryset_object_permissions_view = GetQuerysetObjectPermissionInstanceView.as_view()
326328

327329

330+
@pytest.mark.usefixtures("reset_sequences")
328331
@unittest.skipUnless('guardian' in settings.INSTALLED_APPS, 'django-guardian not installed')
329332
class ObjectPermissionsIntegrationTests(TestCase):
330333
"""
@@ -504,6 +507,7 @@ class DeniedObjectViewWithDetail(PermissionInstanceView):
504507
denied_object_view_with_detail = DeniedObjectViewWithDetail.as_view()
505508

506509

510+
@pytest.mark.usefixtures("reset_sequences")
507511
class CustomPermissionsTests(TestCase):
508512
def setUp(self):
509513
BasicModel(text='foo').save()

tests/test_prefetch_related.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from django.contrib.auth.models import Group, User
23
from django.test import TestCase
34

@@ -18,6 +19,7 @@ class UserUpdate(generics.UpdateAPIView):
1819
serializer_class = UserSerializer
1920

2021

22+
@pytest.mark.usefixtures("reset_sequences")
2123
class TestPrefetchRelatedUpdates(TestCase):
2224
def setUp(self):
2325
self.user = User.objects.create(username='tom', email='tom@example.com')

tests/test_relations_hyperlink.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Meta:
6969
fields = ('url', 'name', 'nullable_source')
7070

7171

72+
@pytest.mark.usefixtures("reset_sequences")
7273
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
7374
class HyperlinkedManyToManyTests(TestCase):
7475
def setUp(self):
@@ -203,6 +204,7 @@ def test_data_cannot_be_accessed_prior_to_is_valid(self):
203204
serializer.data
204205

205206

207+
@pytest.mark.usefixtures("reset_sequences")
206208
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
207209
class HyperlinkedForeignKeyTests(TestCase):
208210
def setUp(self):
@@ -332,6 +334,7 @@ def test_foreign_key_update_with_invalid_null(self):
332334
assert serializer.errors == {'target': ['This field may not be null.']}
333335

334336

337+
@pytest.mark.usefixtures("reset_sequences")
335338
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
336339
class HyperlinkedNullableForeignKeyTests(TestCase):
337340
def setUp(self):

tests/test_relations_pk.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class Meta:
9595
fields = '__all__'
9696

9797

98+
@pytest.mark.usefixtures("reset_sequences")
9899
class PKManyToManyTests(TestCase):
99100
def setUp(self):
100101
for idx in range(1, 4):
@@ -226,6 +227,7 @@ def test_data_cannot_be_accessed_prior_to_is_valid(self):
226227
serializer.data
227228

228229

230+
@pytest.mark.usefixtures("reset_sequences")
229231
class PKForeignKeyTests(TestCase):
230232
def setUp(self):
231233
target = ForeignKeyTarget(name='target-1')
@@ -415,6 +417,7 @@ class Meta:
415417
assert len(queryset) == 1
416418

417419

420+
@pytest.mark.usefixtures("reset_sequences")
418421
class PKRelationTests(TestCase):
419422

420423
def setUp(self):
@@ -443,6 +446,7 @@ def test_relation_field_property_source(self):
443446
self.assertEqual(serializer.data, expected)
444447

445448

449+
@pytest.mark.usefixtures("reset_sequences")
446450
class PKNullableForeignKeyTests(TestCase):
447451
def setUp(self):
448452
target = ForeignKeyTarget(name='target-1')
@@ -559,6 +563,7 @@ def test_nullable_uuid_foreign_key_is_valid_when_none(self):
559563
assert serializer.is_valid(), serializer.errors
560564

561565

566+
@pytest.mark.usefixtures("reset_sequences")
562567
class PKNullableOneToOneTests(TestCase):
563568
def setUp(self):
564569
target = OneToOneTarget(name='target-1')

tests/test_relations_slug.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from django.test import TestCase
23

34
from rest_framework import serializers
@@ -42,6 +43,7 @@ class Meta:
4243

4344

4445
# TODO: M2M Tests, FKTests (Non-nullable), One2One
46+
@pytest.mark.usefixtures("reset_sequences")
4547
class SlugForeignKeyTests(TestCase):
4648
def setUp(self):
4749
target = ForeignKeyTarget(name='target-1')
@@ -182,6 +184,7 @@ def test_foreign_key_update_with_invalid_null(self):
182184
assert serializer.errors == {'target': ['This field may not be null.']}
183185

184186

187+
@pytest.mark.usefixtures("reset_sequences")
185188
class SlugNullableForeignKeyTests(TestCase):
186189
def setUp(self):
187190
target = ForeignKeyTarget(name='target-1')

0 commit comments

Comments
 (0)