tests.py 2.3 KB

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