diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/string_lookup | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/string_lookup')
-rw-r--r-- | tests/string_lookup/__init__.py | 0 | ||||
-rw-r--r-- | tests/string_lookup/models.py | 55 | ||||
-rw-r--r-- | tests/string_lookup/tests.py | 81 |
3 files changed, 136 insertions, 0 deletions
diff --git a/tests/string_lookup/__init__.py b/tests/string_lookup/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/string_lookup/__init__.py diff --git a/tests/string_lookup/models.py b/tests/string_lookup/models.py new file mode 100644 index 0000000000..a2d64cd0b2 --- /dev/null +++ b/tests/string_lookup/models.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + + +@python_2_unicode_compatible +class Foo(models.Model): + name = models.CharField(max_length=50) + friend = models.CharField(max_length=50, blank=True) + + def __str__(self): + return "Foo %s" % self.name + +@python_2_unicode_compatible +class Bar(models.Model): + name = models.CharField(max_length=50) + normal = models.ForeignKey(Foo, related_name='normal_foo') + fwd = models.ForeignKey("Whiz") + back = models.ForeignKey("Foo") + + def __str__(self): + return "Bar %s" % self.place.name + +@python_2_unicode_compatible +class Whiz(models.Model): + name = models.CharField(max_length=50) + + def __str__(self): + return "Whiz %s" % self.name + +@python_2_unicode_compatible +class Child(models.Model): + parent = models.OneToOneField('Base') + name = models.CharField(max_length=50) + + def __str__(self): + return "Child %s" % self.name + +@python_2_unicode_compatible +class Base(models.Model): + name = models.CharField(max_length=50) + + def __str__(self): + return "Base %s" % self.name + +@python_2_unicode_compatible +class Article(models.Model): + name = models.CharField(max_length=50) + text = models.TextField() + submitted_from = models.IPAddressField(blank=True, null=True) + + def __str__(self): + return "Article %s" % self.name diff --git a/tests/string_lookup/tests.py b/tests/string_lookup/tests.py new file mode 100644 index 0000000000..02f766adce --- /dev/null +++ b/tests/string_lookup/tests.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, unicode_literals + +from django.test import TestCase +from .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.assertEqual(b1.normal, f1) + + self.assertEqual(b1.fwd, w1) + + self.assertEqual(b1.back, f2) + + base1 = Base(name="Base1") + base1.save() + + child1 = Child(name="Child1", parent=base1) + child1.save() + + self.assertEqual(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='François') + fx.save() + self.assertEqual(Foo.objects.get(friend__contains='\xe7'), fx) + + # We can also do the above query using UTF-8 strings. + self.assertEqual(Foo.objects.get(friend__contains=b'\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.assertEqual(Article.objects.get(text__exact='The quick brown fox jumps over the lazy dog.'), a) + + self.assertEqual(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.assertEqual(repr(Article.objects.filter(submitted_from__contains='192.0.2')), + repr([a])) |