diff options
author | Nishanth Amuluru | 2011-01-08 11:20:57 +0530 |
---|---|---|
committer | Nishanth Amuluru | 2011-01-08 11:20:57 +0530 |
commit | 65411d01d448ff0cd4abd14eee14cf60b5f8fc20 (patch) | |
tree | b4c404363c4c63a61d6e2f8bd26c5b057c1fb09d /parts/django/tests/modeltests/model_inheritance | |
parent | 2e35094d43b4cc6974172e1febf76abb50f086ec (diff) | |
download | pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.tar.gz pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.tar.bz2 pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.zip |
Added buildout stuff and made changes accordingly
--HG--
rename : profile/management/__init__.py => eggs/djangorecipe-0.20-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/djangorecipe-0.20-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/infrae.subversion-1.4.5-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/infrae.subversion-1.4.5-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/py-1.4.0-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/py-1.4.0-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/zc.buildout-1.5.2-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/zc.buildout-1.5.2-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/zc.recipe.egg-1.3.2-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/zc.recipe.egg-1.3.2-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => parts/django/Django.egg-info/dependency_links.txt
rename : taskapp/models.py => parts/django/django/conf/app_template/models.py
rename : taskapp/tests.py => parts/django/django/conf/app_template/tests.py
rename : taskapp/views.py => parts/django/django/conf/app_template/views.py
rename : taskapp/views.py => parts/django/django/contrib/gis/tests/geo3d/views.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/delete/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/files/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/invalid_models/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/m2m_signals/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/model_package/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/management/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/management/commands/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/models.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/delete_regress/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/file_storage/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/max_lengths/__init__.py
rename : profile/forms.py => pytask/profile/forms.py
rename : profile/management/__init__.py => pytask/profile/management/__init__.py
rename : profile/management/commands/seed_db.py => pytask/profile/management/commands/seed_db.py
rename : profile/models.py => pytask/profile/models.py
rename : profile/templatetags/user_tags.py => pytask/profile/templatetags/user_tags.py
rename : taskapp/tests.py => pytask/profile/tests.py
rename : profile/urls.py => pytask/profile/urls.py
rename : profile/utils.py => pytask/profile/utils.py
rename : profile/views.py => pytask/profile/views.py
rename : static/css/base.css => pytask/static/css/base.css
rename : taskapp/tests.py => pytask/taskapp/tests.py
rename : taskapp/views.py => pytask/taskapp/views.py
rename : templates/base.html => pytask/templates/base.html
rename : templates/profile/browse_notifications.html => pytask/templates/profile/browse_notifications.html
rename : templates/profile/edit.html => pytask/templates/profile/edit.html
rename : templates/profile/view.html => pytask/templates/profile/view.html
rename : templates/profile/view_notification.html => pytask/templates/profile/view_notification.html
rename : templates/registration/activate.html => pytask/templates/registration/activate.html
rename : templates/registration/activation_email.txt => pytask/templates/registration/activation_email.txt
rename : templates/registration/activation_email_subject.txt => pytask/templates/registration/activation_email_subject.txt
rename : templates/registration/logged_out.html => pytask/templates/registration/logged_out.html
rename : templates/registration/login.html => pytask/templates/registration/login.html
rename : templates/registration/logout.html => pytask/templates/registration/logout.html
rename : templates/registration/password_change_done.html => pytask/templates/registration/password_change_done.html
rename : templates/registration/password_change_form.html => pytask/templates/registration/password_change_form.html
rename : templates/registration/password_reset_complete.html => pytask/templates/registration/password_reset_complete.html
rename : templates/registration/password_reset_confirm.html => pytask/templates/registration/password_reset_confirm.html
rename : templates/registration/password_reset_done.html => pytask/templates/registration/password_reset_done.html
rename : templates/registration/password_reset_email.html => pytask/templates/registration/password_reset_email.html
rename : templates/registration/password_reset_form.html => pytask/templates/registration/password_reset_form.html
rename : templates/registration/registration_complete.html => pytask/templates/registration/registration_complete.html
rename : templates/registration/registration_form.html => pytask/templates/registration/registration_form.html
rename : utils.py => pytask/utils.py
Diffstat (limited to 'parts/django/tests/modeltests/model_inheritance')
3 files changed, 426 insertions, 0 deletions
diff --git a/parts/django/tests/modeltests/model_inheritance/__init__.py b/parts/django/tests/modeltests/model_inheritance/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/parts/django/tests/modeltests/model_inheritance/__init__.py diff --git a/parts/django/tests/modeltests/model_inheritance/models.py b/parts/django/tests/modeltests/model_inheritance/models.py new file mode 100644 index 0000000..6cee512 --- /dev/null +++ b/parts/django/tests/modeltests/model_inheritance/models.py @@ -0,0 +1,145 @@ +""" +XX. Model inheritance + +Model inheritance exists in two varieties: + - abstract base classes which are a way of specifying common + information inherited by the subclasses. They don't exist as a separate + model. + - non-abstract base classes (the default), which are models in their own + right with their own database tables and everything. Their subclasses + have references back to them, created automatically. + +Both styles are demonstrated here. +""" + +from django.db import models + +# +# Abstract base classes +# + +class CommonInfo(models.Model): + name = models.CharField(max_length=50) + age = models.PositiveIntegerField() + + class Meta: + abstract = True + ordering = ['name'] + + def __unicode__(self): + return u'%s %s' % (self.__class__.__name__, self.name) + +class Worker(CommonInfo): + job = models.CharField(max_length=50) + +class Student(CommonInfo): + school_class = models.CharField(max_length=10) + + class Meta: + pass + +class StudentWorker(Student, Worker): + pass + +# +# Abstract base classes with related models +# + +class Post(models.Model): + title = models.CharField(max_length=50) + +class Attachment(models.Model): + post = models.ForeignKey(Post, related_name='attached_%(class)s_set') + content = models.TextField() + + class Meta: + abstract = True + + def __unicode__(self): + return self.content + +class Comment(Attachment): + is_spam = models.BooleanField() + +class Link(Attachment): + url = models.URLField() + +# +# Multi-table inheritance +# + +class Chef(models.Model): + name = models.CharField(max_length=50) + + def __unicode__(self): + return u"%s the chef" % self.name + +class Place(models.Model): + name = models.CharField(max_length=50) + address = models.CharField(max_length=80) + + def __unicode__(self): + return u"%s the place" % self.name + +class Rating(models.Model): + rating = models.IntegerField(null=True, blank=True) + + class Meta: + abstract = True + ordering = ['-rating'] + +class Restaurant(Place, Rating): + serves_hot_dogs = models.BooleanField() + serves_pizza = models.BooleanField() + chef = models.ForeignKey(Chef, null=True, blank=True) + + class Meta(Rating.Meta): + db_table = 'my_restaurant' + + def __unicode__(self): + return u"%s the restaurant" % self.name + +class ItalianRestaurant(Restaurant): + serves_gnocchi = models.BooleanField() + + def __unicode__(self): + return u"%s the italian restaurant" % self.name + +class Supplier(Place): + customers = models.ManyToManyField(Restaurant, related_name='provider') + + def __unicode__(self): + return u"%s the supplier" % self.name + +class ParkingLot(Place): + # An explicit link to the parent (we can control the attribute name). + parent = models.OneToOneField(Place, primary_key=True, parent_link=True) + main_site = models.ForeignKey(Place, related_name='lot') + + def __unicode__(self): + return u"%s the parking lot" % self.name + +# +# Abstract base classes with related models where the sub-class has the +# same name in a different app and inherits from the same abstract base +# class. +# NOTE: The actual API tests for the following classes are in +# model_inheritance_same_model_name/models.py - They are defined +# here in order to have the name conflict between apps +# + +class Title(models.Model): + title = models.CharField(max_length=50) + +class NamedURL(models.Model): + title = models.ForeignKey(Title, related_name='attached_%(app_label)s_%(class)s_set') + url = models.URLField() + + class Meta: + abstract = True + +class Copy(NamedURL): + content = models.TextField() + + def __unicode__(self): + return self.content diff --git a/parts/django/tests/modeltests/model_inheritance/tests.py b/parts/django/tests/modeltests/model_inheritance/tests.py new file mode 100644 index 0000000..80dd0de --- /dev/null +++ b/parts/django/tests/modeltests/model_inheritance/tests.py @@ -0,0 +1,281 @@ +from operator import attrgetter + +from django.conf import settings +from django.core.exceptions import FieldError +from django.db import connection +from django.test import TestCase + +from models import (Chef, CommonInfo, ItalianRestaurant, ParkingLot, Place, + Post, Restaurant, Student, StudentWorker, Supplier, Worker) + + +class ModelInheritanceTests(TestCase): + def test_abstract(self): + # The Student and Worker models both have 'name' and 'age' fields on + # them and inherit the __unicode__() method, just as with normal Python + # subclassing. This is useful if you want to factor out common + # information for programming purposes, but still completely + # independent separate models at the database level. + w1 = Worker.objects.create(name="Fred", age=35, job="Quarry worker") + w2 = Worker.objects.create(name="Barney", age=34, job="Quarry worker") + + s = Student.objects.create(name="Pebbles", age=5, school_class="1B") + + self.assertEqual(unicode(w1), "Worker Fred") + self.assertEqual(unicode(s), "Student Pebbles") + + # The children inherit the Meta class of their parents (if they don't + # specify their own). + self.assertQuerysetEqual( + Worker.objects.values("name"), [ + {"name": "Barney"}, + {"name": "Fred"}, + ], + lambda o: o + ) + + # Since Student does not subclass CommonInfo's Meta, it has the effect + # of completely overriding it. So ordering by name doesn't take place + # for Students. + self.assertEqual(Student._meta.ordering, []) + + # However, the CommonInfo class cannot be used as a normal model (it + # doesn't exist as a model). + self.assertRaises(AttributeError, lambda: CommonInfo.objects.all()) + + # A StudentWorker which does not exist is both a Student and Worker + # which does not exist. + self.assertRaises(Student.DoesNotExist, + StudentWorker.objects.get, pk=12321321 + ) + self.assertRaises(Worker.DoesNotExist, + StudentWorker.objects.get, pk=12321321 + ) + + # MultipleObjectsReturned is also inherited. + # This is written out "long form", rather than using __init__/create() + # because of a bug with diamond inheritance (#10808) + sw1 = StudentWorker() + sw1.name = "Wilma" + sw1.age = 35 + sw1.save() + sw2 = StudentWorker() + sw2.name = "Betty" + sw2.age = 24 + sw2.save() + + self.assertRaises(Student.MultipleObjectsReturned, + StudentWorker.objects.get, pk__lt=sw2.pk + 100 + ) + self.assertRaises(Worker.MultipleObjectsReturned, + StudentWorker.objects.get, pk__lt=sw2.pk + 100 + ) + + def test_multiple_table(self): + post = Post.objects.create(title="Lorem Ipsum") + # The Post model has distinct accessors for the Comment and Link models. + post.attached_comment_set.create(content="Save $ on V1agr@", is_spam=True) + post.attached_link_set.create( + content="The Web framework for perfections with deadlines.", + url="http://www.djangoproject.com/" + ) + + # The Post model doesn't have an attribute called + # 'attached_%(class)s_set'. + self.assertRaises(AttributeError, + getattr, post, "attached_%(class)s_set" + ) + + # The Place/Restaurant/ItalianRestaurant models all exist as + # independent models. However, the subclasses also have transparent + # access to the fields of their ancestors. + # Create a couple of Places. + p1 = Place.objects.create(name="Master Shakes", address="666 W. Jersey") + p2 = Place.objects.create(name="Ace Harware", address="1013 N. Ashland") + + # Test constructor for Restaurant. + r = Restaurant.objects.create( + name="Demon Dogs", + address="944 W. Fullerton", + serves_hot_dogs=True, + serves_pizza=False, + rating=2 + ) + # Test the constructor for ItalianRestaurant. + c = Chef.objects.create(name="Albert") + ir = ItalianRestaurant.objects.create( + name="Ristorante Miron", + address="1234 W. Ash", + serves_hot_dogs=False, + serves_pizza=False, + serves_gnocchi=True, + rating=4, + chef=c + ) + self.assertQuerysetEqual( + ItalianRestaurant.objects.filter(address="1234 W. Ash"), [ + "Ristorante Miron", + ], + attrgetter("name") + ) + ir.address = "1234 W. Elm" + ir.save() + self.assertQuerysetEqual( + ItalianRestaurant.objects.filter(address="1234 W. Elm"), [ + "Ristorante Miron", + ], + attrgetter("name") + ) + + # Make sure Restaurant and ItalianRestaurant have the right fields in + # the right order. + self.assertEqual( + [f.name for f in Restaurant._meta.fields], + ["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs", "serves_pizza", "chef"] + ) + self.assertEqual( + [f.name for f in ItalianRestaurant._meta.fields], + ["id", "name", "address", "place_ptr", "rating", "serves_hot_dogs", "serves_pizza", "chef", "restaurant_ptr", "serves_gnocchi"], + ) + self.assertEqual(Restaurant._meta.ordering, ["-rating"]) + + # Even though p.supplier for a Place 'p' (a parent of a Supplier), a + # Restaurant object cannot access that reverse relation, since it's not + # part of the Place-Supplier Hierarchy. + self.assertQuerysetEqual(Place.objects.filter(supplier__name="foo"), []) + self.assertRaises(FieldError, + Restaurant.objects.filter, supplier__name="foo" + ) + + # Parent fields can be used directly in filters on the child model. + self.assertQuerysetEqual( + Restaurant.objects.filter(name="Demon Dogs"), [ + "Demon Dogs", + ], + attrgetter("name") + ) + self.assertQuerysetEqual( + ItalianRestaurant.objects.filter(address="1234 W. Elm"), [ + "Ristorante Miron", + ], + attrgetter("name") + ) + + # Filters against the parent model return objects of the parent's type. + p = Place.objects.get(name="Demon Dogs") + self.assertTrue(type(p) is Place) + + # Since the parent and child are linked by an automatically created + # OneToOneField, you can get from the parent to the child by using the + # child's name. + self.assertEqual( + p.restaurant, Restaurant.objects.get(name="Demon Dogs") + ) + self.assertEqual( + Place.objects.get(name="Ristorante Miron").restaurant.italianrestaurant, + ItalianRestaurant.objects.get(name="Ristorante Miron") + ) + self.assertEqual( + Restaurant.objects.get(name="Ristorante Miron").italianrestaurant, + ItalianRestaurant.objects.get(name="Ristorante Miron") + ) + + # This won't work because the Demon Dogs restaurant is not an Italian + # restaurant. + self.assertRaises(ItalianRestaurant.DoesNotExist, + lambda: p.restaurant.italianrestaurant + ) + # An ItalianRestaurant which does not exist is also a Place which does + # not exist. + self.assertRaises(Place.DoesNotExist, + ItalianRestaurant.objects.get, name="The Noodle Void" + ) + # MultipleObjectsReturned is also inherited. + self.assertRaises(Place.MultipleObjectsReturned, + Restaurant.objects.get, id__lt=12321 + ) + + # Related objects work just as they normally do. + s1 = Supplier.objects.create(name="Joe's Chickens", address="123 Sesame St") + s1.customers = [r, ir] + s2 = Supplier.objects.create(name="Luigi's Pasta", address="456 Sesame St") + s2.customers = [ir] + + # This won't work because the Place we select is not a Restaurant (it's + # a Supplier). + p = Place.objects.get(name="Joe's Chickens") + self.assertRaises(Restaurant.DoesNotExist, + lambda: p.restaurant + ) + + self.assertEqual(p.supplier, s1) + self.assertQuerysetEqual( + ir.provider.order_by("-name"), [ + "Luigi's Pasta", + "Joe's Chickens" + ], + attrgetter("name") + ) + self.assertQuerysetEqual( + Restaurant.objects.filter(provider__name__contains="Chickens"), [ + "Ristorante Miron", + "Demon Dogs", + ], + attrgetter("name") + ) + self.assertQuerysetEqual( + ItalianRestaurant.objects.filter(provider__name__contains="Chickens"), [ + "Ristorante Miron", + ], + attrgetter("name"), + ) + + park1 = ParkingLot.objects.create( + name="Main St", address="111 Main St", main_site=s1 + ) + park2 = ParkingLot.objects.create( + name="Well Lit", address="124 Sesame St", main_site=ir + ) + + self.assertEqual( + Restaurant.objects.get(lot__name="Well Lit").name, + "Ristorante Miron" + ) + + # The update() command can update fields in parent and child classes at + # once (although it executed multiple SQL queries to do so). + rows = Restaurant.objects.filter( + serves_hot_dogs=True, name__contains="D" + ).update( + name="Demon Puppies", serves_hot_dogs=False + ) + self.assertEqual(rows, 1) + + r1 = Restaurant.objects.get(pk=r.pk) + self.assertFalse(r1.serves_hot_dogs) + self.assertEqual(r1.name, "Demon Puppies") + + # The values() command also works on fields from parent models. + self.assertQuerysetEqual( + ItalianRestaurant.objects.values("name", "rating"), [ + {"rating": 4, "name": "Ristorante Miron"} + ], + lambda o: o + ) + + # select_related works with fields from the parent object as if they + # were a normal part of the model. + old_DEBUG = settings.DEBUG + try: + settings.DEBUG = True + starting_queries = len(connection.queries) + ItalianRestaurant.objects.all()[0].chef + self.assertEqual(len(connection.queries) - starting_queries, 2) + + starting_queries = len(connection.queries) + ItalianRestaurant.objects.select_related("chef")[0].chef + self.assertEqual(len(connection.queries) - starting_queries, 1) + finally: + settings.DEBUG = old_DEBUG + + |