0
0
mirror of https://github.com/django/django.git synced 2024-11-29 06:03:25 +01:00

Avoided uncessary table creation in model_inheritance tests.

This commit is contained in:
Simon Charette 2016-05-21 13:41:13 -04:00
parent 53a5fb3cc0
commit d02e33141d
4 changed files with 28 additions and 67 deletions

View File

@ -168,14 +168,6 @@ class NamedURL(models.Model):
abstract = True
@python_2_unicode_compatible
class Copy(NamedURL):
content = models.TextField()
def __str__(self):
return self.content
class Mixin(object):
def __init__(self):
self.other_attr = 1

View File

@ -1,19 +0,0 @@
"""
Model inheritance across apps can result in models with the same name,
requiring an %(app_label)s format string. This app tests this feature by
redefining the Copy model from model_inheritance/models.py.
"""
from model_inheritance.models import NamedURL
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Copy(NamedURL):
content = models.TextField()
def __str__(self):
return self.content

View File

@ -3,14 +3,13 @@ from __future__ import unicode_literals
from operator import attrgetter
from django.core.exceptions import FieldError, ValidationError
from django.core.management import call_command
from django.db import connection, models
from django.test import TestCase, TransactionTestCase
from django.test import SimpleTestCase, TestCase
from django.test.utils import CaptureQueriesContext, isolate_apps
from django.utils import six
from .models import (
Base, Chef, CommonInfo, Copy, GrandChild, GrandParent, ItalianRestaurant,
Base, Chef, CommonInfo, GrandChild, GrandParent, ItalianRestaurant,
MixinModel, ParkingLot, Place, Post, Restaurant, Student, SubBase,
Supplier, Title, Worker,
)
@ -399,48 +398,37 @@ class ModelInheritanceDataTests(TestCase):
)
class InheritanceSameModelNameTests(TransactionTestCase):
@isolate_apps('model_inheritance', 'model_inheritance.tests')
class InheritanceSameModelNameTests(SimpleTestCase):
def test_abstract_fk_related_name(self):
related_name = '%(app_label)s_%(class)s_references'
available_apps = ['model_inheritance']
class Referenced(models.Model):
class Meta:
app_label = 'model_inheritance'
def setUp(self):
# The Title model has distinct accessors for both
# model_inheritance.Copy and model_inheritance_same_model_name.Copy
# models.
self.title = Title.objects.create(title='Lorem Ipsum')
class AbstractReferent(models.Model):
reference = models.ForeignKey(Referenced, models.CASCADE, related_name=related_name)
def test_inheritance_related_name(self):
self.assertEqual(
self.title.attached_model_inheritance_copy_set.create(
content='Save $ on V1agr@',
url='http://v1agra.com/',
title='V1agra is spam',
), Copy.objects.get(
content='Save $ on V1agr@',
))
class Meta:
app_label = 'model_inheritance'
abstract = True
def test_inheritance_with_same_model_name(self):
with self.modify_settings(
INSTALLED_APPS={'append': ['model_inheritance.same_model_name']}):
call_command('migrate', verbosity=0, run_syncdb=True)
from .same_model_name.models import Copy
copy = self.title.attached_same_model_name_copy_set.create(
content='The Web framework for perfectionists with deadlines.',
url='http://www.djangoproject.com/',
title='Django Rocks'
)
self.assertEqual(
copy,
Copy.objects.get(
content='The Web framework for perfectionists with deadlines.',
))
# We delete the copy manually so that it doesn't block the flush
# command under Oracle (which does not cascade deletions).
copy.delete()
class Referent(AbstractReferent):
class Meta:
app_label = 'model_inheritance'
def test_related_name_attribute_exists(self):
# The Post model doesn't have an attribute called 'attached_%(app_label)s_%(class)s_set'.
self.assertFalse(hasattr(self.title, 'attached_%(app_label)s_%(class)s_set'))
LocalReferent = Referent
class Referent(AbstractReferent):
class Meta:
app_label = 'tests'
ForeignReferent = Referent
self.assertFalse(hasattr(Referenced, related_name))
self.assertTrue(Referenced.model_inheritance_referent_references.rel.model, LocalReferent)
self.assertTrue(Referenced.tests_referent_references.rel.model, ForeignReferent)
class InheritanceUniqueTests(TestCase):