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