Ticket #11387: test_11387.diff

File test_11387.diff, 2.2 KB (added by Andy Durdin, 15 years ago)
  • tests/regressiontests/generic_relations_regress/tests.py

     
    11from django.test import TestCase
    22from django.contrib.contenttypes.models import ContentType
    3 from models import Link, Place, Restaurant
     3from models import Link, Place, Restaurant, Company
    44
    55class GenericRelationTests(TestCase):
    66   
     
    1616        l2 = Link.objects.create(content_object=r)
    1717        self.assertEqual(list(p.links.all()), [l1])
    1818        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

     
    1919   
    2020class Restaurant(Place):
    2121    def __unicode__(self):
    22         return "Restaurant: %s" % self.name
    23  No newline at end of file
     22        return "Restaurant: %s" % self.name
     23
     24class 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
Back to Top