Test cases

This commit is contained in:
zandercymatics 2024-04-01 12:41:59 -06:00
parent 58b8e4649d
commit 198977bb6e
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 213 additions and 32 deletions

View file

@ -48,14 +48,24 @@ def create_or_update_organization_type(sender, instance, **kwargs):
# we have to update one or the other, not both. # we have to update one or the other, not both.
if instance.organization_type and instance.generic_org_type: if instance.organization_type and instance.generic_org_type:
organization_type = str(instance.organization_type) organization_type = str(instance.organization_type)
# Strip "_election" if it exists
mapped_org_type = election_org_to_generic_org_map.get(organization_type)
generic_org_type = str(instance.generic_org_type) generic_org_type = str(instance.generic_org_type)
should_proceed = True
# We can only proceed if all values match (fixtures, load_from_da). # We can only proceed if all values match (fixtures, load_from_da).
# Otherwise, we're overwriting data so lets forbid this. # Otherwise, we're overwriting data so lets forbid this.
if ( is_election_type = "_election" in organization_type
"_election" in organization_type != instance.is_election_board or can_have_election_board = organization_type in generic_org_to_org_map
organization_type != generic_org_type if is_election_type != instance.is_election_board and can_have_election_board:
): # This means that there is a mismatch between the booleans
# (i.e. FEDERAL is not equal to is_election_board = True)
should_proceed = False
elif mapped_org_type is not None and generic_org_type != mapped_org_type:
# This means that there is as mismatch between the org types
should_proceed = False
if not should_proceed:
message = ( message = (
"Cannot add organization_type and generic_org_type simultaneously " "Cannot add organization_type and generic_org_type simultaneously "
"when generic_org_type, is_election_board, and organization_type values do not match." "when generic_org_type, is_election_board, and organization_type values do not match."
@ -73,9 +83,11 @@ def create_or_update_organization_type(sender, instance, **kwargs):
# If a field is none, it indicates (per prior checks) that the # If a field is none, it indicates (per prior checks) that the
# 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) _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:
_update_generic_org_and_election_from_org_type(instance) _update_generic_org_and_election_from_org_type(
instance, election_org_to_generic_org_map, generic_org_to_org_map
)
else: else:
# This indicates that all data already matches, # This indicates that all data already matches,
# so we should just do nothing because there is nothing to update. # so we should just do nothing because there is nothing to update.
@ -109,30 +121,41 @@ def create_or_update_organization_type(sender, instance, **kwargs):
# Update that field # Update that field
if organization_type_needs_update: if organization_type_needs_update:
_update_org_type_from_generic_org_and_election(instance) _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:
_update_generic_org_and_election_from_org_type(instance) _update_generic_org_and_election_from_org_type(
instance, election_org_to_generic_org_map, generic_org_to_org_map
)
def _update_org_type_from_generic_org_and_election(instance):
def _update_org_type_from_generic_org_and_election(instance, org_map):
"""Given a field values for generic_org_type and is_election_board, update the """Given a field values for generic_org_type and is_election_board, update the
organization_type field.""" organization_type field."""
# We convert to a string because the enum types are different. # We convert to a string because the enum types are different.
generic_org_type = str(instance.generic_org_type) generic_org_type = str(instance.generic_org_type)
# For any given organization type, return the "_election" variant. # If the election board is none, then it tells us that it is an invalid field.
# For example: STATE_OR_TERRITORY => STATE_OR_TERRITORY_ELECTION # Such as federal, interstate, or school_district.
election_org_choices = DomainRequest.OrgChoicesElectionOffice if instance.is_election_board is None and generic_org_type not in org_map:
org_map = election_org_choices.get_org_generic_to_org_election()
# This essentially means: instance.generic_org_type not in invalid_types
if generic_org_type in org_map and instance.is_election_board:
instance.organization_type = org_map[generic_org_type]
else:
instance.organization_type = generic_org_type instance.organization_type = generic_org_type
return instance
elif instance.is_election_board is None and generic_org_type in org_map:
# This can only happen with manual data tinkering, which causes these to be out of sync.
instance.is_election_board = False
logger.warning("create_or_update_organization_type() -> is_election_board is out of sync. Updating value.")
if generic_org_type in org_map:
# Swap to the election type if it is an election board. Otherwise, stick to the normal one.
instance.organization_type = org_map[generic_org_type] if instance.is_election_board else generic_org_type
elif generic_org_type not in org_map:
# Election board should be reset to None if the record
# can't have one. For example, federal.
instance.organization_type = generic_org_type
instance.is_election_board = None
def _update_generic_org_and_election_from_org_type(instance): 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 """Given the field value for organization_type, update the
generic_org_type and is_election_board field.""" generic_org_type and is_election_board field."""
@ -141,19 +164,22 @@ def _update_generic_org_and_election_from_org_type(instance):
# But their names are the same (for the most part). # But their names are the same (for the most part).
current_org_type = str(instance.organization_type) current_org_type = str(instance.organization_type)
# For any given organization type, return the generic variant. # This essentially means: "_election" in current_org_type.
# For example: STATE_OR_TERRITORY_ELECTION => STATE_OR_TERRITORY if current_org_type in election_org_map:
election_org_choices = DomainRequest.OrgChoicesElectionOffice new_org = election_org_map[current_org_type]
org_map = election_org_choices.get_org_election_to_org_generic()
# This essentially means: "_election" in current_org_type
if current_org_type in org_map:
new_org = org_map[current_org_type]
instance.generic_org_type = new_org instance.generic_org_type = new_org
instance.is_election_board = True instance.is_election_board = True
else: else:
instance.generic_org_type = current_org_type instance.generic_org_type = current_org_type
# This basically checks if the given org type
# can even have an election board in the first place.
# For instance, federal cannot so is_election_board = None
if current_org_type in generic_org_map:
instance.is_election_board = False instance.is_election_board = False
else:
instance.is_election_board = None
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def handle_profile(sender, instance, **kwargs): def handle_profile(sender, instance, **kwargs):

View file

@ -782,6 +782,9 @@ def completed_domain_request(
submitter=False, submitter=False,
name="city.gov", name="city.gov",
investigator=None, investigator=None,
generic_org_type="federal",
is_election_board=False,
organization_type=None,
): ):
"""A completed domain request.""" """A completed domain request."""
if not user: if not user:
@ -819,7 +822,8 @@ def completed_domain_request(
is_staff=True, is_staff=True,
) )
domain_request_kwargs = dict( domain_request_kwargs = dict(
generic_org_type="federal", generic_org_type=generic_org_type,
is_election_board=is_election_board,
federal_type="executive", federal_type="executive",
purpose="Purpose of the site", purpose="Purpose of the site",
is_policy_acknowledged=True, is_policy_acknowledged=True,
@ -840,6 +844,9 @@ def completed_domain_request(
if has_anything_else: if has_anything_else:
domain_request_kwargs["anything_else"] = "There is more" domain_request_kwargs["anything_else"] = "There is more"
if organization_type:
domain_request_kwargs["organization_type"] = organization_type
domain_request, _ = DomainRequest.objects.get_or_create(**domain_request_kwargs) domain_request, _ = DomainRequest.objects.get_or_create(**domain_request_kwargs)
if has_other_contacts: if has_other_contacts:

View file

@ -2,6 +2,8 @@ from django.test import TestCase
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from registrar.models import Contact from registrar.models import Contact
from registrar.models.domain_request import DomainRequest
from registrar.tests.common import completed_domain_request
class TestUserPostSave(TestCase): class TestUserPostSave(TestCase):
@ -99,3 +101,149 @@ class TestUserPostSave(TestCase):
self.assertEqual(actual.last_name, self.last_name) self.assertEqual(actual.last_name, self.last_name)
self.assertEqual(actual.email, self.email) self.assertEqual(actual.email, self.email)
self.assertEqual(actual.phone, self.phone) self.assertEqual(actual.phone, self.phone)
class TestDomainRequestSignals(TestCase):
"""Tests hooked signals on the DomainRequest object"""
def tearDown(self):
DomainRequest.objects.all().delete()
super().tearDown()
def test_create_or_update_organization_type_new_instance(self):
"""Test create_or_update_organization_type when creating a new instance"""
domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED,
name="started.gov",
generic_org_type=DomainRequest.OrganizationChoices.CITY,
is_election_board=True,
)
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY_ELECTION)
def test_create_or_update_organization_type_new_instance_federal_does_nothing(self):
"""Test if create_or_update_organization_type does nothing when creating a new instance for federal"""
domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED,
name="started.gov",
generic_org_type=DomainRequest.OrganizationChoices.FEDERAL,
is_election_board=True,
)
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.FEDERAL)
def test_create_or_update_organization_type_existing_instance_updates_election_board(self):
"""Test create_or_update_organization_type for an existing instance."""
domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED,
name="started.gov",
generic_org_type=DomainRequest.OrganizationChoices.CITY,
is_election_board=False,
)
domain_request.is_election_board = True
domain_request.save()
self.assertEqual(domain_request.is_election_board, True)
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY_ELECTION)
# Try reverting the election board value
domain_request.is_election_board = False
domain_request.save()
self.assertEqual(domain_request.is_election_board, False)
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY)
# Try reverting setting an invalid value for election board (should revert to False)
domain_request.is_election_board = None
domain_request.save()
self.assertEqual(domain_request.is_election_board, False)
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY)
def test_create_or_update_organization_type_existing_instance_updates_generic_org_type(self):
"""Test create_or_update_organization_type when modifying generic_org_type on an existing instance."""
domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED,
name="started.gov",
generic_org_type=DomainRequest.OrganizationChoices.CITY,
is_election_board=True,
)
domain_request.generic_org_type = DomainRequest.OrganizationChoices.INTERSTATE
domain_request.save()
# Election board should be None because interstate cannot have an election board.
self.assertEqual(domain_request.is_election_board, None)
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.INTERSTATE)
# Try changing the org Type to something that CAN have an election board.
domain_request_tribal = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED,
name="startedTribal.gov",
generic_org_type=DomainRequest.OrganizationChoices.TRIBAL,
is_election_board=True,
)
self.assertEqual(
domain_request_tribal.organization_type, DomainRequest.OrgChoicesElectionOffice.TRIBAL_ELECTION
)
# Change the org type
domain_request_tribal.generic_org_type = DomainRequest.OrganizationChoices.STATE_OR_TERRITORY
domain_request_tribal.save()
self.assertEqual(domain_request_tribal.is_election_board, True)
self.assertEqual(
domain_request_tribal.organization_type, DomainRequest.OrgChoicesElectionOffice.STATE_OR_TERRITORY_ELECTION
)
def test_create_or_update_organization_type_no_update(self):
"""Test create_or_update_organization_type when there are no values to update."""
# Test for when both generic_org_type and organization_type is declared,
# and are both non-election board
domain_request = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED,
name="started.gov",
generic_org_type=DomainRequest.OrganizationChoices.CITY,
is_election_board=False,
)
domain_request.save()
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY)
self.assertEqual(domain_request.is_election_board, False)
self.assertEqual(domain_request.generic_org_type, DomainRequest.OrganizationChoices.CITY)
# Test for when both generic_org_type and organization_type is declared,
# and are both election board
domain_request_election = completed_domain_request(
status=DomainRequest.DomainRequestStatus.STARTED,
name="startedElection.gov",
generic_org_type=DomainRequest.OrganizationChoices.CITY,
is_election_board=True,
organization_type=DomainRequest.OrgChoicesElectionOffice.CITY_ELECTION,
)
self.assertEqual(
domain_request_election.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY_ELECTION
)
self.assertEqual(domain_request_election.is_election_board, True)
self.assertEqual(domain_request_election.generic_org_type, DomainRequest.OrganizationChoices.CITY)
# Modify an unrelated existing value for both, and ensure that everything is still consistent
domain_request.city = "Fudge"
domain_request_election.city = "Caramel"
domain_request.save()
domain_request_election.save()
self.assertEqual(domain_request.city, "Fudge")
self.assertEqual(domain_request_election.city, "Caramel")
# Test for non-election
self.assertEqual(domain_request.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY)
self.assertEqual(domain_request.is_election_board, False)
self.assertEqual(domain_request.generic_org_type, DomainRequest.OrganizationChoices.CITY)
# Test for election
self.assertEqual(
domain_request_election.organization_type, DomainRequest.OrgChoicesElectionOffice.CITY_ELECTION
)
self.assertEqual(domain_request_election.is_election_board, True)
self.assertEqual(domain_request_election.generic_org_type, DomainRequest.OrganizationChoices.CITY)