-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1848 Added cross validation for award reference uniqueness
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
backend/audit/cross_validation/check_award_reference_uniqueness.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from .errors import ( | ||
err_duplicate_award_reference, | ||
) | ||
from collections import Counter | ||
|
||
|
||
def check_award_reference_uniqueness(sac_dict, *_args, **_kwargs): | ||
""" | ||
Check that all award references in the Federal Award workbook are distinct. | ||
""" | ||
|
||
all_sections = sac_dict.get("sf_sac_sections", {}) | ||
federal_awards_section = all_sections.get("federal_awards") or {} | ||
federal_awards = federal_awards_section.get("federal_awards", []) | ||
award_refs = [] | ||
errors = [] | ||
|
||
for award in federal_awards: | ||
award_reference = award.get("award_reference", None) | ||
if award_reference: | ||
award_refs.append(award_reference) | ||
|
||
duplicated_award_refs = _find_duplicates(award_refs) | ||
|
||
for dup_award_ref in duplicated_award_refs: | ||
errors.append({"error": err_duplicate_award_reference(dup_award_ref)}) | ||
|
||
return errors | ||
|
||
|
||
def _find_duplicates(lst): | ||
count = Counter(lst) | ||
return [item for item, count in count.items() if count > 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
backend/audit/cross_validation/test_check_award_reference_uniqueness.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.test import TestCase | ||
from audit.models import SingleAuditChecklist | ||
from .check_award_reference_uniqueness import check_award_reference_uniqueness | ||
from .sac_validation_shape import sac_validation_shape | ||
from .errors import err_duplicate_award_reference | ||
from model_bakery import baker | ||
|
||
|
||
class CheckAwardReferenceUniquenessTests(TestCase): | ||
def setUp(self): | ||
"""Set up the common variables for the test cases.""" | ||
self.award1 = {"award_reference": "AWARD-0001"} | ||
self.award2 = {"award_reference": "AWARD-2219"} | ||
self.award3 = {"award_reference": "AWARD-2910"} | ||
|
||
def _make_federal_awards(self, award_refs) -> dict: | ||
return { | ||
"FederalAwards": { | ||
"federal_awards": award_refs, | ||
} | ||
} | ||
|
||
def _make_sac(self, award_refs) -> SingleAuditChecklist: | ||
sac = baker.make(SingleAuditChecklist) | ||
sac.federal_awards = self._make_federal_awards(award_refs) | ||
return sac | ||
|
||
def test_unique_award_references_raise_no_errors(self): | ||
"""When all award references are unique, no errors should be raised.""" | ||
sac = self._make_sac([self.award1, self.award2, self.award3]) | ||
errors = check_award_reference_uniqueness(sac_validation_shape(sac)) | ||
self.assertEqual(errors, []) | ||
|
||
def test_duplicate_award_references_raise_errors(self): | ||
"""When there are duplicate award references, the appropriate errors should be raised.""" | ||
sac = self._make_sac( | ||
[self.award1, self.award1, self.award3, self.award3, self.award3] | ||
) | ||
errors = errors = check_award_reference_uniqueness(sac_validation_shape(sac)) | ||
self.assertEqual(len(errors), 2) | ||
|
||
errors = check_award_reference_uniqueness(sac_validation_shape(sac)) | ||
expected_error1 = err_duplicate_award_reference(self.award1["award_reference"]) | ||
expected_error2 = err_duplicate_award_reference(self.award3["award_reference"]) | ||
self.assertIn({"error": expected_error1}, errors) | ||
self.assertIn({"error": expected_error2}, errors) |