Finish unit tests

This commit is contained in:
zandercymatics 2025-01-08 10:38:50 -07:00
parent 6da50477b5
commit 5ca74fdc53
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 68 additions and 14 deletions

View file

@ -79,7 +79,7 @@ class Command(BaseCommand):
else: else:
raise CommandError(f"Cannot find '{branch}' federal agencies in our database.") raise CommandError(f"Cannot find '{branch}' federal agencies in our database.")
portfolio_set = set() portfolios = []
for federal_agency in agencies: for federal_agency in agencies:
message = f"Processing federal agency '{federal_agency.agency}'..." message = f"Processing federal agency '{federal_agency.agency}'..."
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message) TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
@ -87,7 +87,7 @@ class Command(BaseCommand):
# C901 'Command.handle' is too complex (12) # C901 'Command.handle' is too complex (12)
# We currently only grab the list of changed domain requests, but we may want to grab the domains too # We currently only grab the list of changed domain requests, but we may want to grab the domains too
portfolio = self.handle_populate_portfolio(federal_agency, parse_domains, parse_requests, both) portfolio = self.handle_populate_portfolio(federal_agency, parse_domains, parse_requests, both)
portfolio_set.add(portfolio) portfolios.append(portfolio)
except Exception as exec: except Exception as exec:
self.failed_portfolios.add(federal_agency) self.failed_portfolios.add(federal_agency)
logger.error(exec) logger.error(exec)
@ -106,9 +106,14 @@ class Command(BaseCommand):
# POST PROCESSING STEP: Remove the federal agency if it matches the portfolio name. # POST PROCESSING STEP: Remove the federal agency if it matches the portfolio name.
# We only do this for started domain requests. # We only do this for started domain requests.
if parse_requests: if parse_requests:
self.post_process_started_domain_requests(portfolio_set) TerminalHelper.prompt_for_execution(
system_exit_on_terminate=True,
prompt_message="This action will update domain requests even if they aren't on a portfolio.",
prompt_title="Do you want to clear federal agency on started domain requests?",
)
self.post_process_started_domain_requests(agencies, portfolios)
def post_process_started_domain_requests(self, portfolio_set): def post_process_started_domain_requests(self, agencies, portfolios):
""" """
Removes duplicate organization data by clearing federal_agency when it matches the portfolio name. Removes duplicate organization data by clearing federal_agency when it matches the portfolio name.
Only processes domain requests in STARTED status. Only processes domain requests in STARTED status.
@ -116,15 +121,24 @@ class Command(BaseCommand):
message = "Removing duplicate portfolio and federal_agency values from domain requests..." message = "Removing duplicate portfolio and federal_agency values from domain requests..."
TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message) TerminalHelper.colorful_logger(logger.info, TerminalColors.MAGENTA, message)
# For each request, clear the federal agency under these conditions:
# 1. A portfolio *already exists* with the same name as the federal agency.
# 2. Said portfolio (or portfolios) are only the ones specified at the start of the script.
# 3. The domain request is in status "started".
# Note: Both names are normalized so excess spaces are stripped and the string is lowercased.
domain_requests_to_update = DomainRequest.objects.filter( domain_requests_to_update = DomainRequest.objects.filter(
portfolio__in=portfolio_set, federal_agency__in=agencies,
status=DomainRequest.DomainRequestStatus.STARTED,
federal_agency__agency__isnull=False, federal_agency__agency__isnull=False,
portfolio__organization_name__isnull=False, status=DomainRequest.DomainRequestStatus.STARTED,
organization_name__isnull=False,
) )
portfolio_set = {normalize_string(portfolio.organization_name) for portfolio in portfolios if portfolio}
# Update the request, assuming the given agency name matches the portfolio name
updated_requests = [] updated_requests = []
for req in domain_requests_to_update: for req in domain_requests_to_update:
if normalize_string(req.federal_agency.agency) == normalize_string(req.portfolio.organization_name): agency_name = normalize_string(req.federal_agency.agency)
if agency_name in portfolio_set:
req.federal_agency = None req.federal_agency = None
updated_requests.append(req) updated_requests.append(req)
DomainRequest.objects.bulk_update(updated_requests, ["federal_agency"]) DomainRequest.objects.bulk_update(updated_requests, ["federal_agency"])
@ -140,8 +154,6 @@ class Command(BaseCommand):
def handle_populate_portfolio(self, federal_agency, parse_domains, parse_requests, both): def handle_populate_portfolio(self, federal_agency, parse_domains, parse_requests, both):
"""Attempts to create a portfolio. If successful, this function will """Attempts to create a portfolio. If successful, this function will
also create new suborganizations. also create new suborganizations.
Returns the processed portfolio
""" """
portfolio, created = self.create_portfolio(federal_agency) portfolio, created = self.create_portfolio(federal_agency)
if created: if created:

View file

@ -1424,7 +1424,6 @@ class TestCreateFederalPortfolio(TestCase):
# Create an agency wih no federal type (can only be created via specifiying it manually) # Create an agency wih no federal type (can only be created via specifiying it manually)
self.federal_agency = FederalAgency.objects.create(agency="Test Federal Agency") self.federal_agency = FederalAgency.objects.create(agency="Test Federal Agency")
self.federal_agency_2 = FederalAgency.objects.create(agency="Sugarcane")
# And create some with federal_type ones with creative names # And create some with federal_type ones with creative names
self.executive_agency_1 = FederalAgency.objects.create( self.executive_agency_1 = FederalAgency.objects.create(
@ -1518,8 +1517,13 @@ class TestCreateFederalPortfolio(TestCase):
call_command("create_federal_portfolio", **kwargs) call_command("create_federal_portfolio", **kwargs)
@less_console_noise_decorator @less_console_noise_decorator
def test_handle_portfolio_requests_sync_federal_agency(self): def test_post_process_started_domain_requests_existing_portfolio(self):
"""Test that federal agency is cleared when org name matches portfolio name""" """Ensures that federal agency is cleared when agency name matches portfolio name.
As the name implies, this implicitly tests the "post_process_started_domain_requests" function.
"""
federal_agency_2 = FederalAgency.objects.create(agency="Sugarcane", federal_type=BranchChoices.EXECUTIVE)
# Test records with portfolios and no org names
# Create a portfolio. This script skips over "started" # Create a portfolio. This script skips over "started"
portfolio = Portfolio.objects.create(organization_name="Sugarcane", creator=self.user) portfolio = Portfolio.objects.create(organization_name="Sugarcane", creator=self.user)
# Create a domain request with matching org name # Create a domain request with matching org name
@ -1527,7 +1531,7 @@ class TestCreateFederalPortfolio(TestCase):
name="matching.gov", name="matching.gov",
status=DomainRequest.DomainRequestStatus.STARTED, status=DomainRequest.DomainRequestStatus.STARTED,
generic_org_type=DomainRequest.OrganizationChoices.FEDERAL, generic_org_type=DomainRequest.OrganizationChoices.FEDERAL,
federal_agency=self.federal_agency_2, federal_agency=federal_agency_2,
user=self.user, user=self.user,
portfolio=portfolio, portfolio=portfolio,
) )
@ -1558,6 +1562,44 @@ class TestCreateFederalPortfolio(TestCase):
self.assertIsNotNone(matching_request_in_wrong_status.portfolio) self.assertIsNotNone(matching_request_in_wrong_status.portfolio)
self.assertEqual(matching_request_in_wrong_status.portfolio.organization_name, "Test Federal Agency") self.assertEqual(matching_request_in_wrong_status.portfolio.organization_name, "Test Federal Agency")
@less_console_noise_decorator
def test_post_process_started_domain_requests(self):
"""Tests that federal agency is cleared when agency name
matches an existing portfolio's name, even if the domain request isn't
directly on that portfolio."""
federal_agency_2 = FederalAgency.objects.create(agency="Sugarcane", federal_type=BranchChoices.EXECUTIVE)
# Create a request with matching federal_agency name but no direct portfolio association
matching_agency_request = completed_domain_request(
name="agency-match.gov",
status=DomainRequest.DomainRequestStatus.STARTED,
generic_org_type=DomainRequest.OrganizationChoices.FEDERAL,
federal_agency=federal_agency_2,
user=self.user,
)
# Create a control request that shouldn't match
non_matching_request = completed_domain_request(
name="no-match.gov",
status=DomainRequest.DomainRequestStatus.STARTED,
generic_org_type=DomainRequest.OrganizationChoices.FEDERAL,
federal_agency=self.federal_agency,
user=self.user,
)
# We expect the matching agency to have its fed agency cleared.
self.run_create_federal_portfolio(agency_name="Sugarcane", parse_requests=True)
matching_agency_request.refresh_from_db()
non_matching_request.refresh_from_db()
# Request with matching agency name should have federal_agency cleared
self.assertIsNone(matching_agency_request.federal_agency)
# Non-matching request should keep its federal_agency
self.assertIsNotNone(non_matching_request.federal_agency)
self.assertEqual(non_matching_request.federal_agency, self.federal_agency)
@less_console_noise_decorator @less_console_noise_decorator
def test_create_single_portfolio(self): def test_create_single_portfolio(self):
"""Test portfolio creation with suborg and senior official.""" """Test portfolio creation with suborg and senior official."""