2010-09-27 15:17:36 +00:00
|
|
|
from django.test import TestCase
|
2015-01-28 12:35:27 +00:00
|
|
|
|
|
|
|
from .models import Article, Bar, Base, Child, Foo, Whiz
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2010-09-27 15:17:36 +00:00
|
|
|
|
|
|
|
class StringLookupTests(TestCase):
|
|
|
|
def test_string_form_referencing(self):
|
|
|
|
"""
|
|
|
|
Regression test for #1661 and #1662
|
|
|
|
|
2016-10-27 07:53:39 +00:00
|
|
|
String form referencing of models works, both as pre and post
|
|
|
|
reference, on all RelatedField types.
|
2010-09-27 15:17:36 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
f1 = Foo(name="Foo1")
|
|
|
|
f1.save()
|
|
|
|
f2 = Foo(name="Foo2")
|
|
|
|
f2.save()
|
|
|
|
|
|
|
|
w1 = Whiz(name="Whiz1")
|
|
|
|
w1.save()
|
|
|
|
|
|
|
|
b1 = Bar(name="Bar1", normal=f1, fwd=w1, back=f2)
|
|
|
|
b1.save()
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(b1.normal, f1)
|
2010-09-27 15:17:36 +00:00
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(b1.fwd, w1)
|
2010-09-27 15:17:36 +00:00
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(b1.back, f2)
|
2010-09-27 15:17:36 +00:00
|
|
|
|
|
|
|
base1 = Base(name="Base1")
|
|
|
|
base1.save()
|
|
|
|
|
|
|
|
child1 = Child(name="Child1", parent=base1)
|
|
|
|
child1.save()
|
|
|
|
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(child1.parent, base1)
|
2010-09-27 15:17:36 +00:00
|
|
|
|
|
|
|
def test_unicode_chars_in_queries(self):
|
|
|
|
"""
|
|
|
|
Regression tests for #3937
|
|
|
|
|
|
|
|
make sure we can use unicode characters in queries.
|
|
|
|
If these tests fail on MySQL, it's a problem with the test setup.
|
|
|
|
A properly configured UTF-8 database can handle this.
|
|
|
|
"""
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
fx = Foo(name="Bjorn", friend="François")
|
2010-09-27 15:17:36 +00:00
|
|
|
fx.save()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(Foo.objects.get(friend__contains="\xe7"), fx)
|
2010-09-27 15:17:36 +00:00
|
|
|
|
|
|
|
def test_queries_on_textfields(self):
|
|
|
|
"""
|
|
|
|
Regression tests for #5087
|
|
|
|
|
|
|
|
make sure we can perform queries on TextFields.
|
|
|
|
"""
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
a = Article(name="Test", text="The quick brown fox jumps over the lazy dog.")
|
2010-09-27 15:17:36 +00:00
|
|
|
a.save()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.get(
|
|
|
|
text__exact="The quick brown fox jumps over the lazy dog."
|
|
|
|
),
|
|
|
|
a,
|
|
|
|
)
|
2010-09-27 15:17:36 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(Article.objects.get(text__contains="quick brown fox"), a)
|
2010-09-27 15:17:36 +00:00
|
|
|
|
|
|
|
def test_ipaddress_on_postgresql(self):
|
|
|
|
"""
|
|
|
|
Regression test for #708
|
|
|
|
|
2013-05-19 11:28:09 +00:00
|
|
|
"like" queries on IP address fields require casting with HOST() (on PostgreSQL).
|
2010-09-27 15:17:36 +00:00
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
a = Article(name="IP test", text="The body", submitted_from="192.0.2.100")
|
2010-09-27 15:17:36 +00:00
|
|
|
a.save()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Article.objects.filter(submitted_from__contains="192.0.2"), [a]
|
|
|
|
)
|
2016-10-27 07:53:39 +00:00
|
|
|
# The searches do not match the subnet mask (/32 in this case)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
Article.objects.filter(submitted_from__contains="32").count(), 0
|
|
|
|
)
|