mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #24201 -- Added order_with_respect_to support to GenericForeignKey.
This commit is contained in:
@@ -10,13 +10,17 @@ from .models import Answer, Post, Question
|
||||
|
||||
class OrderWithRespectToTests(TestCase):
|
||||
|
||||
# Hook to allow subclasses to run these tests with alternate models.
|
||||
Answer = Answer
|
||||
Question = Question
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.q1 = Question.objects.create(text="Which Beatle starts with the letter 'R'?")
|
||||
Answer.objects.create(text="John", question=cls.q1)
|
||||
Answer.objects.create(text="Paul", question=cls.q1)
|
||||
Answer.objects.create(text="George", question=cls.q1)
|
||||
Answer.objects.create(text="Ringo", question=cls.q1)
|
||||
cls.q1 = cls.Question.objects.create(text="Which Beatle starts with the letter 'R'?")
|
||||
cls.Answer.objects.create(text="John", question=cls.q1)
|
||||
cls.Answer.objects.create(text="Paul", question=cls.q1)
|
||||
cls.Answer.objects.create(text="George", question=cls.q1)
|
||||
cls.Answer.objects.create(text="Ringo", question=cls.q1)
|
||||
|
||||
def test_default_to_insertion_order(self):
|
||||
# Answers will always be ordered in the order they were inserted.
|
||||
@@ -30,30 +34,30 @@ class OrderWithRespectToTests(TestCase):
|
||||
def test_previous_and_next_in_order(self):
|
||||
# We can retrieve the answers related to a particular object, in the
|
||||
# order they were created, once we have a particular object.
|
||||
a1 = Answer.objects.filter(question=self.q1)[0]
|
||||
a1 = self.q1.answer_set.all()[0]
|
||||
self.assertEqual(a1.text, "John")
|
||||
self.assertEqual(a1.get_next_in_order().text, "Paul")
|
||||
|
||||
a2 = list(Answer.objects.filter(question=self.q1))[-1]
|
||||
a2 = list(self.q1.answer_set.all())[-1]
|
||||
self.assertEqual(a2.text, "Ringo")
|
||||
self.assertEqual(a2.get_previous_in_order().text, "George")
|
||||
|
||||
def test_item_ordering(self):
|
||||
# We can retrieve the ordering of the queryset from a particular item.
|
||||
a1 = Answer.objects.filter(question=self.q1)[1]
|
||||
a1 = self.q1.answer_set.all()[1]
|
||||
id_list = [o.pk for o in self.q1.answer_set.all()]
|
||||
self.assertEqual(a1.question.get_answer_order(), id_list)
|
||||
self.assertSequenceEqual(a1.question.get_answer_order(), id_list)
|
||||
|
||||
# It doesn't matter which answer we use to check the order, it will
|
||||
# always be the same.
|
||||
a2 = Answer.objects.create(text="Number five", question=self.q1)
|
||||
self.assertEqual(
|
||||
a1.question.get_answer_order(), a2.question.get_answer_order()
|
||||
a2 = self.Answer.objects.create(text="Number five", question=self.q1)
|
||||
self.assertListEqual(
|
||||
list(a1.question.get_answer_order()), list(a2.question.get_answer_order())
|
||||
)
|
||||
|
||||
def test_change_ordering(self):
|
||||
# The ordering can be altered
|
||||
a = Answer.objects.create(text="Number five", question=self.q1)
|
||||
a = self.Answer.objects.create(text="Number five", question=self.q1)
|
||||
|
||||
# Swap the last two items in the order list
|
||||
id_list = [o.pk for o in self.q1.answer_set.all()]
|
||||
@@ -61,7 +65,7 @@ class OrderWithRespectToTests(TestCase):
|
||||
id_list.insert(-1, x)
|
||||
|
||||
# By default, the ordering is different from the swapped version
|
||||
self.assertNotEqual(a.question.get_answer_order(), id_list)
|
||||
self.assertNotEqual(list(a.question.get_answer_order()), id_list)
|
||||
|
||||
# Change the ordering to the swapped version -
|
||||
# this changes the ordering of the queryset.
|
||||
@@ -76,19 +80,25 @@ class OrderWithRespectToTests(TestCase):
|
||||
|
||||
class OrderWithRespectToTests2(TestCase):
|
||||
|
||||
# Provide the Post model as a class attribute so that we can subclass this
|
||||
# test case in contenttypes_tests.test_order_with_respect_to and run these
|
||||
# tests with alternative implementations of Post.
|
||||
Post = Post
|
||||
|
||||
def test_recursive_ordering(self):
|
||||
p1 = Post.objects.create(title='1')
|
||||
p2 = Post.objects.create(title='2')
|
||||
p1_1 = Post.objects.create(title="1.1", parent=p1)
|
||||
p1_2 = Post.objects.create(title="1.2", parent=p1)
|
||||
Post.objects.create(title="2.1", parent=p2)
|
||||
p1_3 = Post.objects.create(title="1.3", parent=p1)
|
||||
self.assertEqual(p1.get_post_order(), [p1_1.pk, p1_2.pk, p1_3.pk])
|
||||
p1 = self.Post.objects.create(title="1")
|
||||
p2 = self.Post.objects.create(title="2")
|
||||
p1_1 = self.Post.objects.create(title="1.1", parent=p1)
|
||||
p1_2 = self.Post.objects.create(title="1.2", parent=p1)
|
||||
self.Post.objects.create(title="2.1", parent=p2)
|
||||
p1_3 = self.Post.objects.create(title="1.3", parent=p1)
|
||||
self.assertSequenceEqual(p1.get_post_order(), [p1_1.pk, p1_2.pk, p1_3.pk])
|
||||
|
||||
def test_duplicate_order_field(self):
|
||||
|
||||
class Bar(models.Model):
|
||||
pass
|
||||
class Meta:
|
||||
app_label = 'order_with_respect_to'
|
||||
|
||||
class Foo(models.Model):
|
||||
bar = models.ForeignKey(Bar, models.CASCADE)
|
||||
@@ -96,6 +106,7 @@ class OrderWithRespectToTests2(TestCase):
|
||||
|
||||
class Meta:
|
||||
order_with_respect_to = 'bar'
|
||||
app_label = 'order_with_respect_to'
|
||||
|
||||
count = 0
|
||||
for field in Foo._meta.local_fields:
|
||||
|
||||
Reference in New Issue
Block a user