# SPDX-License-Identifier: AGPL-3.0-or-later """Bedingungen: Auswertung, Designer-Eingaben, Pflege (ohne Datenbank).""" from __future__ import annotations from dataclasses import dataclass, field from typing import Any import pytest from surveybox.forms import conditions as c @dataclass class Q: id: int type: str label: str = "" config: dict[str, Any] = field(default_factory=dict) position: int = 0 required: bool = False def form() -> list[Q]: qs = [ Q(1, "yes_no", "Kommen Sie?"), Q(2, "number", "Wie viele?", {"decimals": 0}), Q(3, "single_choice", "Essen", {"options": [{"id": "veg01", "label": "Vegetarisch"}, {"id": "mea01", "label": "Fleisch"}]}), Q(4, "multiple_choice", "Extras", {"options": [{"id": "dri01", "label": "Getränke"}, {"id": "des01", "label": "Nachtisch"}]}), Q(5, "short_text", "Unverträglichkeiten"), Q(6, "page_break", "Seite 2"), Q(7, "heading", "Übernachtung"), Q(8, "date", "Anreise"), Q(9, "file_upload", "Nachweis"), ] for index, q in enumerate(qs): q.position = index return qs def rule(q: int, op: str, value: str = "") -> dict[str, Any]: return {"q": q, "op": op, "value": value} def cond(*rules: dict[str, Any], action: str = "show", match: str = "all") -> dict[str, Any]: return {"action": action, "match": match, "rules": list(rules)} def test_show_when_yes_and_hidden_answers_count_as_empty() -> None: qs = form() qs[1].config["condition"] = cond(rule(1, "eq", "yes")) qs[2].config["condition"] = cond(rule(2, "gt", "2")) # Nein: Frage 2 aus, also gilt ihr Wert als leer, also auch Frage 3 aus hidden = c.hidden_ids(qs, {"q1": ["no"], "q2": ["5"]}) assert {2, 3} <= hidden hidden = c.hidden_ids(qs, {"q1": ["yes"], "q2": ["5"]}) assert not {2, 3} & hidden hidden = c.hidden_ids(qs, {"q1": ["yes"], "q2": ["2"]}) assert 3 in hidden and 2 not in hidden def test_page_and_section_conditions() -> None: qs = form() qs[5].config["condition"] = cond(rule(1, "eq", "yes")) # ganze Seite 2 qs[6].config["condition"] = cond(rule(4, "contains", "des01"), action="hide") hidden = c.hidden_ids(qs, {"q1": ["no"]}) assert {6, 7, 8, 9} <= hidden hidden = c.hidden_ids(qs, {"q1": ["yes"], "q4": ["dri01", "des01"]}) assert 6 not in hidden and {7, 8, 9} <= hidden hidden = c.hidden_ids(qs, {"q1": ["yes"], "q4": ["dri01"]}) assert not {6, 7, 8, 9} & hidden @pytest.mark.parametrize( ("question", "rule_", "fields", "expected"), [ (5, rule(5, "contains", "NUSS"), {"q5": ["Haselnuss, Gluten"]}, True), (5, rule(5, "eq", "gluten"), {"q5": [" Gluten "]}, True), (5, rule(5, "not_contains", "nuss"), {"q5": [""]}, True), (5, rule(5, "empty"), {"q5": [" "]}, True), (2, rule(2, "lt", "3"), {"q2": ["2,0"]}, True), (2, rule(2, "eq", "3"), {"q2": ["abc"]}, False), (2, rule(2, "ne", "3"), {}, True), (3, rule(3, "ne", "veg01"), {"q3": ["mea01"]}, True), (4, rule(4, "not_contains", "des01"), {"q4": ["dri01"]}, True), (8, rule(8, "gt", "2026-07-01"), {"q8": ["2026-07-02"]}, True), (8, rule(8, "lt", "2026-07-01"), {"q8": [""]}, False), ], ) def test_operators(question: int, rule_: dict[str, Any], fields: dict[str, list[str]], expected: bool) -> None: qs = form() target = Q(99, "info", "Ziel", {"condition": cond(rule_)}, position=100) assert (99 not in c.hidden_ids([*qs, target], fields)) is expected def test_files_and_any_match() -> None: qs = form() target = Q(99, "info", "Ziel", {"condition": cond(rule(9, "not_empty"), rule(1, "eq", "no"), match="any")}, position=100) assert 99 in c.hidden_ids([*qs, target], {"q1": ["yes"]}) assert 99 not in c.hidden_ids([*qs, target], {"q1": ["yes"]}, with_files={9}) def test_parse_from_designer() -> None: qs = form() question = qs[7] # Anreise, Position 7 fields = { "cond_action": ["show"], "cond_match": ["any"], "rule_q": ["1", "3", "2", ""], "rule_op": ["eq", "ne", "gt", "eq"], "rule_text": ["", "", "1,50", ""], "rule_option": ["yes", "veg01", "", ""], } parsed = c.parse(question, fields, qs) assert parsed == { "action": "show", "match": "any", "rules": [rule(1, "eq", "yes"), rule(3, "ne", "veg01"), rule(2, "gt", "1.5")], } assert c.parse(question, {"rule_q": [""]}, qs) is None @pytest.mark.parametrize( ("fields", "code"), [ ({"rule_q": ["9"], "rule_op": ["empty"]}, "condition_ref_invalid"), # spätere Frage ({"rule_q": ["7"], "rule_op": ["empty"]}, "condition_ref_invalid"), # keine Eingabe ({"rule_q": ["1"], "rule_op": ["gt"], "rule_option": ["yes"]}, "condition_op_invalid"), ({"rule_q": ["3"], "rule_op": ["eq"], "rule_option": ["xxx"]}, "condition_value_invalid"), ({"rule_q": ["2"], "rule_op": ["eq"], "rule_text": ["viele"]}, "condition_value_invalid"), ], ) def test_parse_rejects(fields: dict[str, list[str]], code: str) -> None: qs = form() with pytest.raises(c.ConditionError) as exc: c.parse(qs[7], fields, qs) assert exc.value.code == code def test_order_delete_and_copy_helpers() -> None: qs = form() qs[4].config["condition"] = cond(rule(1, "eq", "yes"), rule(3, "eq", "veg01")) assert c.order_violations(qs) == [] reordered = [qs[4], *qs[:4], *qs[5:]] assert c.order_violations(reordered) == [5] cleaned = c.without_reference(qs[4].config, 3) assert cleaned is not None and cleaned["condition"]["rules"] == [rule(1, "eq", "yes")] assert c.without_reference(qs[4].config, 8) is None assert "condition" not in c.remap(qs[4].config, {}) assert c.remap(qs[4].config, {1: 11, 3: 13})["condition"]["rules"][1]["q"] == 13 def test_layout() -> None: pages = c.layout(form()) assert len(pages) == 2 assert [q.id for q in pages[0]["sections"][0]["items"]] == [1, 2, 3, 4, 5] assert pages[1]["break"].id == 6 assert pages[1]["sections"][1]["heading"].id == 7 starts_with_break = [Q(1, "page_break"), Q(2, "short_text")] assert len(c.layout(starts_with_break)) == 1 def test_poll_and_slots_in_conditions() -> None: qs = [Q(1, "poll", config={"options": [{"id": "t1", "label": "A"}]}), Q(2, "slots", config={"options": [{"id": "s1", "label": "Früh"}]}, position=1), Q(3, "info", config={"condition": cond(rule(1, "not_empty"))}, position=2), Q(4, "info", config={"condition": cond(rule(2, "contains", "s1"))}, position=3)] assert c.hidden_ids(qs, {"q1_propose_text": ["x"]}) == {3, 4} assert c.hidden_ids(qs, {"q1_t1": ["no"], "q2": ["s1"]}) == set()