feat(poll): reorder options without invalidating answers

This commit is contained in:
2026-07-22 03:22:04 +02:00
parent 38ecff60a5
commit 8292c9d709
4 changed files with 151 additions and 2 deletions

View File

@@ -5,7 +5,12 @@ import unittest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from govoplan_core.core.poll import PollCapabilityError, PollOptionUpdateCommand, PollSchedulingProvider
from govoplan_core.core.poll import (
PollCapabilityError,
PollOptionOrderCommand,
PollOptionUpdateCommand,
PollSchedulingProvider,
)
from govoplan_core.db.base import Base
from govoplan_poll.backend.capabilities import SqlPollSchedulingProvider
from govoplan_poll.backend.db.models import Poll, PollOption, PollResponse
@@ -174,6 +179,61 @@ class PollResponseEditingTests(unittest.TestCase):
self.assertEqual(poll.options[0].label, "Monday")
self.assertEqual(len(response.answers), 2)
def test_reorder_preserves_option_identity_and_existing_answers(self) -> None:
poll = self._poll(allow_response_update=False)
response = self._submit_both(poll, "person-1")
first_id, second_id = (option.id for option in poll.options)
original_answers = [dict(answer) for answer in response.answers]
provider = SqlPollSchedulingProvider()
reordered = provider.reorder_options(
self.session,
tenant_id="tenant-1",
poll_id=poll.id,
command=PollOptionOrderCommand(option_ids=(second_id, first_id)),
)
self.assertEqual(
[(option.id, option.position) for option in reordered.options],
[(second_id, 0), (first_id, 1)],
)
self.assertEqual(response.answers, original_answers)
self.assertIsNone(response.deleted_at)
replayed = provider.reorder_options(
self.session,
tenant_id="tenant-1",
poll_id=poll.id,
command=PollOptionOrderCommand(option_ids=(second_id, first_id)),
)
self.assertEqual(replayed.options, reordered.options)
self.assertEqual(response.answers, original_answers)
def test_reorder_rejects_partial_or_duplicate_active_option_order(self) -> None:
poll = self._poll()
first_id, second_id = (option.id for option in poll.options)
provider = SqlPollSchedulingProvider()
with self.assertRaisesRegex(PollCapabilityError, "every active option"):
provider.reorder_options(
self.session,
tenant_id="tenant-1",
poll_id=poll.id,
command=PollOptionOrderCommand(option_ids=(first_id,)),
)
with self.assertRaisesRegex(PollCapabilityError, "duplicate"):
provider.reorder_options(
self.session,
tenant_id="tenant-1",
poll_id=poll.id,
command=PollOptionOrderCommand(option_ids=(first_id, first_id)),
)
self.assertEqual(
[(option.id, option.position) for option in poll.options],
[(first_id, 0), (second_id, 1)],
)
if __name__ == "__main__":
unittest.main()