mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-21 18:25:58 +02:00
Unit tests
This commit is contained in:
parent
17c231a22a
commit
7edc6e75f4
3 changed files with 77 additions and 62 deletions
|
@ -90,24 +90,30 @@ class Command(BaseCommand):
|
|||
self.update_domain_informations(domain_infos, debug)
|
||||
|
||||
def update_domain_requests(self, domain_requests, debug):
|
||||
with transaction.atomic():
|
||||
for request in domain_requests:
|
||||
try:
|
||||
if request.generic_org_type is not None:
|
||||
domain_name = request.requested_domain.name
|
||||
request.is_election_board = domain_name in self.domains_with_election_offices_set
|
||||
request = create_or_update_organization_type(DomainRequest, request, return_instance=True)
|
||||
self.request_to_update.append(request)
|
||||
if debug:
|
||||
logger.info(f"Updating {request} => {request.organization_type}")
|
||||
else:
|
||||
for request in domain_requests:
|
||||
try:
|
||||
if request.generic_org_type is not None:
|
||||
domain_name = request.requested_domain.name
|
||||
request.is_election_board = domain_name in self.domains_with_election_offices_set
|
||||
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)
|
||||
if debug:
|
||||
logger.warning(f"Skipped updating {request}. No generic_org_type was found.")
|
||||
except Exception as err:
|
||||
self.request_failed_to_update.append(request)
|
||||
logger.error(err)
|
||||
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {request}" f"{TerminalColors.ENDC}")
|
||||
logger.warning(f"Skipped updating {request}. No changes to be made.")
|
||||
else:
|
||||
request = new_request
|
||||
self.request_to_update.append(request)
|
||||
|
||||
if debug:
|
||||
logger.info(f"Updating {request} => {request.organization_type}")
|
||||
else:
|
||||
self.request_skipped.append(request)
|
||||
if debug:
|
||||
logger.warning(f"Skipped updating {request}. No generic_org_type was found.")
|
||||
except Exception as err:
|
||||
self.request_failed_to_update.append(request)
|
||||
logger.error(err)
|
||||
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {request}" f"{TerminalColors.ENDC}")
|
||||
|
||||
# Do a bulk update on the organization_type field
|
||||
ScriptDataHelper.bulk_update_fields(
|
||||
|
@ -121,24 +127,23 @@ class Command(BaseCommand):
|
|||
)
|
||||
|
||||
def update_domain_informations(self, domain_informations, debug):
|
||||
with transaction.atomic():
|
||||
for info in domain_informations:
|
||||
try:
|
||||
if info.generic_org_type is not None:
|
||||
domain_name = info.domain.name
|
||||
info.is_election_board = domain_name in self.domains_with_election_offices_set
|
||||
info = create_or_update_organization_type(DomainInformation, info, return_instance=True)
|
||||
self.di_to_update.append(info)
|
||||
if debug:
|
||||
logger.info(f"Updating {info} => {info.organization_type}")
|
||||
else:
|
||||
self.di_skipped.append(info)
|
||||
if debug:
|
||||
logger.warning(f"Skipped updating {info}. No generic_org_type was found.")
|
||||
except Exception as err:
|
||||
self.di_failed_to_update.append(info)
|
||||
logger.error(err)
|
||||
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {info}" f"{TerminalColors.ENDC}")
|
||||
for info in domain_informations:
|
||||
try:
|
||||
if info.generic_org_type is not None:
|
||||
domain_name = info.domain.name
|
||||
info.is_election_board = domain_name in self.domains_with_election_offices_set
|
||||
info = create_or_update_organization_type(DomainInformation, info, return_instance=True)
|
||||
self.di_to_update.append(info)
|
||||
if debug:
|
||||
logger.info(f"Updating {info} => {info.organization_type}")
|
||||
else:
|
||||
self.di_skipped.append(info)
|
||||
if debug:
|
||||
logger.warning(f"Skipped updating {info}. No generic_org_type was found.")
|
||||
except Exception as err:
|
||||
self.di_failed_to_update.append(info)
|
||||
logger.error(err)
|
||||
logger.error(f"{TerminalColors.FAIL}" f"Failed to update {info}" f"{TerminalColors.ENDC}")
|
||||
|
||||
# Do a bulk update on the organization_type field
|
||||
ScriptDataHelper.bulk_update_fields(
|
||||
|
|
|
@ -37,7 +37,6 @@ def create_or_update_organization_type(sender: DomainRequest | DomainInformation
|
|||
# A new record is added with organization_type not defined.
|
||||
# This happens from the regular domain request flow.
|
||||
is_new_instance = instance.id is None
|
||||
|
||||
if is_new_instance:
|
||||
|
||||
# == 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.
|
||||
if organization_type_needs_update:
|
||||
_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(
|
||||
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 == #
|
||||
# Instance is already in the database, fetch its current state
|
||||
current_instance = sender.objects.get(id=instance.id)
|
||||
|
||||
print(f"what is the current instance? {current_instance.__dict__}")
|
||||
# Check the new and old values
|
||||
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
|
||||
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 == #
|
||||
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,
|
||||
|
@ -88,7 +87,7 @@ def create_or_update_organization_type(sender: DomainRequest | DomainInformation
|
|||
# Update the field
|
||||
if organization_type_needs_update:
|
||||
_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(
|
||||
instance, election_org_to_generic_org_map, generic_org_to_org_map
|
||||
)
|
||||
|
@ -114,13 +113,13 @@ 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.")
|
||||
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):
|
||||
"""Given the field value for organization_type, update the
|
||||
generic_org_type and is_election_board field."""
|
||||
|
||||
|
||||
# We convert to a string because the enum types are different
|
||||
# between OrgChoicesElectionOffice and OrganizationChoices.
|
||||
# But their names are the same (for the most part).
|
||||
|
|
|
@ -37,19 +37,23 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
name="lasers.gov",
|
||||
generic_org_type=DomainRequest.OrganizationChoices.FEDERAL,
|
||||
is_election_board=True,
|
||||
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
|
||||
)
|
||||
self.domain_request_2 = completed_domain_request(
|
||||
name="readysetgo.gov",
|
||||
generic_org_type=DomainRequest.OrganizationChoices.CITY,
|
||||
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
|
||||
)
|
||||
self.domain_request_3 = completed_domain_request(
|
||||
name="manualtransmission.gov",
|
||||
generic_org_type=DomainRequest.OrganizationChoices.TRIBAL,
|
||||
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
|
||||
)
|
||||
self.domain_request_4 = completed_domain_request(
|
||||
name="saladandfries.gov",
|
||||
generic_org_type=DomainRequest.OrganizationChoices.TRIBAL,
|
||||
is_election_board=True,
|
||||
status=DomainRequest.DomainRequestStatus.IN_REVIEW,
|
||||
)
|
||||
|
||||
# Approve all three requests
|
||||
|
@ -82,7 +86,7 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
Contact.objects.all().delete()
|
||||
Website.objects.all().delete()
|
||||
|
||||
@less_console_noise_decorator
|
||||
#@less_console_noise_decorator
|
||||
def run_populate_organization_type(self):
|
||||
"""
|
||||
This method executes the populate_organization_type command.
|
||||
|
@ -109,14 +113,16 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
"""
|
||||
|
||||
# Test domain request
|
||||
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.organization_type, expected_values['organization_type'])
|
||||
with self.subTest(field="DomainRequest"):
|
||||
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.organization_type, expected_values['organization_type'])
|
||||
|
||||
# Test domain info
|
||||
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.organization_type, expected_values['organization_type'])
|
||||
with self.subTest(field="DomainInformation"):
|
||||
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.organization_type, expected_values['organization_type'])
|
||||
|
||||
def test_request_and_info_city_not_in_csv(self):
|
||||
"""Tests what happens to a city domain that is not defined in the CSV"""
|
||||
|
@ -174,18 +180,19 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
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.organization_type = None
|
||||
tribal_request.save()
|
||||
tribal_info = self.domain_info_3
|
||||
tribal_info.organization_type = None
|
||||
tribal_info.save()
|
||||
|
||||
# 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 = {
|
||||
'is_election_board': False,
|
||||
'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)
|
||||
|
||||
|
@ -194,7 +201,10 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
self.run_populate_organization_type()
|
||||
except Exception as 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,
|
||||
# and organization_type will now be tribal_election
|
||||
expected_values["is_election_board"] = True
|
||||
|
@ -209,8 +219,13 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
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.organization_type = None
|
||||
tribal_election_request.save()
|
||||
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.
|
||||
# Because the presave fixture is in place when creating this, we should expect that the
|
||||
|
@ -219,7 +234,7 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
expected_values = {
|
||||
'is_election_board': True,
|
||||
'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)
|
||||
|
||||
|
@ -233,14 +248,10 @@ class TestPopulateOrganizationType(MockEppLib):
|
|||
# and organization_type will now be tribal
|
||||
expected_values["is_election_board"] = False
|
||||
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)
|
||||
|
||||
@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):
|
||||
"""Tests for the populate_first_ready script"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue