Merge pull request #303 from WhyNotHugo/pgtests

Run CI with postgres too
fix_request_path_info
Diederik van der Boor 2017-09-30 15:52:31 +02:00 committed by GitHub
commit 6febf7a069
13 changed files with 1642 additions and 475 deletions

View File

@ -14,6 +14,12 @@ env:
- TOXENV=py35-djangomaster - TOXENV=py35-djangomaster
- TOXENV=py36-django111 - TOXENV=py36-django111
- TOXENV=py36-djangomaster - TOXENV=py36-djangomaster
# XXX: Use a matrix to build these?
- TOXENV=py36-django111-postgres DB=postgres
- TOXENV=py36-djangomaster-postgres DB=postgres
services:
- postgres
matrix: matrix:
fast_finish: true fast_finish: true
@ -40,6 +46,10 @@ cache:
- $HOME/.cache/pip - $HOME/.cache/pip
- $TRAVIS_BUILD_DIR/.tox - $TRAVIS_BUILD_DIR/.tox
before_install:
- psql -c 'CREATE DATABASE default;' -U postgres || true
- psql -c 'CREATE DATABASE secondary;' -U postgres || true
install: install:
- pip install --upgrade pip wheel setuptools - pip install --upgrade pip wheel setuptools
- pip install codecov coverage tox - pip install codecov coverage tox

View File

@ -1,416 +0,0 @@
# -*- coding: utf-8 -*-
import uuid
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.query import QuerySet
from polymorphic.managers import PolymorphicManager
from polymorphic.models import PolymorphicModel
from polymorphic.query import PolymorphicQuerySet
from polymorphic.showfields import ShowFieldContent, ShowFieldType, ShowFieldTypeAndContent
class PlainA(models.Model):
field1 = models.CharField(max_length=10)
class PlainB(PlainA):
field2 = models.CharField(max_length=10)
class PlainC(PlainB):
field3 = models.CharField(max_length=10)
class Model2A(ShowFieldType, PolymorphicModel):
field1 = models.CharField(max_length=10)
polymorphic_showfield_deferred = True
class Model2B(Model2A):
field2 = models.CharField(max_length=10)
class Model2C(Model2B):
field3 = models.CharField(max_length=10)
class Model2D(Model2C):
field4 = models.CharField(max_length=10)
class ModelExtraA(ShowFieldTypeAndContent, PolymorphicModel):
field1 = models.CharField(max_length=10)
class ModelExtraB(ModelExtraA):
field2 = models.CharField(max_length=10)
class ModelExtraC(ModelExtraB):
field3 = models.CharField(max_length=10)
class ModelExtraExternal(models.Model):
topic = models.CharField(max_length=10)
class ModelShow1(ShowFieldType, PolymorphicModel):
field1 = models.CharField(max_length=10)
m2m = models.ManyToManyField('self')
class ModelShow2(ShowFieldContent, PolymorphicModel):
field1 = models.CharField(max_length=10)
m2m = models.ManyToManyField('self')
class ModelShow3(ShowFieldTypeAndContent, PolymorphicModel):
field1 = models.CharField(max_length=10)
m2m = models.ManyToManyField('self')
class ModelShow1_plain(PolymorphicModel):
field1 = models.CharField(max_length=10)
class ModelShow2_plain(ModelShow1_plain):
field2 = models.CharField(max_length=10)
class Base(ShowFieldType, PolymorphicModel):
field_b = models.CharField(max_length=10)
class ModelX(Base):
field_x = models.CharField(max_length=10)
class ModelY(Base):
field_y = models.CharField(max_length=10)
class Enhance_Plain(models.Model):
field_p = models.CharField(max_length=10)
class Enhance_Base(ShowFieldTypeAndContent, PolymorphicModel):
base_id = models.AutoField(primary_key=True)
field_b = models.CharField(max_length=10)
class Enhance_Inherit(Enhance_Base, Enhance_Plain):
field_i = models.CharField(max_length=10)
class RelationBase(ShowFieldTypeAndContent, PolymorphicModel):
field_base = models.CharField(max_length=10)
fk = models.ForeignKey('self', on_delete=models.CASCADE, null=True, related_name='relationbase_set')
m2m = models.ManyToManyField('self')
class RelationA(RelationBase):
field_a = models.CharField(max_length=10)
class RelationB(RelationBase):
field_b = models.CharField(max_length=10)
class RelationBC(RelationB):
field_c = models.CharField(max_length=10)
class RelatingModel(models.Model):
many2many = models.ManyToManyField(Model2A)
class One2OneRelatingModel(PolymorphicModel):
one2one = models.OneToOneField(Model2A, on_delete=models.CASCADE)
field1 = models.CharField(max_length=10)
class One2OneRelatingModelDerived(One2OneRelatingModel):
field2 = models.CharField(max_length=10)
class ModelUnderRelParent(PolymorphicModel):
field1 = models.CharField(max_length=10)
_private = models.CharField(max_length=10)
class ModelUnderRelChild(PolymorphicModel):
parent = models.ForeignKey(ModelUnderRelParent, on_delete=models.CASCADE, related_name='children')
_private2 = models.CharField(max_length=10)
class MyManagerQuerySet(PolymorphicQuerySet):
def my_queryset_foo(self):
return self.all() # Just a method to prove the existance of the custom queryset.
class MyManager(PolymorphicManager):
queryset_class = MyManagerQuerySet
def get_queryset(self):
return super(MyManager, self).get_queryset().order_by('-field1')
def my_queryset_foo(self):
return self.all().my_queryset_foo()
class ModelWithMyManager(ShowFieldTypeAndContent, Model2A):
objects = MyManager()
field4 = models.CharField(max_length=10)
class ModelWithMyManagerNoDefault(ShowFieldTypeAndContent, Model2A):
objects = PolymorphicManager()
my_objects = MyManager()
field4 = models.CharField(max_length=10)
class ModelWithMyManagerDefault(ShowFieldTypeAndContent, Model2A):
my_objects = MyManager()
objects = PolymorphicManager()
field4 = models.CharField(max_length=10)
class ModelWithMyManager2(ShowFieldTypeAndContent, Model2A):
objects = MyManagerQuerySet.as_manager()
field4 = models.CharField(max_length=10)
class MROBase1(ShowFieldType, PolymorphicModel):
objects = MyManager()
field1 = models.CharField(max_length=10) # needed as MyManager uses it
class MROBase2(MROBase1):
pass # Django vanilla inheritance does not inherit MyManager as _default_manager here
class MROBase3(models.Model):
base_3_id = models.AutoField(primary_key=True) # make sure 'id' field doesn't clash, detected by Django 1.11
objects = PolymorphicManager()
class MRODerived(MROBase2, MROBase3):
pass
class ParentModelWithManager(PolymorphicModel):
pass
class ChildModelWithManager(PolymorphicModel):
# Also test whether foreign keys receive the manager:
fk = models.ForeignKey(ParentModelWithManager, on_delete=models.CASCADE, related_name='childmodel_set')
objects = MyManager()
class PlainMyManagerQuerySet(QuerySet):
def my_queryset_foo(self):
return self.all() # Just a method to prove the existance of the custom queryset.
class PlainMyManager(models.Manager):
def my_queryset_foo(self):
return self.get_queryset().my_queryset_foo()
def get_queryset(self):
return PlainMyManagerQuerySet(self.model, using=self._db)
class PlainParentModelWithManager(models.Model):
pass
class PlainChildModelWithManager(models.Model):
fk = models.ForeignKey(PlainParentModelWithManager, on_delete=models.CASCADE, related_name='childmodel_set')
objects = PlainMyManager()
class MgrInheritA(models.Model):
mgrA = models.Manager()
mgrA2 = models.Manager()
field1 = models.CharField(max_length=10)
class MgrInheritB(MgrInheritA):
mgrB = models.Manager()
field2 = models.CharField(max_length=10)
class MgrInheritC(ShowFieldTypeAndContent, MgrInheritB):
pass
class BlogBase(ShowFieldTypeAndContent, PolymorphicModel):
name = models.CharField(max_length=10)
class BlogA(BlogBase):
info = models.CharField(max_length=10)
class BlogB(BlogBase):
pass
class BlogEntry(ShowFieldTypeAndContent, PolymorphicModel):
blog = models.ForeignKey(BlogA, on_delete=models.CASCADE)
text = models.CharField(max_length=10)
class BlogEntry_limit_choices_to(ShowFieldTypeAndContent, PolymorphicModel):
blog = models.ForeignKey(BlogBase, on_delete=models.CASCADE)
text = models.CharField(max_length=10)
class ModelFieldNameTest(ShowFieldType, PolymorphicModel):
modelfieldnametest = models.CharField(max_length=10)
class InitTestModel(ShowFieldType, PolymorphicModel):
bar = models.CharField(max_length=100)
def __init__(self, *args, **kwargs):
kwargs['bar'] = self.x()
super(InitTestModel, self).__init__(*args, **kwargs)
class InitTestModelSubclass(InitTestModel):
def x(self):
return 'XYZ'
# models from github issue
class Top(PolymorphicModel):
name = models.CharField(max_length=50)
class Middle(Top):
description = models.TextField()
class Bottom(Middle):
author = models.CharField(max_length=50)
class UUIDProject(ShowFieldTypeAndContent, PolymorphicModel):
uuid_primary_key = models.UUIDField(primary_key=True, default=uuid.uuid1)
topic = models.CharField(max_length=30)
class UUIDArtProject(UUIDProject):
artist = models.CharField(max_length=30)
class UUIDResearchProject(UUIDProject):
supervisor = models.CharField(max_length=30)
class UUIDPlainA(models.Model):
uuid_primary_key = models.UUIDField(primary_key=True, default=uuid.uuid1)
field1 = models.CharField(max_length=10)
class UUIDPlainB(UUIDPlainA):
field2 = models.CharField(max_length=10)
class UUIDPlainC(UUIDPlainB):
field3 = models.CharField(max_length=10)
# base -> proxy
class ProxyBase(PolymorphicModel):
some_data = models.CharField(max_length=128)
class ProxyChild(ProxyBase):
class Meta:
proxy = True
class NonProxyChild(ProxyBase):
name = 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):
# instance_of = models.CharField(max_length=10)
# validation error: "polymorphic.relatednameclash: Accessor for field 'polymorphic_ctype' clashes
# with related field 'ContentType.relatednameclash_set'." (reported by Andrew Ingram)
# fixed with related_name
class RelatedNameClash(ShowFieldType, PolymorphicModel):
ctype = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, editable=False)
# class with a parent_link to superclass, and a related_name back to subclass
class TestParentLinkAndRelatedName(ModelShow1_plain):
superclass = models.OneToOneField(
ModelShow1_plain, on_delete=models.CASCADE, parent_link=True, related_name='related_name_subclass'
)
class CustomPkBase(ShowFieldTypeAndContent, PolymorphicModel):
b = models.CharField(max_length=1)
class CustomPkInherit(CustomPkBase):
custom_id = models.AutoField(primary_key=True)
i = models.CharField(max_length=1)
class DateModel(PolymorphicModel):
date = models.DateTimeField()
# Define abstract and swappable (being swapped for SwappedModel) models
# To test manager validation (should be skipped for such models)
class AbstractModel(PolymorphicModel):
class Meta:
abstract = True
class SwappableModel(AbstractModel):
class Meta:
swappable = 'POLYMORPHIC_TEST_SWAPPABLE'
class SwappedModel(AbstractModel):
pass

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,416 @@
# -*- coding: utf-8 -*-
import uuid
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.query import QuerySet
from polymorphic.managers import PolymorphicManager
from polymorphic.models import PolymorphicModel
from polymorphic.query import PolymorphicQuerySet
from polymorphic.showfields import ShowFieldContent, ShowFieldType, ShowFieldTypeAndContent
class PlainA(models.Model):
field1 = models.CharField(max_length=10)
class PlainB(PlainA):
field2 = models.CharField(max_length=10)
class PlainC(PlainB):
field3 = models.CharField(max_length=10)
class Model2A(ShowFieldType, PolymorphicModel):
field1 = models.CharField(max_length=10)
polymorphic_showfield_deferred = True
class Model2B(Model2A):
field2 = models.CharField(max_length=10)
class Model2C(Model2B):
field3 = models.CharField(max_length=10)
class Model2D(Model2C):
field4 = models.CharField(max_length=10)
class ModelExtraA(ShowFieldTypeAndContent, PolymorphicModel):
field1 = models.CharField(max_length=10)
class ModelExtraB(ModelExtraA):
field2 = models.CharField(max_length=10)
class ModelExtraC(ModelExtraB):
field3 = models.CharField(max_length=10)
class ModelExtraExternal(models.Model):
topic = models.CharField(max_length=10)
class ModelShow1(ShowFieldType, PolymorphicModel):
field1 = models.CharField(max_length=10)
m2m = models.ManyToManyField('self')
class ModelShow2(ShowFieldContent, PolymorphicModel):
field1 = models.CharField(max_length=10)
m2m = models.ManyToManyField('self')
class ModelShow3(ShowFieldTypeAndContent, PolymorphicModel):
field1 = models.CharField(max_length=10)
m2m = models.ManyToManyField('self')
class ModelShow1_plain(PolymorphicModel):
field1 = models.CharField(max_length=10)
class ModelShow2_plain(ModelShow1_plain):
field2 = models.CharField(max_length=10)
class Base(ShowFieldType, PolymorphicModel):
field_b = models.CharField(max_length=10)
class ModelX(Base):
field_x = models.CharField(max_length=10)
class ModelY(Base):
field_y = models.CharField(max_length=10)
class Enhance_Plain(models.Model):
field_p = models.CharField(max_length=10)
class Enhance_Base(ShowFieldTypeAndContent, PolymorphicModel):
base_id = models.AutoField(primary_key=True)
field_b = models.CharField(max_length=10)
class Enhance_Inherit(Enhance_Base, Enhance_Plain):
field_i = models.CharField(max_length=10)
class RelationBase(ShowFieldTypeAndContent, PolymorphicModel):
field_base = models.CharField(max_length=10)
fk = models.ForeignKey('self', on_delete=models.CASCADE, null=True, related_name='relationbase_set')
m2m = models.ManyToManyField('self')
class RelationA(RelationBase):
field_a = models.CharField(max_length=10)
class RelationB(RelationBase):
field_b = models.CharField(max_length=10)
class RelationBC(RelationB):
field_c = models.CharField(max_length=10)
class RelatingModel(models.Model):
many2many = models.ManyToManyField(Model2A)
class One2OneRelatingModel(PolymorphicModel):
one2one = models.OneToOneField(Model2A, on_delete=models.CASCADE)
field1 = models.CharField(max_length=10)
class One2OneRelatingModelDerived(One2OneRelatingModel):
field2 = models.CharField(max_length=10)
class ModelUnderRelParent(PolymorphicModel):
field1 = models.CharField(max_length=10)
_private = models.CharField(max_length=10)
class ModelUnderRelChild(PolymorphicModel):
parent = models.ForeignKey(ModelUnderRelParent, on_delete=models.CASCADE, related_name='children')
_private2 = models.CharField(max_length=10)
class MyManagerQuerySet(PolymorphicQuerySet):
def my_queryset_foo(self):
return self.all() # Just a method to prove the existance of the custom queryset.
class MyManager(PolymorphicManager):
queryset_class = MyManagerQuerySet
def get_queryset(self):
return super(MyManager, self).get_queryset().order_by('-field1')
def my_queryset_foo(self):
return self.all().my_queryset_foo()
class ModelWithMyManager(ShowFieldTypeAndContent, Model2A):
objects = MyManager()
field4 = models.CharField(max_length=10)
class ModelWithMyManagerNoDefault(ShowFieldTypeAndContent, Model2A):
objects = PolymorphicManager()
my_objects = MyManager()
field4 = models.CharField(max_length=10)
class ModelWithMyManagerDefault(ShowFieldTypeAndContent, Model2A):
my_objects = MyManager()
objects = PolymorphicManager()
field4 = models.CharField(max_length=10)
class ModelWithMyManager2(ShowFieldTypeAndContent, Model2A):
objects = MyManagerQuerySet.as_manager()
field4 = models.CharField(max_length=10)
class MROBase1(ShowFieldType, PolymorphicModel):
objects = MyManager()
field1 = models.CharField(max_length=10) # needed as MyManager uses it
class MROBase2(MROBase1):
pass # Django vanilla inheritance does not inherit MyManager as _default_manager here
class MROBase3(models.Model):
base_3_id = models.AutoField(primary_key=True) # make sure 'id' field doesn't clash, detected by Django 1.11
objects = models.Manager()
class MRODerived(MROBase2, MROBase3):
pass
class ParentModelWithManager(PolymorphicModel):
pass
class ChildModelWithManager(PolymorphicModel):
# Also test whether foreign keys receive the manager:
fk = models.ForeignKey(ParentModelWithManager, on_delete=models.CASCADE, related_name='childmodel_set')
objects = MyManager()
class PlainMyManagerQuerySet(QuerySet):
def my_queryset_foo(self):
return self.all() # Just a method to prove the existance of the custom queryset.
class PlainMyManager(models.Manager):
def my_queryset_foo(self):
return self.get_queryset().my_queryset_foo()
def get_queryset(self):
return PlainMyManagerQuerySet(self.model, using=self._db)
class PlainParentModelWithManager(models.Model):
pass
class PlainChildModelWithManager(models.Model):
fk = models.ForeignKey(PlainParentModelWithManager, on_delete=models.CASCADE, related_name='childmodel_set')
objects = PlainMyManager()
class MgrInheritA(models.Model):
mgrA = models.Manager()
mgrA2 = models.Manager()
field1 = models.CharField(max_length=10)
class MgrInheritB(MgrInheritA):
mgrB = models.Manager()
field2 = models.CharField(max_length=10)
class MgrInheritC(ShowFieldTypeAndContent, MgrInheritB):
pass
class BlogBase(ShowFieldTypeAndContent, PolymorphicModel):
name = models.CharField(max_length=10)
class BlogA(BlogBase):
info = models.CharField(max_length=10)
class BlogB(BlogBase):
pass
class BlogEntry(ShowFieldTypeAndContent, PolymorphicModel):
blog = models.ForeignKey(BlogA, on_delete=models.CASCADE)
text = models.CharField(max_length=10)
class BlogEntry_limit_choices_to(ShowFieldTypeAndContent, PolymorphicModel):
blog = models.ForeignKey(BlogBase, on_delete=models.CASCADE)
text = models.CharField(max_length=10)
class ModelFieldNameTest(ShowFieldType, PolymorphicModel):
modelfieldnametest = models.CharField(max_length=10)
class InitTestModel(ShowFieldType, PolymorphicModel):
bar = models.CharField(max_length=100)
def __init__(self, *args, **kwargs):
kwargs['bar'] = self.x()
super(InitTestModel, self).__init__(*args, **kwargs)
class InitTestModelSubclass(InitTestModel):
def x(self):
return 'XYZ'
# models from github issue
class Top(PolymorphicModel):
name = models.CharField(max_length=50)
class Middle(Top):
description = models.TextField()
class Bottom(Middle):
author = models.CharField(max_length=50)
class UUIDProject(ShowFieldTypeAndContent, PolymorphicModel):
uuid_primary_key = models.UUIDField(primary_key=True, default=uuid.uuid1)
topic = models.CharField(max_length=30)
class UUIDArtProject(UUIDProject):
artist = models.CharField(max_length=30)
class UUIDResearchProject(UUIDProject):
supervisor = models.CharField(max_length=30)
class UUIDPlainA(models.Model):
uuid_primary_key = models.UUIDField(primary_key=True, default=uuid.uuid1)
field1 = models.CharField(max_length=10)
class UUIDPlainB(UUIDPlainA):
field2 = models.CharField(max_length=10)
class UUIDPlainC(UUIDPlainB):
field3 = models.CharField(max_length=10)
# base -> proxy
class ProxyBase(PolymorphicModel):
some_data = models.CharField(max_length=128)
class ProxyChild(ProxyBase):
class Meta:
proxy = True
class NonProxyChild(ProxyBase):
name = 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):
# instance_of = models.CharField(max_length=10)
# validation error: "polymorphic.relatednameclash: Accessor for field 'polymorphic_ctype' clashes
# with related field 'ContentType.relatednameclash_set'." (reported by Andrew Ingram)
# fixed with related_name
class RelatedNameClash(ShowFieldType, PolymorphicModel):
ctype = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, editable=False)
# class with a parent_link to superclass, and a related_name back to subclass
class TestParentLinkAndRelatedName(ModelShow1_plain):
superclass = models.OneToOneField(
ModelShow1_plain, on_delete=models.CASCADE, parent_link=True, related_name='related_name_subclass'
)
class CustomPkBase(ShowFieldTypeAndContent, PolymorphicModel):
b = models.CharField(max_length=1)
class CustomPkInherit(CustomPkBase):
custom_id = models.AutoField(primary_key=True)
i = models.CharField(max_length=1)
class DateModel(PolymorphicModel):
date = models.DateTimeField()
# Define abstract and swappable (being swapped for SwappedModel) models
# To test manager validation (should be skipped for such models)
class AbstractModel(PolymorphicModel):
class Meta:
abstract = True
class SwappableModel(AbstractModel):
class Meta:
swappable = 'POLYMORPHIC_TEST_SWAPPABLE'
class SwappedModel(AbstractModel):
pass

View File

@ -2,7 +2,7 @@ from django.contrib.admin import AdminSite
from django.test import TestCase from django.test import TestCase
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin, PolymorphicChildModelFilter from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin, PolymorphicChildModelFilter
from polymorphic.tests import Model2A, Model2B, Model2C, Model2D from polymorphic.tests.models import Model2A, Model2B, Model2C, Model2D
class MultipleDatabasesTests(TestCase): class MultipleDatabasesTests(TestCase):

View File

@ -1,7 +1,10 @@
from unittest import TestCase from unittest import TestCase
from polymorphic.contrib.guardian import get_polymorphic_base_content_type from polymorphic.contrib.guardian import get_polymorphic_base_content_type
from polymorphic.tests import * # all models from polymorphic.tests.models import (
Model2D,
PlainC,
)
class ContribTests(TestCase): class ContribTests(TestCase):

View File

@ -4,7 +4,19 @@ from django.contrib.contenttypes.models import ContentType
from django.db.models import Q from django.db.models import Q
from django.test import TestCase from django.test import TestCase
from polymorphic.tests import * # all models from polymorphic.tests.models import (
Base,
BlogA,
BlogEntry,
Model2A,
Model2B,
Model2C,
Model2D,
ModelX,
ModelY,
One2OneRelatingModel,
RelatingModel,
)
class MultipleDatabasesTests(TestCase): class MultipleDatabasesTests(TestCase):
@ -16,15 +28,17 @@ class MultipleDatabasesTests(TestCase):
Model2B.objects.create(field1='B1', field2='B2') Model2B.objects.create(field1='B1', field2='B2')
Model2D(field1='D1', field2='D2', field3='D3', field4='D4').save() Model2D(field1='D1', field2='D2', field3='D3', field4='D4').save()
default_objects = list(Model2A.objects.order_by('id')) self.assertQuerysetEqual(
self.assertEqual(len(default_objects), 2) Model2A.objects.order_by('id'),
self.assertEqual(repr(default_objects[0]), '<Model2B: id 1, field1 (CharField), field2 (CharField)>') [Model2B, Model2D],
self.assertEqual(repr(default_objects[1]), '<Model2D: id 2, field1 (CharField), field2 (CharField), field3 (CharField), field4 (CharField)>') transform=lambda o: o.__class__,
)
secondary_objects = list(Model2A.objects.db_manager('secondary').order_by('id')) self.assertQuerysetEqual(
self.assertEqual(len(secondary_objects), 2) Model2A.objects.db_manager('secondary').order_by('id'),
self.assertEqual(repr(secondary_objects[0]), '<Model2A: id 1, field1 (CharField)>') [Model2A, Model2C],
self.assertEqual(repr(secondary_objects[1]), '<Model2C: id 2, field1 (CharField), field2 (CharField), field3 (CharField)>') transform=lambda o: o.__class__,
)
def test_instance_of_filter_on_non_default_database(self): def test_instance_of_filter_on_non_default_database(self):
Base.objects.db_manager('secondary').create(field_b='B1') Base.objects.db_manager('secondary').create(field_b='B1')
@ -32,23 +46,33 @@ class MultipleDatabasesTests(TestCase):
ModelY.objects.db_manager('secondary').create(field_b='Y', field_y='Y') ModelY.objects.db_manager('secondary').create(field_b='Y', field_y='Y')
objects = Base.objects.db_manager('secondary').filter(instance_of=Base) objects = Base.objects.db_manager('secondary').filter(instance_of=Base)
self.assertEqual(len(objects), 3) self.assertQuerysetEqual(
self.assertEqual(repr(objects[0]), '<Base: id 1, field_b (CharField)>') objects,
self.assertEqual(repr(objects[1]), '<ModelX: id 2, field_b (CharField), field_x (CharField)>') [Base, ModelX, ModelY],
self.assertEqual(repr(objects[2]), '<ModelY: id 3, field_b (CharField), field_y (CharField)>') transform=lambda o: o.__class__,
ordered=False,
)
objects = Base.objects.db_manager('secondary').filter(instance_of=ModelX) self.assertQuerysetEqual(
self.assertEqual(len(objects), 1) Base.objects.db_manager('secondary').filter(instance_of=ModelX),
self.assertEqual(repr(objects[0]), '<ModelX: id 2, field_b (CharField), field_x (CharField)>') [ModelX],
transform=lambda o: o.__class__,
)
objects = Base.objects.db_manager('secondary').filter(instance_of=ModelY) self.assertQuerysetEqual(
self.assertEqual(len(objects), 1) Base.objects.db_manager('secondary').filter(instance_of=ModelY),
self.assertEqual(repr(objects[0]), '<ModelY: id 3, field_b (CharField), field_y (CharField)>') [ModelY],
transform=lambda o: o.__class__,
)
objects = Base.objects.db_manager('secondary').filter(Q(instance_of=ModelX) | Q(instance_of=ModelY)) self.assertQuerysetEqual(
self.assertEqual(len(objects), 2) Base.objects.db_manager('secondary').filter(
self.assertEqual(repr(objects[0]), '<ModelX: id 2, field_b (CharField), field_x (CharField)>') Q(instance_of=ModelX) | Q(instance_of=ModelY)
self.assertEqual(repr(objects[1]), '<ModelY: id 3, field_b (CharField), field_y (CharField)>') ),
[ModelX, ModelY],
transform=lambda o: o.__class__,
ordered=False,
)
def test_forward_many_to_one_descriptor_on_non_default_database(self): def test_forward_many_to_one_descriptor_on_non_default_database(self):
def func(): def func():

View File

@ -1,12 +1,82 @@
import re import re
import uuid
import django import django
from django.db import models
from django.db.models import Case, Count, Q, When from django.db.models import Case, Count, Q, When
from django.test import TestCase, TransactionTestCase from django.test import TestCase, TransactionTestCase
from django.utils import six from django.utils import six
from polymorphic.managers import PolymorphicManager
from polymorphic.models import PolymorphicTypeUndefined from polymorphic.models import PolymorphicTypeUndefined
from polymorphic.tests import * # all models from polymorphic.tests.models import (
Base,
BlogA,
BlogB,
BlogBase,
BlogEntry,
BlogEntry_limit_choices_to,
ChildModelWithManager,
CustomPkBase,
CustomPkInherit,
Enhance_Base,
Enhance_Inherit,
InitTestModelSubclass,
MRODerived,
Model2A,
Model2B,
Model2C,
Model2D,
ModelExtraA,
ModelExtraB,
ModelExtraC,
ModelExtraExternal,
ModelFieldNameTest,
ModelShow1,
ModelShow1_plain,
ModelShow2,
ModelShow2_plain,
ModelShow3,
ModelUnderRelChild,
ModelUnderRelParent,
ModelWithMyManager,
ModelWithMyManager2,
ModelWithMyManagerDefault,
ModelWithMyManagerNoDefault,
ModelX,
ModelY,
MyManager,
MyManagerQuerySet,
NonProxyChild,
One2OneRelatingModel,
One2OneRelatingModelDerived,
ParentModelWithManager,
PlainA,
PlainB,
PlainC,
PlainChildModelWithManager,
PlainMyManager,
PlainMyManagerQuerySet,
PlainParentModelWithManager,
ProxiedBase,
ProxyBase,
ProxyChild,
ProxyModelA,
ProxyModelB,
ProxyModelBase,
QuerySet,
RelationA,
RelationB,
RelationBC,
RelationBase,
TestParentLinkAndRelatedName,
UUIDArtProject,
UUIDPlainA,
UUIDPlainB,
UUIDPlainC,
UUIDProject,
UUIDResearchProject,
)
class PolymorphicTests(TransactionTestCase): class PolymorphicTests(TransactionTestCase):
@ -252,28 +322,52 @@ class PolymorphicTests(TransactionTestCase):
) )
def test_base_manager(self): def test_base_manager(self):
def show_base_manager(model): def base_manager(model):
return "{0} {1}".format( return (
repr(type(model._base_manager)), type(model._base_manager),
repr(model._base_manager.model) model._base_manager.model
) )
self.assertEqual(show_base_manager(PlainA), "<class 'django.db.models.manager.Manager'> <class 'polymorphic.tests.PlainA'>") self.assertEqual(
self.assertEqual(show_base_manager(PlainB), "<class 'django.db.models.manager.Manager'> <class 'polymorphic.tests.PlainB'>") base_manager(PlainA),
self.assertEqual(show_base_manager(PlainC), "<class 'django.db.models.manager.Manager'> <class 'polymorphic.tests.PlainC'>") (models.Manager, PlainA),
)
self.assertEqual(
base_manager(PlainB),
(models.Manager, PlainB),
)
self.assertEqual(
base_manager(PlainC),
(models.Manager, PlainC),
)
self.assertEqual(show_base_manager(Model2A), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.Model2A'>") self.assertEqual(
self.assertEqual(show_base_manager(Model2B), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.Model2B'>") base_manager(Model2A),
self.assertEqual(show_base_manager(Model2C), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.Model2C'>") (PolymorphicManager, Model2A),
)
self.assertEqual(
base_manager(Model2B),
(PolymorphicManager, Model2B),
)
self.assertEqual(
base_manager(Model2C),
(PolymorphicManager, Model2C),
)
self.assertEqual(show_base_manager(One2OneRelatingModel), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.One2OneRelatingModel'>") self.assertEqual(
self.assertEqual(show_base_manager(One2OneRelatingModelDerived), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.One2OneRelatingModelDerived'>") base_manager(One2OneRelatingModel),
(PolymorphicManager, One2OneRelatingModel),
)
self.assertEqual(
base_manager(One2OneRelatingModelDerived),
(PolymorphicManager, One2OneRelatingModelDerived),
)
def test_instance_default_manager(self): def test_instance_default_manager(self):
def show_default_manager(instance): def default_manager(instance):
return "{0} {1}".format( return (
repr(type(instance.__class__._default_manager)), type(instance.__class__._default_manager),
repr(instance.__class__._default_manager.model) instance.__class__._default_manager.model
) )
plain_a = PlainA(field1='C1') plain_a = PlainA(field1='C1')
@ -284,13 +378,31 @@ class PolymorphicTests(TransactionTestCase):
model_2b = Model2B(field2='C1') model_2b = Model2B(field2='C1')
model_2c = Model2C(field3='C1') model_2c = Model2C(field3='C1')
self.assertEqual(show_default_manager(plain_a), "<class 'django.db.models.manager.Manager'> <class 'polymorphic.tests.PlainA'>") self.assertEqual(
self.assertEqual(show_default_manager(plain_b), "<class 'django.db.models.manager.Manager'> <class 'polymorphic.tests.PlainB'>") default_manager(plain_a),
self.assertEqual(show_default_manager(plain_c), "<class 'django.db.models.manager.Manager'> <class 'polymorphic.tests.PlainC'>") (models.Manager, PlainA),
)
self.assertEqual(
default_manager(plain_b),
(models.Manager, PlainB),
)
self.assertEqual(
default_manager(plain_c),
(models.Manager, PlainC),
)
self.assertEqual(show_default_manager(model_2a), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.Model2A'>") self.assertEqual(
self.assertEqual(show_default_manager(model_2b), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.Model2B'>") default_manager(model_2a),
self.assertEqual(show_default_manager(model_2c), "<class 'polymorphic.managers.PolymorphicManager'> <class 'polymorphic.tests.Model2C'>") (PolymorphicManager, Model2A),
)
self.assertEqual(
default_manager(model_2b),
(PolymorphicManager, Model2B),
)
self.assertEqual(
default_manager(model_2c),
(PolymorphicManager, Model2C),
)
def test_foreignkey_field(self): def test_foreignkey_field(self):
self.create_model2abcd() self.create_model2abcd()
@ -376,7 +488,11 @@ class PolymorphicTests(TransactionTestCase):
ModelExtraExternal.objects.create(topic='extra1') ModelExtraExternal.objects.create(topic='extra1')
ModelExtraExternal.objects.create(topic='extra2') ModelExtraExternal.objects.create(topic='extra2')
ModelExtraExternal.objects.create(topic='extra3') ModelExtraExternal.objects.create(topic='extra3')
objects = ModelExtraA.objects.extra(tables=["polymorphic_modelextraexternal"], select={"topic": "polymorphic_modelextraexternal.topic"}, where=["polymorphic_modelextraa.id = polymorphic_modelextraexternal.id"]) objects = ModelExtraA.objects.extra(
tables=["tests_modelextraexternal"],
select={"topic": "tests_modelextraexternal.topic"},
where=["tests_modelextraa.id = tests_modelextraexternal.id"],
)
if six.PY3: if six.PY3:
self.assertEqual(repr(objects[0]), '<ModelExtraA: id 1, field1 (CharField) "A1" - Extra: topic (str) "extra1">') self.assertEqual(repr(objects[0]), '<ModelExtraA: id 1, field1 (CharField) "A1" - Extra: topic (str) "extra1">')
self.assertEqual(repr(objects[1]), '<ModelExtraB: id 2, field1 (CharField) "B1", field2 (CharField) "B2" - Extra: topic (str) "extra2">') self.assertEqual(repr(objects[1]), '<ModelExtraB: id 2, field1 (CharField) "B1", field2 (CharField) "B2" - Extra: topic (str) "extra2">')

View File

@ -1,5 +1,5 @@
from django.test import TestCase from django.test import TestCase
from . import Top, Middle, Bottom from polymorphic.tests.models import Bottom, Middle, Top
class RegressionTests(TestCase): class RegressionTests(TestCase):

View File

@ -1,7 +1,7 @@
from django.test import TransactionTestCase from django.test import TransactionTestCase
from polymorphic.models import PolymorphicTypeUndefined from polymorphic.models import PolymorphicTypeUndefined
from polymorphic.tests import Model2A, Model2B, Model2C, Model2D from polymorphic.tests.models import Model2A, Model2B, Model2C, Model2D
from polymorphic.utils import reset_polymorphic_ctype, sort_by_subclass from polymorphic.utils import reset_polymorphic_ctype, sort_by_subclass

View File

@ -2,6 +2,7 @@
import sys import sys
from os.path import abspath, dirname from os.path import abspath, dirname
import dj_database_url
import django import django
from django.conf import settings from django.conf import settings
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
@ -18,14 +19,14 @@ if not settings.configured:
settings.configure( settings.configure(
DEBUG=False, DEBUG=False,
DATABASES={ DATABASES={
'default': { 'default': dj_database_url.config(
'ENGINE': 'django.db.backends.sqlite3', env='PRIMARY_DATABASE',
'NAME': ':memory:' default='sqlite://:memory:',
}, ),
'secondary': { 'secondary': dj_database_url.config(
'ENGINE': 'django.db.backends.sqlite3', env='SECONDARY_DATABASE',
'NAME': ':memory:' default='sqlite://:memory:',
} ),
}, },
TEST_RUNNER="django.test.runner.DiscoverRunner", TEST_RUNNER="django.test.runner.DiscoverRunner",
INSTALLED_APPS=( INSTALLED_APPS=(
@ -35,6 +36,7 @@ if not settings.configured:
'django.contrib.sites', 'django.contrib.sites',
'django.contrib.admin', 'django.contrib.admin',
'polymorphic', 'polymorphic',
'polymorphic.tests',
), ),
MIDDLEWARE_CLASSES=(), MIDDLEWARE_CLASSES=(),
SITE_ID=3, SITE_ID=3,

View File

@ -9,11 +9,15 @@ envlist =
[testenv] [testenv]
setenv = setenv =
PYTHONWARNINGS = all PYTHONWARNINGS = all
postgres: DEFAULT_DATABASE = postgres:///default
postgres: SECONDARY_DATABASE = postgres:///secondary
deps = deps =
coverage coverage
dj-database-url
django110: Django >= 1.10, < 1.11 django110: Django >= 1.10, < 1.11
django111: Django >= 1.11, < 2.0 django111: Django >= 1.11, < 2.0
djangomaster: https://github.com/django/django/archive/master.tar.gz djangomaster: https://github.com/django/django/archive/master.tar.gz
postgres: psycopg2
commands = commands =
coverage run --source polymorphic runtests.py coverage run --source polymorphic runtests.py