mockdb tests now more efficient

This commit is contained in:
David Kennedy 2024-07-04 11:00:03 -04:00
parent a6f7cc6f02
commit 36985353c2
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
3 changed files with 40 additions and 16 deletions

View file

@ -525,10 +525,16 @@ class AuditedAdminMockData:
class MockDb(TestCase):
"""Hardcoded mocks make test case assertions straightforward."""
"""Hardcoded mocks make test case assertions straightforward.
setUpClass and tearDownClass are used so that multiple tests
can be executed using the same mock db data without having to
setUp and tearDown the data in between.
This strategy requires that any changes to data within a test
must be cleaned up within the test rather than relying on tearDown."""
def setUp(self):
super().setUp()
@classmethod
def setUpClass(self):
super().setUpClass()
username = "test_user"
first_name = "First"
last_name = "Last"
@ -782,8 +788,9 @@ class MockDb(TestCase):
self.domain_request_6.submission_date = get_time_aware_date(datetime(2024, 4, 2))
self.domain_request_6.save()
def tearDown(self):
super().tearDown()
@classmethod
def tearDownClass(self):
super().tearDownClass()
PublicContact.objects.all().delete()
Domain.objects.all().delete()
DomainInformation.objects.all().delete()

View file

@ -898,8 +898,9 @@ class TestListHeaderAdmin(TestCase):
class TestMyUserAdmin(MockDb):
def setUp(self):
super().setUp()
@classmethod
def setUpClass(self):
super().setUpClass()
admin_site = AdminSite()
self.admin = MyUserAdmin(model=get_user_model(), admin_site=admin_site)
self.client = Client(HTTP_HOST="localhost:8080")
@ -907,8 +908,9 @@ class TestMyUserAdmin(MockDb):
self.staffuser = create_user()
self.test_helper = GenericTestHelper(admin=self.admin)
def tearDown(self):
super().tearDown()
@classmethod
def tearDownClass(self):
super().tearDownClass()
DomainRequest.objects.all().delete()
User.objects.all().delete()
@ -1094,6 +1096,15 @@ class TestMyUserAdmin(MockDb):
expected_href = reverse("admin:registrar_domain_change", args=[domain_deleted.pk])
self.assertNotContains(response, expected_href)
# Must clean up within test since MockDB is shared across tests for performance reasons
domain_request_started.delete()
domain_request_submitted.delete()
domain_request_in_review.delete()
domain_request_withdrawn.delete()
domain_request_approved.delete()
domain_request_rejected.delete()
domain_request_ineligible.delete()
domain_deleted.delete()
class AuditedAdminTest(TestCase):
def setUp(self):

View file

@ -29,10 +29,15 @@ from .common import MockDb, MockEppLib, less_console_noise, get_time_aware_date
class CsvReportsTest(MockDb):
"""Tests to determine if we are uploading our reports correctly"""
"""Tests to determine if we are uploading our reports correctly.
These tests use MockDb, which runs setUpClass and tearDownClass to handle
creation of fake domain data.
setUp and tearDown in this class set up client and factory.
This is for efficiency purposes when running tests, but
also means that care must be taken to clean up within each test.
"""
def setUp(self):
"""Create fake domain data"""
super().setUp()
self.client = Client(HTTP_HOST="localhost:8080")
self.factory = RequestFactory()
@ -196,11 +201,12 @@ class CsvReportsTest(MockDb):
class ExportDataTest(MockDb, MockEppLib):
def setUp(self):
super().setUp()
def tearDown(self):
super().tearDown()
"""Test the ExportData class from csv_export.
These tests use MockDb, which runs setUpClass and tearDownClass.
setUp and tearDown in this test run from MockEppLib to set up EPP mocks.
This is for efficiency purposes when running tests, but
also means that care must be taken to clean up within each test.
"""
def test_export_domains_to_writer_security_emails_and_first_ready(self):
"""Test that export_domains_to_writer returns the