Skip to content

Commit

Permalink
fix: over eager corporate name truncate (#3219)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke authored Aug 21, 2024
1 parent ec07dd4 commit f95cc66
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
25 changes: 16 additions & 9 deletions backend/benefit/applications/services/ahjo_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,32 @@ def prepare_case_title(application: Application, company_name: str) -> str:
return full_title


def prepare_final_case_title(application: Application) -> str:
def prepare_final_case_title(application: Application, limit: int = 100) -> str:
"""Prepare the final case title for Ahjo, if the full title is over 100 characters, \
truncate the company name to fit the limit."""
limit = 100
full_case_title = prepare_case_title(application, application.company_name)
length_of_full_title = len(full_case_title)

if length_of_full_title <= limit:
return full_case_title
else:
over_limit = length_of_full_title - limit
truncated_company_name = truncate_company_name(
application.company_name, over_limit
truncated_company_name = truncate_string_to_limit(
application.company_name, len(application.company_name) - over_limit
)
return prepare_case_title(application, truncated_company_name)


def truncate_company_name(company_name: str, chars_to_truncate: int) -> str:
"""Truncate the company name to a number of characters, \
because Ahjo has a technical limitation of 100 characters for the company name."""
return company_name[:-chars_to_truncate]
def truncate_string_to_limit(string_to_truncate: str, final_length: int) -> str:
"""Truncate the given string to the specified final length."""
if len(string_to_truncate) > final_length:
return string_to_truncate[:final_length]
return string_to_truncate


def truncate_from_end_of_string(string_to_truncate: str, limit: int):
"""Truncate the given number of characters from the end of the string"""
return string_to_truncate[-limit:]


def resolve_payload_language(application: Application) -> str:
Expand Down Expand Up @@ -113,7 +118,9 @@ def _prepare_top_level_dict(
"Agents": [
{
"Role": "sender_initiator",
"CorporateName": truncate_company_name(application.company.name, 100),
"CorporateName": truncate_string_to_limit(
application.company.name, 100
),
"ContactPerson": application.contact_person,
"Type": "External",
"Email": application.company_contact_person_email,
Expand Down
6 changes: 4 additions & 2 deletions backend/benefit/applications/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from applications.services.ahjo_payload import (
resolve_payload_language,
truncate_company_name,
truncate_string_to_limit,
)
from applications.services.application_alteration_csv_report import (
AlterationCsvConfigurableFields,
Expand Down Expand Up @@ -438,7 +438,9 @@ def ahjo_open_case_top_level_dict(decided_application):
"Agents": [
{
"Role": "sender_initiator",
"CorporateName": truncate_company_name(application.company.name, 100),
"CorporateName": truncate_string_to_limit(
application.company.name, 100
),
"ContactPerson": application.contact_person,
"Type": "External",
"Email": application.company_contact_person_email,
Expand Down
43 changes: 32 additions & 11 deletions backend/benefit/applications/tests/test_ahjo_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
prepare_final_case_title,
prepare_update_application_payload,
resolve_payload_language,
truncate_company_name,
truncate_string_to_limit,
)
from common.utils import hash_file

Expand All @@ -35,19 +35,40 @@ def test_prepare_case_title(decided_application):
assert wanted_title == got


def test_truncate_company_name():
short = "a" * 50
too_long = "a" * 105
not_too_long = "a" * 100
assert len(truncate_company_name(too_long, 5)) == 100
assert len(truncate_company_name(not_too_long, 5)) == 95
assert len(truncate_company_name(short, 5)) == 45
@pytest.mark.parametrize(
"input_string, limit, expected_length, resulting_string",
[
# ("a" * 200, 100, 100),
("a" * 100 + "b" * 5, 100, 100, "a" * 100),
("a" * 100, 100, 100, "a" * 100),
("a" * 50, 100, 50, "a" * 50),
("1234567890AB", 10, 10, "1234567890"),
],
)
def test_truncate_string_to_limit(
input_string, limit, expected_length, resulting_string
):
result = truncate_string_to_limit(input_string, limit)
assert len(result) == expected_length
assert result == resulting_string


def test_prepare_final_case_title_truncate(decided_application):
@pytest.mark.parametrize(
"company_name, limit, expected_length",
[
("a" * 100 + "b" * 100, 100, 100),
("a" * 100 + "b" * 5, 100, 100),
("a" * 100, 100, 100),
("a" * 50, 100, 100),
("1234567890AB", 10, 100),
],
)
def test_prepare_final_case_title_truncate(
decided_application, company_name, limit, expected_length
):
application = decided_application
application.company_name = "a" * 105
assert len(prepare_final_case_title(application)) == 100
application.company_name = company_name
assert len(prepare_final_case_title(application, limit)) <= expected_length


@pytest.mark.parametrize(
Expand Down

0 comments on commit f95cc66

Please sign in to comment.