Explicit ordering for Lesson, Question, Choice (+id tie-break on Unit/Node): lessons no longer reshuffle after row updates
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
22
content/tests_ordering.py
Normal file
22
content/tests_ordering.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from content.models import Choice, Grade, Lesson, Node, Question, Unit
|
||||
|
||||
|
||||
class StableOrderTests(TestCase):
|
||||
"""گزارشِ مالک ۲۰۲۶-۱۰-۱۵: درسِ اولِ جزیره بعد از دیپلوی عوض شد، چون Lesson ترتیبِ صریح نداشت
|
||||
و Postgres سطرهای ویرایششده را جابهجا برمیگرداند. SQLite سطرها را به ترتیبِ id برمیگرداند و
|
||||
این باگ را نشان نمیدهد؛ پس خودِ ORDER BY را بررسی میکنیم."""
|
||||
|
||||
def test_content_querysets_have_explicit_order(self):
|
||||
for qs in (Lesson.objects.all(), Question.objects.all(), Choice.objects.all()):
|
||||
self.assertIn('ORDER BY', str(qs.query), qs.model.__name__)
|
||||
self.assertEqual(qs.query.order_by or qs.model._meta.ordering, ['id'])
|
||||
|
||||
def test_lessons_come_back_in_creation_order_after_updates(self):
|
||||
grade = Grade.objects.create(title='پایه', number=99)
|
||||
unit = Unit.objects.create(grade=grade, title='فصل', order=1)
|
||||
node = Node.objects.create(unit=unit, title='جزیره', order=1)
|
||||
ids = [Lesson.objects.create(node=node, title=f'درس {i}').id for i in range(5)]
|
||||
Lesson.objects.filter(pk=ids[0]).update(title='ویرایششده')
|
||||
self.assertEqual([l.id for l in node.lessons.all()], ids)
|
||||
Reference in New Issue
Block a user