summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2016-01-02 06:00:25 -0500
committerTim Graham <timograham@gmail.com>2016-01-04 11:48:50 -0500
commit775291d4c767cd17dc5bbd2e8a605fd4fdfd8aed (patch)
tree3a1374bd1fb342e752daaa7434327bda14911b0d
parent0a72ad3952c0a2e335ed8518d31647e6536ee3fe (diff)
[1.9.x] Fixed #26009 -- Fixed contenttypes_tests isolation.
Backport of 2c6c873e3fca6c0c54c2335e131f7742fcf2cf26 from master
-rw-r--r--tests/contenttypes_tests/test_order_with_respect_to.py17
-rw-r--r--tests/order_with_respect_to/base_tests.py86
-rw-r--r--tests/order_with_respect_to/models.py9
-rw-r--r--tests/order_with_respect_to/tests.py85
4 files changed, 95 insertions, 102 deletions
diff --git a/tests/contenttypes_tests/test_order_with_respect_to.py b/tests/contenttypes_tests/test_order_with_respect_to.py
index 5d7f99d48b..3f5fe41ff1 100644
--- a/tests/contenttypes_tests/test_order_with_respect_to.py
+++ b/tests/contenttypes_tests/test_order_with_respect_to.py
@@ -1,18 +1,11 @@
-from order_with_respect_to.tests import (
- OrderWithRespectToTests, OrderWithRespectToTests2,
-)
+from order_with_respect_to.base_tests import BaseOrderWithRespectToTests
+
+from django.test import TestCase
from .models import Answer, Post, Question
-class OrderWithRespectToGFKTests(OrderWithRespectToTests):
+class OrderWithRespectToGFKTests(BaseOrderWithRespectToTests, TestCase):
Answer = Answer
- Question = Question
-
-del OrderWithRespectToTests
-
-
-class OrderWithRespectToGFKTests2(OrderWithRespectToTests2):
Post = Post
-
-del OrderWithRespectToTests2
+ Question = Question
diff --git a/tests/order_with_respect_to/base_tests.py b/tests/order_with_respect_to/base_tests.py
new file mode 100644
index 0000000000..ed80ab13e5
--- /dev/null
+++ b/tests/order_with_respect_to/base_tests.py
@@ -0,0 +1,86 @@
+"""
+The tests are shared with contenttypes_tests and so shouldn't import or
+reference any models directly. Subclasses should inherit django.test.TestCase.
+"""
+from __future__ import unicode_literals
+
+from operator import attrgetter
+
+
+class BaseOrderWithRespectToTests(object):
+ # Hook to allow subclasses to run these tests with alternate models.
+ Answer = None
+ Post = None
+ Question = None
+
+ @classmethod
+ def setUpTestData(cls):
+ 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)
+
+ def test_default_to_insertion_order(self):
+ # Answers will always be ordered in the order they were inserted.
+ self.assertQuerysetEqual(
+ self.q1.answer_set.all(), [
+ "John", "Paul", "George", "Ringo",
+ ],
+ attrgetter("text"),
+ )
+
+ def test_previous_and_next_in_order(self):
+ # We can retrieve the answers related to a particular object, in the
+ # order they were created, once we have a particular object.
+ a1 = self.q1.answer_set.all()[0]
+ self.assertEqual(a1.text, "John")
+ self.assertEqual(a1.get_next_in_order().text, "Paul")
+
+ a2 = list(self.q1.answer_set.all())[-1]
+ self.assertEqual(a2.text, "Ringo")
+ self.assertEqual(a2.get_previous_in_order().text, "George")
+
+ def test_item_ordering(self):
+ # We can retrieve the ordering of the queryset from a particular item.
+ a1 = self.q1.answer_set.all()[1]
+ id_list = [o.pk for o in self.q1.answer_set.all()]
+ self.assertSequenceEqual(a1.question.get_answer_order(), id_list)
+
+ # It doesn't matter which answer we use to check the order, it will
+ # always be the same.
+ 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())
+ )
+
+ def test_change_ordering(self):
+ # The ordering can be altered
+ a = self.Answer.objects.create(text="Number five", question=self.q1)
+
+ # Swap the last two items in the order list
+ id_list = [o.pk for o in self.q1.answer_set.all()]
+ x = id_list.pop()
+ id_list.insert(-1, x)
+
+ # By default, the ordering is different from the swapped version
+ self.assertNotEqual(list(a.question.get_answer_order()), id_list)
+
+ # Change the ordering to the swapped version -
+ # this changes the ordering of the queryset.
+ a.question.set_answer_order(id_list)
+ self.assertQuerysetEqual(
+ self.q1.answer_set.all(), [
+ "John", "Paul", "George", "Number five", "Ringo"
+ ],
+ attrgetter("text")
+ )
+
+ def test_recursive_ordering(self):
+ 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])
diff --git a/tests/order_with_respect_to/models.py b/tests/order_with_respect_to/models.py
index 742818337c..17c09dfeae 100644
--- a/tests/order_with_respect_to/models.py
+++ b/tests/order_with_respect_to/models.py
@@ -1,9 +1,5 @@
"""
Tests for the order_with_respect_to Meta attribute.
-
-We explicitly declare app_label on these models, because they are reused by
-contenttypes_tests. When those tests are run in isolation, these models need
-app_label because order_with_respect_to isn't in INSTALLED_APPS.
"""
from django.db import models
@@ -14,9 +10,6 @@ from django.utils.encoding import python_2_unicode_compatible
class Question(models.Model):
text = models.CharField(max_length=200)
- class Meta:
- app_label = 'order_with_respect_to'
-
@python_2_unicode_compatible
class Answer(models.Model):
@@ -25,7 +18,6 @@ class Answer(models.Model):
class Meta:
order_with_respect_to = 'question'
- app_label = 'order_with_respect_to'
def __str__(self):
return six.text_type(self.text)
@@ -38,7 +30,6 @@ class Post(models.Model):
class Meta:
order_with_respect_to = "parent"
- app_label = 'order_with_respect_to'
def __str__(self):
return self.title
diff --git a/tests/order_with_respect_to/tests.py b/tests/order_with_respect_to/tests.py
index 6157f51d4e..c22f9b4ea4 100644
--- a/tests/order_with_respect_to/tests.py
+++ b/tests/order_with_respect_to/tests.py
@@ -6,94 +6,17 @@ from django.apps.registry import Apps
from django.db import models
from django.test import TestCase
+from .base_tests import BaseOrderWithRespectToTests
from .models import Answer, Dimension, Entity, Post, Question
-class OrderWithRespectToTests(TestCase):
-
- # Hook to allow subclasses to run these tests with alternate models.
+class OrderWithRespectToBaseTests(BaseOrderWithRespectToTests, TestCase):
Answer = Answer
+ Post = Post
Question = Question
- @classmethod
- def setUpTestData(cls):
- 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)
-
- def test_default_to_insertion_order(self):
- # Answers will always be ordered in the order they were inserted.
- self.assertQuerysetEqual(
- self.q1.answer_set.all(), [
- "John", "Paul", "George", "Ringo",
- ],
- attrgetter("text"),
- )
-
- def test_previous_and_next_in_order(self):
- # We can retrieve the answers related to a particular object, in the
- # order they were created, once we have a particular object.
- a1 = self.q1.answer_set.all()[0]
- self.assertEqual(a1.text, "John")
- self.assertEqual(a1.get_next_in_order().text, "Paul")
-
- a2 = list(self.q1.answer_set.all())[-1]
- self.assertEqual(a2.text, "Ringo")
- self.assertEqual(a2.get_previous_in_order().text, "George")
-
- def test_item_ordering(self):
- # We can retrieve the ordering of the queryset from a particular item.
- a1 = self.q1.answer_set.all()[1]
- id_list = [o.pk for o in self.q1.answer_set.all()]
- self.assertSequenceEqual(a1.question.get_answer_order(), id_list)
-
- # It doesn't matter which answer we use to check the order, it will
- # always be the same.
- 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())
- )
-
- def test_change_ordering(self):
- # The ordering can be altered
- a = self.Answer.objects.create(text="Number five", question=self.q1)
- # Swap the last two items in the order list
- id_list = [o.pk for o in self.q1.answer_set.all()]
- x = id_list.pop()
- id_list.insert(-1, x)
-
- # By default, the ordering is different from the swapped version
- self.assertNotEqual(list(a.question.get_answer_order()), id_list)
-
- # Change the ordering to the swapped version -
- # this changes the ordering of the queryset.
- a.question.set_answer_order(id_list)
- self.assertQuerysetEqual(
- self.q1.answer_set.all(), [
- "John", "Paul", "George", "Number five", "Ringo"
- ],
- attrgetter("text")
- )
-
-
-class OrderWithRespectToTests2(TestCase):
-
- # 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
-
- def test_recursive_ordering(self):
- 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])
+class OrderWithRespectToTests(TestCase):
def test_duplicate_order_field(self):
test_apps = Apps(['order_with_respect_to'])