test(surveys): assert stable order when created_at timestamps tie

Patch _now_ts so two answers share the same second; verify both fetch
paths return rows ordered by id when timestamps match.

Co-authored-by: nic <nicsins@users.noreply.github.com>
This commit is contained in:
Cursor Agent 2026-03-22 06:44:24 +00:00
parent b0677baf30
commit eaefc4f2ec
No known key found for this signature in database

View file

@ -1,4 +1,5 @@
import unittest
from unittest.mock import patch
from python.surveys.db import SurveyDB
from python.surveys.parser import parse_survey_page
@ -55,6 +56,29 @@ class TestSurveyDB(unittest.TestCase):
finally:
db.close()
def test_unprocessed_order_stable_when_created_at_ties(self):
"""Same-second inserts must not rely on undefined SQLite ordering."""
fixed_ts = 1_700_000_000
with patch("python.surveys.db._now_ts", return_value=fixed_ts):
db = SurveyDB(":memory:")
try:
persona = Persona(id="p1", name="Test", description="x", constraints={})
db.upsert_persona(persona)
profile = UserProfile(id="default", persona_id="p1", data={})
db.upsert_profile(profile)
db.create_session("s1", url="file://demo", persona_id="p1", profile_id="default")
field = SurveyField(selector="1a", kind=FieldKind.TEXT, label="Q")
# Insert lexicographically later id first; tie-break must still order by id ASC.
db.insert_answer("b2", "s1", "Q", field, "second")
db.insert_answer("a1", "s1", "Q", field, "first")
ids_plain = [r["id"] for r in db.fetch_unprocessed_answers()]
ids_events = [r["id"] for r in db.fetch_unprocessed_answer_events()]
self.assertEqual(ids_plain, ["a1", "b2"])
self.assertEqual(ids_events, ["a1", "b2"])
finally:
db.close()
class TestDeepMerge(unittest.TestCase):
def test_deep_merge(self):