Ticket #7125: failing_many_to_one_mutiple_test.diff

File failing_many_to_one_mutiple_test.diff, 2.1 KB (added by oyvind, 16 years ago)

Adds a test for this issue to many_to_one modeltest.

  • tests/modeltests/many_to_one/models.py

    diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py
    index 53ad446..e79a894 100644
    a b class Article(models.Model):  
    2525    class Meta:
    2626        ordering = ('headline',)
    2727
     28class Category(models.Model):
     29    name = models.CharField(max_length=20)
     30
     31class Record(models.Model):
     32    category = models.ForeignKey(Category)
     33
     34class Relation(models.Model):
     35    left = models.ForeignKey(Record, related_name='left_set')
     36    right = models.ForeignKey(Record, related_name='right_set')
     37
    2838__test__ = {'API_TESTS':"""
    2939# Create a few Reporters.
    3040>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com')
    True  
    286296[datetime.datetime(1980, 4, 1, 0, 0)]
    287297>>> Article.objects.select_related().dates('pub_date', 'year')
    288298[datetime.datetime(1980, 1, 1, 0, 0)]
     299
     300# Test of multiple ForeignKeys to the same model
     301# Add some categories/records/relations
     302
     303>>> c1 = Category.objects.create(name='First')
     304>>> c2 = Category.objects.create(name='Second')
     305>>> r1 = Record.objects.create(category=c1)
     306>>> r2 = Record.objects.create(category=c1)
     307>>> r3 = Record.objects.create(category=c2)
     308>>> r4 = Record.objects.create(category=c2)
     309>>> r = Relation.objects.create(left=r1, right=r2)
     310>>> r = Relation.objects.create(left=r3, right=r4)
     311>>> r = Relation.objects.create(left=r1, right=r3)
     312
     313# Yields wrong sql
     314>>> Relation.objects.filter(left__category__name__in=['Second'], right__category__name__in=['First']).query.as_sql()
     315('SELECT "many_to_one_relation"."id", "many_to_one_relation"."left_id", "many_to_one_relation"."right_id" FROM "many_to_one_relation" INNER JOIN "many_to_one_record" ON ("many_to_one_relation"."right_id" = "many_to_one_record"."id") INNER JOIN "many_to_one_category" ON ("many_to_one_record"."category_id" = "many_to_one_category"."id") INNER JOIN "many_to_one_record" T4 ON ("many_to_one_relation"."left_id" = T4."id") INNER JOIN "many_to_one_record" T5 ON ("many_to_one_relation"."right_id" = T5."id") WHERE T5."name" IN (%s) AND T4."name" IN (%s)', ('First', 'Second'))
    289316"""}
Back to Top