feature:verlauf backup papierkorb
This commit is contained in:
1 parent
4cb6cd8cf9
commit
8b383d2552
41 files changed
+4369
-641
No files matched your search
@@ -0,0 +1,132 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Verlauf der Formulare ohne Datenbank: Kodierung, Prüfsummen, Vergleich, Bereinigung."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, date, datetime, time
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("sqlalchemy")
|
||||
|
||||
from surveybox.forms import history
|
||||
from surveybox.forms.booking import Availability
|
||||
|
||||
|
||||
def test_values_survive_encoding() -> None:
|
||||
values = [None, True, 3, 1.5, "Text", Decimal("12.500000"),
|
||||
datetime(2026, 11, 26, 9, 30, tzinfo=UTC), date(2026, 11, 26), time(9, 30),
|
||||
uuid.uuid4(), b"\x00\xffmarker", {"selected": ["a", "b"]}, ["x", 1]]
|
||||
for value in values:
|
||||
assert history.decode_value(history.encode_value(value)) == value
|
||||
|
||||
|
||||
def payload(**tables: list[dict[str, Any]]) -> history.Payload:
|
||||
return history.Payload(1, dict(tables))
|
||||
|
||||
|
||||
def form_row(**values: Any) -> dict[str, Any]:
|
||||
return {"id": 1, "title": "Sommerfest", "updated_at": datetime(2026, 1, 1, tzinfo=UTC),
|
||||
**values}
|
||||
|
||||
|
||||
def test_hash_ignores_updated_at_and_separates_answers() -> None:
|
||||
base = payload(form=[form_row()], response=[{"id": 5, "form_id": 1}])
|
||||
touched = payload(form=[form_row(updated_at=datetime(2026, 2, 1, tzinfo=UTC))],
|
||||
response=[{"id": 5, "form_id": 1}])
|
||||
assert history.pack(base).full_hash == history.pack(touched).full_hash
|
||||
renamed = payload(form=[form_row(title="Herbstfest")], response=[{"id": 5, "form_id": 1}])
|
||||
assert history.pack(base).full_hash != history.pack(renamed).full_hash
|
||||
# Nur Einstellungen geändert: Antworten-Prüfsumme bleibt (Entprellen)
|
||||
assert history.pack(base).data_hash == history.pack(renamed).data_hash
|
||||
more = payload(form=[form_row()], response=[{"id": 5, "form_id": 1},
|
||||
{"id": 6, "form_id": 1}])
|
||||
assert history.pack(base).data_hash != history.pack(more).data_hash
|
||||
|
||||
|
||||
def question(**values: Any) -> dict[str, Any]:
|
||||
return {"id": 7, "label": "Schicht", "help_text": None, "required": False,
|
||||
"personal": False, "config": {"options": [{"id": "a", "label": "Früh"},
|
||||
{"id": "b", "label": "Spät"}]},
|
||||
**values}
|
||||
|
||||
|
||||
def test_removed_option_is_not_a_changed_question() -> None:
|
||||
old = question()
|
||||
fewer = question(config={"options": [{"id": "b", "label": "Spät"}]})
|
||||
added = question(config={"options": [{"id": "a", "label": "Früh"},
|
||||
{"id": "b", "label": "Spät"},
|
||||
{"id": "c", "label": "Abend"}]})
|
||||
relabeled = question(config={"options": [{"id": "a", "label": "Morgens"},
|
||||
{"id": "b", "label": "Spät"}]})
|
||||
confirmed = question(config={**old["config"], "confirmed": ["a"]})
|
||||
assert not history._definition_differs(old, fewer)
|
||||
assert not history._definition_differs(old, added)
|
||||
assert not history._definition_differs(old, confirmed)
|
||||
assert history._definition_differs(old, relabeled)
|
||||
assert history._definition_differs(old, question(label="Dienst"))
|
||||
|
||||
|
||||
def test_compare_lists_deleted_question_and_removed_option() -> None:
|
||||
old = payload(form=[form_row()],
|
||||
question=[question(position=0), question(id=8, label="Name", position=1)],
|
||||
answer=[{"question_id": 8, "response_id": 1},
|
||||
{"question_id": 7, "response_id": 1}],
|
||||
booking=[{"question_id": 7, "option_id": "a", "response_id": 1}])
|
||||
now = payload(form=[form_row()],
|
||||
question=[question(position=0,
|
||||
config={"options": [{"id": "b", "label": "Spät"}]})])
|
||||
result = history.Comparison(snapshot=None) # type: ignore[arg-type]
|
||||
history._compare_questions(old, now, result)
|
||||
keys = {item.key: item for item in result.questions}
|
||||
assert set(keys) == {"q_del:8", "opt:7:a"}
|
||||
assert keys["q_del:8"].answers == 1
|
||||
assert keys["opt:7:a"].bookings == 1 and keys["opt:7:a"].title == "Früh"
|
||||
|
||||
|
||||
def test_settings_compare_only_known_fields() -> None:
|
||||
old = payload(form=[form_row(closes_at=None, status="open")])
|
||||
now = payload(form=[form_row(title="Neu", closes_at=datetime(2026, 12, 1, tzinfo=UTC),
|
||||
status="closed")])
|
||||
result = history.Comparison(snapshot=None) # type: ignore[arg-type]
|
||||
history._compare_settings(old, now, result)
|
||||
assert [i.key for i in result.settings] == ["setting:title", "setting:closes_at"]
|
||||
|
||||
|
||||
def test_drop_responses_removes_dependent_rows() -> None:
|
||||
data = payload(response=[{"id": 1}, {"id": 2}],
|
||||
answer=[{"response_id": 1}, {"response_id": 2}],
|
||||
booking=[{"response_id": 1}], response_note=[{"response_id": 1}],
|
||||
media_file=[{"response_id": 1, "storage_key": "k1"},
|
||||
{"response_id": None, "storage_key": "k2"}])
|
||||
assert history._drop_responses(data, {1})
|
||||
assert [r["id"] for r in data.rows("response")] == [2]
|
||||
assert data.rows("answer") == [{"response_id": 2}]
|
||||
assert data.rows("booking") == [] and data.rows("response_note") == []
|
||||
assert [m["storage_key"] for m in data.rows("media_file")] == ["k2"]
|
||||
assert not history._drop_responses(data, {99})
|
||||
|
||||
|
||||
def test_people_index() -> None:
|
||||
data = payload(form=[form_row(owner_id=3)],
|
||||
response=[{"id": 1, "user_id": 4, "guest_contact_id": None},
|
||||
{"id": 2, "user_id": None, "guest_contact_id": 9}],
|
||||
response_note=[{"author_id": 5}], form_access=[{"user_id": None}])
|
||||
assert history._people(data) == ([3, 4, 5], [9])
|
||||
|
||||
|
||||
def test_conditions_on_missing_questions_are_dropped() -> None:
|
||||
config = {"condition": {"mode": "all", "rules": [{"q": 3, "op": "eq", "value": "x"},
|
||||
{"q": 4, "op": "eq", "value": "y"}]}}
|
||||
cleaned = history._clean_conditions(config, {3})
|
||||
assert [r["q"] for r in cleaned["condition"]["rules"]] == [3]
|
||||
assert "condition" not in history._clean_conditions(config, set())
|
||||
|
||||
|
||||
def test_overbooked() -> None:
|
||||
assert Availability(capacity=1, confirmed=2, waiting=0).overbooked
|
||||
assert not Availability(capacity=2, confirmed=2, waiting=1).overbooked
|
||||
assert not Availability(capacity=None, confirmed=9, waiting=0).overbooked
|
||||
Reference in new issue
Block a user