Ticket #11387: test_11387.diff
File test_11387.diff, 2.2 KB (added by , 15 years ago) |
---|
-
tests/regressiontests/generic_relations_regress/tests.py
1 1 from django.test import TestCase 2 2 from django.contrib.contenttypes.models import ContentType 3 from models import Link, Place, Restaurant 3 from models import Link, Place, Restaurant, Company 4 4 5 5 class GenericRelationTests(TestCase): 6 6 … … 16 16 l2 = Link.objects.create(content_object=r) 17 17 self.assertEqual(list(p.links.all()), [l1]) 18 18 self.assertEqual(list(r.links.all()), [l2]) 19 20 No newline at end of file 19 20 def test_generic_relation_ordering(self): 21 """ 22 Test that ordering over a generic relation does not include extraneous 23 duplicate results, nor excludes rows not participating in the relation. 24 """ 25 p1 = Place.objects.create(name="South Park") 26 p2 = Place.objects.create(name="The City") 27 c = Company.objects.create(name="Chubby's Intl.") 28 l1 = Link.objects.create(content_object=p1) 29 l2 = Link.objects.create(content_object=c) 30 31 places = list(Place.objects.order_by('links__id')) 32 def count_places(place): 33 return len(filter(lambda p: p.id == place.id, places)) 34 35 self.assertEqual(len(places), 2) 36 self.assertEqual(count_places(p1), 1) 37 self.assertEqual(count_places(p2), 1) -
tests/regressiontests/generic_relations_regress/models.py
19 19 20 20 class Restaurant(Place): 21 21 def __unicode__(self): 22 return "Restaurant: %s" % self.name 23 No newline at end of file 22 return "Restaurant: %s" % self.name 23 24 class Company(models.Model): 25 name = models.CharField(max_length=100) 26 links = generic.GenericRelation(Link) 27 28 def __unicode__(self): 29 return "Company: %s" % self.name 30 No newline at end of file