tests.py 2.5 KB

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