Skip to content

Commit

Permalink
#4331 Updated logic to handle discrepancies in award reference lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
sambodeme committed Sep 27, 2024
1 parent 07c1062 commit 17f64e7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
65 changes: 64 additions & 1 deletion backend/audit/cross_validation/check_award_ref_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,82 @@ def check_award_ref_declaration(sac_dict, *_args, **_kwargs):
declared_award_refs = set()
reported_award_refs = set()
errors = []

declared_award_ref_max_length = 0
reported_award_ref_max_length = 0
for award in federal_awards:
award_ref = award.get("award_reference")
if award_ref:
declared_award_refs.add(award_ref)
if len(award_ref) > declared_award_ref_max_length:
declared_award_ref_max_length = len(award_ref)

for finding in findings_uniform_guidance:
award_ref = finding["program"]["award_reference"]
if award_ref:
reported_award_refs.add(award_ref)
if len(award_ref) > reported_award_ref_max_length:
reported_award_ref_max_length = len(award_ref)

updated_declared_refs, updated_reported_refs = _normalize_award_ref_lengths(
declared_award_ref_max_length,
reported_award_ref_max_length,
federal_awards,
findings_uniform_guidance,
)
if updated_declared_refs:
declared_award_refs = updated_declared_refs
if updated_reported_refs:
reported_award_refs = updated_reported_refs

difference = reported_award_refs.difference(declared_award_refs)
if difference:
errors.append({"error": err_award_ref_not_declared(list(difference))})

return errors


def _normalize_award_ref_lengths(
declared_award_ref_max_length,
reported_award_ref_max_length,
federal_awards,
findings_uniform_guidance,
):
"""
Normalize the lengths of the award references in the Federal Awards and
Federal Awards Audit Findings workbooks before validation.
"""
reported_award_refs = set()
declared_award_refs = set()
if declared_award_ref_max_length > reported_award_ref_max_length:
# This is unlikely to happen, but still a good check. It means
# that the version of the Federal Awards workbook is newer than
# the version of the Federal Awards Audit Findings workbook.
diff = declared_award_ref_max_length - reported_award_ref_max_length
padding = "0" * diff

for finding in findings_uniform_guidance:
award_ref = finding["program"]["award_reference"]
if award_ref:
award_ref = (
f"{award_ref.split('-')[0]}-{padding}{award_ref.split('-')[1]}"
)
reported_award_refs.add(award_ref)
elif declared_award_ref_max_length < reported_award_ref_max_length:
# This is more likely to happen. It means the version of
# the Federal Awards Audit Findings workbook is newer than
# the version of the Federal Awards workbook.
diff = reported_award_ref_max_length - declared_award_ref_max_length
padding = "0" * diff

for award in federal_awards:
award_ref = award.get("award_reference")
if award_ref:
award_ref = (
f"{award_ref.split('-')[0]}-{padding}{award_ref.split('-')[1]}"
)
declared_award_refs.add(award_ref)
else:
# If the lengths are the same, do nothing.
pass

return declared_award_refs, reported_award_refs
20 changes: 20 additions & 0 deletions backend/audit/cross_validation/test_check_award_ref_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def setUp(self):
self.award3 = {
"award_reference": f"AWARD-{generate_random_integer(self.AWARD_MIN *3,self.AWARD_MAX *3)}"
}
self.award_with_longer_ref = {"award_reference": "AWARD-00123"}
self.award_with_shorter_ref = {"award_reference": "AWARD-0123"}

def _make_federal_awards(self, award_refs) -> dict:
return {
Expand Down Expand Up @@ -82,3 +84,21 @@ def test_errors_for_findings_with_undeclared_award_refs(self):
self.assertEqual(len(errors), 1)
expected_error = err_award_ref_not_declared([self.award2["award_reference"]])
self.assertIn({"error": expected_error}, errors)

def test_padding_when_declared_award_ref_max_length_greater(self):
"""Test case where declared award reference length is greater than reported award reference length."""
sac = self._make_sac(
[self.award_with_longer_ref], [self.award_with_shorter_ref]
)
errors = check_award_ref_declaration(sac_validation_shape(sac))
# No errors expected
self.assertEqual(errors, [])

def test_padding_when_reported_award_ref_max_length_greater(self):
"""Test case where reported award reference length is greater than declared award reference length."""
sac = self._make_sac(
[self.award_with_shorter_ref], [self.award_with_longer_ref]
)
errors = check_award_ref_declaration(sac_validation_shape(sac))
# No errors expected
self.assertEqual(errors, [])

0 comments on commit 17f64e7

Please sign in to comment.