diff --git a/backend/audit/intakelib/transforms/xform_resize_award_references.py b/backend/audit/intakelib/transforms/xform_resize_award_references.py index 822ce8090..aa2f444b8 100644 --- a/backend/audit/intakelib/transforms/xform_resize_award_references.py +++ b/backend/audit/intakelib/transforms/xform_resize_award_references.py @@ -9,6 +9,8 @@ logger = logging.getLogger(__name__) +AWARD_REFERENCE_PREFIX = "AWARD" + def resize_award_reference(ir): references = get_range_by_name(ir, "award_reference") @@ -19,9 +21,17 @@ def resize_award_reference(ir): def _format_reference(v): + """Format the award reference to have 5 digits, padding with zeros if necessary""" + if not v or len(v) >= AWARD_LEN_5_DIGITS: + return v + + parts = v.split("-") + if len(parts) != 2 or not parts[0] or not parts[1]: + return v + + prefix, number = parts + if prefix.upper() != AWARD_REFERENCE_PREFIX: + return v - if v and len(v) < AWARD_LEN_5_DIGITS: - parts = v.split("-") - padding = "0" * (AWARD_LEN_5_DIGITS - len(v)) - return f"{parts[0]}-{padding}{parts[1]}" - return v + padding = "0" * (AWARD_LEN_5_DIGITS - len(v)) + return f"{prefix}-{padding}{number}" diff --git a/backend/audit/test_xform_resize_award_references.py b/backend/audit/test_xform_resize_award_references.py index 90648c87a..da053da15 100644 --- a/backend/audit/test_xform_resize_award_references.py +++ b/backend/audit/test_xform_resize_award_references.py @@ -51,3 +51,10 @@ def test_format_reference(self): self.assertEqual(_format_reference("AWARD-12345"), "AWARD-12345") self.assertEqual(_format_reference(None), None) self.assertEqual(_format_reference(""), "") + + def test_format_reference_malformed(self): + """Test the _format_reference function with malformed inputs""" + # Malformed cases should return as is, as the function does not raise exceptions + self.assertEqual(_format_reference("AWARD123"), "AWARD123") # Missing dash + self.assertEqual(_format_reference("123-456"), "123-456") # Incorrect prefix + self.assertEqual(_format_reference("AWARD-"), "AWARD-") # Missing second part