From 74713f5d73490a66c55618ff0638b8c02154947d Mon Sep 17 00:00:00 2001 From: Kevin Costner <120246174+kevincostner17@users.noreply.github.com> Date: Tue, 15 Sep 2026 14:00:55 +0530 Subject: [PATCH] fix(parsers): warn on malformed FHIR resources, strip UTF-8 BOM, reject non-finite GPX coordinates FHIR (#313): the flatteners indexed nested elements assuming R4 list/dict shapes, so an object-valued address/coding, a list-valued valueQuantity or R5 Encounter.class, or a list-valued resourceType raised KeyError, AttributeError or TypeError. Add _as_list/_as_dict coercion helpers and use them wherever a flattener or the Bundle entry loop reaches into a nested element. A resource whose resourceType is not a string is skipped with a warning, and any remaining flattener error becomes a per-resource " resource skipped: malformed ()" warning instead of aborting the parse. Well-formed resources flatten exactly as before. BOM (#314): Parser.read_text decoded with "utf-8", so a leading UTF-8 BOM became part of the text: HL7 saw "\ufeffMSH" and found no messages, FHIR json.loads rejected the input, and EDIFACT ignored the UNA delimiters. read_text now defaults to "utf-8-sig" for bytes, binary file objects and paths, and strips a leading BOM from str input and text file objects. This covers the HL7 v2, FHIR and EDIFACT parsers; the XML parsers are unchanged. GPX (#320): float() accepts "nan" and "inf", so such points were kept with no warning. _point now rejects coordinates that are non-finite or outside |lat| <= 90, |lon| <= 180 through the existing skip-with-warning path. Tests for all three live in tests/parsers/test_bom_and_malformed.py and include each issue's reproduction. Closes #313 Closes #314 Closes #320 --- src/freshdata/parsers/base.py | 15 +- src/freshdata/parsers/fhir.py | 56 ++- src/freshdata/parsers/gpx.py | 4 + tests/parsers/test_bom_and_malformed.py | 467 ++++++++++++++++++++++++ 4 files changed, 521 insertions(+), 21 deletions(-) create mode 100644 tests/parsers/test_bom_and_malformed.py diff --git a/src/freshdata/parsers/base.py b/src/freshdata/parsers/base.py index 92a16261..b4f38e57 100644 --- a/src/freshdata/parsers/base.py +++ b/src/freshdata/parsers/base.py @@ -96,17 +96,24 @@ class Parser(ABC): def parse(self, source: Any) -> ParseResult: """Parse *source* (Path, text, bytes, or file-like) into a :class:`ParseResult`.""" - def read_text(self, source: Any, *, encoding: str = "utf-8") -> str: - """Read *source* into text, accepting a Path, str content, bytes, or file-like.""" + def read_text(self, source: Any, *, encoding: str = "utf-8-sig") -> str: + """Read *source* into text, accepting a Path, str content, bytes, or file-like. + + A leading UTF-8 byte-order mark (common in files exported by Windows tools) is + dropped: ``utf-8-sig`` strips it when decoding bytes, and already-decoded text + has it removed explicitly. + """ if isinstance(source, (bytes, bytearray)): return bytes(source).decode(encoding) if hasattr(source, "read"): data = source.read() - return data.decode(encoding) if isinstance(data, (bytes, bytearray)) else data + if isinstance(data, (bytes, bytearray)): + return bytes(data).decode(encoding) + return data.lstrip("\ufeff") if isinstance(data, str) else data if isinstance(source, Path): return source.read_text(encoding=encoding) if isinstance(source, str): - return source + return source.lstrip("\ufeff") raise TypeError(f"cannot read a {type(source).__name__} source") def open_binary(self, source: Any) -> io.BufferedIOBase | io.BytesIO: diff --git a/src/freshdata/parsers/fhir.py b/src/freshdata/parsers/fhir.py index 75301673..30a9f82e 100644 --- a/src/freshdata/parsers/fhir.py +++ b/src/freshdata/parsers/fhir.py @@ -21,12 +21,24 @@ from .base import Parser, ParseResult +def _as_list(value: Any) -> list[Any]: + """Coerce a FHIR repeating element: a dict becomes ``[dict]``, a non-list ``[]``.""" + if isinstance(value, dict): + return [value] + return value if isinstance(value, list) else [] + + +def _as_dict(value: Any) -> dict[str, Any]: + """Coerce a FHIR complex element: a list yields its first item if that is a dict.""" + items = _as_list(value) + return items[0] if items and isinstance(items[0], dict) else {} + + def _coding(concept: Any) -> tuple[Any, Any, Any]: """Return ``(system, code, display)`` from a FHIR CodeableConcept's first coding.""" if not isinstance(concept, dict): return None, None, None - codings = concept.get("coding") or [] - first = codings[0] if codings and isinstance(codings[0], dict) else {} + first = _as_dict(concept.get("coding")) return first.get("system"), first.get("code"), first.get("display") or concept.get("text") @@ -43,8 +55,7 @@ def _ref_id(reference: Any) -> Any: def _flatten_patient(r: dict[str, Any]) -> dict[str, Any]: - addresses = r.get("address") or [{}] - addr = addresses[0] if isinstance(addresses[0], dict) else {} + addr = _as_dict(r.get("address")) deceased_dt = r.get("deceasedDateTime") deceased = r.get("deceasedBoolean") if deceased is None and deceased_dt is not None: @@ -63,8 +74,8 @@ def _flatten_patient(r: dict[str, Any]) -> dict[str, Any]: def _flatten_observation(r: dict[str, Any]) -> dict[str, Any]: system, code, display = _coding(r.get("code")) - vq = r.get("valueQuantity") or {} - interpretation = (r.get("interpretation") or [None])[0] + vq = _as_dict(r.get("valueQuantity")) + interpretation = _as_dict(r.get("interpretation")) return { "observation_id": r.get("id"), "patient_id": _ref_id(r.get("subject")), @@ -73,7 +84,7 @@ def _flatten_observation(r: dict[str, Any]) -> dict[str, Any]: "code_value": code, "display": display, "effective_date": (r.get("effectiveDateTime") - or (r.get("effectivePeriod") or {}).get("start")), + or _as_dict(r.get("effectivePeriod")).get("start")), "value_quantity": vq.get("value"), "value_unit": vq.get("unit") or vq.get("code"), "value_string": r.get("valueString"), @@ -82,10 +93,10 @@ def _flatten_observation(r: dict[str, Any]) -> dict[str, Any]: def _flatten_encounter(r: dict[str, Any]) -> dict[str, Any]: - cls = r.get("class") or {} - period = r.get("period") or {} - rsystem, rcode, _ = _coding((r.get("reasonCode") or [None])[0]) - hosp = r.get("hospitalization") or {} + cls = _as_dict(r.get("class")) + period = _as_dict(r.get("period")) + rsystem, rcode, _ = _coding(_as_dict(r.get("reasonCode"))) + hosp = _as_dict(r.get("hospitalization")) return { "encounter_id": r.get("id"), "patient_id": _ref_id(r.get("subject")), @@ -107,11 +118,11 @@ def _flatten_condition(r: dict[str, Any]) -> dict[str, Any]: "patient_id": _ref_id(r.get("subject")), "clinical_status": _coding_code(r.get("clinicalStatus")), "verification_status": _coding_code(r.get("verificationStatus")), - "category": _coding_code((r.get("category") or [None])[0]), + "category": _coding_code(_as_dict(r.get("category"))), "code_system": system, "code_value": code, "display": display, - "onset_date": r.get("onsetDateTime") or (r.get("onsetPeriod") or {}).get("start"), + "onset_date": r.get("onsetDateTime") or _as_dict(r.get("onsetPeriod")).get("start"), "recorded_date": r.get("recordedDate"), } @@ -167,13 +178,24 @@ def parse(self, source: Any) -> ParseResult: for res in self._iter_resources(data, warnings): rtype = res.get("resourceType") - counts[str(rtype)] += 1 + if not isinstance(rtype, str): + warnings.append( + f"resource with non-string resourceType ({type(rtype).__name__}) skipped") + continue + counts[rtype] += 1 handler = _FLATTENERS.get(rtype) if handler is None: - unsupported[str(rtype)] += 1 + unsupported[rtype] += 1 continue frame_name, flatten = handler - rows[frame_name].append(flatten(res)) + try: + row = flatten(res) + except (AttributeError, KeyError, TypeError, IndexError, ValueError) as exc: + warnings.append( + f"{rtype} resource {res.get('id')!r} skipped: malformed " + f"({type(exc).__name__})") + continue + rows[frame_name].append(row) if unsupported: listed = ", ".join(f"{k}({v})" for k, v in sorted(unsupported.items())) @@ -203,7 +225,7 @@ def _load_json(self, source: Any) -> Any: def _iter_resources(self, data: Any, warnings: list[str]): """Yield resource dicts from a Bundle, a single resource, or a list.""" if isinstance(data, dict) and data.get("resourceType") == "Bundle": - for entry in data.get("entry") or []: + for entry in _as_list(data.get("entry")): resource = entry.get("resource") if isinstance(entry, dict) else None if isinstance(resource, dict): yield resource diff --git a/src/freshdata/parsers/gpx.py b/src/freshdata/parsers/gpx.py index d1376328..8c758258 100644 --- a/src/freshdata/parsers/gpx.py +++ b/src/freshdata/parsers/gpx.py @@ -53,6 +53,10 @@ def _point(elem: ET.Element, kind: str, **extra: Any) -> None: try: row["lat"] = float(elem.get("lat")) # type: ignore[arg-type] row["lon"] = float(elem.get("lon")) # type: ignore[arg-type] + # float() accepts "nan"/"inf"; NaN fails every comparison, so this range + # check also rejects non-finite coordinates. + if not (-90.0 <= row["lat"] <= 90.0 and -180.0 <= row["lon"] <= 180.0): + raise ValueError("coordinates not finite or out of range") except (TypeError, ValueError): warnings.append(f"{_local(elem.tag)} with missing/invalid lat/lon skipped") return diff --git a/tests/parsers/test_bom_and_malformed.py b/tests/parsers/test_bom_and_malformed.py new file mode 100644 index 00000000..18e48682 --- /dev/null +++ b/tests/parsers/test_bom_and_malformed.py @@ -0,0 +1,467 @@ +"""Malformed-input and UTF-8 BOM handling across the structural parsers. + +Covers three gaps in the "malformed input is recorded in ``ParseResult.warnings`` +rather than raising" contract: + +* FHIR flatteners raising on shape-malformed resources (#313). +* ``Parser.read_text`` keeping a leading UTF-8 BOM (#314). +* GPX keeping non-finite or out-of-range coordinates (#320). +""" + +from __future__ import annotations + +import io +import json + +import pandas as pd +import pytest + +import freshdata as fd +from freshdata.parsers import fhir +from freshdata.parsers.edifact import EDIFACTParser +from freshdata.parsers.fhir import FHIRParser, _as_dict, _as_list +from freshdata.parsers.hl7v2 import HL7v2Parser + +BOM = "\ufeff" +HL7 = "MSH|^~\\&|A|B|C|D|2024||ADT^A01|1|P|2.5\rPID|1||123||DOE^J||1980|F" +FHIR_PATIENT = '{"resourceType":"Patient","id":"1"}' +# Custom UNA delimiters: component "*", element "|", release "?", segment "~". +EDIFACT_UNA = ( + "UNA*|.? ~UNB|UNOA*1|SENDER|RECIPIENT|240101*1200|REF1~" + "UNH|1|ORDERS*D*96A*UN~UNT|2|1~UNZ|1|REF1~" +) + + +# -- #314: UTF-8 BOM ------------------------------------------------------------------ + + +def _sources(text: str, tmp_path): + """Every source kind ``read_text`` accepts, each carrying *text* verbatim.""" + path = tmp_path / "input.txt" + path.write_bytes(text.encode("utf-8")) + return { + "str": text, + "bytes": text.encode("utf-8"), + "bytearray": bytearray(text.encode("utf-8")), + "binary file": io.BytesIO(text.encode("utf-8")), + "text file": io.StringIO(text), + "path": path, + } + + +SOURCE_KINDS = ["str", "bytes", "bytearray", "binary file", "text file", "path"] + + +@pytest.mark.parametrize("kind", SOURCE_KINDS) +def test_read_text_strips_leading_bom(kind, tmp_path): + source = _sources(BOM + "MSH|x", tmp_path)[kind] + assert HL7v2Parser().read_text(source) == "MSH|x" + + +@pytest.mark.parametrize("kind", SOURCE_KINDS) +def test_read_text_without_bom_is_unchanged(kind, tmp_path): + # "\n" rather than "\r": Path.read_text applies universal-newline translation. + source = _sources("MSH|x\nPID|1", tmp_path)[kind] + assert HL7v2Parser().read_text(source) == "MSH|x\nPID|1" + + +def test_read_text_keeps_bom_that_is_not_leading(): + assert HL7v2Parser().read_text(f"a{BOM}b") == f"a{BOM}b" + assert HL7v2Parser().read_text(f"a{BOM}b".encode()) == f"a{BOM}b" + + +def test_issue_314_repro_hl7_and_fhir_bytes_with_bom(): + h = fd.parse_domain((BOM + HL7).encode("utf-8"), format="hl7v2") + f = fd.parse_domain(b"\xef\xbb\xbf" + FHIR_PATIENT.encode(), format="fhir") + assert h.metadata["messages"] == 1 + assert len(h.frames["patient"]) == 1 + assert h.warnings == [] + assert len(f.frames["patient"]) == 1 + assert f.warnings == [] + + +@pytest.mark.parametrize("kind", SOURCE_KINDS) +def test_hl7_with_bom_every_source_kind(kind, tmp_path): + result = fd.parse_domain(_sources(BOM + HL7, tmp_path)[kind], format="hl7v2") + assert result.metadata["messages"] == 1 + assert result.frames["patient"].iloc[0]["patient_id"] == "123" + assert not any("MSH" in w for w in result.warnings) + + +@pytest.mark.parametrize("kind", SOURCE_KINDS) +def test_fhir_with_bom_every_source_kind(kind, tmp_path): + result = fd.parse_domain(_sources(BOM + FHIR_PATIENT, tmp_path)[kind], format="fhir") + assert list(result.frames["patient"]["patient_id"]) == ["1"] + assert result.warnings == [] + + +@pytest.mark.parametrize("kind", SOURCE_KINDS) +def test_edifact_with_bom_honours_una_delimiters(kind, tmp_path): + with_bom = EDIFACTParser().parse(_sources(BOM + EDIFACT_UNA, tmp_path)[kind]) + without_bom = EDIFACTParser().parse(EDIFACT_UNA) + assert with_bom.metadata == without_bom.metadata + assert with_bom.metadata["sender"] == "SENDER" + assert with_bom.metadata["message_type"] == "ORDERS" + assert with_bom.warnings == [] + pd.testing.assert_frame_equal(with_bom.frames["segments"], without_bom.frames["segments"]) + + +# -- #313: FHIR shape-malformed resources --------------------------------------------- + + +def test_as_list_coercion(): + assert _as_list({"a": 1}) == [{"a": 1}] + assert _as_list([1, 2]) == [1, 2] + assert _as_list(None) == [] + assert _as_list("text") == [] + assert _as_list(5) == [] + + +def test_as_dict_coercion(): + assert _as_dict({"a": 1}) == {"a": 1} + assert _as_dict([{"a": 1}, {"b": 2}]) == {"a": 1} + assert _as_dict(["not-a-dict", {"b": 2}]) == {} + assert _as_dict([]) == {} + assert _as_dict(None) == {} + assert _as_dict("text") == {} + assert _as_dict(3.5) == {} + + +ISSUE_313_CASES = { + "address dict": {"resourceType": "Patient", "id": "1", "address": {"postalCode": "1"}}, + "coding dict": {"resourceType": "Observation", "id": "1", "code": {"coding": {"code": "x"}}}, + "valueQuantity list": { + "resourceType": "Observation", + "id": "1", + "valueQuantity": [{"value": 1}], + }, + "Encounter.class list (R5)": { + "resourceType": "Encounter", + "id": "1", + "class": [{"coding": []}], + }, + "resourceType list": {"resourceType": ["Patient"]}, +} + + +@pytest.mark.parametrize("case", list(ISSUE_313_CASES)) +def test_issue_313_repro_does_not_raise(case): + result = fd.parse_domain(ISSUE_313_CASES[case], format="fhir") + assert set(result.frames) == { + "patient", + "observation", + "encounter", + "condition", + "medication_request", + } + + +def test_object_valued_repeating_elements_are_read_as_single_item(): + frames = ( + FHIRParser() + .parse( + [ + ISSUE_313_CASES["address dict"], + ISSUE_313_CASES["coding dict"], + {"resourceType": "Observation", "id": "2", "valueQuantity": [{"value": 1}]}, + ] + ) + .frames + ) + assert frames["patient"].iloc[0]["address_postal_code"] == "1" + assert list(frames["observation"]["code_value"])[0] == "x" + assert list(frames["observation"]["value_quantity"])[1] == 1 + + +def test_r5_list_valued_encounter_class_does_not_raise(): + result = FHIRParser().parse(ISSUE_313_CASES["Encounter.class list (R5)"]) + enc = result.frames["encounter"] + assert list(enc["encounter_id"]) == ["1"] + assert enc.iloc[0]["class_code"] is None + + +def test_non_string_resource_type_is_skipped_with_warning(): + result = FHIRParser().parse( + [ + {"resourceType": ["Patient"], "id": "bad"}, + {"resourceType": {"x": 1}}, + {"resourceType": "Patient", "id": "ok"}, + ] + ) + assert list(result.frames["patient"]["patient_id"]) == ["ok"] + assert result.metadata["resource_types"] == {"Patient": 1} + assert "resource with non-string resourceType (list) skipped" in result.warnings + assert "resource with non-string resourceType (dict) skipped" in result.warnings + + +def test_scalar_shaped_fields_do_not_raise(): + result = FHIRParser().parse( + [ + {"resourceType": "Patient", "id": "p", "address": 5, "maritalStatus": []}, + { + "resourceType": "Observation", + "id": "o", + "valueQuantity": "72", + "interpretation": {"coding": [{"code": "H"}]}, + "effectivePeriod": ["x"], + }, + { + "resourceType": "Encounter", + "id": "e", + "class": "IMP", + "period": 7, + "reasonCode": {"coding": [{"code": "R"}]}, + "hospitalization": [], + }, + { + "resourceType": "Condition", + "id": "c", + "category": {"coding": [{"code": "k"}]}, + "onsetPeriod": 1, + }, + ] + ) + frames = result.frames + assert frames["patient"].iloc[0]["address_postal_code"] is None + assert frames["observation"].iloc[0]["value_quantity"] is None + assert frames["observation"].iloc[0]["interpretation"] == "H" + assert frames["encounter"].iloc[0]["class_code"] is None + assert frames["encounter"].iloc[0]["reason_code"] == "R" + assert frames["condition"].iloc[0]["category"] == "k" + + +def test_flattener_exception_becomes_per_resource_warning(monkeypatch): + def boom(resource): + raise KeyError("bad shape") + + monkeypatch.setitem(fhir._FLATTENERS, "Patient", ("patient", boom)) + result = FHIRParser().parse( + [ + {"resourceType": "Patient", "id": "p1"}, + {"resourceType": "Condition", "id": "c1"}, + ] + ) + assert result.frames["patient"].empty + assert list(result.frames["condition"]["condition_id"]) == ["c1"] + assert "Patient resource 'p1' skipped: malformed (KeyError)" in result.warnings + assert result.metadata["resource_types"] == {"Patient": 1, "Condition": 1} + + +@pytest.mark.parametrize("entry", [5, "text", None]) +def test_bundle_with_non_list_entry_does_not_raise(entry): + result = FHIRParser().parse({"resourceType": "Bundle", "entry": entry}) + assert all(df.empty for df in result.frames.values()) + assert "no FHIR resources found" in result.warnings + + +def test_bundle_with_object_valued_entry_reads_single_entry(): + result = FHIRParser().parse( + {"resourceType": "Bundle", "entry": {"resource": {"resourceType": "Patient", "id": "p"}}} + ) + assert list(result.frames["patient"]["patient_id"]) == ["p"] + + +def test_well_formed_resources_flatten_exactly_as_before(): + bundle = { + "resourceType": "Bundle", + "id": "b", + "type": "collection", + "entry": [ + { + "resource": { + "resourceType": "Patient", + "id": "p1", + "birthDate": "1970-01-01", + "gender": "female", + "maritalStatus": {"coding": [{"code": "M"}]}, + "address": [{"postalCode": "10001", "country": "US"}, {"postalCode": "2"}], + "deceasedDateTime": "2020-01-01", + } + }, + { + "resource": { + "resourceType": "Observation", + "id": "o1", + "status": "final", + "subject": {"reference": "Patient/p1"}, + "code": {"coding": [{"system": "loinc", "code": "8867-4"}], "text": "HR"}, + "effectivePeriod": {"start": "2024-01-01"}, + "valueQuantity": {"value": 72, "code": "/min"}, + "interpretation": [{"coding": [{"code": "N"}]}], + } + }, + { + "resource": { + "resourceType": "Encounter", + "id": "e1", + "status": "finished", + "subject": {"reference": "Patient/p1"}, + "class": {"code": "IMP"}, + "period": {"start": "2024-01-01", "end": "2024-01-03"}, + "reasonCode": [{"coding": [{"system": "sct", "code": "R1"}]}], + "hospitalization": {"admitSource": {"coding": [{"code": "emd"}]}}, + "serviceType": {"coding": [{"code": "st"}]}, + } + }, + { + "resource": { + "resourceType": "Condition", + "id": "c1", + "subject": {"reference": "Patient/p1"}, + "clinicalStatus": {"coding": [{"code": "active"}]}, + "verificationStatus": {"coding": [{"code": "confirmed"}]}, + "category": [{"coding": [{"code": "problem-list-item"}]}], + "code": {"coding": [{"system": "icd", "code": "E11", "display": "DM"}]}, + "onsetPeriod": {"start": "2019-06-01"}, + "recordedDate": "2019-07-01", + } + }, + { + "resource": { + "resourceType": "MedicationRequest", + "id": "m1", + "status": "active", + "intent": "order", + "subject": {"reference": "Patient/p1"}, + "requester": {"reference": "Practitioner/dr1"}, + "medicationCodeableConcept": {"coding": [{"system": "rx", "code": "860975"}]}, + "authoredOn": "2024-01-02", + } + }, + ], + } + result = fd.parse_domain(json.dumps(bundle), format="fhir") + records = {name: df.to_dict("records") for name, df in result.frames.items()} + assert result.warnings == [] + assert records == { + "patient": [ + { + "patient_id": "p1", + "birth_date": "1970-01-01", + "gender": "female", + "deceased": True, + "deceased_date": "2020-01-01", + "marital_status": "M", + "address_postal_code": "10001", + "address_country": "US", + } + ], + "observation": [ + { + "observation_id": "o1", + "patient_id": "p1", + "status": "final", + "code_system": "loinc", + "code_value": "8867-4", + "display": "HR", + "effective_date": "2024-01-01", + "value_quantity": 72, + "value_unit": "/min", + "value_string": None, + "interpretation": "N", + } + ], + "encounter": [ + { + "encounter_id": "e1", + "patient_id": "p1", + "status": "finished", + "class_code": "IMP", + "period_start": "2024-01-01", + "period_end": "2024-01-03", + "reason_code": "R1", + "reason_code_system": "sct", + "hospitalization_admit_source": "emd", + "service_type": "st", + } + ], + "condition": [ + { + "condition_id": "c1", + "patient_id": "p1", + "clinical_status": "active", + "verification_status": "confirmed", + "category": "problem-list-item", + "code_system": "icd", + "code_value": "E11", + "display": "DM", + "onset_date": "2019-06-01", + "recorded_date": "2019-07-01", + } + ], + "medication_request": [ + { + "medication_request_id": "m1", + "patient_id": "p1", + "status": "active", + "intent": "order", + "medication_system": "rx", + "medication_code": "860975", + "medication_display": None, + "authored_on": "2024-01-02", + "requester": "dr1", + } + ], + } + + +# -- #320: GPX non-finite / out-of-range coordinates ---------------------------------- + + +def test_issue_320_repro_nan_inf_and_non_numeric_points_skipped(): + r = fd.parse_domain( + 'x', + format="gpx", + ) + assert r.frames["waypoints"].empty + assert r.warnings.count("wpt with missing/invalid lat/lon skipped") == 2 + + +@pytest.mark.parametrize( + ("lat", "lon"), + [ + ("nan", "0"), + ("0", "nan"), + ("NaN", "NaN"), + ("inf", "0"), + ("0", "-inf"), + ("Infinity", "1"), + ("90.0001", "0"), + ("-90.5", "0"), + ("0", "180.01"), + ("0", "-181"), + ], +) +def test_gpx_rejects_non_finite_or_out_of_range_coordinates(lat, lon): + r = fd.parse_domain( + f'', format="gpx" + ) + assert r.frames["waypoints"].to_dict("records") == [{"lat": 1.0, "lon": 2.0}] + assert r.warnings == ["wpt with missing/invalid lat/lon skipped"] + + +def test_gpx_keeps_boundary_coordinates(): + r = fd.parse_domain( + '', format="gpx" + ) + assert r.frames["waypoints"].to_dict("records") == [ + {"lat": 90.0, "lon": 180.0}, + {"lat": -90.0, "lon": -180.0}, + ] + assert r.warnings == [] + + +def test_gpx_non_finite_track_and_route_points_skipped(): + gpx = ( + '' + '' + ) + r = fd.parse_domain(gpx, format="gpx") + assert r.frames["route_points"].to_dict("records") == [ + {"lat": 1.0, "lon": 1.0, "route_index": 0} + ] + assert r.frames["track_points"].to_dict("records") == [ + {"lat": 2.0, "lon": 2.0, "track_index": 0} + ] + assert "rtept with missing/invalid lat/lon skipped" in r.warnings + assert "trkpt with missing/invalid lat/lon skipped" in r.warnings