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

#4513 fix logic to prevent index out of range error #4519

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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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}"
7 changes: 7 additions & 0 deletions backend/audit/test_xform_resize_award_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading