2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2010-10-09 07:47:11 +08:00
|
|
|
from operator import attrgetter
|
|
|
|
|
2015-11-10 03:13:23 +08:00
|
|
|
from django.apps.registry import Apps
|
2014-05-29 07:38:59 +08:00
|
|
|
from django.db import models
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import TestCase
|
2014-05-29 07:38:59 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Answer, Post, Question
|
2010-10-09 07:47:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OrderWithRespectToTests(TestCase):
|
2014-11-28 06:00:28 +08:00
|
|
|
|
2015-03-27 00:52:11 +08:00
|
|
|
# Hook to allow subclasses to run these tests with alternate models.
|
|
|
|
Answer = Answer
|
|
|
|
Question = Question
|
|
|
|
|
2014-11-28 06:00:28 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2015-03-27 00:52:11 +08:00
|
|
|
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)
|
2014-11-28 06:00:28 +08:00
|
|
|
|
|
|
|
def test_default_to_insertion_order(self):
|
|
|
|
# Answers will always be ordered in the order they were inserted.
|
2010-10-09 07:47:11 +08:00
|
|
|
self.assertQuerysetEqual(
|
2014-11-28 06:00:28 +08:00
|
|
|
self.q1.answer_set.all(), [
|
2010-10-09 07:47:11 +08:00
|
|
|
"John", "Paul", "George", "Ringo",
|
|
|
|
],
|
|
|
|
attrgetter("text"),
|
|
|
|
)
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2014-11-28 06:00:28 +08:00
|
|
|
def test_previous_and_next_in_order(self):
|
2010-10-09 07:47:11 +08:00
|
|
|
# We can retrieve the answers related to a particular object, in the
|
|
|
|
# order they were created, once we have a particular object.
|
2015-03-27 00:52:11 +08:00
|
|
|
a1 = self.q1.answer_set.all()[0]
|
2010-10-09 07:47:11 +08:00
|
|
|
self.assertEqual(a1.text, "John")
|
2014-11-28 06:00:28 +08:00
|
|
|
self.assertEqual(a1.get_next_in_order().text, "Paul")
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2015-03-27 00:52:11 +08:00
|
|
|
a2 = list(self.q1.answer_set.all())[-1]
|
2014-11-28 06:00:28 +08:00
|
|
|
self.assertEqual(a2.text, "Ringo")
|
|
|
|
self.assertEqual(a2.get_previous_in_order().text, "George")
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2014-11-28 06:00:28 +08:00
|
|
|
def test_item_ordering(self):
|
|
|
|
# We can retrieve the ordering of the queryset from a particular item.
|
2015-03-27 00:52:11 +08:00
|
|
|
a1 = self.q1.answer_set.all()[1]
|
2014-11-28 06:00:28 +08:00
|
|
|
id_list = [o.pk for o in self.q1.answer_set.all()]
|
2015-03-27 00:52:11 +08:00
|
|
|
self.assertSequenceEqual(a1.question.get_answer_order(), id_list)
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2010-10-09 07:47:11 +08:00
|
|
|
# It doesn't matter which answer we use to check the order, it will
|
|
|
|
# always be the same.
|
2015-03-27 00:52:11 +08:00
|
|
|
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())
|
2010-10-09 07:47:11 +08:00
|
|
|
)
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2014-11-28 06:00:28 +08:00
|
|
|
def test_change_ordering(self):
|
|
|
|
# The ordering can be altered
|
2015-03-27 00:52:11 +08:00
|
|
|
a = self.Answer.objects.create(text="Number five", question=self.q1)
|
2014-11-28 06:00:28 +08:00
|
|
|
|
|
|
|
# Swap the last two items in the order list
|
|
|
|
id_list = [o.pk for o in self.q1.answer_set.all()]
|
2010-10-09 07:47:11 +08:00
|
|
|
x = id_list.pop()
|
|
|
|
id_list.insert(-1, x)
|
2014-11-28 06:00:28 +08:00
|
|
|
|
|
|
|
# By default, the ordering is different from the swapped version
|
2015-03-27 00:52:11 +08:00
|
|
|
self.assertNotEqual(list(a.question.get_answer_order()), id_list)
|
2014-11-28 06:00:28 +08:00
|
|
|
|
|
|
|
# Change the ordering to the swapped version -
|
|
|
|
# this changes the ordering of the queryset.
|
|
|
|
a.question.set_answer_order(id_list)
|
2010-10-09 07:47:11 +08:00
|
|
|
self.assertQuerysetEqual(
|
2014-11-28 06:00:28 +08:00
|
|
|
self.q1.answer_set.all(), [
|
2010-10-09 07:47:11 +08:00
|
|
|
"John", "Paul", "George", "Number five", "Ringo"
|
|
|
|
],
|
|
|
|
attrgetter("text")
|
|
|
|
)
|
2010-10-09 07:54:43 +08:00
|
|
|
|
2014-11-28 06:00:28 +08:00
|
|
|
|
|
|
|
class OrderWithRespectToTests2(TestCase):
|
|
|
|
|
2015-03-27 00:52:11 +08:00
|
|
|
# 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
|
|
|
|
|
2010-10-09 07:54:43 +08:00
|
|
|
def test_recursive_ordering(self):
|
2015-03-27 00:52:11 +08:00
|
|
|
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])
|
2014-05-29 07:38:59 +08:00
|
|
|
|
|
|
|
def test_duplicate_order_field(self):
|
2015-11-10 03:13:23 +08:00
|
|
|
test_apps = Apps(['order_with_respect_to'])
|
2014-05-29 07:38:59 +08:00
|
|
|
|
|
|
|
class Bar(models.Model):
|
2015-03-27 00:52:11 +08:00
|
|
|
class Meta:
|
2015-11-10 03:13:23 +08:00
|
|
|
apps = test_apps
|
2015-03-27 00:52:11 +08:00
|
|
|
app_label = 'order_with_respect_to'
|
2014-05-29 07:38:59 +08:00
|
|
|
|
|
|
|
class Foo(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
bar = models.ForeignKey(Bar, models.CASCADE)
|
2014-05-29 07:38:59 +08:00
|
|
|
order = models.OrderWrt()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
order_with_respect_to = 'bar'
|
2015-11-10 03:13:23 +08:00
|
|
|
apps = test_apps
|
2015-03-27 00:52:11 +08:00
|
|
|
app_label = 'order_with_respect_to'
|
2014-05-29 07:38:59 +08:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
for field in Foo._meta.local_fields:
|
|
|
|
if isinstance(field, models.OrderWrt):
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
self.assertEqual(count, 1)
|