Skip to content

Commit

Permalink
Merge pull request #3353 from GSA-TTS/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jadudm authored Feb 6, 2024
2 parents e7b96e5 + fcedda2 commit d13faad
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 162 deletions.
Binary file not shown.
11 changes: 7 additions & 4 deletions backend/audit/intake_to_dissemination.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, sac, mode=DISSEMINATION) -> None:
self.audit_year = int(audit_date.split("-")[0])
self.loaded_objects: dict[str, list] = {}
self.mode = mode
self.errors: list[str] = []

def load_all(self):
load_methods = {
Expand All @@ -54,9 +55,11 @@ def load_all(self):
# Each method writes results into self.loaded_objects
load_method()
except KeyError as key_error:
logger.warning(
error = (
f"{type(key_error).__name__} in {load_method.__name__}: {key_error}"
)
logger.warning(error)
self.errors.append(error)
return self.loaded_objects

def get_dissemination_objects(self):
Expand All @@ -69,9 +72,9 @@ def save_dissemination_objects(self):
model_class = type(object_list[0])
model_class.objects.bulk_create(object_list)
except IntegrityError as e:
logging.warning(
f"An error occurred during bulk creation for {key}: {e}"
)
error = f"An error occurred during bulk creation for {key}: {e}"
logger.warning(error)
self.errors.append(error)

def load_finding_texts(self):
findings_text = self.single_audit_checklist.findings_text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def loan_balance_present(ir):
for index, (guarantee, balance) in enumerate(
zip(is_guaranteed, loan_balance_at_period_end)
):
if (guarantee == "N") and balance:
if (guarantee == "N") and balance is not None:
errors.append(
build_cell_error_tuple(
ir,
Expand Down
29 changes: 20 additions & 9 deletions backend/audit/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging

from django.db import models
from django.db.transaction import TransactionManagementError
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.postgres.fields import ArrayField
Expand Down Expand Up @@ -198,15 +199,25 @@ def disseminate(self):
Cognizant/Oversight agency assignment followed by dissemination
ETL.
"""
# try:
if not self.cognizant_agency and not self.oversight_agency:
self.assign_cog_over()
intake_to_dissem = IntakeToDissemination(self)
intake_to_dissem.load_all()
intake_to_dissem.save_dissemination_objects()
# TODO: figure out what exceptions to catch here
# except Exception as err:
# return {"error": err}
try:
if not self.cognizant_agency and not self.oversight_agency:
self.assign_cog_over()
intake_to_dissem = IntakeToDissemination(self)
intake_to_dissem.load_all()
intake_to_dissem.save_dissemination_objects()
if intake_to_dissem.errors:
return {"errors": intake_to_dissem.errors}
except TransactionManagementError as err:
# We want to re-raise this to catch at the view level because we
# think it's due to a race condition where the user's submission
# has been disseminated successfully; see
# https://github.com/GSA-TTS/FAC/issues/3347
raise err
# TODO: figure out what narrower exceptions to catch here
except Exception as err:
return {"errors": [err]}

return None

def assign_cog_over(self):
"""
Expand Down
11 changes: 11 additions & 0 deletions backend/audit/test_intake_to_dissemination.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime, time, timezone, timedelta
import json
import logging
from unittest import mock
from django.test import TestCase
from model_bakery import baker
from faker import Faker
Expand Down Expand Up @@ -429,6 +431,15 @@ def test_load_general(self):
general.gaap_results,
)

def test_load_general_race_condition_logging(self):
self.intake_to_dissemination.load_general()
logger = logging.getLogger(self.intake_to_dissemination.__module__)
self.intake_to_dissemination.save_dissemination_objects()
with mock.patch.object(logger, "warning") as mock_warning:
self.intake_to_dissemination.save_dissemination_objects()
mock_warning.assert_called()
self.assertEquals(1, len(self.intake_to_dissemination.errors))

def test_submitted_date(self):
"""
The date of submission should be disseminated using the time in American Samoa.
Expand Down
Loading

0 comments on commit d13faad

Please sign in to comment.