2009-04-03 19:52:14 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2010-01-29 17:58:22 +00:00
|
|
|
from models import Link, Place, Restaurant, Person, Address, CharLink, TextLink, OddRelation1, OddRelation2
|
2009-04-03 19:52:14 +00:00
|
|
|
|
|
|
|
class GenericRelationTests(TestCase):
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2009-04-03 19:52:14 +00:00
|
|
|
def test_inherited_models_content_type(self):
|
|
|
|
"""
|
|
|
|
Test that GenericRelations on inherited classes use the correct content
|
|
|
|
type.
|
|
|
|
"""
|
2010-01-22 18:39:48 +00:00
|
|
|
|
2009-04-03 19:52:14 +00:00
|
|
|
p = Place.objects.create(name="South Park")
|
2010-01-22 18:39:48 +00:00
|
|
|
r = Restaurant.objects.create(name="Chubby's")
|
2009-04-03 19:52:14 +00:00
|
|
|
l1 = Link.objects.create(content_object=p)
|
|
|
|
l2 = Link.objects.create(content_object=r)
|
|
|
|
self.assertEqual(list(p.links.all()), [l1])
|
|
|
|
self.assertEqual(list(r.links.all()), [l2])
|
2010-01-22 18:39:48 +00:00
|
|
|
|
|
|
|
def test_reverse_relation_pk(self):
|
|
|
|
"""
|
|
|
|
Test that the correct column name is used for the primary key on the
|
|
|
|
originating model of a query. See #12664.
|
|
|
|
"""
|
|
|
|
p = Person.objects.create(account=23, name='Chef')
|
|
|
|
a = Address.objects.create(street='123 Anywhere Place',
|
|
|
|
city='Conifer', state='CO',
|
|
|
|
zipcode='80433', content_object=p)
|
|
|
|
|
|
|
|
qs = Person.objects.filter(addresses__zipcode='80433')
|
|
|
|
self.assertEqual(1, qs.count())
|
|
|
|
self.assertEqual('Chef', qs[0].name)
|
2010-01-29 17:58:22 +00:00
|
|
|
|
|
|
|
def test_charlink_delete(self):
|
|
|
|
oddrel = OddRelation1.objects.create(name='clink')
|
|
|
|
cl = CharLink.objects.create(content_object=oddrel)
|
|
|
|
oddrel.delete()
|
|
|
|
|
|
|
|
def test_textlink_delete(self):
|
|
|
|
oddrel = OddRelation2.objects.create(name='tlink')
|
|
|
|
tl = TextLink.objects.create(content_object=oddrel)
|
|
|
|
oddrel.delete()
|