From 6b5a240d50e83a0aef8c74d7423cc4e5178d357f Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 27 Sep 2010 15:17:36 +0000 Subject: [PATCH] Migrated string_lookup doctests. Thanks to Stephan Jaekel. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13895 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/string_lookup/models.py | 64 --------------- tests/regressiontests/string_lookup/tests.py | 78 +++++++++++++++++++ 2 files changed, 78 insertions(+), 64 deletions(-) create mode 100644 tests/regressiontests/string_lookup/tests.py diff --git a/tests/regressiontests/string_lookup/models.py b/tests/regressiontests/string_lookup/models.py index 1bdb2d4452..037854dadd 100644 --- a/tests/regressiontests/string_lookup/models.py +++ b/tests/regressiontests/string_lookup/models.py @@ -43,67 +43,3 @@ class Article(models.Model): def __str__(self): return "Article %s" % self.name - -__test__ = {'API_TESTS': ur""" -# Regression test for #1661 and #1662: Check that string form referencing of -# models works, both as pre and post reference, on all RelatedField types. - ->>> 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() - ->>> b1.normal - - ->>> b1.fwd - - ->>> b1.back - - ->>> base1 = Base(name="Base1") ->>> base1.save() - ->>> child1 = Child(name="Child1", parent=base1) ->>> child1.save() - ->>> child1.parent - - -# Regression tests for #3937: make sure we can use unicode characters in -# queries. -# BUG: These tests fail on MySQL, but it's a problem with the test setup. A -# properly configured UTF-8 database can handle this. - ->>> fx = Foo(name='Bjorn', friend=u'François') ->>> fx.save() ->>> Foo.objects.get(friend__contains=u'\xe7') - - -# We can also do the above query using UTF-8 strings. ->>> Foo.objects.get(friend__contains='\xc3\xa7') - - -# Regression tests for #5087: make sure we can perform queries on TextFields. ->>> a = Article(name='Test', text='The quick brown fox jumps over the lazy dog.') ->>> a.save() ->>> Article.objects.get(text__exact='The quick brown fox jumps over the lazy dog.') - - ->>> Article.objects.get(text__contains='quick brown fox') - - -# Regression test for #708: "like" queries on IP address fields require casting -# to text (on PostgreSQL). ->>> Article(name='IP test', text='The body', submitted_from='192.0.2.100').save() ->>> Article.objects.filter(submitted_from__contains='192.0.2') -[] - -"""} diff --git a/tests/regressiontests/string_lookup/tests.py b/tests/regressiontests/string_lookup/tests.py new file mode 100644 index 0000000000..ddf7a8a76c --- /dev/null +++ b/tests/regressiontests/string_lookup/tests.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +from django.test import TestCase +from regressiontests.string_lookup.models import Foo, Whiz, Bar, Article, Base, Child + +class StringLookupTests(TestCase): + + def test_string_form_referencing(self): + """ + Regression test for #1661 and #1662 + + Check that string form referencing of + models works, both as pre and post reference, on all RelatedField types. + """ + + 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() + + self.assertEquals(b1.normal, f1) + + self.assertEquals(b1.fwd, w1) + + self.assertEquals(b1.back, f2) + + base1 = Base(name="Base1") + base1.save() + + child1 = Child(name="Child1", parent=base1) + child1.save() + + self.assertEquals(child1.parent, base1) + + 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. + """ + + fx = Foo(name='Bjorn', friend=u'François') + fx.save() + self.assertEquals(Foo.objects.get(friend__contains=u'\xe7'), fx) + + # We can also do the above query using UTF-8 strings. + self.assertEquals(Foo.objects.get(friend__contains='\xc3\xa7'), fx) + + def test_queries_on_textfields(self): + """ + Regression tests for #5087 + + make sure we can perform queries on TextFields. + """ + + a = Article(name='Test', text='The quick brown fox jumps over the lazy dog.') + a.save() + self.assertEquals(Article.objects.get(text__exact='The quick brown fox jumps over the lazy dog.'), a) + + self.assertEquals(Article.objects.get(text__contains='quick brown fox'), a) + + def test_ipaddress_on_postgresql(self): + """ + Regression test for #708 + + "like" queries on IP address fields require casting to text (on PostgreSQL). + """ + a = Article(name='IP test', text='The body', submitted_from='192.0.2.100') + a.save() + self.assertEquals(repr(Article.objects.filter(submitted_from__contains='192.0.2')), + repr([a]))