forked from emmericp/marktstammdatenplotter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.py
More file actions
310 lines (251 loc) · 10.2 KB
/
test_parser.py
File metadata and controls
310 lines (251 loc) · 10.2 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Unit tests for the parser module.
Run: pixi run pytest
pixi run python -m pytest tests/ -v
"""
from __future__ import annotations
import sys
from datetime import datetime, timezone
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import parser as mp # noqa: E402
# ---------------------------------------------------------------------------
# parse_dotnet_date
# ---------------------------------------------------------------------------
class TestParseDotnetDate:
def test_valid_epoch_zero(self):
assert mp.parse_dotnet_date("/Date(0)/") == datetime(
1970, 1, 1, tzinfo=timezone.utc
)
def test_known_date(self):
# 2020-01-01 00:00:00 UTC = 1577836800000 ms
got = mp.parse_dotnet_date("/Date(1577836800000)/")
assert got == datetime(2020, 1, 1, tzinfo=timezone.utc)
def test_subsecond_precision(self):
got = mp.parse_dotnet_date("/Date(1577836800500)/")
assert got.year == 2020 and got.microsecond == 500_000
@pytest.mark.parametrize("bad", [None, "", "2020-01-01", "garbage"])
def test_invalid_returns_none(self, bad):
assert mp.parse_dotnet_date(bad) is None
def test_returns_tz_aware(self):
got = mp.parse_dotnet_date("/Date(1577836800000)/")
assert got is not None and got.tzinfo is timezone.utc
# ---------------------------------------------------------------------------
# Enum decoding (PowerPlant.from_json)
# ---------------------------------------------------------------------------
BASE_ENTRY = {
"Id": 1,
"Nettonennleistung": 100.0,
"Leistungsbegrenzung": 802,
"HauptausrichtungSolarModule": None,
"HauptneigungswinkelSolarmodule": None,
"ArtDerSolaranlageId": None,
"Bruttoleistung": 200.0,
"AnzahlSolarModule": None,
"NutzungsbereichGebSA": None,
"WindAnLandOderSeeId": None,
"StandortAnonymisiert": "",
"InbetriebnahmeDatum": "/Date(1577836800000)/",
"EndgueltigeStilllegungDatum": None,
"Plz": "10115",
"AnlagenbetreiberPersonenArt": 0,
"AnlagenbetreiberName": "Test GmbH",
"EnergietraegerName": "Solare Strahlungsenergie",
"Laengengrad": 13.4,
"Breitengrad": 52.5,
}
def make_entry(**overrides):
e = dict(BASE_ENTRY)
e.update(overrides)
return e
class TestLeistungsbegrenzung:
@pytest.mark.parametrize(
"code,expected_factor",
[(805, 0.5), (804, 0.6), (803, 0.7), (802, 1.0), (1535, 1.0), (9999, 1.0)],
)
def test_inverter_factor(self, code, expected_factor):
pp = mp.PowerPlant.from_json(make_entry(Leistungsbegrenzung=code))
assert pp.inverter == pytest.approx(100.0 * expected_factor)
class TestFacing:
@pytest.mark.parametrize(
"code,expected",
[
(695, 0), (696, 45), (697, 90), (698, 135), (699, 180),
(700, 225), (701, 270), (702, 315),
(703, "tracked"), (704, "east-west"),
],
)
def test_known_codes(self, code, expected):
pp = mp.PowerPlant.from_json(make_entry(HauptausrichtungSolarModule=code))
assert pp.facing == expected
def test_unknown_returns_none(self):
pp = mp.PowerPlant.from_json(make_entry(HauptausrichtungSolarModule=42))
assert pp.facing is None
class TestTilt:
@pytest.mark.parametrize(
"code,expected",
[
(810, (0, 19)), (809, (20, 40)), (808, (40, 60)),
(807, (61, 90)), (806, 90), (811, "tracked"),
],
)
def test_known_codes(self, code, expected):
pp = mp.PowerPlant.from_json(make_entry(HauptneigungswinkelSolarmodule=code))
assert pp.tilt == expected
def test_unknown_returns_none(self):
pp = mp.PowerPlant.from_json(make_entry(HauptneigungswinkelSolarmodule=99))
assert pp.tilt is None
class TestInstallationType:
@pytest.mark.parametrize(
"code,expected",
[
(853, "building"), (2484, "building_other"), (852, "free"),
(3002, "water"), (3058, "parking_lot"), (2961, "balkonkraftwerk"),
],
)
def test_known_codes(self, code, expected):
pp = mp.PowerPlant.from_json(make_entry(ArtDerSolaranlageId=code))
assert pp.installation_type == expected
class TestBuildingType:
@pytest.mark.parametrize(
"code,expected",
[
(713, "household"), (714, "commercial"), (715, "industry"),
(716, "farming"), (717, "public"), (718, "other"),
],
)
def test_known_codes(self, code, expected):
pp = mp.PowerPlant.from_json(make_entry(NutzungsbereichGebSA=code))
assert pp.building_type == expected
class TestOffshore:
def test_nordsee(self):
pp = mp.PowerPlant.from_json(make_entry(
WindAnLandOderSeeId=889, StandortAnonymisiert="Nordsee bei Borkum",
))
assert pp.off_shore == "Nordsee"
def test_ostsee(self):
pp = mp.PowerPlant.from_json(make_entry(
WindAnLandOderSeeId=889, StandortAnonymisiert="Ostsee bei Rügen",
))
assert pp.off_shore == "Ostsee"
def test_onshore_returns_none(self):
pp = mp.PowerPlant.from_json(make_entry(
WindAnLandOderSeeId=888, StandortAnonymisiert="Niedersachsen",
))
assert pp.off_shore is None
class TestPanelDropoutHeuristic:
def test_small_panel_ratio_drops_to_none(self):
# 100 kW for 10 panels = 10 kW/panel — too good, drop count.
# 100 kW for 1001 panels = 0.1 kW/panel exactly — also drops.
pp = mp.PowerPlant.from_json(make_entry(
Bruttoleistung=100.0, AnzahlSolarModule=1001,
))
assert pp.num_panels is None
def test_normal_panel_ratio_kept(self):
# 100 kW for 250 panels = 0.4 kW/panel — realistic, kept.
pp = mp.PowerPlant.from_json(make_entry(
Bruttoleistung=100.0, AnzahlSolarModule=250,
))
assert pp.num_panels == 250
class TestPrivateFlag:
def test_private_code_518(self):
pp = mp.PowerPlant.from_json(make_entry(AnlagenbetreiberPersonenArt=518))
assert pp.is_private is True
def test_other_code_is_not_private(self):
pp = mp.PowerPlant.from_json(make_entry(AnlagenbetreiberPersonenArt=1))
assert pp.is_private is False
class TestBessSector:
"""mastr_plot.bess_sector — battery-charts.de classification."""
def setup_method(self):
import importlib, sys
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
self.mp_mod = importlib.import_module("mastr_plot")
def test_hss_bounds(self):
assert self.mp_mod.bess_sector(5) == "HSS (<30 kWh)"
assert self.mp_mod.bess_sector(29.99) == "HSS (<30 kWh)"
def test_css_bounds(self):
assert self.mp_mod.bess_sector(30) == "CSS (30 kWh – 1 MWh)"
assert self.mp_mod.bess_sector(999.99) == "CSS (30 kWh – 1 MWh)"
def test_lss_bounds(self):
assert self.mp_mod.bess_sector(1000) == "LSS (≥1 MWh)"
assert self.mp_mod.bess_sector(4_000_000) == "LSS (≥1 MWh)"
def test_unknown_for_missing_or_zero(self):
assert self.mp_mod.bess_sector(None) == "unknown"
assert self.mp_mod.bess_sector(0) == "unknown"
assert self.mp_mod.bess_sector(-5) == "unknown"
class TestBatteryUnit:
"""BatteryUnit.from_json — Energieträger 2496 only."""
BESS_BASE = {
"Id": 42,
"EinheitName": "Test Battery",
"EnergietraegerId": 2496,
"EnergietraegerName": "Speicher",
"Bruttoleistung": 1000.0,
"Nettonennleistung": 1000.0,
"NutzbareSpeicherkapazitaet": 2000.0,
"InbetriebnahmeDatum": "/Date(1577836800000)/", # 2020-01-01 UTC
"GeplantesInbetriebsnahmeDatum": None,
"EndgueltigeStilllegungDatum": None,
"Stromspeichertechnologie": 524,
"StromspeichertechnologieBezeichnung": "Batterie",
"Batterietechnologie": 727,
"VollTeilEinspeisungBezeichnung": "Volleinspeisung",
"BetriebsStatusName": "In Betrieb",
"SpannungsebenenNamen": "Mittelspannung",
"Laengengrad": 13.4,
"Breitengrad": 52.5,
"Plz": "10115",
"Gemeinde": "Testgemeinde",
"Landkreis": "Test-Kreis",
"Bundesland": "Berlin",
"AnlagenbetreiberName": "Test Battery GmbH",
"AnlagenbetreiberPersonenArt": 517,
}
def test_skips_non_bess_records(self):
e = dict(self.BESS_BASE)
e["EnergietraegerId"] = 2497 # Wind
assert mp.BatteryUnit.from_json(e) is None
def test_round_trip(self):
u = mp.BatteryUnit.from_json(dict(self.BESS_BASE))
assert u is not None
assert u.id == 42
assert u.power_kw == 1000.0
assert u.energy_kwh == 2000.0
assert u.storage_tech == "Batterie"
assert u.battery_tech_code == 727
assert u.feed_in_mode == "Volleinspeisung"
assert u.status == "In Betrieb"
assert u.voltage_level == "Mittelspannung"
assert u.landkreis == "Test-Kreis"
assert u.bundesland == "Berlin"
assert u.is_private is False
def test_planned_only_record(self):
e = dict(self.BESS_BASE)
e["InbetriebnahmeDatum"] = None
e["GeplantesInbetriebsnahmeDatum"] = "/Date(1820000000000)/"
u = mp.BatteryUnit.from_json(e)
assert u.install_date is None
assert u.planned_date is not None
def test_private_owner_flag(self):
e = dict(self.BESS_BASE)
e["AnlagenbetreiberPersonenArt"] = 518
u = mp.BatteryUnit.from_json(e)
assert u.is_private is True
def test_missing_capacity_defaults_to_zero(self):
e = dict(self.BESS_BASE)
e["NutzbareSpeicherkapazitaet"] = None
e["Bruttoleistung"] = None
u = mp.BatteryUnit.from_json(e)
assert u.power_kw == 0.0
assert u.energy_kwh == 0.0
class TestCoreFields:
def test_round_trip(self):
pp = mp.PowerPlant.from_json(make_entry())
assert pp.id == 1
assert pp.power == 200.0
assert pp.postal_code == "10115"
assert pp.energy_type == "Solare Strahlungsenergie"
assert pp.longitude == 13.4
assert pp.latitude == 52.5
assert pp.install_date == datetime(2020, 1, 1, tzinfo=timezone.utc)
assert pp.removal_date is None