forked from eth-library/naif
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_common.py
More file actions
249 lines (175 loc) · 7.53 KB
/
test_common.py
File metadata and controls
249 lines (175 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""Tests for dashboards/_common.py shared utilities."""
from __future__ import annotations
import importlib.util
import os
import zipfile
from pathlib import Path
import pandas as pd
import pytest
COMMON_PATH = Path(__file__).resolve().parent.parent / "dashboards" / "_common.py"
COMMON_SPEC = importlib.util.spec_from_file_location("dashboards_common", COMMON_PATH)
assert COMMON_SPEC is not None and COMMON_SPEC.loader is not None
common = importlib.util.module_from_spec(COMMON_SPEC)
COMMON_SPEC.loader.exec_module(common)
DATA_DIR = common.DATA_DIR
TYPE_LABELS = common.TYPE_LABELS
TYPE_ORDER = common.TYPE_ORDER
format_identifier = common.format_identifier
format_iso_date = common.format_iso_date
format_plain_text = common.format_plain_text
format_type_label = common.format_type_label
load_hei_changelog = common.load_hei_changelog
latest_hei_changelog_date = common.latest_hei_changelog_date
make_link = common.make_link
normalise_yes_no = common.normalise_yes_no
pct = common.pct
render_table = common.render_table
type_order_key = common.type_order_key
ensure_csv_xlsx_export = common.ensure_csv_xlsx_export
ensure_hei_xlsx_export = common.ensure_hei_xlsx_export
write_dataframe_xlsx = common.write_dataframe_xlsx
# ---------------------------------------------------------------------------
# DATA_DIR
# ---------------------------------------------------------------------------
def test_data_dir_exists() -> None:
assert DATA_DIR.is_dir(), f"Expected data directory at {DATA_DIR}"
# ---------------------------------------------------------------------------
# normalise_yes_no
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
("raw", "expected"),
[
("Yes", "Yes"),
("yes", "Yes"),
("TRUE", "Yes"),
("1", "Yes"),
("No", "No"),
("no", "No"),
("false", "No"),
("0", "No"),
("", "Unknown"),
("maybe", "Unknown"),
(None, "Unknown"),
(float("nan"), "Unknown"),
],
)
def test_normalise_yes_no(raw: object, expected: str) -> None:
assert normalise_yes_no(raw) == expected
# ---------------------------------------------------------------------------
# format_identifier
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
("raw", "expected"),
[
(None, ""),
(float("nan"), ""),
("", ""),
(" ", ""),
("42.0", "42"),
("hello", "hello"),
("3.14", "3.14"),
("Q12345", "Q12345"),
],
)
def test_format_identifier(raw: object, expected: str) -> None:
assert format_identifier(raw) == expected
def test_format_iso_date() -> None:
assert format_iso_date("2026-04-22") == "22 April 2026"
# ---------------------------------------------------------------------------
# format_plain_text
# ---------------------------------------------------------------------------
def test_format_plain_text_with_value() -> None:
assert format_plain_text("Zurich") == "Zurich"
def test_format_plain_text_empty() -> None:
assert format_plain_text(None) == "-"
# ---------------------------------------------------------------------------
# format_type_label
# ---------------------------------------------------------------------------
def test_format_type_label_default() -> None:
assert format_type_label("UAS") == TYPE_LABELS["UAS"]
def test_format_type_label_custom() -> None:
custom = {"UAS": "Custom label"}
assert format_type_label("UAS", labels=custom) == "Custom label"
def test_format_type_label_unknown() -> None:
assert format_type_label(None) == "Unknown"
# ---------------------------------------------------------------------------
# type_order_key
# ---------------------------------------------------------------------------
def test_type_order_key_known() -> None:
assert type_order_key("UAS") == TYPE_ORDER.index("UAS")
def test_type_order_key_unknown() -> None:
assert type_order_key("Unknown") == len(TYPE_ORDER)
def test_type_order_key_custom() -> None:
custom = ["A", "B"]
assert type_order_key("B", order=custom) == 1
assert type_order_key("C", order=custom) == 2
# ---------------------------------------------------------------------------
# pct
# ---------------------------------------------------------------------------
def test_pct_normal() -> None:
assert pct(1, 4) == "25.0%"
def test_pct_zero_denominator() -> None:
assert pct(5, 0) == "0.0%"
# ---------------------------------------------------------------------------
# make_link
# ---------------------------------------------------------------------------
def test_make_link_escapes() -> None:
result = make_link("https://example.com", "A & B")
assert "A & B" in result
assert 'target="_blank"' in result
assert 'rel="noopener noreferrer"' in result
# ---------------------------------------------------------------------------
# render_table
# ---------------------------------------------------------------------------
def test_render_table_html() -> None:
df = pd.DataFrame({"A": [1], "B": [2]})
html_str = render_table(df)
assert "<table" in html_str
assert "table-sm" in html_str
def test_load_hei_changelog_not_empty() -> None:
entries = load_hei_changelog()
assert isinstance(entries, list)
assert entries, "Expected at least one HEI changelog entry"
def test_latest_hei_changelog_date() -> None:
assert latest_hei_changelog_date() == "20 May 2026"
# ---------------------------------------------------------------------------
# write_dataframe_xlsx
# ---------------------------------------------------------------------------
def test_write_dataframe_xlsx(tmp_path: Path) -> None:
df = pd.DataFrame({"name": ["A", "B"], "value": ["1", "2"]})
out = tmp_path / "test.xlsx"
write_dataframe_xlsx(df, out, sheet_name="Sheet")
assert out.exists()
assert out.stat().st_size > 0
def test_ensure_csv_xlsx_export_creates_missing_workbook(tmp_path: Path) -> None:
csv_path = tmp_path / "source.csv"
xlsx_path = tmp_path / "source.xlsx"
pd.DataFrame({"name": ["A"], "value": ["1"]}).to_csv(csv_path, index=False)
result = ensure_csv_xlsx_export(csv_path, xlsx_path, sheet_name="Sheet")
assert result == xlsx_path
assert xlsx_path.exists()
with zipfile.ZipFile(xlsx_path) as workbook:
shared_strings = workbook.read("xl/sharedStrings.xml").decode("utf-8")
assert "name" in shared_strings
assert "value" in shared_strings
assert ">A<" in shared_strings
assert ">1<" in shared_strings
def test_ensure_csv_xlsx_export_refreshes_stale_workbook(tmp_path: Path) -> None:
csv_path = tmp_path / "source.csv"
xlsx_path = tmp_path / "source.xlsx"
pd.DataFrame({"name": ["Original"]}).to_csv(csv_path, index=False)
write_dataframe_xlsx(
pd.DataFrame({"name": ["Stale"]}), xlsx_path, sheet_name="Sheet"
)
pd.DataFrame({"name": ["Fresh"]}).to_csv(csv_path, index=False)
stale_time = csv_path.stat().st_mtime - 10
os.utime(xlsx_path, (stale_time, stale_time))
previous_mtime = xlsx_path.stat().st_mtime
ensure_csv_xlsx_export(csv_path, xlsx_path, sheet_name="Sheet")
assert xlsx_path.stat().st_mtime > previous_mtime
with zipfile.ZipFile(xlsx_path) as workbook:
shared_strings = workbook.read("xl/sharedStrings.xml").decode("utf-8")
assert ">Fresh<" in shared_strings
assert "Stale" not in shared_strings
def test_ensure_hei_xlsx_export_returns_expected_path() -> None:
assert ensure_hei_xlsx_export() == DATA_DIR / "hei.xlsx"