Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1791 cross validation award refref num set uniqueness #1815

Merged
merged 5 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/audit/cross_validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from .additional_ueis import additional_ueis
from .auditee_ueis_match import auditee_ueis_match
from .audit_findings import audit_findings
from .award_ref_and_references_uniqueness import award_ref_and_references_uniqueness
from .sac_validation_shape import sac_validation_shape # noqa: F401
from .submission_progress_check import submission_progress_check
from .tribal_data_sharing_consent import tribal_data_sharing_consent
Expand All @@ -67,6 +68,7 @@
audit_findings,
auditee_ueis_match,
additional_ueis,
award_ref_and_references_uniqueness,
number_of_findings,
submission_progress_check,
tribal_data_sharing_consent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from .errors import (
err_award_ref_repeat_reference,
)
from collections import defaultdict


def award_ref_and_references_uniqueness(sac_dict, *_args, **_kwargs):
"""
Ensure the REFERENCE numbers uniqueness for a given AWARD in findings.
"""

all_sections = sac_dict.get("sf_sac_sections", {})
findings_uniform_guidance_section = (
all_sections.get("findings_uniform_guidance") or {}
)
findings_uniform_guidance = findings_uniform_guidance_section.get(
"findings_uniform_guidance_entries", []
)

ref_numbers = defaultdict(set)
duplicate_ref_number = defaultdict(set)
errors = []

for finding in findings_uniform_guidance:
award_ref = finding["program"]["award_reference"]
ref_number = finding["findings"]["reference_number"]
if ref_number in ref_numbers[award_ref]:
duplicate_ref_number[award_ref].add(ref_number)
ref_numbers[award_ref].add(ref_number)

for award_ref, ref_nums in duplicate_ref_number.items():
for ref_num in ref_nums:
errors.append(
{
"error": err_award_ref_repeat_reference(
award_ref,
ref_num,
)
}
)

return errors
4 changes: 4 additions & 0 deletions backend/audit/cross_validation/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def err_missing_tribal_data_sharing_consent():
)


def err_award_ref_repeat_reference(award_ref, ref_number):
return f"Award {award_ref} repeats reference {ref_number}. The reference {ref_number} should only appear once for award {award_ref}."


def err_number_of_findings_inconsistent(total_expected, total_counted, award_ref):
return (
f"You reported {total_expected} findings for award {award_ref} in the {SECTION_NAMES.FEDERAL_AWARDS} workbook, "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import random
from django.test import TestCase
from audit.models import SingleAuditChecklist
from .award_ref_and_references_uniqueness import award_ref_and_references_uniqueness
from .sac_validation_shape import sac_validation_shape
from .errors import err_award_ref_repeat_reference
from model_bakery import baker


class AwardRefAndReferencesUniquenessTests(TestCase):
AWARD_MIN = 1000
AWARD_MAX = 2000
REF_MIN = 100
REF_MAX = 200

def _random(self, min, max):
return random.randint(min, max) # nosec

def _award_reference(self) -> str:
return f"AWARD-{self._random(self.AWARD_MIN, self.AWARD_MAX)}"

def _reference_number(self, ref_num=None) -> str:
return (
f"2023-{self._random(self.REF_MIN, self.REF_MAX)}"
if ref_num is None
else f"2023-{ref_num}"
)

def _make_findings_uniform_guidance(
self, award_references, reference_numbers
) -> dict:
entries = []
for award_ref, ref_nums in zip(award_references, reference_numbers):
for ref_num in ref_nums:
entries.append(
{
"program": {"award_reference": award_ref},
"findings": {"reference_number": ref_num},
}
)

return {
"FindingsUniformGuidance": {
"auditee_uei": "ABB123456CCC",
"findings_uniform_guidance_entries": entries,
}
}

def test_no_duplicate_references(self):
"""
Check that no error is returned when there are no duplicate reference numbers.
"""
range_size = self._random(2, 4)
sac = baker.make(SingleAuditChecklist)
sac.findings_uniform_guidance = self._make_findings_uniform_guidance(
[self._award_reference() for _ in range(range_size)],
[[self._reference_number(self.REF_MIN + i)] for i in range(range_size)],
)
errors = award_ref_and_references_uniqueness(sac_validation_shape(sac))
self.assertEqual(errors, [])

def test_duplicate_references_for_award(self):
"""
Check that errors are returned for awards with duplicate reference numbers.
"""
range_size = self._random(2, 4)
sac = baker.make(SingleAuditChecklist)
sac.findings_uniform_guidance = self._make_findings_uniform_guidance(
[self._award_reference() for _ in range(range_size)],
[
[
self._reference_number(self.REF_MIN),
self._reference_number(self.REF_MIN),
self._reference_number(self.REF_MAX),
]
],
)
errors = award_ref_and_references_uniqueness(sac_validation_shape(sac))
for finding in sac.findings_uniform_guidance["FindingsUniformGuidance"][
"findings_uniform_guidance_entries"
]:
expected_error = {
"error": err_award_ref_repeat_reference(
finding["program"]["award_reference"],
self._reference_number(self.REF_MIN),
)
}
self.assertIn(expected_error, errors)