Ticket #10977: 10977_testcase.diff

File 10977_testcase.diff, 3.5 KB (added by Zain Memon, 16 years ago)

Failing test case demonstrating the conditions for this bug.

  • tests/regressiontests/queries/tests.py

     
    11import unittest
    2 from models import Tag, Annotation
     2from models import Tag, Annotation, Recommendation, Recommender
     3from django.db.models.query import QuerySet, Q
    34from django.db.models import Count
     5import operator
     6from django import test
    47
    58class QuerysetOrderedTests(unittest.TestCase):
    69    """
     
    2427        qs = Annotation.objects.annotate(num_notes=Count('notes'))
    2528        self.assertEqual(qs.ordered, False)
    2629        self.assertEqual(qs.order_by('num_notes').ordered, True)
    27        
    28  No newline at end of file
     30
     31class QuerysetMergingTests(test.TestCase):
     32    """
     33    Tests for merging Querysets that have been filtered on multiple elements from the same
     34    base class
     35    """
     36   
     37    fixtures = ['multiple_child_classes']
     38   
     39    def test_merging_models_with_same_parent(self):
     40        match = Recommendation.objects.get(recommender__titletranslation__text__icontains='ar')
     41
     42        qs = Recommendation._default_manager.get_query_set()
     43       
     44        search_fields = [
     45            'titletranslation__text__icontains',
     46            'recommender__titletranslation__text__icontains'
     47        ]
     48       
     49        or_queries = [Q(**{field: u'ar'}) for field in search_fields]
     50       
     51        other_qs = QuerySet(Recommendation)
     52        other_qs = other_qs.filter(reduce(operator.or_, or_queries))
     53        self.assert_(match in qs & other_qs)
     54 No newline at end of file
  • tests/regressiontests/queries/fixtures/multiple_child_classes.json

     
     1[{"pk": 1, "model": "queries.title", "fields": {}}, {"pk": 2, "model": "queries.title", "fields": {}}, {"pk": 3, "model": "queries.title", "fields": {}}, {"pk": 4, "model": "queries.title", "fields": {}}, {"pk": 1, "model": "queries.titletranslation", "fields": {"text": "Bar", "title": 1}}, {"pk": 2, "model": "queries.titletranslation", "fields": {"text": "Foo", "title": 2}}, {"pk": 3, "model": "queries.titletranslation", "fields": {"text": "Few", "title": 3}}, {"pk": 4, "model": "queries.titletranslation", "fields": {"text": "Bas", "title": 4}}, {"pk": 1, "model": "queries.recommender", "fields": {}}, {"pk": 4, "model": "queries.recommender", "fields": {}}, {"pk": 2, "model": "queries.recommendation", "fields": {"recommender": 1}}, {"pk": 3, "model": "queries.recommendation", "fields": {"recommender": 4}}]
  • tests/regressiontests/queries/models.py

     
    271271    def __unicode__(self):
    272272        return self.name
    273273
     274# a base class for Recommender and Recommendation
     275class Title(models.Model):
     276    pass
    274277
     278class TitleTranslation(models.Model):
     279    title = models.ForeignKey(Title)
     280    text = models.CharField(max_length=100)
     281
     282class Recommender(Title):
     283    pass
     284
     285class Recommendation(Title):
     286    recommender = models.ForeignKey(Recommender)
     287
     288
    275289__test__ = {'API_TESTS':"""
    276290>>> generic = NamedCategory.objects.create(name="Generic")
    277291>>> t1 = Tag.objects.create(name='t1', category=generic)
Back to Top