6
6
from django.db import models
7
7
from django.test import TestCase
9
from .base_tests import BaseOrderWithRespectToTests
9
10
from .models import Answer, Dimension, Entity, Post, Question
12
class OrderWithRespectToTests(TestCase):
14
# Hook to allow subclasses to run these tests with alternate models.
13
class OrderWithRespectToBaseTests(BaseOrderWithRespectToTests, TestCase):
16
16
Question = Question
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)
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",
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")
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")
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)
52
# It doesn't matter which answer we use to check the order, it will
54
a2 = self.Answer.objects.create(text="Number five", question=self.q1)
56
list(a1.question.get_answer_order()), list(a2.question.get_answer_order())
59
def test_change_ordering(self):
60
# The ordering can be altered
61
a = self.Answer.objects.create(text="Number five", question=self.q1)
63
# Swap the last two items in the order list
64
id_list = [o.pk for o in self.q1.answer_set.all()]
68
# By default, the ordering is different from the swapped version
69
self.assertNotEqual(list(a.question.get_answer_order()), id_list)
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"
82
class OrderWithRespectToTests2(TestCase):
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.
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])
19
class OrderWithRespectToTests(TestCase):
98
21
def test_duplicate_order_field(self):
99
22
test_apps = Apps(['order_with_respect_to'])