~ubuntuone-pqm-team/django/stable

« back to all changes in this revision

Viewing changes to tests/order_with_respect_to/tests.py

  • Committer: Natalia
  • Date: 2016-05-03 13:51:18 UTC
  • Revision ID: natalia.bidart@ubuntu.com-20160503135118-5yv5ywnfpqv6onnc
- Imported Django 1.9.6 from released tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
from django.db import models
7
7
from django.test import TestCase
8
8
 
 
9
from .base_tests import BaseOrderWithRespectToTests
9
10
from .models import Answer, Dimension, Entity, Post, Question
10
11
 
11
12
 
12
 
class OrderWithRespectToTests(TestCase):
13
 
 
14
 
    # Hook to allow subclasses to run these tests with alternate models.
 
13
class OrderWithRespectToBaseTests(BaseOrderWithRespectToTests, TestCase):
15
14
    Answer = Answer
 
15
    Post = Post
16
16
    Question = Question
17
17
 
18
 
    @classmethod
19
 
    def setUpTestData(cls):
20
 
        cls.q1 = cls.Question.objects.create(text="Which Beatle starts with the letter 'R'?")
21
 
        cls.Answer.objects.create(text="John", question=cls.q1)
22
 
        cls.Answer.objects.create(text="Paul", question=cls.q1)
23
 
        cls.Answer.objects.create(text="George", question=cls.q1)
24
 
        cls.Answer.objects.create(text="Ringo", question=cls.q1)
25
 
 
26
 
    def test_default_to_insertion_order(self):
27
 
        # Answers will always be ordered in the order they were inserted.
28
 
        self.assertQuerysetEqual(
29
 
            self.q1.answer_set.all(), [
30
 
                "John", "Paul", "George", "Ringo",
31
 
            ],
32
 
            attrgetter("text"),
33
 
        )
34
 
 
35
 
    def test_previous_and_next_in_order(self):
36
 
        # We can retrieve the answers related to a particular object, in the
37
 
        # order they were created, once we have a particular object.
38
 
        a1 = self.q1.answer_set.all()[0]
39
 
        self.assertEqual(a1.text, "John")
40
 
        self.assertEqual(a1.get_next_in_order().text, "Paul")
41
 
 
42
 
        a2 = list(self.q1.answer_set.all())[-1]
43
 
        self.assertEqual(a2.text, "Ringo")
44
 
        self.assertEqual(a2.get_previous_in_order().text, "George")
45
 
 
46
 
    def test_item_ordering(self):
47
 
        # We can retrieve the ordering of the queryset from a particular item.
48
 
        a1 = self.q1.answer_set.all()[1]
49
 
        id_list = [o.pk for o in self.q1.answer_set.all()]
50
 
        self.assertSequenceEqual(a1.question.get_answer_order(), id_list)
51
 
 
52
 
        # It doesn't matter which answer we use to check the order, it will
53
 
        # always be the same.
54
 
        a2 = self.Answer.objects.create(text="Number five", question=self.q1)
55
 
        self.assertListEqual(
56
 
            list(a1.question.get_answer_order()), list(a2.question.get_answer_order())
57
 
        )
58
 
 
59
 
    def test_change_ordering(self):
60
 
        # The ordering can be altered
61
 
        a = self.Answer.objects.create(text="Number five", question=self.q1)
62
 
 
63
 
        # Swap the last two items in the order list
64
 
        id_list = [o.pk for o in self.q1.answer_set.all()]
65
 
        x = id_list.pop()
66
 
        id_list.insert(-1, x)
67
 
 
68
 
        # By default, the ordering is different from the swapped version
69
 
        self.assertNotEqual(list(a.question.get_answer_order()), id_list)
70
 
 
71
 
        # Change the ordering to the swapped version -
72
 
        # this changes the ordering of the queryset.
73
 
        a.question.set_answer_order(id_list)
74
 
        self.assertQuerysetEqual(
75
 
            self.q1.answer_set.all(), [
76
 
                "John", "Paul", "George", "Number five", "Ringo"
77
 
            ],
78
 
            attrgetter("text")
79
 
        )
80
 
 
81
 
 
82
 
class OrderWithRespectToTests2(TestCase):
83
 
 
84
 
    # Provide the Post model as a class attribute so that we can subclass this
85
 
    # test case in contenttypes_tests.test_order_with_respect_to and run these
86
 
    # tests with alternative implementations of Post.
87
 
    Post = Post
88
 
 
89
 
    def test_recursive_ordering(self):
90
 
        p1 = self.Post.objects.create(title="1")
91
 
        p2 = self.Post.objects.create(title="2")
92
 
        p1_1 = self.Post.objects.create(title="1.1", parent=p1)
93
 
        p1_2 = self.Post.objects.create(title="1.2", parent=p1)
94
 
        self.Post.objects.create(title="2.1", parent=p2)
95
 
        p1_3 = self.Post.objects.create(title="1.3", parent=p1)
96
 
        self.assertSequenceEqual(p1.get_post_order(), [p1_1.pk, p1_2.pk, p1_3.pk])
 
18
 
 
19
class OrderWithRespectToTests(TestCase):
97
20
 
98
21
    def test_duplicate_order_field(self):
99
22
        test_apps = Apps(['order_with_respect_to'])