summaryrefslogtreecommitdiff
path: root/tests/managers_regress
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2013-02-26 09:53:47 +0100
committerFlorian Apolloner <florian@apolloner.eu>2013-02-26 14:36:57 +0100
commit89f40e36246100df6a11316c31a76712ebc6c501 (patch)
tree6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/managers_regress
parentb3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff)
downloaddjango-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/managers_regress')
-rw-r--r--tests/managers_regress/__init__.py0
-rw-r--r--tests/managers_regress/models.py121
-rw-r--r--tests/managers_regress/tests.py195
3 files changed, 316 insertions, 0 deletions
diff --git a/tests/managers_regress/__init__.py b/tests/managers_regress/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/managers_regress/__init__.py
diff --git a/tests/managers_regress/models.py b/tests/managers_regress/models.py
new file mode 100644
index 0000000000..d72970d86e
--- /dev/null
+++ b/tests/managers_regress/models.py
@@ -0,0 +1,121 @@
+"""
+Various edge-cases for model managers.
+"""
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+class OnlyFred(models.Manager):
+ def get_query_set(self):
+ return super(OnlyFred, self).get_query_set().filter(name='fred')
+
+
+class OnlyBarney(models.Manager):
+ def get_query_set(self):
+ return super(OnlyBarney, self).get_query_set().filter(name='barney')
+
+
+class Value42(models.Manager):
+ def get_query_set(self):
+ return super(Value42, self).get_query_set().filter(value=42)
+
+
+class AbstractBase1(models.Model):
+ name = models.CharField(max_length=50)
+
+ class Meta:
+ abstract = True
+
+ # Custom managers
+ manager1 = OnlyFred()
+ manager2 = OnlyBarney()
+ objects = models.Manager()
+
+
+class AbstractBase2(models.Model):
+ value = models.IntegerField()
+
+ class Meta:
+ abstract = True
+
+ # Custom manager
+ restricted = Value42()
+
+
+# No custom manager on this class to make sure the default case doesn't break.
+class AbstractBase3(models.Model):
+ comment = models.CharField(max_length=50)
+
+ class Meta:
+ abstract = True
+
+
+@python_2_unicode_compatible
+class Parent(models.Model):
+ name = models.CharField(max_length=50)
+
+ manager = OnlyFred()
+
+ def __str__(self):
+ return self.name
+
+
+# Managers from base classes are inherited and, if no manager is specified
+# *and* the parent has a manager specified, the first one (in the MRO) will
+# become the default.
+@python_2_unicode_compatible
+class Child1(AbstractBase1):
+ data = models.CharField(max_length=25)
+
+ def __str__(self):
+ return self.data
+
+
+@python_2_unicode_compatible
+class Child2(AbstractBase1, AbstractBase2):
+ data = models.CharField(max_length=25)
+
+ def __str__(self):
+ return self.data
+
+
+@python_2_unicode_compatible
+class Child3(AbstractBase1, AbstractBase3):
+ data = models.CharField(max_length=25)
+
+ def __str__(self):
+ return self.data
+
+
+@python_2_unicode_compatible
+class Child4(AbstractBase1):
+ data = models.CharField(max_length=25)
+
+ # Should be the default manager, although the parent managers are
+ # inherited.
+ default = models.Manager()
+
+ def __str__(self):
+ return self.data
+
+
+@python_2_unicode_compatible
+class Child5(AbstractBase3):
+ name = models.CharField(max_length=25)
+
+ default = OnlyFred()
+ objects = models.Manager()
+
+ def __str__(self):
+ return self.name
+
+
+# Will inherit managers from AbstractBase1, but not Child4.
+class Child6(Child4):
+ value = models.IntegerField()
+
+
+# Will not inherit default manager from parent.
+class Child7(Parent):
+ pass
diff --git a/tests/managers_regress/tests.py b/tests/managers_regress/tests.py
new file mode 100644
index 0000000000..45059be4e5
--- /dev/null
+++ b/tests/managers_regress/tests.py
@@ -0,0 +1,195 @@
+from __future__ import absolute_import
+import copy
+
+from django.conf import settings
+from django.db import models
+from django.db.models.loading import cache
+from django.test import TestCase
+from django.test.utils import override_settings
+
+from .models import (
+ Child1,
+ Child2,
+ Child3,
+ Child4,
+ Child5,
+ Child6,
+ Child7,
+ AbstractBase1,
+ AbstractBase2,
+ AbstractBase3,
+)
+
+
+class ManagersRegressionTests(TestCase):
+ def test_managers(self):
+ Child1.objects.create(name='fred', data='a1')
+ Child1.objects.create(name='barney', data='a2')
+ Child2.objects.create(name='fred', data='b1', value=1)
+ Child2.objects.create(name='barney', data='b2', value=42)
+ Child3.objects.create(name='fred', data='c1', comment='yes')
+ Child3.objects.create(name='barney', data='c2', comment='no')
+ Child4.objects.create(name='fred', data='d1')
+ Child4.objects.create(name='barney', data='d2')
+ Child5.objects.create(name='fred', comment='yes')
+ Child5.objects.create(name='barney', comment='no')
+ Child6.objects.create(name='fred', data='f1', value=42)
+ Child6.objects.create(name='barney', data='f2', value=42)
+ Child7.objects.create(name='fred')
+ Child7.objects.create(name='barney')
+
+ self.assertQuerysetEqual(Child1.manager1.all(), ["<Child1: a1>"])
+ self.assertQuerysetEqual(Child1.manager2.all(), ["<Child1: a2>"])
+ self.assertQuerysetEqual(Child1._default_manager.all(), ["<Child1: a1>"])
+
+ self.assertQuerysetEqual(Child2._default_manager.all(), ["<Child2: b1>"])
+ self.assertQuerysetEqual(Child2.restricted.all(), ["<Child2: b2>"])
+
+ self.assertQuerysetEqual(Child3._default_manager.all(), ["<Child3: c1>"])
+ self.assertQuerysetEqual(Child3.manager1.all(), ["<Child3: c1>"])
+ self.assertQuerysetEqual(Child3.manager2.all(), ["<Child3: c2>"])
+
+ # Since Child6 inherits from Child4, the corresponding rows from f1 and
+ # f2 also appear here. This is the expected result.
+ self.assertQuerysetEqual(Child4._default_manager.order_by('data'), [
+ "<Child4: d1>",
+ "<Child4: d2>",
+ "<Child4: f1>",
+ "<Child4: f2>"
+ ]
+ )
+ self.assertQuerysetEqual(Child4.manager1.all(), [
+ "<Child4: d1>",
+ "<Child4: f1>"
+ ],
+ ordered=False
+ )
+ self.assertQuerysetEqual(Child5._default_manager.all(), ["<Child5: fred>"])
+ self.assertQuerysetEqual(Child6._default_manager.all(), ["<Child6: f1>"])
+ self.assertQuerysetEqual(Child7._default_manager.order_by('name'), [
+ "<Child7: barney>",
+ "<Child7: fred>"
+ ]
+ )
+
+ def test_abstract_manager(self):
+ # Accessing the manager on an abstract model should
+ # raise an attribute error with an appropriate message.
+ try:
+ AbstractBase3.objects.all()
+ self.fail('Should raise an AttributeError')
+ except AttributeError as e:
+ # This error message isn't ideal, but if the model is abstract and
+ # a lot of the class instantiation logic isn't invoked; if the
+ # manager is implied, then we don't get a hook to install the
+ # error-raising manager.
+ self.assertEqual(str(e), "type object 'AbstractBase3' has no attribute 'objects'")
+
+ def test_custom_abstract_manager(self):
+ # Accessing the manager on an abstract model with an custom
+ # manager should raise an attribute error with an appropriate
+ # message.
+ try:
+ AbstractBase2.restricted.all()
+ self.fail('Should raise an AttributeError')
+ except AttributeError as e:
+ self.assertEqual(str(e), "Manager isn't available; AbstractBase2 is abstract")
+
+ def test_explicit_abstract_manager(self):
+ # Accessing the manager on an abstract model with an explicit
+ # manager should raise an attribute error with an appropriate
+ # message.
+ try:
+ AbstractBase1.objects.all()
+ self.fail('Should raise an AttributeError')
+ except AttributeError as e:
+ self.assertEqual(str(e), "Manager isn't available; AbstractBase1 is abstract")
+
+ def test_swappable_manager(self):
+ try:
+ # This test adds dummy models to the app cache. These
+ # need to be removed in order to prevent bad interactions
+ # with the flush operation in other tests.
+ old_app_models = copy.deepcopy(cache.app_models)
+ old_app_store = copy.deepcopy(cache.app_store)
+
+ settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
+
+ class SwappableModel(models.Model):
+ class Meta:
+ swappable = 'TEST_SWAPPABLE_MODEL'
+
+ # Accessing the manager on a swappable model should
+ # raise an attribute error with a helpful message
+ try:
+ SwappableModel.objects.all()
+ self.fail('Should raise an AttributeError')
+ except AttributeError as e:
+ self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
+
+ finally:
+ del settings.TEST_SWAPPABLE_MODEL
+ cache.app_models = old_app_models
+ cache.app_store = old_app_store
+
+ def test_custom_swappable_manager(self):
+ try:
+ # This test adds dummy models to the app cache. These
+ # need to be removed in order to prevent bad interactions
+ # with the flush operation in other tests.
+ old_app_models = copy.deepcopy(cache.app_models)
+ old_app_store = copy.deepcopy(cache.app_store)
+
+ settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
+
+ class SwappableModel(models.Model):
+
+ stuff = models.Manager()
+
+ class Meta:
+ swappable = 'TEST_SWAPPABLE_MODEL'
+
+ # Accessing the manager on a swappable model with an
+ # explicit manager should raise an attribute error with a
+ # helpful message
+ try:
+ SwappableModel.stuff.all()
+ self.fail('Should raise an AttributeError')
+ except AttributeError as e:
+ self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
+
+ finally:
+ del settings.TEST_SWAPPABLE_MODEL
+ cache.app_models = old_app_models
+ cache.app_store = old_app_store
+
+ def test_explicit_swappable_manager(self):
+ try:
+ # This test adds dummy models to the app cache. These
+ # need to be removed in order to prevent bad interactions
+ # with the flush operation in other tests.
+ old_app_models = copy.deepcopy(cache.app_models)
+ old_app_store = copy.deepcopy(cache.app_store)
+
+ settings.TEST_SWAPPABLE_MODEL = 'managers_regress.Parent'
+
+ class SwappableModel(models.Model):
+
+ objects = models.Manager()
+
+ class Meta:
+ swappable = 'TEST_SWAPPABLE_MODEL'
+
+ # Accessing the manager on a swappable model with an
+ # explicit manager should raise an attribute error with a
+ # helpful message
+ try:
+ SwappableModel.objects.all()
+ self.fail('Should raise an AttributeError')
+ except AttributeError as e:
+ self.assertEqual(str(e), "Manager isn't available; SwappableModel has been swapped for 'managers_regress.Parent'")
+
+ finally:
+ del settings.TEST_SWAPPABLE_MODEL
+ cache.app_models = old_app_models
+ cache.app_store = old_app_store