Skip to content

Refactor TestCases to mixins #5961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a3bed70
refactor(cl): remove SerializeLockFileTestMixin
florean Jul 13, 2025
574d24c
refactor(cl): convert PrayAndPayTestCase to a mixin
florean Jul 13, 2025
c22da50
refactor(cl): convert CourtTestCase to a mixin
florean Jul 13, 2025
3f5ff45
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 14, 2025
a7a6851
refactor(cl): convert PeopleTestCase to a mixin
florean Jul 14, 2025
d6ce517
fix(cl): fix inheritance heirarchy
florean Jul 14, 2025
3ce1ac4
refactor(cl): convert SearchTestCase to a mixin
florean Jul 14, 2025
4be7883
refactor(cl): convert RECAPSearchTestCase to mixin
florean Jul 14, 2025
3cdfbcd
refactor(cl): moved SimpleUserDataMixin to tests/mixins.py
florean Jul 14, 2025
ba965bf
refactor(cl): convert SitemapTest to mixin
florean Jul 14, 2025
f62e8c8
refactor(cl): convert AudioTestCase to mixin
florean Jul 14, 2025
328e1f6
refactor(cl): convert AudioESTestCase to mixin
florean Jul 14, 2025
7943643
refactor(cl): convert SearchAlertsAssertions and V4SearchAPIAssertion…
florean Jul 14, 2025
f072842
refactor(tests): move RestartRateLimitMixin to mixins
florean Jul 14, 2025
a4aa2f2
refactor(cl): move RestartSentEmailQuotaMixin to call site
florean Jul 14, 2025
3d68d8a
refactor(cl): upgrade ESIndexTestCase to full TestCase
florean Jul 14, 2025
b3e3e7f
refactor(tests): changed BaseSeleniumTest to use ESIndexTransactionTe…
florean Jul 14, 2025
02214ca
fix(cl): fix lint problems
florean Jul 14, 2025
65b32bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 14, 2025
21ea072
Merge branch 'main' into refactor-test-mixins
florean Jul 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions cl/alerts/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
from cl.audio.models import Audio
from cl.donate.models import NeonMembership
from cl.favorites.factories import NoteFactory, UserTagFactory
from cl.lib.test_helpers import SimpleUserDataMixin, opinion_v3_search_api_keys
from cl.lib.test_helpers import opinion_v3_search_api_keys
from cl.people_db.factories import PersonFactory
from cl.search.documents import (
ES_CHILD_ID,
Expand Down Expand Up @@ -97,15 +97,18 @@
from cl.tests.cases import (
APITestCase,
ESIndexTestCase,
SearchAlertsAssertions,
TestCase,
)
from cl.tests.mixins import (
SearchAlertsMixin,
SimpleUserDataMixin,
)
from cl.tests.utils import MockResponse, make_client
from cl.users.factories import UserFactory, UserProfileWithParentsFactory
from cl.users.models import EmailSent


class AlertTest(SimpleUserDataMixin, ESIndexTestCase, TestCase):
class AlertTest(SimpleUserDataMixin, ESIndexTestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
Expand Down Expand Up @@ -149,6 +152,7 @@ def assert_form_validation_error(self, expected: str, html_content: str):
)

def setUp(self) -> None:
super().setUp()
# Set up some handy variables
self.alert_params = {
"query": "q=asdf",
Expand All @@ -160,6 +164,7 @@ def setUp(self) -> None:

def tearDown(self) -> None:
Alert.objects.all().delete()
super().tearDown()

async def test_create_alert(self) -> None:
"""Can we create an alert by sending a post?"""
Expand Down Expand Up @@ -751,6 +756,7 @@ class DocketAlertTest(TestCase):

@classmethod
def setUpTestData(cls) -> None:
super().setUpTestData()
cls.user = UserFactory()
cls.court = Court.objects.get(id="scotus")

Expand All @@ -763,6 +769,7 @@ def setUpTestData(cls) -> None:
)

def setUp(self) -> None:
super().setUp()
self.before = now()
# Create a new docket
self.docket = Docket.objects.create(
Expand Down Expand Up @@ -793,6 +800,7 @@ def tearDown(self) -> None:
Docket.objects.all().delete()
DocketAlert.objects.all().delete()
DocketEntry.objects.all().delete()
super().tearDown()

def test_triggering_docket_alert(self) -> None:
"""Does the alert trigger when it should?"""
Expand Down Expand Up @@ -994,16 +1002,19 @@ class AlertAPITests(ESIndexTestCase, APITestCase):

@classmethod
def setUpTestData(cls) -> None:
super().setUpTestData()
cls.user_1 = UserFactory()
cls.user_2 = UserFactory()

def setUp(self) -> None:
super().setUp()
self.alert_path = reverse("alert-list", kwargs={"version": "v4"})
self.client = make_client(self.user_1.pk)
self.client_2 = make_client(self.user_2.pk)

def tearDown(cls):
Alert.objects.all().delete()
super().tearDown()

async def make_an_alert(
self,
Expand Down Expand Up @@ -1480,15 +1491,15 @@ async def test_alert_type_change_for_recap_alerts(self) -> None:


@mock.patch("cl.search.tasks.percolator_alerts_models_supported", new=[Audio])
class SearchAlertsWebhooksTest(
ESIndexTestCase, TestCase, SearchAlertsAssertions
):
class SearchAlertsWebhooksTest(SearchAlertsMixin, ESIndexTestCase):
"""Test Search Alerts Webhooks"""

@classmethod
def setUpTestData(cls):
cls.rebuild_index("alerts.Alert")
cls.rebuild_index("search.OpinionCluster")
# Call to super must come after indices are rebuilt
super().setUpTestData()
cls.user_profile = UserProfileWithParentsFactory()
cls.user_profile_1 = UserProfileWithParentsFactory()
NeonMembership.objects.create(
Expand Down Expand Up @@ -3360,13 +3371,15 @@ def test_get_docket_notes_and_tags_by_user(self) -> None:
side_effect=lambda x, y: True,
)
@override_settings(NO_MATCH_HL_SIZE=100)
class SearchAlertsOAESTests(ESIndexTestCase, TestCase, SearchAlertsAssertions):
class SearchAlertsOAESTests(SearchAlertsMixin, ESIndexTestCase):
"""Test ES Search Alerts"""

@classmethod
def setUpTestData(cls):
cls.rebuild_index("audio.Audio")
cls.rebuild_index("alerts.Alert")
# Call to super must come after indices are rebuilt
super().setUpTestData()
cls.court_1 = CourtFactory(
id="cabc",
full_name="Testing Supreme Court",
Expand Down Expand Up @@ -4730,11 +4743,12 @@ def test_scheduled_hits_limit(self, mock_abort_audio):


@override_settings(ELASTICSEARCH_DISABLED=True)
class SearchAlertsIndexingCommandTests(ESIndexTestCase, TestCase):
class SearchAlertsIndexingCommandTests(ESIndexTestCase):
"""Test the cl_index_search_alerts command"""

@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.user_profile = UserProfileWithParentsFactory()
cls.user_profile_2 = UserProfileWithParentsFactory()

Expand Down Expand Up @@ -4784,6 +4798,7 @@ def setUpTestData(cls):
def tearDown(self) -> None:
self.delete_index("alerts.Alert")
self.create_index("alerts.Alert")
super().tearDown()

@override_settings(ELASTICSEARCH_PAGINATION_BATCH_SIZE=20)
@mock.patch("cl.alerts.management.commands.cl_index_search_alerts.logger")
Expand Down Expand Up @@ -4898,6 +4913,7 @@ def test_avoid_indexing_no_valid_alert_query(self):
class OneClickUnsubscribeTests(TestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.user_profile = UserProfileWithParentsFactory()
cls.alert = DocketAlertWithParentsFactory(
docket__source=Docket.RECAP,
Expand Down Expand Up @@ -4927,6 +4943,7 @@ def test_can_unsubscribe_docket_alert_with_post_request(self):
@override_settings(EMAIL_BACKEND="cl.lib.email_backends.EmailBackend")
class EmailWithSurrogatesTest(TestCase):
def setUp(self):
super().setUp()
EmailSent.objects.all().delete()

@mock.patch("django_ses.SESBackend.get_rate_limit", return_value=10)
Expand Down
10 changes: 6 additions & 4 deletions cl/alerts/tests/tests_recap_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from cl.donate.models import NeonMembership
from cl.lib.date_time import midnight_pt
from cl.lib.redis_utils import get_redis_interface
from cl.lib.test_helpers import RECAPSearchTestCase
from cl.people_db.factories import (
AttorneyFactory,
AttorneyOrganizationFactory,
Expand Down Expand Up @@ -67,7 +66,8 @@
index_docket_parties_in_es,
)
from cl.stats.models import Stat
from cl.tests.cases import ESIndexTestCase, SearchAlertsAssertions, TestCase
from cl.tests.cases import ESIndexTestCase
from cl.tests.mixins import RECAPSearchMixin, SearchAlertsMixin
from cl.tests.utils import MockResponse
from cl.users.factories import UserProfileWithParentsFactory

Expand All @@ -77,7 +77,7 @@
return_value="alert_hits_sweep",
)
class RECAPAlertsSweepIndexTest(
RECAPSearchTestCase, ESIndexTestCase, TestCase, SearchAlertsAssertions
SearchAlertsMixin, RECAPSearchMixin, ESIndexTestCase
):
"""
RECAP Alerts Sweep Index Tests
Expand Down Expand Up @@ -115,6 +115,7 @@ def setUpTestData(cls):
)

def setUp(self):
super().setUp()
self.r = get_redis_interface("CACHE")
self.r.delete("alert_sweep:task_id")
keys = self.r.keys("alert_hits_sweep:*")
Expand Down Expand Up @@ -2653,7 +2654,7 @@ def test_case_only_alerts(self, mock_prefix) -> None:
)
@override_settings(NO_MATCH_HL_SIZE=100)
class RECAPAlertsPercolatorTest(
RECAPSearchTestCase, ESIndexTestCase, TestCase, SearchAlertsAssertions
SearchAlertsMixin, RECAPSearchMixin, ESIndexTestCase
):
"""
RECAP Alerts Percolator Tests
Expand Down Expand Up @@ -2711,6 +2712,7 @@ def setUpTestData(cls):
)

def setUp(self):
super().setUp()
RECAPPercolator._index.delete(ignore=404)
RECAPPercolator.init()

Expand Down
Loading