diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/order_with_respect_to | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/order_with_respect_to')
-rw-r--r-- | tests/order_with_respect_to/__init__.py | 0 | ||||
-rw-r--r-- | tests/order_with_respect_to/models.py | 33 | ||||
-rw-r--r-- | tests/order_with_respect_to/tests.py | 73 |
3 files changed, 106 insertions, 0 deletions
diff --git a/tests/order_with_respect_to/__init__.py b/tests/order_with_respect_to/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/order_with_respect_to/__init__.py diff --git a/tests/order_with_respect_to/models.py b/tests/order_with_respect_to/models.py new file mode 100644 index 0000000000..06bb56b141 --- /dev/null +++ b/tests/order_with_respect_to/models.py @@ -0,0 +1,33 @@ +""" +Tests for the order_with_respect_to Meta attribute. +""" + +from django.db import models +from django.utils import six +from django.utils.encoding import python_2_unicode_compatible + + +class Question(models.Model): + text = models.CharField(max_length=200) + +@python_2_unicode_compatible +class Answer(models.Model): + text = models.CharField(max_length=200) + question = models.ForeignKey(Question) + + class Meta: + order_with_respect_to = 'question' + + def __str__(self): + return six.text_type(self.text) + +@python_2_unicode_compatible +class Post(models.Model): + title = models.CharField(max_length=200) + parent = models.ForeignKey("self", related_name="children", null=True) + + class Meta: + order_with_respect_to = "parent" + + def __str__(self): + return self.title diff --git a/tests/order_with_respect_to/tests.py b/tests/order_with_respect_to/tests.py new file mode 100644 index 0000000000..559cb1d996 --- /dev/null +++ b/tests/order_with_respect_to/tests.py @@ -0,0 +1,73 @@ +from __future__ import absolute_import + +from operator import attrgetter + +from django.test import TestCase + +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) + + # The answers will always be ordered in the order they were inserted. + self.assertQuerysetEqual( + q1.answer_set.all(), [ + "John", "Paul", "George", "Ringo", + ], + attrgetter("text"), + ) + + # 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] + 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") + + # 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) + + a5 = Answer.objects.create(text="Number five", question=q1) + + # It doesn't matter which answer we use to check the order, it will + # always be the same. + self.assertEqual( + a2.question.get_answer_order(), a5.question.get_answer_order() + ) + + # The ordering can be altered: + id_list = [o.pk for o in 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) + self.assertQuerysetEqual( + q1.answer_set.all(), [ + "John", "Paul", "George", "Number five", "Ringo" + ], + attrgetter("text") + ) + + def test_recursive_ordering(self): + p1 = Post.objects.create(title='1') + p2 = Post.objects.create(title='2') + p1_1 = Post.objects.create(title="1.1", parent=p1) + p1_2 = Post.objects.create(title="1.2", parent=p1) + p2_1 = Post.objects.create(title="2.1", parent=p2) + p1_3 = Post.objects.create(title="1.3", parent=p1) + self.assertEqual(p1.get_post_order(), [p1_1.pk, p1_2.pk, p1_3.pk]) |