tests.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. from django.test import TestCase
  3. from regressiontests.string_lookup.models import Foo, Whiz, Bar, Article, Base, Child
  4. class StringLookupTests(TestCase):
  5. def test_string_form_referencing(self):
  6. """
  7. Regression test for #1661 and #1662
  8. Check that string form referencing of
  9. models works, both as pre and post reference, on all RelatedField types.
  10. """
  11. f1 = Foo(name="Foo1")
  12. f1.save()
  13. f2 = Foo(name="Foo2")
  14. f2.save()
  15. w1 = Whiz(name="Whiz1")
  16. w1.save()
  17. b1 = Bar(name="Bar1", normal=f1, fwd=w1, back=f2)
  18. b1.save()
  19. self.assertEqual(b1.normal, f1)
  20. self.assertEqual(b1.fwd, w1)
  21. self.assertEqual(b1.back, f2)
  22. base1 = Base(name="Base1")
  23. base1.save()
  24. child1 = Child(name="Child1", parent=base1)
  25. child1.save()
  26. self.assertEqual(child1.parent, base1)
  27. def test_unicode_chars_in_queries(self):
  28. """
  29. Regression tests for #3937
  30. make sure we can use unicode characters in queries.
  31. If these tests fail on MySQL, it's a problem with the test setup.
  32. A properly configured UTF-8 database can handle this.
  33. """
  34. fx = Foo(name='Bjorn', friend=u'François')
  35. fx.save()
  36. self.assertEqual(Foo.objects.get(friend__contains=u'\xe7'), fx)
  37. # We can also do the above query using UTF-8 strings.
  38. self.assertEqual(Foo.objects.get(friend__contains='\xc3\xa7'), fx)
  39. def test_queries_on_textfields(self):
  40. """
  41. Regression tests for #5087
  42. make sure we can perform queries on TextFields.
  43. """
  44. a = Article(name='Test', text='The quick brown fox jumps over the lazy dog.')
  45. a.save()
  46. self.assertEqual(Article.objects.get(text__exact='The quick brown fox jumps over the lazy dog.'), a)
  47. self.assertEqual(Article.objects.get(text__contains='quick brown fox'), a)
  48. def test_ipaddress_on_postgresql(self):
  49. """
  50. Regression test for #708
  51. "like" queries on IP address fields require casting to text (on PostgreSQL).
  52. """
  53. a = Article(name='IP test', text='The body', submitted_from='192.0.2.100')
  54. a.save()
  55. self.assertEqual(repr(Article.objects.filter(submitted_from__contains='192.0.2')),
  56. repr([a]))