PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/generic_relations_regress/tests.py

https://code.google.com/p/mango-py/
Python | 74 lines | 41 code | 11 blank | 22 comment | 0 complexity | 2cd8060b8738c361ca6268cab7b6f17d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.test import TestCase
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.db.models import Q
  4. from models import *
  5. class GenericRelationTests(TestCase):
  6. def test_inherited_models_content_type(self):
  7. """
  8. Test that GenericRelations on inherited classes use the correct content
  9. type.
  10. """
  11. p = Place.objects.create(name="South Park")
  12. r = Restaurant.objects.create(name="Chubby's")
  13. l1 = Link.objects.create(content_object=p)
  14. l2 = Link.objects.create(content_object=r)
  15. self.assertEqual(list(p.links.all()), [l1])
  16. self.assertEqual(list(r.links.all()), [l2])
  17. def test_reverse_relation_pk(self):
  18. """
  19. Test that the correct column name is used for the primary key on the
  20. originating model of a query. See #12664.
  21. """
  22. p = Person.objects.create(account=23, name='Chef')
  23. a = Address.objects.create(street='123 Anywhere Place',
  24. city='Conifer', state='CO',
  25. zipcode='80433', content_object=p)
  26. qs = Person.objects.filter(addresses__zipcode='80433')
  27. self.assertEqual(1, qs.count())
  28. self.assertEqual('Chef', qs[0].name)
  29. def test_charlink_delete(self):
  30. oddrel = OddRelation1.objects.create(name='clink')
  31. cl = CharLink.objects.create(content_object=oddrel)
  32. oddrel.delete()
  33. def test_textlink_delete(self):
  34. oddrel = OddRelation2.objects.create(name='tlink')
  35. tl = TextLink.objects.create(content_object=oddrel)
  36. oddrel.delete()
  37. def test_q_object_or(self):
  38. """
  39. Tests that SQL query parameters for generic relations are properly
  40. grouped when OR is used.
  41. Test for bug http://code.djangoproject.com/ticket/11535
  42. In this bug the first query (below) works while the second, with the
  43. query parameters the same but in reverse order, does not.
  44. The issue is that the generic relation conditions do not get properly
  45. grouped in parentheses.
  46. """
  47. note_contact = Contact.objects.create()
  48. org_contact = Contact.objects.create()
  49. note = Note.objects.create(note='note', content_object=note_contact)
  50. org = Organization.objects.create(name='org name')
  51. org.contacts.add(org_contact)
  52. # search with a non-matching note and a matching org name
  53. qs = Contact.objects.filter(Q(notes__note__icontains=r'other note') |
  54. Q(organizations__name__icontains=r'org name'))
  55. self.assertTrue(org_contact in qs)
  56. # search again, with the same query parameters, in reverse order
  57. qs = Contact.objects.filter(
  58. Q(organizations__name__icontains=r'org name') |
  59. Q(notes__note__icontains=r'other note'))
  60. self.assertTrue(org_contact in qs)