Ticket #10516: testcase.diff

File testcase.diff, 3.8 KB (added by Zain Memon, 16 years ago)

Failing testcase

  • 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
    46
    57class QuerysetOrderedTests(unittest.TestCase):
    68    """
     
    2426        qs = Annotation.objects.annotate(num_notes=Count('notes'))
    2527        self.assertEqual(qs.ordered, False)
    2628        self.assertEqual(qs.order_by('num_notes').ordered, True)
    27        
    28  No newline at end of file
     29
     30class QuerysetMergingTests(unittest.TestCase):
     31    """
     32    Tests for merging Querysets that have been filtered on multiple elements from the same
     33    base class
     34    """
     35     
     36    def runTest(self):
     37        match = Recommendation.objects.get(recommender__titletranslation__text__icontains='ar')
     38
     39        qs = Recommendation._default_manager.get_query_set()
     40       
     41        search_fields = [
     42            'titletranslation__text__icontains',
     43            'recommender__titletranslation__text__icontains'
     44        ]
     45       
     46        or_queries = [Q(**{field: u'ar'}) for field in search_fields]
     47       
     48        other_qs = QuerySet(Recommendation)
     49        other_qs.dup_select_related(qs)
     50        other_qs = other_qs.filter(reduce(operator.or_, or_queries))
     51       
     52        self.assert_(match in qs & other_qs)
     53 No newline at end of file
  • tests/regressiontests/queries/fixtures/initial_data.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": {"lang": "FI", "text": "Bar", "title": 1}}, {"pk": 2, "model": "queries.titletranslation", "fields": {"lang": "FI", "text": "Foo", "title": 2}}, {"pk": 3, "model": "queries.titletranslation", "fields": {"lang": "FI", "text": "Few", "title": 3}}, {"pk": 4, "model": "queries.titletranslation", "fields": {"lang": "FI", "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  def __unicode__(self):
     277    try:
     278      return self.titletranslation_set.filter(lang="FI")[0].text
     279    except:
     280      return "No finnish name defined!"
    274281
     282LANG_CHOICES = (('FI', 'Finnish'), ('EN', 'English'),)
     283
     284class TitleTranslation(models.Model):
     285  title = models.ForeignKey(Title)
     286  text = models.CharField(max_length = 100)
     287  lang = models.CharField(max_length = 2, choices = LANG_CHOICES)
     288
     289class Recommender(Title):
     290  pass
     291
     292class Recommendation(Title):
     293  recommender = models.ForeignKey(Recommender)
     294
     295
    275296__test__ = {'API_TESTS':"""
    276297>>> generic = NamedCategory.objects.create(name="Generic")
    277298>>> t1 = Tag.objects.create(name='t1', category=generic)
Back to Top