test(#134): test_wahlperioden.py — Datum→WP-Mapping
12 Tests fuer app/wahlperioden.py: - aktuelle WP fuer Datum >= wahlperiode_start - Vorgaenger-WP fuer Datum davor - None bei unbekanntem BL - Empty/None Datum → aktuelle WP (Default) - Boundary-Tag (= start) gehoert zur neuen WP - ISO-lexikographische Vergleichsannahme stimmt fuer alle BL - all_wahlperioden() enthaelt aktuelle + Vorgaenger pro BL, keine Duplikate
This commit is contained in:
parent
d2fc11f21b
commit
3a8c03db6c
87
tests/test_wahlperioden.py
Normal file
87
tests/test_wahlperioden.py
Normal file
@ -0,0 +1,87 @@
|
||||
"""Tests fuer app/wahlperioden.py — Datum→WP-Mapping fuer Aggregations-Sicht (#58).
|
||||
|
||||
Backfill aus #134.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from app.wahlperioden import wahlperiode_for, all_wahlperioden
|
||||
from app.bundeslaender import BUNDESLAENDER
|
||||
|
||||
|
||||
class TestWahlperiodeFor:
|
||||
def test_returns_current_wp_for_date_after_start(self):
|
||||
bl = BUNDESLAENDER["NRW"]
|
||||
# ein Tag nach Wahlperiode-Start → aktuelle WP
|
||||
# (lexikographische ISO-Vergleich-Grenze)
|
||||
date_after = bl.wahlperiode_start
|
||||
assert wahlperiode_for(date_after, "NRW") == f"NRW-WP{bl.wahlperiode}"
|
||||
|
||||
def test_returns_previous_wp_for_date_before_start(self):
|
||||
bl = BUNDESLAENDER["NRW"]
|
||||
# ein Datum klar vor dem WP-Start
|
||||
assert wahlperiode_for("2010-01-01", "NRW") == f"NRW-WP{bl.wahlperiode - 1}"
|
||||
|
||||
def test_returns_none_for_unknown_bundesland(self):
|
||||
assert wahlperiode_for("2026-03-18", "XX") is None
|
||||
|
||||
def test_empty_datum_falls_back_to_current_wp(self):
|
||||
bl = BUNDESLAENDER["NRW"]
|
||||
assert wahlperiode_for("", "NRW") == f"NRW-WP{bl.wahlperiode}"
|
||||
|
||||
def test_none_datum_falls_back_to_current_wp(self):
|
||||
bl = BUNDESLAENDER["NRW"]
|
||||
# Aufrufer schickt None; der Code prueft `if not datum`
|
||||
assert wahlperiode_for(None, "NRW") == f"NRW-WP{bl.wahlperiode}"
|
||||
|
||||
def test_boundary_date_equals_wp_start(self):
|
||||
"""An der WP-Start-Grenze gehoert der Tag zur neuen WP (>=)."""
|
||||
bl = BUNDESLAENDER["MV"]
|
||||
assert wahlperiode_for(bl.wahlperiode_start, "MV") == f"MV-WP{bl.wahlperiode}"
|
||||
|
||||
def test_doctest_examples(self):
|
||||
"""Die Docstring-Examples muessen halten."""
|
||||
# 2026-03-18 ist nach MV WP8-Start (2021-09-26)
|
||||
assert wahlperiode_for("2026-03-18", "MV") == "MV-WP8"
|
||||
# 2020-01-01 ist davor → WP7
|
||||
assert wahlperiode_for("2020-01-01", "MV") == "MV-WP7"
|
||||
|
||||
def test_lexicographic_iso_date_works(self):
|
||||
"""ISO-Format YYYY-MM-DD vergleicht lexikographisch korrekt."""
|
||||
bl = BUNDESLAENDER["NRW"]
|
||||
start = bl.wahlperiode_start # z.B. "2022-06-01"
|
||||
# Ein Tag davor (gleiches Jahr) gehoert zur Vorgaenger-WP
|
||||
if start[5:7] != "01" or start[8:10] != "01":
|
||||
# nicht 1. Januar — Day-1 Test einfach moeglich
|
||||
year, month, day = int(start[:4]), int(start[5:7]), int(start[8:10])
|
||||
if day > 1:
|
||||
day_before = f"{year:04d}-{month:02d}-{day-1:02d}"
|
||||
else:
|
||||
day_before = f"{year:04d}-{month-1:02d}-28"
|
||||
assert wahlperiode_for(day_before, "NRW") == f"NRW-WP{bl.wahlperiode - 1}"
|
||||
|
||||
|
||||
class TestAllWahlperioden:
|
||||
def test_includes_each_bundesland(self):
|
||||
all_wp = all_wahlperioden()
|
||||
# pro BL zwei Eintraege (current + previous)
|
||||
assert len(all_wp) == len(BUNDESLAENDER) * 2
|
||||
|
||||
def test_format_is_BL_WPn(self):
|
||||
for entry in all_wahlperioden():
|
||||
parts = entry.split("-WP")
|
||||
assert len(parts) == 2, entry
|
||||
bl_code, wp_num = parts
|
||||
assert bl_code in BUNDESLAENDER, bl_code
|
||||
assert wp_num.isdigit(), wp_num
|
||||
|
||||
def test_no_duplicates(self):
|
||||
all_wp = all_wahlperioden()
|
||||
assert len(all_wp) == len(set(all_wp))
|
||||
|
||||
def test_contains_known_examples(self):
|
||||
all_wp = all_wahlperioden()
|
||||
# NRW WP18 + 17 muessen drin sein
|
||||
assert "NRW-WP18" in all_wp
|
||||
assert "NRW-WP17" in all_wp
|
||||
Loading…
Reference in New Issue
Block a user