refactor: Move quote removal funtion to string utils, adapt to requirements

This commit is contained in:
TrisNol 2023-10-03 16:37:54 +02:00
parent 2a446a9937
commit 259259953e
3 changed files with 49 additions and 7 deletions

View File

@ -25,7 +25,10 @@ from aki_prj23_transparenzregister.models.company import (
PersonToCompanyRelationship,
RelationshipRoleEnum,
)
from aki_prj23_transparenzregister.utils.string_tools import transform_date_to_iso
from aki_prj23_transparenzregister.utils.string_tools import (
remove_traling_and_leading_quotes,
transform_date_to_iso,
)
def transform_xml_to_json(source_dir: str, target_dir: str) -> None:
@ -63,9 +66,11 @@ def parse_stakeholder(data: dict) -> CompanyRelationship | None:
if data["Beteiligter"]["Natuerliche_Person"]["Voller_Name"]["Vorname"] is None:
return CompanyToCompanyRelationship(
**{
"name": data["Beteiligter"]["Natuerliche_Person"]["Voller_Name"][
"name": remove_traling_and_leading_quotes(
data["Beteiligter"]["Natuerliche_Person"]["Voller_Name"][
"Nachname"
].replace('"', ""),
]
),
"location": Location(
**{
"city": data["Beteiligter"]["Natuerliche_Person"][
@ -128,9 +133,11 @@ def parse_stakeholder(data: dict) -> CompanyRelationship | None:
"role": RelationshipRoleEnum(
data["Rolle"]["Rollenbezeichnung"]["content"]
),
"name": data["Beteiligter"]["Organisation"]["Bezeichnung"][
"name": remove_traling_and_leading_quotes(
data["Beteiligter"]["Organisation"]["Bezeichnung"][
"Bezeichnung_Aktuell"
].replace('"', ""),
]
),
"location": Location(
**{
"city": data["Beteiligter"]["Organisation"]["Anschrift"]["Ort"],

View File

@ -34,3 +34,24 @@ def transform_date_to_iso(date: str) -> str:
input_format = "%d.%m.%y" if re.match(regex_yy, date) else "%d.%m.%Y"
date_temp = datetime.strptime(date, input_format)
return date_temp.strftime("%Y-%m-%d")
def remove_traling_and_leading_quotes(value: str) -> str:
"""Removes trailing and leading doulbe-quotes from given string if present.
Args:
value (str): _description_
Returns:
str: _description_
"""
if value is not None:
count_quotes = value.count('"')
if count_quotes > 0:
if value.startswith('"') and count_quotes % 2 != 0:
value = value[1:]
if value.endswith('"') and count_quotes % 2 != 0:
value = value[:-1]
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
return value

View File

@ -45,3 +45,17 @@ def test_simplify_string_type_error(value: Any) -> None:
def test_transform_date_to_iso(value: str, expected: str) -> None:
result = string_tools.transform_date_to_iso(value)
assert result == expected
@pytest.mark.parametrize(
("value", "expected_result"),
[
(None, None),
('"Siemes Verwaltungs-GmbH"', "Siemes Verwaltungs-GmbH"),
('"Hans"-Wurst GmbH', '"Hans"-Wurst GmbH'),
('Hans-Wurst GmbH"', "Hans-Wurst GmbH"),
],
)
def test_remove_trailing_and_leading_quotes(value: str, expected_result: str) -> None:
result = string_tools.remove_traling_and_leading_quotes(value)
assert result == expected_result