Skip to content

Commit f824af2

Browse files
test(Top): verify dj.Top preserves ORDER BY in results
Update test_top_restriction_with_keywords to verify that dj.Top properly preserves ordering in fetch results. Use secondary sort by 'id' to ensure deterministic results when there are ties. Fixes #1205 Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent f195110 commit f824af2

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

tests/integration/test_relational_operand.py

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -570,42 +570,32 @@ def test_restrictions_by_top(self, schema_simp_pop):
570570
]
571571

572572
def test_top_restriction_with_keywords(self, schema_simp_pop):
573-
# dj.Top only guarantees which elements are selected, not their order
574-
select = SelectPK() & dj.Top(limit=9, order_by=["select desc"])
575-
key = KeyPK() & dj.Top(limit=9, order_by="key desc")
576-
# Convert to sets of tuples for order-independent comparison
577-
select_result = {tuple(sorted(d.items())) for d in select.to_dicts()}
578-
select_expected = {
579-
tuple(sorted(d.items()))
580-
for d in [
581-
{"id": 2, "select": 8},
582-
{"id": 2, "select": 6},
583-
{"id": 1, "select": 4},
584-
{"id": 2, "select": 4},
585-
{"id": 1, "select": 3},
586-
{"id": 1, "select": 2},
587-
{"id": 2, "select": 2},
588-
{"id": 1, "select": 1},
589-
{"id": 0, "select": 0},
590-
]
591-
}
592-
assert select_result == select_expected
593-
key_result = {tuple(sorted(d.items())) for d in key.to_dicts()}
594-
key_expected = {
595-
tuple(sorted(d.items()))
596-
for d in [
597-
{"id": 2, "key": 6},
598-
{"id": 2, "key": 5},
599-
{"id": 1, "key": 5},
600-
{"id": 0, "key": 4},
601-
{"id": 1, "key": 4},
602-
{"id": 2, "key": 4},
603-
{"id": 0, "key": 3},
604-
{"id": 1, "key": 3},
605-
{"id": 2, "key": 3},
606-
]
607-
}
608-
assert key_result == key_expected
573+
# dj.Top preserves the ORDER BY clause in results
574+
# Use secondary sort by 'id' to ensure deterministic ordering when there are ties
575+
select = SelectPK() & dj.Top(limit=9, order_by=["select desc", "id"])
576+
key = KeyPK() & dj.Top(limit=9, order_by=["key desc", "id"])
577+
assert select.to_dicts() == [
578+
{"id": 2, "select": 8},
579+
{"id": 2, "select": 6},
580+
{"id": 1, "select": 4},
581+
{"id": 2, "select": 4},
582+
{"id": 1, "select": 3},
583+
{"id": 1, "select": 2},
584+
{"id": 2, "select": 2},
585+
{"id": 1, "select": 1},
586+
{"id": 0, "select": 0},
587+
]
588+
assert key.to_dicts() == [
589+
{"id": 2, "key": 6},
590+
{"id": 1, "key": 5},
591+
{"id": 2, "key": 5},
592+
{"id": 0, "key": 4},
593+
{"id": 1, "key": 4},
594+
{"id": 2, "key": 4},
595+
{"id": 0, "key": 3},
596+
{"id": 1, "key": 3},
597+
{"id": 2, "key": 3},
598+
]
609599

610600
def test_top_errors(self, schema_simp_pop):
611601
with pytest.raises(DataJointError) as err1:

0 commit comments

Comments
 (0)