Kaydet (Commit) 7d28bed1 authored tarafından Anssi Kääriäinen's avatar Anssi Kääriäinen

PEP 8 cleanup

üst 630b9df4
...@@ -8,8 +8,9 @@ from django.test import TestCase ...@@ -8,8 +8,9 @@ from django.test import TestCase
from django.test.utils import CaptureQueriesContext from django.test.utils import CaptureQueriesContext
from django.utils import six from django.utils import six
from .models import (Chef, CommonInfo, ItalianRestaurant, ParkingLot, Place, from .models import (
Post, Restaurant, Student, StudentWorker, Supplier, Worker, MixinModel) Chef, CommonInfo, ItalianRestaurant, ParkingLot, Place, Post,
Restaurant, Student, StudentWorker, Supplier, Worker, MixinModel)
class ModelInheritanceTests(TestCase): class ModelInheritanceTests(TestCase):
...@@ -20,7 +21,7 @@ class ModelInheritanceTests(TestCase): ...@@ -20,7 +21,7 @@ class ModelInheritanceTests(TestCase):
# information for programming purposes, but still completely # information for programming purposes, but still completely
# independent separate models at the database level. # independent separate models at the database level.
w1 = Worker.objects.create(name="Fred", age=35, job="Quarry worker") w1 = Worker.objects.create(name="Fred", age=35, job="Quarry worker")
w2 = Worker.objects.create(name="Barney", age=34, job="Quarry worker") Worker.objects.create(name="Barney", age=34, job="Quarry worker")
s = Student.objects.create(name="Pebbles", age=5, school_class="1B") s = Student.objects.create(name="Pebbles", age=5, school_class="1B")
...@@ -48,10 +49,12 @@ class ModelInheritanceTests(TestCase): ...@@ -48,10 +49,12 @@ class ModelInheritanceTests(TestCase):
# A StudentWorker which does not exist is both a Student and Worker # A StudentWorker which does not exist is both a Student and Worker
# which does not exist. # which does not exist.
self.assertRaises(Student.DoesNotExist, self.assertRaises(
Student.DoesNotExist,
StudentWorker.objects.get, pk=12321321 StudentWorker.objects.get, pk=12321321
) )
self.assertRaises(Worker.DoesNotExist, self.assertRaises(
Worker.DoesNotExist,
StudentWorker.objects.get, pk=12321321 StudentWorker.objects.get, pk=12321321
) )
...@@ -67,10 +70,12 @@ class ModelInheritanceTests(TestCase): ...@@ -67,10 +70,12 @@ class ModelInheritanceTests(TestCase):
sw2.age = 24 sw2.age = 24
sw2.save() sw2.save()
self.assertRaises(Student.MultipleObjectsReturned, self.assertRaises(
Student.MultipleObjectsReturned,
StudentWorker.objects.get, pk__lt=sw2.pk + 100 StudentWorker.objects.get, pk__lt=sw2.pk + 100
) )
self.assertRaises(Worker.MultipleObjectsReturned, self.assertRaises(
Worker.MultipleObjectsReturned,
StudentWorker.objects.get, pk__lt=sw2.pk + 100 StudentWorker.objects.get, pk__lt=sw2.pk + 100
) )
...@@ -85,16 +90,16 @@ class ModelInheritanceTests(TestCase): ...@@ -85,16 +90,16 @@ class ModelInheritanceTests(TestCase):
# The Post model doesn't have an attribute called # The Post model doesn't have an attribute called
# 'attached_%(class)s_set'. # 'attached_%(class)s_set'.
self.assertRaises(AttributeError, self.assertRaises(
getattr, post, "attached_%(class)s_set" AttributeError, getattr, post, "attached_%(class)s_set"
) )
# The Place/Restaurant/ItalianRestaurant models all exist as # The Place/Restaurant/ItalianRestaurant models all exist as
# independent models. However, the subclasses also have transparent # independent models. However, the subclasses also have transparent
# access to the fields of their ancestors. # access to the fields of their ancestors.
# Create a couple of Places. # Create a couple of Places.
p1 = Place.objects.create(name="Master Shakes", address="666 W. Jersey") Place.objects.create(name="Master Shakes", address="666 W. Jersey")
p2 = Place.objects.create(name="Ace Harware", address="1013 N. Ashland") Place.objects.create(name="Ace Harware", address="1013 N. Ashland")
# Test constructor for Restaurant. # Test constructor for Restaurant.
r = Restaurant.objects.create( r = Restaurant.objects.create(
...@@ -134,11 +139,13 @@ class ModelInheritanceTests(TestCase): ...@@ -134,11 +139,13 @@ class ModelInheritanceTests(TestCase):
# the right order. # the right order.
self.assertEqual( self.assertEqual(
[f.name for f in Restaurant._meta.fields], [f.name for f in Restaurant._meta.fields],
["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs", "serves_pizza", "chef"] ["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs",
"serves_pizza", "chef"]
) )
self.assertEqual( self.assertEqual(
[f.name for f in ItalianRestaurant._meta.fields], [f.name for f in ItalianRestaurant._meta.fields],
["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs", "serves_pizza", "chef", "restaurant_ptr", "serves_gnocchi"], ["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs",
"serves_pizza", "chef", "restaurant_ptr", "serves_gnocchi"],
) )
self.assertEqual(Restaurant._meta.ordering, ["-rating"]) self.assertEqual(Restaurant._meta.ordering, ["-rating"])
...@@ -146,8 +153,8 @@ class ModelInheritanceTests(TestCase): ...@@ -146,8 +153,8 @@ class ModelInheritanceTests(TestCase):
# Restaurant object cannot access that reverse relation, since it's not # Restaurant object cannot access that reverse relation, since it's not
# part of the Place-Supplier Hierarchy. # part of the Place-Supplier Hierarchy.
self.assertQuerysetEqual(Place.objects.filter(supplier__name="foo"), []) self.assertQuerysetEqual(Place.objects.filter(supplier__name="foo"), [])
self.assertRaises(FieldError, self.assertRaises(
Restaurant.objects.filter, supplier__name="foo" FieldError, Restaurant.objects.filter, supplier__name="foo"
) )
# Parent fields can be used directly in filters on the child model. # Parent fields can be used directly in filters on the child model.
...@@ -185,16 +192,19 @@ class ModelInheritanceTests(TestCase): ...@@ -185,16 +192,19 @@ class ModelInheritanceTests(TestCase):
# This won't work because the Demon Dogs restaurant is not an Italian # This won't work because the Demon Dogs restaurant is not an Italian
# restaurant. # restaurant.
self.assertRaises(ItalianRestaurant.DoesNotExist, self.assertRaises(
ItalianRestaurant.DoesNotExist,
lambda: p.restaurant.italianrestaurant lambda: p.restaurant.italianrestaurant
) )
# An ItalianRestaurant which does not exist is also a Place which does # An ItalianRestaurant which does not exist is also a Place which does
# not exist. # not exist.
self.assertRaises(Place.DoesNotExist, self.assertRaises(
Place.DoesNotExist,
ItalianRestaurant.objects.get, name="The Noodle Void" ItalianRestaurant.objects.get, name="The Noodle Void"
) )
# MultipleObjectsReturned is also inherited. # MultipleObjectsReturned is also inherited.
self.assertRaises(Place.MultipleObjectsReturned, self.assertRaises(
Place.MultipleObjectsReturned,
Restaurant.objects.get, id__lt=12321 Restaurant.objects.get, id__lt=12321
) )
...@@ -207,8 +217,8 @@ class ModelInheritanceTests(TestCase): ...@@ -207,8 +217,8 @@ class ModelInheritanceTests(TestCase):
# This won't work because the Place we select is not a Restaurant (it's # This won't work because the Place we select is not a Restaurant (it's
# a Supplier). # a Supplier).
p = Place.objects.get(name="Joe's Chickens") p = Place.objects.get(name="Joe's Chickens")
self.assertRaises(Restaurant.DoesNotExist, self.assertRaises(
lambda: p.restaurant Restaurant.DoesNotExist, lambda: p.restaurant
) )
self.assertEqual(p.supplier, s1) self.assertEqual(p.supplier, s1)
...@@ -233,10 +243,10 @@ class ModelInheritanceTests(TestCase): ...@@ -233,10 +243,10 @@ class ModelInheritanceTests(TestCase):
attrgetter("name"), attrgetter("name"),
) )
park1 = ParkingLot.objects.create( ParkingLot.objects.create(
name="Main St", address="111 Main St", main_site=s1 name="Main St", address="111 Main St", main_site=s1
) )
park2 = ParkingLot.objects.create( ParkingLot.objects.create(
name="Well Lit", address="124 Sesame St", main_site=ir name="Well Lit", address="124 Sesame St", main_site=ir
) )
...@@ -268,11 +278,11 @@ class ModelInheritanceTests(TestCase): ...@@ -268,11 +278,11 @@ class ModelInheritanceTests(TestCase):
# select_related works with fields from the parent object as if they # select_related works with fields from the parent object as if they
# were a normal part of the model. # were a normal part of the model.
self.assertNumQueries(2, self.assertNumQueries(
lambda: ItalianRestaurant.objects.all()[0].chef 2, lambda: ItalianRestaurant.objects.all()[0].chef
) )
self.assertNumQueries(1, self.assertNumQueries(
lambda: ItalianRestaurant.objects.select_related("chef")[0].chef 1, lambda: ItalianRestaurant.objects.select_related("chef")[0].chef
) )
def test_mixin_init(self): def test_mixin_init(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment