Unit tests

This commit is contained in:
zandercymatics 2024-04-05 12:04:42 -06:00
parent 17c231a22a
commit 7edc6e75f4
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 77 additions and 62 deletions

View file

@ -90,14 +90,20 @@ class Command(BaseCommand):
self.update_domain_informations(domain_infos, debug) self.update_domain_informations(domain_infos, debug)
def update_domain_requests(self, domain_requests, debug): def update_domain_requests(self, domain_requests, debug):
with transaction.atomic():
for request in domain_requests: for request in domain_requests:
try: try:
if request.generic_org_type is not None: if request.generic_org_type is not None:
domain_name = request.requested_domain.name domain_name = request.requested_domain.name
request.is_election_board = domain_name in self.domains_with_election_offices_set request.is_election_board = domain_name in self.domains_with_election_offices_set
request = create_or_update_organization_type(DomainRequest, request, return_instance=True) new_request = create_or_update_organization_type(DomainRequest, request, return_instance=True)
print(f"what is the new request? {new_request}")
if not new_request:
self.request_skipped.append(request)
logger.warning(f"Skipped updating {request}. No changes to be made.")
else:
request = new_request
self.request_to_update.append(request) self.request_to_update.append(request)
if debug: if debug:
logger.info(f"Updating {request} => {request.organization_type}") logger.info(f"Updating {request} => {request.organization_type}")
else: else:
@ -121,7 +127,6 @@ class Command(BaseCommand):
) )
def update_domain_informations(self, domain_informations, debug): def update_domain_informations(self, domain_informations, debug):
with transaction.atomic():
for info in domain_informations: for info in domain_informations:
try: try:
if info.generic_org_type is not None: if info.generic_org_type is not None:

View file

@ -37,7 +37,6 @@ def create_or_update_organization_type(sender: DomainRequest | DomainInformation
# A new record is added with organization_type not defined. # A new record is added with organization_type not defined.
# This happens from the regular domain request flow. # This happens from the regular domain request flow.
is_new_instance = instance.id is None is_new_instance = instance.id is None
if is_new_instance: if is_new_instance:
# == Check for invalid conditions before proceeding == # # == Check for invalid conditions before proceeding == #
@ -54,7 +53,7 @@ def create_or_update_organization_type(sender: DomainRequest | DomainInformation
# related field (generic org type <-> org type) has data and we should update according to that. # related field (generic org type <-> org type) has data and we should update according to that.
if organization_type_needs_update: if organization_type_needs_update:
_update_org_type_from_generic_org_and_election(instance, generic_org_to_org_map) _update_org_type_from_generic_org_and_election(instance, generic_org_to_org_map)
elif generic_org_type_needs_update: elif generic_org_type_needs_update and instance.organization_type is not None:
_update_generic_org_and_election_from_org_type( _update_generic_org_and_election_from_org_type(
instance, election_org_to_generic_org_map, generic_org_to_org_map instance, election_org_to_generic_org_map, generic_org_to_org_map
) )
@ -63,12 +62,12 @@ def create_or_update_organization_type(sender: DomainRequest | DomainInformation
# == Init variables == # # == Init variables == #
# Instance is already in the database, fetch its current state # Instance is already in the database, fetch its current state
current_instance = sender.objects.get(id=instance.id) current_instance = sender.objects.get(id=instance.id)
print(f"what is the current instance? {current_instance.__dict__}")
# Check the new and old values # Check the new and old values
generic_org_type_changed = instance.generic_org_type != current_instance.generic_org_type generic_org_type_changed = instance.generic_org_type != current_instance.generic_org_type
is_election_board_changed = instance.is_election_board != current_instance.is_election_board is_election_board_changed = instance.is_election_board != current_instance.is_election_board
organization_type_changed = instance.organization_type != current_instance.organization_type organization_type_changed = instance.organization_type != current_instance.organization_type
print(f"whats changing? generic {generic_org_type_changed} vs election {is_election_board_changed} vs org {organization_type_changed}")
# == Check for invalid conditions before proceeding == # # == Check for invalid conditions before proceeding == #
if organization_type_changed and (generic_org_type_changed or is_election_board_changed): if organization_type_changed and (generic_org_type_changed or is_election_board_changed):
# Since organization type is linked with generic_org_type and election board, # Since organization type is linked with generic_org_type and election board,
@ -88,7 +87,7 @@ def create_or_update_organization_type(sender: DomainRequest | DomainInformation
# Update the field # Update the field
if organization_type_needs_update: if organization_type_needs_update:
_update_org_type_from_generic_org_and_election(instance, generic_org_to_org_map) _update_org_type_from_generic_org_and_election(instance, generic_org_to_org_map)
elif generic_org_type_needs_update: elif generic_org_type_needs_update and instance.organization_type is not None:
_update_generic_org_and_election_from_org_type( _update_generic_org_and_election_from_org_type(
instance, election_org_to_generic_org_map, generic_org_to_org_map instance, election_org_to_generic_org_map, generic_org_to_org_map
) )
@ -114,7 +113,7 @@ def _update_org_type_from_generic_org_and_election(instance, org_map):
logger.warning("create_or_update_organization_type() -> is_election_board is out of sync. Updating value.") logger.warning("create_or_update_organization_type() -> is_election_board is out of sync. Updating value.")
instance.is_election_board = False instance.is_election_board = False
instance.organization_type = org_map[generic_org_type] if instance.is_election_board else generic_org_type instance.organization_type = org_map.get(generic_org_type) if instance.is_election_board else generic_org_type
def _update_generic_org_and_election_from_org_type(instance, election_org_map, generic_org_map): def _update_generic_org_and_election_from_org_type(instance, election_org_map, generic_org_map):

View file

@ -37,19 +37,23 @@ class TestPopulateOrganizationType(MockEppLib):
name="lasers.gov", name="lasers.gov",
generic_org_type=DomainRequest.OrganizationChoices.FEDERAL, generic_org_type=DomainRequest.OrganizationChoices.FEDERAL,
is_election_board=True, is_election_board=True,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
) )
self.domain_request_2 = completed_domain_request( self.domain_request_2 = completed_domain_request(
name="readysetgo.gov", name="readysetgo.gov",
generic_org_type=DomainRequest.OrganizationChoices.CITY, generic_org_type=DomainRequest.OrganizationChoices.CITY,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
) )
self.domain_request_3 = completed_domain_request( self.domain_request_3 = completed_domain_request(
name="manualtransmission.gov", name="manualtransmission.gov",
generic_org_type=DomainRequest.OrganizationChoices.TRIBAL, generic_org_type=DomainRequest.OrganizationChoices.TRIBAL,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
) )
self.domain_request_4 = completed_domain_request( self.domain_request_4 = completed_domain_request(
name="saladandfries.gov", name="saladandfries.gov",
generic_org_type=DomainRequest.OrganizationChoices.TRIBAL, generic_org_type=DomainRequest.OrganizationChoices.TRIBAL,
is_election_board=True, is_election_board=True,
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
) )
# Approve all three requests # Approve all three requests
@ -82,7 +86,7 @@ class TestPopulateOrganizationType(MockEppLib):
Contact.objects.all().delete() Contact.objects.all().delete()
Website.objects.all().delete() Website.objects.all().delete()
@less_console_noise_decorator #@less_console_noise_decorator
def run_populate_organization_type(self): def run_populate_organization_type(self):
""" """
This method executes the populate_organization_type command. This method executes the populate_organization_type command.
@ -109,11 +113,13 @@ class TestPopulateOrganizationType(MockEppLib):
""" """
# Test domain request # Test domain request
with self.subTest(field="DomainRequest"):
self.assertEqual(domain_request.generic_org_type, expected_values['generic_org_type']) self.assertEqual(domain_request.generic_org_type, expected_values['generic_org_type'])
self.assertEqual(domain_request.is_election_board, expected_values['is_election_board']) self.assertEqual(domain_request.is_election_board, expected_values['is_election_board'])
self.assertEqual(domain_request.organization_type, expected_values['organization_type']) self.assertEqual(domain_request.organization_type, expected_values['organization_type'])
# Test domain info # Test domain info
with self.subTest(field="DomainInformation"):
self.assertEqual(domain_info.generic_org_type, expected_values['generic_org_type']) self.assertEqual(domain_info.generic_org_type, expected_values['generic_org_type'])
self.assertEqual(domain_info.is_election_board, expected_values['is_election_board']) self.assertEqual(domain_info.is_election_board, expected_values['is_election_board'])
self.assertEqual(domain_info.organization_type, expected_values['organization_type']) self.assertEqual(domain_info.organization_type, expected_values['organization_type'])
@ -174,18 +180,19 @@ class TestPopulateOrganizationType(MockEppLib):
for the domain request and the domain info for the domain request and the domain info
""" """
# Set org type fields to none to mimic an environment without this data
tribal_request = self.domain_request_3 tribal_request = self.domain_request_3
tribal_request.organization_type = None
tribal_request.save()
tribal_info = self.domain_info_3 tribal_info = self.domain_info_3
tribal_info.organization_type = None
tribal_info.save()
# Make sure that all data is correct before proceeding. # Make sure that all data is correct before proceeding.
# Because the presave fixture is in place when creating this, we should expect that the
# organization_type variable is already pre-populated. We will test what happens when
# it is not in another test.
expected_values = { expected_values = {
'is_election_board': False, 'is_election_board': False,
'generic_org_type': DomainRequest.OrganizationChoices.TRIBAL, 'generic_org_type': DomainRequest.OrganizationChoices.TRIBAL,
'organization_type': DomainRequest.OrgChoicesElectionOffice.TRIBAL 'organization_type': None,
} }
self.assert_expected_org_values_on_request_and_info(tribal_request, tribal_info, expected_values) self.assert_expected_org_values_on_request_and_info(tribal_request, tribal_info, expected_values)
@ -195,6 +202,9 @@ class TestPopulateOrganizationType(MockEppLib):
except Exception as e: except Exception as e:
self.fail(f"Could not run populate_organization_type script. Failed with exception: {e}") self.fail(f"Could not run populate_organization_type script. Failed with exception: {e}")
tribal_request.refresh_from_db()
tribal_info.refresh_from_db()
# Because we define this in the "csv", we expect that is election board will switch to True, # Because we define this in the "csv", we expect that is election board will switch to True,
# and organization_type will now be tribal_election # and organization_type will now be tribal_election
expected_values["is_election_board"] = True expected_values["is_election_board"] = True
@ -209,8 +219,13 @@ class TestPopulateOrganizationType(MockEppLib):
for the domain request and the domain info for the domain request and the domain info
""" """
# Set org type fields to none to mimic an environment without this data
tribal_election_request = self.domain_request_4 tribal_election_request = self.domain_request_4
tribal_election_request.organization_type = None
tribal_election_request.save()
tribal_election_info = self.domain_info_4 tribal_election_info = self.domain_info_4
tribal_election_info.organization_type = None
tribal_election_info.save()
# Make sure that all data is correct before proceeding. # Make sure that all data is correct before proceeding.
# Because the presave fixture is in place when creating this, we should expect that the # Because the presave fixture is in place when creating this, we should expect that the
@ -219,7 +234,7 @@ class TestPopulateOrganizationType(MockEppLib):
expected_values = { expected_values = {
'is_election_board': True, 'is_election_board': True,
'generic_org_type': DomainRequest.OrganizationChoices.TRIBAL, 'generic_org_type': DomainRequest.OrganizationChoices.TRIBAL,
'organization_type': DomainRequest.OrgChoicesElectionOffice.TRIBAL_ELECTION 'organization_type': None
} }
self.assert_expected_org_values_on_request_and_info(tribal_election_request, tribal_election_info, expected_values) self.assert_expected_org_values_on_request_and_info(tribal_election_request, tribal_election_info, expected_values)
@ -233,14 +248,10 @@ class TestPopulateOrganizationType(MockEppLib):
# and organization_type will now be tribal # and organization_type will now be tribal
expected_values["is_election_board"] = False expected_values["is_election_board"] = False
expected_values["organization_type"] = DomainRequest.OrgChoicesElectionOffice.TRIBAL expected_values["organization_type"] = DomainRequest.OrgChoicesElectionOffice.TRIBAL
tribal_election_request.refresh_from_db()
tribal_election_info.refresh_from_db()
self.assert_expected_org_values_on_request_and_info(tribal_election_request, tribal_election_info, expected_values) self.assert_expected_org_values_on_request_and_info(tribal_election_request, tribal_election_info, expected_values)
@skip("TODO")
def test_transition_data(self):
"""Tests for how this script interacts with prexisting data (for instance, stable)"""
# Make instead of mocking we can literally just run the transition domain scripts?
pass
class TestPopulateFirstReady(TestCase): class TestPopulateFirstReady(TestCase):
"""Tests for the populate_first_ready script""" """Tests for the populate_first_ready script"""