Add unit test and move things around slightly

This commit is contained in:
zandercymatics 2024-05-08 14:36:08 -06:00
parent 6a3eb005bc
commit f7208a80ea
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -25,6 +25,56 @@ from registrar.utility.s3_bucket import S3ClientError, S3ClientErrorCodes # typ
from datetime import datetime
from django.utils import timezone
from .common import MockDb, MockEppLib, less_console_noise
from api.tests.common import less_console_noise_decorator
class HelperFunctions(MockDb):
"""This asserts that 1=1. Its limited usefulness lies in making sure the helper methods stay healthy."""
def get_time_aware_date(self, date=datetime(2023, 11, 1)):
"""Returns a time aware date"""
return timezone.make_aware(date)
def test_get_default_start_date(self):
expected_date = self.get_time_aware_date()
actual_date = get_default_start_date()
self.assertEqual(actual_date, expected_date)
def test_get_default_end_date(self):
# Note: You may need to mock timezone.now() for accurate testing
expected_date = timezone.now()
actual_date = get_default_end_date()
self.assertEqual(actual_date.date(), expected_date.date())
def test_get_sliced_domains(self):
"""Should get fitered domains counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"domain__permissions__isnull": False,
"domain__first_ready__lte": self.end_date,
}
# Test with distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
# Test without distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
def test_get_sliced_requests(self):
"""Should get fitered requests counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"status": DomainRequest.DomainRequestStatus.SUBMITTED,
"submission_date__lte": self.end_date,
}
submitted_requests_sliced_at_end_date = get_sliced_requests(filter_condition)
expected_content = [2, 2, 0, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(submitted_requests_sliced_at_end_date, expected_content)
class CsvReportsTest(MockDb):
@ -194,18 +244,17 @@ class CsvReportsTest(MockDb):
self.assertEqual(expected_file_content, response.content)
class ExportDataTest(MockDb, MockEppLib):
class ExportDataTest(HelperFunctions, MockEppLib):
def setUp(self):
super().setUp()
def tearDown(self):
super().tearDown()
def test_export_domains_to_writer_security_emails(self):
@less_console_noise_decorator
def test_export_domains_to_writer_security_emails_and_first_ready(self):
"""Test that export_domains_to_writer returns the
expected security email"""
with less_console_noise():
expected security email and first_ready value"""
# Add security email information
self.domain_1.name = "defaultsecurity.gov"
self.domain_1.save()
@ -215,6 +264,11 @@ class ExportDataTest(MockDb, MockEppLib):
self.domain_2.security_contact
# Invoke setter
self.domain_3.security_contact
# Add a first ready date on the first domain. Leaving the others blank.
self.domain_1.first_ready = get_default_start_date()
self.domain_1.save()
# Create a CSV file in memory
csv_file = StringIO()
writer = csv.writer(csv_file)
@ -231,6 +285,7 @@ class ExportDataTest(MockDb, MockEppLib):
"Security contact email",
"Status",
"Expiration date",
"First ready on",
]
sort_fields = ["domain__name"]
filter_condition = {
@ -259,13 +314,13 @@ class ExportDataTest(MockDb, MockEppLib):
# sorted alphabetially by domain name
expected_content = (
"Domain name,Domain type,Agency,Organization name,City,State,AO,"
"AO email,Security contact email,Status,Expiration date\n"
"adomain10.gov,Federal,Armed Forces Retirement Home,Ready\n"
"adomain2.gov,Interstate,(blank),Dns needed\n"
"cdomain11.govFederal-ExecutiveWorldWarICentennialCommissionReady\n"
"ddomain3.gov,Federal,Armed Forces Retirement Home,security@mail.gov,On hold,2023-11-15\n"
"defaultsecurity.gov,Federal - Executive,World War I Centennial Commission,(blank),Ready\n"
"zdomain12.govInterstateReady\n"
"AO email,Security contact email,Status,Expiration date, First ready on\n"
"adomain10.gov,Federal,Armed Forces Retirement Home,Ready,2024-05-09\n"
"adomain2.gov,Interstate,(blank),Dns needed,(blank)\n"
"cdomain11.govFederal-ExecutiveWorldWarICentennialCommissionReady,2024-05-08\n"
"ddomain3.gov,Federal,Armed Forces Retirement Home,security@mail.gov,On hold,2023-11-15,(blank)\n"
"defaultsecurity.gov,Federal - Executive,World War I Centennial Commission,(blank),Ready,2023-11-01\n"
"zdomain12.govInterstateReady,2024-05-08\n"
)
# Normalize line endings and remove commas,
# spaces and leading/trailing whitespace
@ -692,48 +747,3 @@ class ExportDataTest(MockDb, MockEppLib):
expected_content = expected_content.replace(",,", "").replace(",", "").replace(" ", "").strip()
self.assertEqual(csv_content, expected_content)
class HelperFunctions(MockDb):
"""This asserts that 1=1. Its limited usefulness lies in making sure the helper methods stay healthy."""
def test_get_default_start_date(self):
expected_date = timezone.make_aware(datetime(2023, 11, 1))
actual_date = get_default_start_date()
self.assertEqual(actual_date, expected_date)
def test_get_default_end_date(self):
# Note: You may need to mock timezone.now() for accurate testing
expected_date = timezone.now()
actual_date = get_default_end_date()
self.assertEqual(actual_date.date(), expected_date.date())
def test_get_sliced_domains(self):
"""Should get fitered domains counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"domain__permissions__isnull": False,
"domain__first_ready__lte": self.end_date,
}
# Test with distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
# Test without distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
def test_get_sliced_requests(self):
"""Should get fitered requests counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"status": DomainRequest.DomainRequestStatus.SUBMITTED,
"submission_date__lte": self.end_date,
}
submitted_requests_sliced_at_end_date = get_sliced_requests(filter_condition)
expected_content = [2, 2, 0, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(submitted_requests_sliced_at_end_date, expected_content)