mloda-plugin-govdata 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mloda_plugin_govdata/__init__.py +0 -0
- mloda_plugin_govdata/feature_groups/__init__.py +1 -0
- mloda_plugin_govdata/feature_groups/govdata/__init__.py +55 -0
- mloda_plugin_govdata/feature_groups/govdata/bundeswahlleiterin.py +48 -0
- mloda_plugin_govdata/feature_groups/govdata/core/__init__.py +1 -0
- mloda_plugin_govdata/feature_groups/govdata/core/cache.py +114 -0
- mloda_plugin_govdata/feature_groups/govdata/core/client.py +72 -0
- mloda_plugin_govdata/feature_groups/govdata/core/discovery.py +177 -0
- mloda_plugin_govdata/feature_groups/govdata/core/locator.py +42 -0
- mloda_plugin_govdata/feature_groups/govdata/core/parse.py +275 -0
- mloda_plugin_govdata/feature_groups/govdata/feature.py +17 -0
- mloda_plugin_govdata/feature_groups/govdata/population.py +22 -0
- mloda_plugin_govdata/feature_groups/govdata/reader.py +123 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/__init__.py +0 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/conftest.py +8 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/test_cache.py +62 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/test_client.py +49 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/test_discovery.py +157 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/test_parse.py +183 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/test_parse_hypothesis.py +38 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/test_reader.py +327 -0
- mloda_plugin_govdata/feature_groups/govdata/tests/test_uba.py +193 -0
- mloda_plugin_govdata/feature_groups/govdata/uba.py +220 -0
- mloda_plugin_govdata-0.2.1.dist-info/METADATA +29 -0
- mloda_plugin_govdata-0.2.1.dist-info/RECORD +28 -0
- mloda_plugin_govdata-0.2.1.dist-info/WHEEL +5 -0
- mloda_plugin_govdata-0.2.1.dist-info/licenses/LICENSE +201 -0
- mloda_plugin_govdata-0.2.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Level 1/2: CKAN discovery, metadata model, and license resolution."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
import pytest
|
|
8
|
+
import respx
|
|
9
|
+
|
|
10
|
+
from mloda_plugin_govdata.feature_groups.govdata.core.client import build_client
|
|
11
|
+
from mloda_plugin_govdata.feature_groups.govdata.core.discovery import (
|
|
12
|
+
Dataset,
|
|
13
|
+
Resource,
|
|
14
|
+
_select_resource,
|
|
15
|
+
normalize_license,
|
|
16
|
+
resolve_distribution,
|
|
17
|
+
search_datasets,
|
|
18
|
+
)
|
|
19
|
+
from mloda_plugin_govdata.feature_groups.govdata.core.locator import GovDataLocator
|
|
20
|
+
|
|
21
|
+
PACKAGE_SHOW = "https://ckan.govdata.de/api/3/action/package_show"
|
|
22
|
+
PACKAGE_SEARCH = "https://ckan.govdata.de/api/3/action/package_search"
|
|
23
|
+
SLUG = "einwohner-nach-altersgruppen-und-stadtbezirken"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_normalize_license_known_and_free_text() -> None:
|
|
27
|
+
assert normalize_license("http://dcat-ap.de/def/licenses/cc-by/4.0") == "CC-BY-4.0"
|
|
28
|
+
# dataset-level spelling drift (underscore) normalizes to the resource-level label.
|
|
29
|
+
assert normalize_license("http://dcat-ap.de/def/licenses/dl-by-de/2_0") == "DL-DE-BY-2.0"
|
|
30
|
+
assert normalize_license("Es gelten keine Zugriffsbeschränkungen") == "Es gelten keine Zugriffsbeschränkungen"
|
|
31
|
+
assert normalize_license(None) is None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_dataset_from_ckan_fixture(fixtures_dir: Path) -> None:
|
|
35
|
+
payload = json.loads((fixtures_dir / "package_show.json").read_text(encoding="utf-8"))
|
|
36
|
+
dataset = Dataset.from_ckan(payload["result"])
|
|
37
|
+
assert dataset.name == SLUG
|
|
38
|
+
assert dataset.resources[0].license == "http://dcat-ap.de/def/licenses/cc-by/4.0"
|
|
39
|
+
assert dataset.resources[0].format == "CSV"
|
|
40
|
+
assert "modified" in dataset.extras # DCAT freshness extra is preserved
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@respx.mock
|
|
44
|
+
def test_resolve_distribution_reads_license_from_distribution(fixtures_dir: Path) -> None:
|
|
45
|
+
payload = (fixtures_dir / "package_show.json").read_text(encoding="utf-8")
|
|
46
|
+
route = respx.get(PACKAGE_SHOW).mock(return_value=httpx.Response(200, text=payload))
|
|
47
|
+
with build_client() as client:
|
|
48
|
+
resolved = resolve_distribution(GovDataLocator(dataset_id=SLUG), client)
|
|
49
|
+
assert route.called
|
|
50
|
+
assert resolved.license == "CC-BY-4.0"
|
|
51
|
+
assert resolved.url.endswith(".csv")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_resolve_direct_url_skips_discovery() -> None:
|
|
55
|
+
with build_client() as client:
|
|
56
|
+
resolved = resolve_distribution(GovDataLocator(distribution_url="https://example.org/data.csv"), client)
|
|
57
|
+
assert resolved.url == "https://example.org/data.csv"
|
|
58
|
+
assert resolved.license is None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_select_resource_prefers_csv_over_first_non_csv() -> None:
|
|
62
|
+
resources = [
|
|
63
|
+
Resource(url="https://example.org/page.html", format="HTML"),
|
|
64
|
+
Resource(url="https://example.org/data.csv", format="CSV"),
|
|
65
|
+
]
|
|
66
|
+
assert _select_resource(resources, 0).url.endswith(".csv")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _search_page(count: int, names: list[str]) -> httpx.Response:
|
|
70
|
+
results = [{"name": name, "title": name.replace("-", " "), "resources": []} for name in names]
|
|
71
|
+
return httpx.Response(200, json={"success": True, "result": {"count": count, "results": results}})
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@respx.mock
|
|
75
|
+
def test_search_datasets_paginates_until_count() -> None:
|
|
76
|
+
route = respx.get(PACKAGE_SEARCH).mock(
|
|
77
|
+
side_effect=[_search_page(3, ["ds-a", "ds-b"]), _search_page(3, ["ds-c"])],
|
|
78
|
+
)
|
|
79
|
+
with build_client() as client:
|
|
80
|
+
names = [d.name for d in search_datasets(client, "einwohner", page_size=2)]
|
|
81
|
+
assert names == ["ds-a", "ds-b", "ds-c"]
|
|
82
|
+
assert route.call_count == 2
|
|
83
|
+
first, second = route.calls
|
|
84
|
+
assert dict(first.request.url.params) == {"q": "einwohner", "rows": "2", "start": "0"}
|
|
85
|
+
assert dict(second.request.url.params) == {"q": "einwohner", "rows": "2", "start": "2"}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@respx.mock
|
|
89
|
+
def test_search_datasets_empty_result() -> None:
|
|
90
|
+
route = respx.get(PACKAGE_SEARCH).mock(return_value=_search_page(0, []))
|
|
91
|
+
with build_client() as client:
|
|
92
|
+
assert list(search_datasets(client, "no-such-dataset")) == []
|
|
93
|
+
assert route.call_count == 1
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@respx.mock
|
|
97
|
+
def test_search_datasets_unsuccessful_payload_raises() -> None:
|
|
98
|
+
respx.get(PACKAGE_SEARCH).mock(return_value=httpx.Response(200, json={"success": False}))
|
|
99
|
+
with build_client() as client, pytest.raises(ValueError, match="package_search"):
|
|
100
|
+
list(search_datasets(client, "einwohner"))
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@respx.mock
|
|
104
|
+
def test_search_datasets_caps_at_max_results_and_requests_no_extra_rows() -> None:
|
|
105
|
+
route = respx.get(PACKAGE_SEARCH).mock(return_value=_search_page(5, ["ds-a", "ds-b"]))
|
|
106
|
+
with build_client() as client:
|
|
107
|
+
names = [d.name for d in search_datasets(client, "einwohner", page_size=100, max_results=2)]
|
|
108
|
+
assert names == ["ds-a", "ds-b"]
|
|
109
|
+
assert route.call_count == 1
|
|
110
|
+
assert dict(route.calls[0].request.url.params) == {"q": "einwohner", "rows": "2", "start": "0"}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@respx.mock
|
|
114
|
+
def test_search_datasets_stops_on_stalled_server_page() -> None:
|
|
115
|
+
# A server whose count promises more than it delivers must not loop forever.
|
|
116
|
+
route = respx.get(PACKAGE_SEARCH).mock(
|
|
117
|
+
side_effect=[_search_page(10, ["ds-a"]), _search_page(10, [])],
|
|
118
|
+
)
|
|
119
|
+
with build_client() as client:
|
|
120
|
+
names = [d.name for d in search_datasets(client, "einwohner", page_size=1)]
|
|
121
|
+
assert names == ["ds-a"]
|
|
122
|
+
assert route.call_count == 2
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@respx.mock
|
|
126
|
+
def test_search_datasets_shrinks_rows_near_max_results_and_honors_ckan_base() -> None:
|
|
127
|
+
custom_search = "https://ckan.example.org/api/3/action/package_search"
|
|
128
|
+
route = respx.get(custom_search).mock(
|
|
129
|
+
side_effect=[_search_page(5, ["ds-a", "ds-b"]), _search_page(5, ["ds-c"])],
|
|
130
|
+
)
|
|
131
|
+
with build_client() as client:
|
|
132
|
+
names = [
|
|
133
|
+
d.name
|
|
134
|
+
for d in search_datasets(
|
|
135
|
+
client,
|
|
136
|
+
"einwohner",
|
|
137
|
+
ckan_base="https://ckan.example.org/api/3/action",
|
|
138
|
+
page_size=2,
|
|
139
|
+
max_results=3,
|
|
140
|
+
)
|
|
141
|
+
]
|
|
142
|
+
assert names == ["ds-a", "ds-b", "ds-c"]
|
|
143
|
+
assert route.call_count == 2
|
|
144
|
+
# The second request only asks for the one row still allowed by max_results.
|
|
145
|
+
assert dict(route.calls[1].request.url.params) == {"q": "einwohner", "rows": "1", "start": "2"}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def test_search_datasets_rejects_bad_page_size() -> None:
|
|
149
|
+
with build_client() as client, pytest.raises(ValueError, match="page_size"):
|
|
150
|
+
list(search_datasets(client, "einwohner", page_size=0))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@pytest.mark.live
|
|
154
|
+
def test_live_search_finds_population_dataset() -> None:
|
|
155
|
+
with build_client() as client:
|
|
156
|
+
names = [d.name for d in search_datasets(client, "einwohner altersgruppen stadtbezirken", max_results=100)]
|
|
157
|
+
assert SLUG in names
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""Level 1: deterministic German-CSV parsing on captured byte fixtures."""
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import pyarrow as pa
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from mloda_plugin_govdata.feature_groups.govdata.core.parse import (
|
|
10
|
+
ColumnType,
|
|
11
|
+
detect_encoding,
|
|
12
|
+
parse_german_csv,
|
|
13
|
+
parse_german_csv_bytes,
|
|
14
|
+
parse_multi_header_csv,
|
|
15
|
+
parse_multi_header_csv_bytes,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
POPULATION_COLUMNS: dict[str, ColumnType] = {
|
|
19
|
+
"Stichtag": ColumnType.DATE,
|
|
20
|
+
"Stadtbezirk": ColumnType.STRING,
|
|
21
|
+
"Alter in 10 Gruppen": ColumnType.STRING,
|
|
22
|
+
"Einwohner": ColumnType.INTEGER,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
VALUE_MARKER_COLUMNS: dict[str, ColumnType] = {
|
|
26
|
+
"Stichtag": ColumnType.DATE,
|
|
27
|
+
"Gebiet": ColumnType.STRING,
|
|
28
|
+
"Wert": ColumnType.INTEGER,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_detect_encoding_population(fixtures_dir: Path) -> None:
|
|
33
|
+
data = (fixtures_dir / "population_sample.csv").read_bytes()
|
|
34
|
+
assert detect_encoding(data) == "cp1252"
|
|
35
|
+
with pytest.raises(UnicodeDecodeError):
|
|
36
|
+
data.decode("utf-8")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_detect_encoding_prefers_utf8_sig_with_bom() -> None:
|
|
40
|
+
assert detect_encoding("Wert\r\n123\r\n".encode("utf-8-sig")) == "utf-8-sig"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_detect_encoding_falls_back_for_cp1252_undefined_byte() -> None:
|
|
44
|
+
# 0x81 is undefined in cp1252, so the ladder must reach an 8859 fallback.
|
|
45
|
+
assert detect_encoding(b"\x81") in {"iso-8859-15", "latin-1"}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_parse_population_sample(fixtures_dir: Path) -> None:
|
|
49
|
+
table = parse_german_csv(fixtures_dir / "population_sample.csv", POPULATION_COLUMNS)
|
|
50
|
+
assert table.schema.names == ["Stichtag", "Stadtbezirk", "Alter in 10 Gruppen", "Einwohner"]
|
|
51
|
+
assert table.num_rows == 1000
|
|
52
|
+
assert table.schema.field("Stichtag").type == pa.date32()
|
|
53
|
+
assert table.schema.field("Einwohner").type == pa.int64()
|
|
54
|
+
first = table.slice(0, 1).to_pylist()[0]
|
|
55
|
+
assert first["Stichtag"] == datetime.date(1986, 6, 30)
|
|
56
|
+
assert first["Stadtbezirk"] == "Mitte"
|
|
57
|
+
assert first["Einwohner"] == 494
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_value_markers_map_correctly(fixtures_dir: Path) -> None:
|
|
61
|
+
table = parse_german_csv(fixtures_dir / "value_markers.csv", VALUE_MARKER_COLUMNS)
|
|
62
|
+
# '-' -> 0 (never null); '.', '...', '/', 'x' -> null; '1.234' -> 1234.
|
|
63
|
+
assert table.column("Wert").to_pylist() == [0, 123, None, None, None, None, 1234]
|
|
64
|
+
# cp1252 umlauts decode correctly.
|
|
65
|
+
assert "Möhringen" in table.column("Gebiet").to_pylist()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_unknown_columns_default_to_string(fixtures_dir: Path) -> None:
|
|
69
|
+
table = parse_german_csv(fixtures_dir / "value_markers.csv")
|
|
70
|
+
assert all(field_type == pa.string() for field_type in table.schema.types)
|
|
71
|
+
# Untyped: the '-' marker is preserved verbatim as a string.
|
|
72
|
+
assert table.column("Wert").to_pylist()[0] == "-"
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_missing_column_raises(fixtures_dir: Path) -> None:
|
|
76
|
+
with pytest.raises(KeyError):
|
|
77
|
+
parse_german_csv(fixtures_dir / "value_markers.csv", {"DoesNotExist": ColumnType.STRING})
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_integer_validation_raises_on_non_numeric() -> None:
|
|
81
|
+
data = "Wert\r\nnotanumber\r\n".encode("cp1252")
|
|
82
|
+
with pytest.raises(ValueError):
|
|
83
|
+
parse_german_csv_bytes(data, {"Wert": ColumnType.INTEGER})
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_date_validation_raises_on_bad_date() -> None:
|
|
87
|
+
data = "Stichtag\r\n99.99.9999\r\n".encode("cp1252")
|
|
88
|
+
with pytest.raises(ValueError):
|
|
89
|
+
parse_german_csv_bytes(data, {"Stichtag": ColumnType.DATE})
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_large_integer_parses_exactly() -> None:
|
|
93
|
+
# Values above 2**53 must not be rounded through float.
|
|
94
|
+
big = 9007199254740993
|
|
95
|
+
data = f"Wert\r\n{big}\r\n".encode("cp1252")
|
|
96
|
+
table = parse_german_csv_bytes(data, {"Wert": ColumnType.INTEGER})
|
|
97
|
+
assert table.column("Wert").to_pylist() == [big]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_multi_header_flattens_and_types_kerg(fixtures_dir: Path) -> None:
|
|
101
|
+
table = parse_multi_header_csv(
|
|
102
|
+
fixtures_dir / "kerg_sample.csv", skiprows=5, header_rows=3, label_columns=4, value_type=ColumnType.INTEGER
|
|
103
|
+
)
|
|
104
|
+
assert table.num_columns == 140 # 140 real columns; the trailing ';' phantom column is dropped
|
|
105
|
+
assert table.num_rows == 16 # 17 data lines minus one ';' separator row
|
|
106
|
+
assert table.schema.names[:4] == ["Nr", "Gebiet", "gehört zu", "Gewählt"]
|
|
107
|
+
# the 3 merged header rows flatten into one combined name
|
|
108
|
+
assert "Wahlberechtigte Erststimmen Endgültig" in table.schema.names
|
|
109
|
+
assert table.schema.field("Gebiet").type == pa.string()
|
|
110
|
+
assert table.schema.field("Wahlberechtigte Erststimmen Endgültig").type == pa.int64()
|
|
111
|
+
assert table.column("Gebiet").to_pylist()[0] == "Flensburg – Schleswig"
|
|
112
|
+
assert table.column("Wahlberechtigte Erststimmen Endgültig").to_pylist()[0] == 232131
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_multi_header_forward_fills_merged_cells() -> None:
|
|
116
|
+
data = b"Group;;Other\r\na;b;c\r\n1;2;3\r\n"
|
|
117
|
+
table = parse_multi_header_csv_bytes(data, skiprows=0, header_rows=2, label_columns=3, value_type=ColumnType.STRING)
|
|
118
|
+
assert table.schema.names == ["Group a", "Group b", "Other c"]
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_multi_header_dedupes_duplicate_names() -> None:
|
|
122
|
+
data = b"skip\r\nA;A\r\n1;2\r\n3;4\r\n"
|
|
123
|
+
table = parse_multi_header_csv_bytes(
|
|
124
|
+
data, skiprows=1, header_rows=1, label_columns=0, value_type=ColumnType.INTEGER
|
|
125
|
+
)
|
|
126
|
+
assert table.schema.names == ["A", "A (2)"]
|
|
127
|
+
assert table.column("A").to_pylist() == [1, 3]
|
|
128
|
+
assert table.column("A (2)").to_pylist() == [2, 4]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def test_multi_header_does_not_fill_padded_cells() -> None:
|
|
132
|
+
# The top header row omits a trailing cell; padding must not forward-fill into it.
|
|
133
|
+
data = b"A;B\r\nx;y;z\r\n1;2;3\r\n"
|
|
134
|
+
table = parse_multi_header_csv_bytes(data, skiprows=0, header_rows=2, label_columns=3, value_type=ColumnType.STRING)
|
|
135
|
+
assert table.schema.names == ["A x", "B y", "z"]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def test_multi_header_dedupe_handles_existing_suffix() -> None:
|
|
139
|
+
data = b"A;A (2);A\r\n1;2;3\r\n"
|
|
140
|
+
table = parse_multi_header_csv_bytes(
|
|
141
|
+
data, skiprows=0, header_rows=1, label_columns=0, value_type=ColumnType.INTEGER
|
|
142
|
+
)
|
|
143
|
+
assert table.schema.names == ["A", "A (2)", "A (3)"]
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def test_multi_header_empty_measure_is_null() -> None:
|
|
147
|
+
data = b"Region;Votes\r\nBerlin;\r\nHamburg;5\r\n"
|
|
148
|
+
table = parse_multi_header_csv_bytes(
|
|
149
|
+
data, skiprows=0, header_rows=1, label_columns=1, value_type=ColumnType.INTEGER
|
|
150
|
+
)
|
|
151
|
+
assert table.column("Votes").to_pylist() == [None, 5]
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def test_berlin_wahl_sample_degenerate_geometry(fixtures_dir: Path) -> None:
|
|
155
|
+
# wahlen-berlin.de Datenexport: single header row, 12 label columns, float values
|
|
156
|
+
# (vote counts and German-decimal percentages share one value type).
|
|
157
|
+
table = parse_multi_header_csv(
|
|
158
|
+
fixtures_dir / "berlin_wahl_sample.csv",
|
|
159
|
+
skiprows=0,
|
|
160
|
+
header_rows=1,
|
|
161
|
+
label_columns=12,
|
|
162
|
+
value_type=ColumnType.FLOAT,
|
|
163
|
+
)
|
|
164
|
+
assert table.num_columns == 268
|
|
165
|
+
assert table.num_rows == 12
|
|
166
|
+
assert table.schema.names[:4] == ["Adresse", "StimmArt", "Bezirk", "Bezirksname"]
|
|
167
|
+
assert table.schema.field("Bezirksname").type == pa.string()
|
|
168
|
+
assert table.schema.field("Gueltig").type == pa.float64()
|
|
169
|
+
first = table.slice(0, 1).to_pylist()[0]
|
|
170
|
+
assert first["Adresse"] == "01W100"
|
|
171
|
+
assert first["Bezirksname"] == "Mitte"
|
|
172
|
+
assert first["Gueltig"] == 428.0
|
|
173
|
+
assert first["Gueltigp"] == 99.3 # German decimal comma parsed as float
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def test_degenerate_geometry_matches_plain_single_header_parse() -> None:
|
|
177
|
+
# header_rows=1 with skiprows=0 is exactly a plain single-header German CSV.
|
|
178
|
+
data = b"Ort;Stimmen\r\nMitte;1.234\r\nPankow;56\r\n"
|
|
179
|
+
table = parse_multi_header_csv_bytes(
|
|
180
|
+
data, skiprows=0, header_rows=1, label_columns=1, value_type=ColumnType.INTEGER
|
|
181
|
+
)
|
|
182
|
+
plain = parse_german_csv_bytes(data, {"Ort": ColumnType.STRING, "Stimmen": ColumnType.INTEGER})
|
|
183
|
+
assert table.equals(plain)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Level 1: property-based round-trip invariants for the German-CSV parser."""
|
|
2
|
+
|
|
3
|
+
from hypothesis import given
|
|
4
|
+
from hypothesis import strategies as st
|
|
5
|
+
|
|
6
|
+
from mloda_plugin_govdata.feature_groups.govdata.core.parse import ColumnType, parse_german_csv_bytes
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _german_thousands(value: int) -> str:
|
|
10
|
+
"""Format an int with '.' as the German thousands separator (e.g. 1234 -> '1.234')."""
|
|
11
|
+
return f"{value:,}".replace(",", ".")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@given(values=st.lists(st.integers(min_value=0, max_value=10_000_000), min_size=1, max_size=40))
|
|
15
|
+
def test_integer_thousands_roundtrip(values: list[int]) -> None:
|
|
16
|
+
lines = ["Wert", *[_german_thousands(v) for v in values]]
|
|
17
|
+
data = ("\r\n".join(lines) + "\r\n").encode("cp1252")
|
|
18
|
+
table = parse_german_csv_bytes(data, {"Wert": ColumnType.INTEGER})
|
|
19
|
+
assert table.column("Wert").to_pylist() == values
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
_LABELS = (
|
|
23
|
+
st.text(
|
|
24
|
+
alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 äöüÄÖÜß.-",
|
|
25
|
+
min_size=1,
|
|
26
|
+
max_size=20,
|
|
27
|
+
)
|
|
28
|
+
.map(str.strip)
|
|
29
|
+
.filter(lambda s: s != "")
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@given(labels=st.lists(_LABELS, min_size=1, max_size=20))
|
|
34
|
+
def test_string_cp1252_roundtrip(labels: list[str]) -> None:
|
|
35
|
+
lines = ["Gebiet", *labels]
|
|
36
|
+
data = ("\r\n".join(lines) + "\r\n").encode("cp1252")
|
|
37
|
+
table = parse_german_csv_bytes(data, {"Gebiet": ColumnType.STRING})
|
|
38
|
+
assert table.column("Gebiet").to_pylist() == labels
|