Skip to content

Commit 4ecede5

Browse files
committed
test(api): cover the CRE.id tie-break in _root_cres_query's ordering
Same gap as the all_cres test on #1060: every ordering fixture used a distinct external_id, so the test passed whether or not the sort carried its CRE.id half. external_id is not unique on its own -- the constraint is on (name, external_id) -- so ties are possible and the tie-break is what makes the order total. Two root CREs share an external_id here and are inserted in the opposite order to their primary keys, which are fixed rather than generated. Dropping CRE.id from the sort now fails the test.
1 parent 9e2582a commit 4ecede5

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎application/tests/web_main_test.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,33 @@ def test_find_root_cres_pagination_integration(self) -> None:
625625
)
626626
self.assertEqual(404, response.status_code)
627627

628+
def test_find_root_cres_orders_duplicate_external_ids_by_primary_key(self) -> None:
629+
"""external_id is not unique on its own: the constraint is on
630+
(name, external_id), so two root CREs may carry the same one. Ordering
631+
by external_id alone would leave those ties undefined, which is what the
632+
CRE.id half of _root_cres_query's sort is for. The primary keys are
633+
fixed here rather than generated, so the expected order cannot drift."""
634+
collection = db.Node_collection().with_graph()
635+
# Inserted in the opposite order to their primary keys.
636+
for pk, name in (
637+
("bbbbbbbb-0000-0000-0000-000000000000", "tie-second"),
638+
("aaaaaaaa-0000-0000-0000-000000000000", "tie-first"),
639+
):
640+
collection.session.add(
641+
db.CRE(id=pk, external_id="500-500", name=name, description="")
642+
)
643+
collection.session.commit()
644+
645+
with self.app.test_client() as client:
646+
response = client.get(
647+
"/rest/v1/root_cres?per_page=10&page=1",
648+
headers={"Content-Type": "application/json"},
649+
)
650+
self.assertEqual(200, response.status_code)
651+
names = [doc["name"] for doc in json.loads(response.data)["data"]]
652+
653+
self.assertEqual(["tie-first", "tie-second"], names)
654+
628655
def test_find_root_cres_rejects_malformed_pagination(self) -> None:
629656
"""A non-integer page or per_page is a client error, not a 500."""
630657
collection = db.Node_collection().with_graph()

0 commit comments

Comments
 (0)