fix: period split in profile_to_library handles ISO dates with hyphens
Fixes a bug where ISO-formatted dates (e.g. '2023-01 – 2025-03') in the period field were split incorrectly. The old code replaced the en-dash with a hyphen first, then split on the first hyphen, causing dates like '2023-01' to be split into '2023' and '01' instead of the expected start/end pair. The fix splits on the dash/dash separator *before* normalizing to plain hyphens, ensuring round-trip conversion of dates with embedded hyphens. Adds two regression tests: - test_profile_to_library_period_split_iso_dates: verifies en-dash separation - test_profile_to_library_period_split_em_dash: verifies em-dash separation
This commit is contained in:
parent
048a5f4cc3
commit
cd787a2509
2 changed files with 35 additions and 3 deletions
|
|
@ -122,9 +122,15 @@ def profile_to_library(payload: dict[str, Any]) -> tuple[str, dict[str, Any]]:
|
|||
period = (exp.get("period") or "").strip()
|
||||
location = (exp.get("location") or "").strip()
|
||||
|
||||
# Split period back to start_date / end_date
|
||||
sep_period = period.replace("\u2013", "-").replace("\u2014", "-")
|
||||
date_parts = [p.strip() for p in sep_period.split("-", 1)]
|
||||
# Split period back to start_date / end_date.
|
||||
# Split on the dash/dash separator BEFORE normalising to plain hyphens
|
||||
# so that ISO dates like "2023-01 – 2025-03" round-trip correctly.
|
||||
if "\u2013" in period: # en-dash
|
||||
date_parts = [p.strip() for p in period.split("\u2013", 1)]
|
||||
elif "\u2014" in period: # em-dash
|
||||
date_parts = [p.strip() for p in period.split("\u2014", 1)]
|
||||
else:
|
||||
date_parts = [period.strip()] if period.strip() else []
|
||||
start_date = date_parts[0] if date_parts else ""
|
||||
end_date = date_parts[1] if len(date_parts) > 1 else ""
|
||||
|
||||
|
|
|
|||
|
|
@ -139,6 +139,32 @@ def test_profile_to_library_period_split():
|
|||
assert struct["experience"][0]["start_date"] == "2023"
|
||||
assert struct["experience"][0]["end_date"] == "present"
|
||||
|
||||
def test_profile_to_library_period_split_iso_dates():
|
||||
"""ISO dates (with hyphens) must round-trip through the period field correctly."""
|
||||
payload = {
|
||||
**PROFILE_PAYLOAD,
|
||||
"experience": [{
|
||||
**PROFILE_PAYLOAD["experience"][0],
|
||||
"period": "2023-01 \u2013 2025-03",
|
||||
}],
|
||||
}
|
||||
_, struct = profile_to_library(payload)
|
||||
assert struct["experience"][0]["start_date"] == "2023-01"
|
||||
assert struct["experience"][0]["end_date"] == "2025-03"
|
||||
|
||||
def test_profile_to_library_period_split_em_dash():
|
||||
"""Em-dash separator is also handled."""
|
||||
payload = {
|
||||
**PROFILE_PAYLOAD,
|
||||
"experience": [{
|
||||
**PROFILE_PAYLOAD["experience"][0],
|
||||
"period": "2022-06 \u2014 2023-12",
|
||||
}],
|
||||
}
|
||||
_, struct = profile_to_library(payload)
|
||||
assert struct["experience"][0]["start_date"] == "2022-06"
|
||||
assert struct["experience"][0]["end_date"] == "2023-12"
|
||||
|
||||
def test_profile_to_library_education_round_trip():
|
||||
_, struct = profile_to_library(PROFILE_PAYLOAD)
|
||||
assert struct["education"][0]["institution"] == "State University"
|
||||
|
|
|
|||
Loading…
Reference in a new issue