From d02e33141dcb39de76bf581d9335351bdf80c9a1 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 21 May 2016 13:41:13 -0400 Subject: [PATCH] Avoided uncessary table creation in model_inheritance tests. --- tests/model_inheritance/models.py | 8 --- .../same_model_name/__init__.py | 0 .../same_model_name/models.py | 19 ------ tests/model_inheritance/tests.py | 68 ++++++++----------- 4 files changed, 28 insertions(+), 67 deletions(-) delete mode 100644 tests/model_inheritance/same_model_name/__init__.py delete mode 100644 tests/model_inheritance/same_model_name/models.py diff --git a/tests/model_inheritance/models.py b/tests/model_inheritance/models.py index 652eea4eaa..45f22df0bc 100644 --- a/tests/model_inheritance/models.py +++ b/tests/model_inheritance/models.py @@ -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 diff --git a/tests/model_inheritance/same_model_name/__init__.py b/tests/model_inheritance/same_model_name/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/model_inheritance/same_model_name/models.py b/tests/model_inheritance/same_model_name/models.py deleted file mode 100644 index 6b45e28dfe..0000000000 --- a/tests/model_inheritance/same_model_name/models.py +++ /dev/null @@ -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 diff --git a/tests/model_inheritance/tests.py b/tests/model_inheritance/tests.py index d47ad08680..7362c446ba 100644 --- a/tests/model_inheritance/tests.py +++ b/tests/model_inheritance/tests.py @@ -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):