| 1 |
from django.db.models import F |
| 2 |
from django.test import TestCase |
| 3 |
|
| 4 |
from .models import Comment, Tenant, User |
| 5 |
|
| 6 |
|
| 7 |
class CompositePKOrderByTests(TestCase): |
| 8 |
maxDiff = None |
| 9 |
|
| 10 |
@classmethod |
| 11 |
def setUpTestData(cls): |
| 12 |
cls.tenant_1 = Tenant.objects.create() |
| 13 |
cls.tenant_2 = Tenant.objects.create() |
| 14 |
cls.tenant_3 = Tenant.objects.create() |
| 15 |
cls.user_1 = User.objects.create( |
| 16 |
tenant=cls.tenant_1, |
| 17 |
id=1, |
| 18 |
email="[email protected]", |
| 19 |
) |
| 20 |
cls.user_2 = User.objects.create( |
| 21 |
tenant=cls.tenant_1, |
| 22 |
id=2, |
| 23 |
email="[email protected]", |
| 24 |
) |
| 25 |
cls.user_3 = User.objects.create( |
| 26 |
tenant=cls.tenant_2, |
| 27 |
id=3, |
| 28 |
email="[email protected]", |
| 29 |
) |
| 30 |
cls.comment_1 = Comment.objects.create(id=1, user=cls.user_1) |
| 31 |
cls.comment_2 = Comment.objects.create(id=2, user=cls.user_1) |
| 32 |
cls.comment_3 = Comment.objects.create(id=3, user=cls.user_2) |
| 33 |
cls.comment_4 = Comment.objects.create(id=4, user=cls.user_3) |
| 34 |
cls.comment_5 = Comment.objects.create(id=5, user=cls.user_1) |
| 35 |
|
| 36 |
def test_order_comments_by_pk_asc(self): |
| 37 |
self.assertSequenceEqual( |
| 38 |
Comment.objects.order_by("pk"), |
| 39 |
( |
| 40 |
self.comment_1, # (1, 1) |
| 41 |
self.comment_2, # (1, 2) |
| 42 |
self.comment_3, # (1, 3) |
| 43 |
self.comment_5, # (1, 5) |
| 44 |
self.comment_4, # (2, 4) |
| 45 |
), |
| 46 |
) |
| 47 |
|
| 48 |
def test_order_comments_by_pk_desc(self): |
| 49 |
self.assertSequenceEqual( |
| 50 |
Comment.objects.order_by("-pk"), |
| 51 |
( |
| 52 |
self.comment_4, # (2, 4) |
| 53 |
self.comment_5, # (1, 5) |
| 54 |
self.comment_3, # (1, 3) |
| 55 |
self.comment_2, # (1, 2) |
| 56 |
self.comment_1, # (1, 1) |
| 57 |
), |
| 58 |
) |
| 59 |
|
| 60 |
def test_order_comments_by_pk_expr(self): |
| 61 |
self.assertQuerySetEqual( |
| 62 |
Comment.objects.order_by("pk"), |
| 63 |
Comment.objects.order_by(F("pk")), |
| 64 |
) |
| 65 |
self.assertQuerySetEqual( |
| 66 |
Comment.objects.order_by("-pk"), |
| 67 |
Comment.objects.order_by(F("pk").desc()), |
| 68 |
) |
| 69 |
self.assertQuerySetEqual( |
| 70 |
Comment.objects.order_by("-pk"), |
| 71 |
Comment.objects.order_by(F("pk").desc(nulls_last=True)), |
| 72 |
) |