Files
marco.morath 42dd1f2d4a
CI / audit (push) Skipped
CI / checks (push) Failing after 37s
CI / image (push) Skipped
Initial Commit: Status Testing
2026-11-25 18:46:04 +01:00

265 lines
11 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""Fragengruppen und Teilnahme mit einem Eintrag (ohne Datenbank)."""
from __future__ import annotations
from dataclasses import dataclass, field
from decimal import Decimal
from typing import Any
from zoneinfo import ZoneInfo
import pytest
from surveybox.forms import conditions, groups, types
TZ = ZoneInfo("Europe/Berlin")
@dataclass
class Q:
id: int
type: str
label: str = ""
config: dict[str, Any] = field(default_factory=dict)
position: int = 0
required: bool = False
def slot(qid: int, label: str, capacity: int | None = None, **extra: Any) -> Q:
option: dict[str, Any] = {"id": types.SINGLE_SLOT, "label": label}
if capacity:
option["capacity"] = capacity
return Q(qid, "slots", label, {"single": True, "options": [option], **extra})
def numbered(qs: list[Q]) -> list[Q]:
for index, q in enumerate(qs):
q.position = index
return qs
def shifts() -> list[Q]:
return numbered([
Q(1, "yes_no", "Helfen Sie mit?"),
Q(2, groups.GROUP, "Schichten", {"member_type": "slots", "max_select": 2}),
slot(3, "Fr früh", 3),
slot(4, "Fr spät", 3),
slot(5, "Sa früh"),
Q(6, "short_text", "Bemerkung"),
slot(7, "Einzeln"), # nach einer anderen Frage: nicht mehr in der Gruppe
])
# --------------------------------------------------------------------------
# Zugehörigkeit
# --------------------------------------------------------------------------
def test_members_are_following_questions_of_the_type() -> None:
qs = shifts()
assert [m.id for m in groups.resolve(qs)[2]] == [3, 4, 5]
assert groups.member_of(qs) == {3: 2, 4: 2, 5: 2}
def test_heading_or_other_group_ends_a_group() -> None:
qs = numbered([
Q(1, groups.GROUP, "A", {"member_type": "yes_no"}),
Q(2, "yes_no", "a"),
Q(3, "heading", "H"),
Q(4, "yes_no", "b"),
Q(5, groups.GROUP, "B", {"member_type": "yes_no"}),
Q(6, groups.GROUP, "C", {"member_type": "yes_no"}),
Q(7, "yes_no", "c"),
])
found = groups.resolve(qs)
assert [m.id for m in found[1]] == [2]
assert found[5] == []
assert [m.id for m in found[6]] == [7]
def test_legacy_slot_lists_do_not_join_a_group() -> None:
legacy = Q(3, "slots", "Alt", {"options": [{"id": "aaaaaa", "label": "x"},
{"id": "bbbbbb", "label": "y"}]})
qs = numbered([Q(1, groups.GROUP, "G", {"member_type": "slots"}), slot(2, "neu"), legacy])
assert [m.id for m in groups.resolve(qs)[1]] == [2]
def test_members_must_match_the_first_one() -> None:
a = {"options": [{"id": "aaaaaa", "label": "Rot"}, {"id": "bbbbbb", "label": "Grün"}]}
b = {"options": [{"id": "cccccc", "label": "Rot"}, {"id": "dddddd", "label": "Grün"}]}
c = {"options": [{"id": "eeeeee", "label": "Blau"}, {"id": "ffffff", "label": "Grün"}]}
qs = numbered([
Q(1, groups.GROUP, "M", {"member_type": "single_choice"}),
Q(2, "single_choice", "x", a), Q(3, "single_choice", "y", b), Q(4, "single_choice", "z", c),
Q(5, groups.GROUP, "R", {"member_type": "rating"}),
Q(6, "rating", "r1", {"scale_max": 5}), Q(7, "rating", "r2", {"scale_max": 7}),
Q(8, groups.GROUP, "J", {"member_type": "yes_no"}),
Q(9, "yes_no", "j1"), Q(10, "yes_no", "j2", {"label_yes": "Gern"}),
])
found = groups.resolve(qs)
assert [m.id for m in found[1]] == [2, 3]
assert [m.id for m in found[5]] == [6]
assert [m.id for m in found[8]] == [9]
def test_choice_with_other_cannot_be_grouped() -> None:
cfg = {"allow_other": True, "options": [{"id": "aaaaaa", "label": "x"}]}
qs = numbered([Q(1, groups.GROUP, "M", {"member_type": "single_choice"}),
Q(2, "single_choice", "x", cfg)])
assert groups.resolve(qs)[1] == []
def test_max_select_only_for_counting_types() -> None:
def group(**cfg: Any) -> Q:
return Q(1, groups.GROUP, config=cfg)
assert groups.max_select(group(member_type="slots", max_select=2)) == 2
assert groups.max_select(group(member_type="rating", max_select=2)) == 0
assert groups.max_select(group(member_type="slots")) == 0
assert groups.min_select(group(member_type="yes_no", min_select=1)) == 1
assert groups.min_select(group(member_type="rating", min_select=1)) == 0
# --------------------------------------------------------------------------
# Höchstzahl
# --------------------------------------------------------------------------
def booked() -> dict[str, Any]:
return {"value_json": {"selected": [types.SINGLE_SLOT]}}
def test_too_many_counts_bookings() -> None:
qs = shifts()
check = groups.count_errors
assert check(qs, {3: booked(), 4: booked()}, set()) == {}
assert check(qs, {3: booked(), 4: booked(), 5: booked()}, set()) == {2: "group_too_many"}
# Ausgeblendete Fragen und Gruppen zählen nicht
assert check(qs, {3: booked(), 4: booked(), 5: booked()}, {5}) == {}
assert check(qs, {3: booked(), 4: booked(), 5: booked()}, {2}) == {}
# Fragen außerhalb der Gruppe zählen nicht
assert check(qs, {3: booked(), 4: booked(), 7: booked()}, set()) == {}
def test_too_many_counts_yes_only() -> None:
qs = numbered([Q(1, groups.GROUP, "J", {"member_type": "yes_no", "max_select": 1}),
Q(2, "yes_no", "a"), Q(3, "yes_no", "b")])
yes, no = {"value_bool": True}, {"value_bool": False}
assert groups.count_errors(qs, {2: yes, 3: no}, set()) == {}
assert groups.count_errors(qs, {2: yes, 3: yes}, set()) == {1: "group_too_many"}
def test_min_select() -> None:
qs = shifts()
qs[1].config.update(min_select=1, max_select=1)
assert groups.count_errors(qs, {}, set()) == {2: "group_too_few"}
assert groups.count_errors(qs, {4: booked()}, set()) == {}
# Weniger sichtbare Zeilen als die Mindestzahl: dann zählen alle sichtbaren
qs[1].config.update(min_select=3, max_select=0)
assert groups.count_errors(qs, {3: booked(), 4: booked()}, {5}) == {}
assert groups.count_errors(qs, {3: booked()}, {5}) == {2: "group_too_few"}
# Ausgeblendete Gruppe verlangt nichts
assert groups.count_errors(qs, {}, {2, 3, 4, 5}) == {}
def test_required_rows_in_slot_group_are_ruled_by_the_group() -> None:
qs = shifts()
for q in qs[2:5]:
q.required = True # drei Pflicht-Schichten, höchstens eine wählbar
ruled = groups.required_by_group(qs)
assert ruled == {3, 4, 5}
# Die einzelne Schicht ist dann keine Pflicht mehr
assert types.parse_answer(qs[2], {}, TZ, required=False).error is None
assert types.parse_answer(qs[2], {}, TZ).error == "required"
# Ja/Nein-Zeilen behalten ihre Pflicht
yn = numbered([Q(1, groups.GROUP, "J", {"member_type": "yes_no"}), Q(2, "yes_no", "a")])
assert groups.required_by_group(yn) == set()
# --------------------------------------------------------------------------
# Bedingungen und Aufbau der Seite
# --------------------------------------------------------------------------
def test_condition_on_group_hides_all_members() -> None:
qs = shifts()
qs[1].config["condition"] = {"action": "show", "match": "all",
"rules": [{"q": 1, "op": "eq", "value": "yes"}]}
assert conditions.hidden_ids(qs, {"q1": ["no"]}) == {2, 3, 4, 5}
assert conditions.hidden_ids(qs, {"q1": ["yes"]}) == set()
def test_member_keeps_its_own_condition() -> None:
qs = shifts()
qs[3].config["condition"] = {"action": "show", "match": "all",
"rules": [{"q": 3, "op": "contains", "value": "single"}]}
assert conditions.hidden_ids(qs, {}) == {4}
assert conditions.hidden_ids(qs, {"q3": ["single"]}) == set()
def test_layout_shows_group_instead_of_members() -> None:
qs = shifts()
items = conditions.layout(qs)[0]["sections"][0]["items"]
assert [q.id for q in items] == [1, 2, 6, 7]
def test_single_slot_rule_option_is_signed_up() -> None:
assert conditions.options_for_rules(slot(1, "x")) == [(types.SINGLE_SLOT, "signed up")]
# --------------------------------------------------------------------------
# Einstellungen und Antworten
# --------------------------------------------------------------------------
def test_group_config() -> None:
cfg = types.parse_config(groups.GROUP, {"member_type": ["slots"], "max_select": ["3"],
"min_select": ["1"]})
assert cfg == {"member_type": "slots", "min_select": 1, "max_select": 3}
cfg = types.parse_config(groups.GROUP, {"member_type": ["rating"], "max_select": ["3"]})
assert cfg == {"member_type": "rating", "min_select": 0, "max_select": 0}
with pytest.raises(types.ConfigError):
types.parse_config(groups.GROUP, {"member_type": ["slots"], "min_select": ["2"],
"max_select": ["1"]})
with pytest.raises(types.ConfigError):
types.parse_config(groups.GROUP, {"member_type": ["poll"]})
with pytest.raises(types.ConfigError):
types.parse_config(groups.GROUP, {"member_type": ["slots"], "max_select": ["-1"]})
def test_single_slot_config_keeps_option_id() -> None:
cfg = types.parse_config("slots", {"label": ["Fr früh"], "capacity": ["4"],
"waitlist": ["on"]})
assert cfg["single"] is True
assert cfg["options"] == [{"id": types.SINGLE_SLOT, "label": "Fr früh", "capacity": 4}]
assert cfg["waitlist"] is True
unlimited = types.parse_config("slots", {"label": ["x"], "capacity": [""]})
assert "capacity" not in unlimited["options"][0]
with pytest.raises(types.ConfigError):
types.parse_config("slots", {"label": ["x"], "capacity": ["0"]})
def test_slot_list_mode_still_works() -> None:
cfg = types.parse_config("slots", {"slot_mode": ["list"], "option_label": ["A", "B"],
"option_id": ["", ""], "option_capacity": ["2", ""]})
assert "single" not in cfg
assert [o["label"] for o in cfg["options"]] == ["A", "B"]
def test_single_slot_answer() -> None:
q = slot(5, "Fr früh", 2)
result = types.parse_answer(q, {"q5": ["single"]}, TZ)
assert result.stored == {"value_json": {"selected": ["single"]}}
assert types.parse_answer(q, {}, TZ).stored is None
assert types.parse_answer(q, {"q5": ["other"]}, TZ).error == "choice"
assert types.display_answer(q, result.stored or {}, TZ) == "signed up"
assert types.display_answer(q, {}, TZ) == ""
assert types.raw_from_answer(q, result.stored or {}, TZ) == {"q5": ["single"]}
def test_group_has_no_answer() -> None:
assert not types.TYPES[groups.GROUP].is_input
assert types.parse_answer(Q(1, groups.GROUP), {"q1": ["x"]}, TZ).stored is None
assert types.parse_answer(Q(2, "rating", config={"scale_max": 5}), {"q2": ["3"]},
TZ).stored == {"value_number": Decimal(3)}