From ca4067e2797759d1a12676cb756ada2c32df5a8e Mon Sep 17 00:00:00 2001 From: Diederik van der Boor Date: Fri, 5 Apr 2013 13:14:36 +0200 Subject: [PATCH] Add proxy model test that fails in Django 1.5 --- polymorphic/tests.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/polymorphic/tests.py b/polymorphic/tests.py index 7d62d33..c6188e0 100644 --- a/polymorphic/tests.py +++ b/polymorphic/tests.py @@ -178,6 +178,17 @@ class UUIDPlainB(UUIDPlainA): class UUIDPlainC(UUIDPlainB): field3 = models.CharField(max_length=10) +# base -> proxy -> real models +class ProxiedBase(ShowFieldTypeAndContent, PolymorphicModel): + name = models.CharField(max_length=10) +class ProxyModelBase(ProxiedBase): + class Meta: + proxy = True +class ProxyModelA(ProxyModelBase): + field1 = models.CharField(max_length=10) +class ProxyModelB(ProxyModelBase): + field2 = models.CharField(max_length=10) + # test bad field name #class TestBadFieldModel(ShowFieldType, PolymorphicModel): @@ -194,7 +205,6 @@ class PolymorphicTests(TestCase): """ The test suite """ - def test_diamond_inheritance(self): # Django diamond problem o1 = DiamondXY.objects.create(field_b='b', field_x='x', field_y='y') @@ -613,6 +623,37 @@ class PolymorphicTests(TestCase): self.assertEqual(repr(type(MROBase2._default_manager)), "") + def test_proxy_model_inheritance(self): + """ + Polymorphic abilities should also work when the base model is a proxy object. + """ + # The managers should point to the proper objects. + # otherwise, the whole excersise is pointless. + self.assertEqual(ProxiedBase.objects.model, ProxiedBase) + self.assertEqual(ProxyModelBase.objects.model, ProxyModelBase) + self.assertEqual(ProxyModelA.objects.model, ProxyModelA) + self.assertEqual(ProxyModelB.objects.model, ProxyModelB) + + # Create objects + ProxyModelA.objects.create(name="object1") + ProxyModelB.objects.create(name="object2", field2="bb") + + # Getting single objects + object1 = ProxyModelBase.objects.get(name='object1') + object2 = ProxyModelBase.objects.get(name='object2') + self.assertEqual(repr(object1), '') + self.assertEqual(repr(object2), '') + self.assertIsInstance(object1, ProxyModelA) + self.assertIsInstance(object2, ProxyModelB) + + # Same for lists + objects = list(ProxyModelBase.objects.all().order_by('name')) + self.assertEqual(repr(objects[0]), '') + self.assertEqual(repr(objects[1]), '') + self.assertIsInstance(objects[0], ProxyModelA) + self.assertIsInstance(objects[1], ProxyModelB) + + def test_fix_getattribute(self): ### fixed issue in PolymorphicModel.__getattribute__: field name same as model name o = ModelFieldNameTest.objects.create(modelfieldnametest='1')