From 3f01e82c591b880faa5066ba28a3f11b2b82154c Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 21 Apr 2014 11:50:53 +0200 Subject: [PATCH] Further consolidated the model_inheritance tests. --- tests/model_inheritance_regress/models.py | 5 +++ tests/model_inheritance_regress/tests.py | 22 +++++++++++ .../__init__.py | 0 .../models.py | 37 ------------------- .../model_inheritance_select_related/tests.py | 31 ---------------- 5 files changed, 27 insertions(+), 68 deletions(-) delete mode 100644 tests/model_inheritance_select_related/__init__.py delete mode 100644 tests/model_inheritance_select_related/models.py delete mode 100644 tests/model_inheritance_select_related/tests.py diff --git a/tests/model_inheritance_regress/models.py b/tests/model_inheritance_regress/models.py index cb0b2fe72f..48adf94ddf 100644 --- a/tests/model_inheritance_regress/models.py +++ b/tests/model_inheritance_regress/models.py @@ -73,9 +73,14 @@ class ParkingLot4B(Place, ParkingLot4): pass +@python_2_unicode_compatible class Supplier(models.Model): + name = models.CharField(max_length=50) restaurant = models.ForeignKey(Restaurant) + def __str__(self): + return self.name + class Wholesaler(Supplier): retailer = models.ForeignKey(Supplier, related_name='wholesale_supplier') diff --git a/tests/model_inheritance_regress/tests.py b/tests/model_inheritance_regress/tests.py index 211877d7c6..5297898e2f 100644 --- a/tests/model_inheritance_regress/tests.py +++ b/tests/model_inheritance_regress/tests.py @@ -466,3 +466,25 @@ class ModelInheritanceTest(TestCase): serves_pizza=True, serves_hot_dogs=True) p = Place.objects.all().select_related('restaurant')[0] self.assertIsInstance(p.restaurant.serves_pizza, bool) + + def test_inheritance_select_related(self): + # Regression test for #7246 + r1 = Restaurant.objects.create( + name="Nobu", serves_hot_dogs=True, serves_pizza=False + ) + r2 = Restaurant.objects.create( + name="Craft", serves_hot_dogs=False, serves_pizza=True + ) + Supplier.objects.create(name="John", restaurant=r1) + Supplier.objects.create(name="Jane", restaurant=r2) + + self.assertQuerysetEqual( + Supplier.objects.order_by("name").select_related(), [ + "Jane", + "John", + ], + attrgetter("name") + ) + + jane = Supplier.objects.order_by("name").select_related("restaurant")[0] + self.assertEqual(jane.restaurant.name, "Craft") diff --git a/tests/model_inheritance_select_related/__init__.py b/tests/model_inheritance_select_related/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/model_inheritance_select_related/models.py b/tests/model_inheritance_select_related/models.py deleted file mode 100644 index 81f267589c..0000000000 --- a/tests/model_inheritance_select_related/models.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -Regression tests for the interaction between model inheritance and -select_related(). -""" -from __future__ import unicode_literals - -from django.db import models -from django.utils.encoding import python_2_unicode_compatible - - -@python_2_unicode_compatible -class Place(models.Model): - name = models.CharField(max_length=50) - - class Meta: - ordering = ('name',) - - def __str__(self): - return "%s the place" % self.name - - -@python_2_unicode_compatible -class Restaurant(Place): - serves_sushi = models.BooleanField(default=False) - serves_steak = models.BooleanField(default=False) - - def __str__(self): - return "%s the restaurant" % self.name - - -@python_2_unicode_compatible -class Person(models.Model): - name = models.CharField(max_length=50) - favorite_restaurant = models.ForeignKey(Restaurant) - - def __str__(self): - return self.name diff --git a/tests/model_inheritance_select_related/tests.py b/tests/model_inheritance_select_related/tests.py deleted file mode 100644 index 195ccc80ec..0000000000 --- a/tests/model_inheritance_select_related/tests.py +++ /dev/null @@ -1,31 +0,0 @@ -from __future__ import unicode_literals - -from operator import attrgetter - -from django.test import TestCase - -from .models import Restaurant, Person - - -class ModelInheritanceSelectRelatedTests(TestCase): - def test_inherited_select_related(self): - # Regression test for #7246 - r1 = Restaurant.objects.create( - name="Nobu", serves_sushi=True, serves_steak=False - ) - r2 = Restaurant.objects.create( - name="Craft", serves_sushi=False, serves_steak=True - ) - Person.objects.create(name="John", favorite_restaurant=r1) - Person.objects.create(name="Jane", favorite_restaurant=r2) - - self.assertQuerysetEqual( - Person.objects.order_by("name").select_related(), [ - "Jane", - "John", - ], - attrgetter("name") - ) - - jane = Person.objects.order_by("name").select_related("favorite_restaurant")[0] - self.assertEqual(jane.favorite_restaurant.name, "Craft")