From 7daf00679d2848fdb6f5618009817b56486fce1b Mon Sep 17 00:00:00 2001 From: Helen ST Date: Thu, 27 Nov 2014 22:00:28 +0000 Subject: [PATCH] Refs #18586 -- Split up order_with_respect_to tests --- tests/order_with_respect_to/tests.py | 68 ++++++++++++++++------------ 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/tests/order_with_respect_to/tests.py b/tests/order_with_respect_to/tests.py index 2d5e85cb04..8fc09b496b 100644 --- a/tests/order_with_respect_to/tests.py +++ b/tests/order_with_respect_to/tests.py @@ -10,61 +10,73 @@ from .models import Post, Question, Answer class OrderWithRespectToTests(TestCase): - def test_basic(self): - q1 = Question.objects.create(text="Which Beatle starts with the letter 'R'?") - q2 = Question.objects.create(text="What is your name?") - Answer.objects.create(text="John", question=q1) - Answer.objects.create(text="Jonno", question=q2) - Answer.objects.create(text="Paul", question=q1) - Answer.objects.create(text="Paulo", question=q2) - Answer.objects.create(text="George", question=q1) - Answer.objects.create(text="Ringo", question=q1) + @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) - # The answers will always be ordered in the order they were inserted. + def test_default_to_insertion_order(self): + # Answers will always be ordered in the order they were inserted. self.assertQuerysetEqual( - q1.answer_set.all(), [ + self.q1.answer_set.all(), [ "John", "Paul", "George", "Ringo", ], attrgetter("text"), ) + 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=q1)[0] + a1 = Answer.objects.filter(question=self.q1)[0] self.assertEqual(a1.text, "John") - a2 = a1.get_next_in_order() - self.assertEqual(a2.text, "Paul") - a4 = list(Answer.objects.filter(question=q1))[-1] - self.assertEqual(a4.text, "Ringo") - self.assertEqual(a4.get_previous_in_order().text, "George") + self.assertEqual(a1.get_next_in_order().text, "Paul") - # Determining (and setting) the ordering for a particular item is also - # possible. - id_list = [o.pk for o in q1.answer_set.all()] - self.assertEqual(a2.question.get_answer_order(), id_list) + a2 = list(Answer.objects.filter(question=self.q1))[-1] + self.assertEqual(a2.text, "Ringo") + self.assertEqual(a2.get_previous_in_order().text, "George") - a5 = Answer.objects.create(text="Number five", question=q1) + 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] + id_list = [o.pk for o in self.q1.answer_set.all()] + self.assertEqual(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( - a2.question.get_answer_order(), a5.question.get_answer_order() + a1.question.get_answer_order(), a2.question.get_answer_order() ) - # The ordering can be altered: - id_list = [o.pk for o in q1.answer_set.all()] + def test_change_ordering(self): + # The ordering can be altered + a = 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()] x = id_list.pop() id_list.insert(-1, x) - self.assertNotEqual(a5.question.get_answer_order(), id_list) - a5.question.set_answer_order(id_list) + + # By default, the ordering is different from the swapped version + self.assertNotEqual(a.question.get_answer_order(), id_list) + + # Change the ordering to the swapped version - + # this changes the ordering of the queryset. + a.question.set_answer_order(id_list) self.assertQuerysetEqual( - q1.answer_set.all(), [ + self.q1.answer_set.all(), [ "John", "Paul", "George", "Number five", "Ringo" ], attrgetter("text") ) + +class OrderWithRespectToTests2(TestCase): + def test_recursive_ordering(self): p1 = Post.objects.create(title='1') p2 = Post.objects.create(title='2')