parts/django/tests/modeltests/order_with_respect_to/tests.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 from operator import attrgetter
       
     2 
       
     3 from django.test import TestCase
       
     4 
       
     5 from models import Post, Question, Answer
       
     6 
       
     7 
       
     8 class OrderWithRespectToTests(TestCase):
       
     9     def test_basic(self):
       
    10         q1 = Question.objects.create(text="Which Beatle starts with the letter 'R'?")
       
    11         q2 = Question.objects.create(text="What is your name?")
       
    12         
       
    13         Answer.objects.create(text="John", question=q1)
       
    14         Answer.objects.create(text="Jonno", question=q2)
       
    15         Answer.objects.create(text="Paul", question=q1)
       
    16         Answer.objects.create(text="Paulo", question=q2)
       
    17         Answer.objects.create(text="George", question=q1)
       
    18         Answer.objects.create(text="Ringo", question=q1)
       
    19         
       
    20         # The answers will always be ordered in the order they were inserted.
       
    21         self.assertQuerysetEqual(
       
    22             q1.answer_set.all(), [
       
    23                 "John", "Paul", "George", "Ringo",
       
    24             ],
       
    25             attrgetter("text"),
       
    26         )
       
    27         
       
    28         # We can retrieve the answers related to a particular object, in the
       
    29         # order they were created, once we have a particular object.
       
    30         a1 = Answer.objects.filter(question=q1)[0]
       
    31         self.assertEqual(a1.text, "John")
       
    32         a2 = a1.get_next_in_order()
       
    33         self.assertEqual(a2.text, "Paul")
       
    34         a4 = list(Answer.objects.filter(question=q1))[-1]
       
    35         self.assertEqual(a4.text, "Ringo")
       
    36         self.assertEqual(a4.get_previous_in_order().text, "George")
       
    37         
       
    38         # Determining (and setting) the ordering for a particular item is also
       
    39         # possible.
       
    40         id_list = [o.pk for o in q1.answer_set.all()]
       
    41         self.assertEqual(a2.question.get_answer_order(), id_list)
       
    42         
       
    43         a5 = Answer.objects.create(text="Number five", question=q1)
       
    44         
       
    45         # It doesn't matter which answer we use to check the order, it will
       
    46         # always be the same.
       
    47         self.assertEqual(
       
    48             a2.question.get_answer_order(), a5.question.get_answer_order()
       
    49         )
       
    50         
       
    51         # The ordering can be altered:
       
    52         id_list = [o.pk for o in q1.answer_set.all()]
       
    53         x = id_list.pop()
       
    54         id_list.insert(-1, x)
       
    55         self.assertNotEqual(a5.question.get_answer_order(), id_list)
       
    56         a5.question.set_answer_order(id_list)
       
    57         self.assertQuerysetEqual(
       
    58             q1.answer_set.all(), [
       
    59                 "John", "Paul", "George", "Number five", "Ringo"
       
    60             ],
       
    61             attrgetter("text")
       
    62         )
       
    63 
       
    64     def test_recursive_ordering(self):
       
    65         p1 = Post.objects.create(title='1')
       
    66         p2 = Post.objects.create(title='2')
       
    67         p1_1 = Post.objects.create(title="1.1", parent=p1)
       
    68         p1_2 = Post.objects.create(title="1.2", parent=p1)
       
    69         p2_1 = Post.objects.create(title="2.1", parent=p2)
       
    70         p1_3 = Post.objects.create(title="1.3", parent=p1)
       
    71         self.assertEqual(p1.get_post_order(), [p1_1.pk, p1_2.pk, p1_3.pk])