summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2007-06-27 18:58:10 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2007-06-27 18:58:10 +0000
commit24512a74befc6282a1d299cab452ee9463cc2baa (patch)
tree7654c2ed72d28b7a0b9f47c0bfbebb9b75949ae6 /tests
parent7dc8b1a1a8a8e2771f37568a125fd51a3283b043 (diff)
Fixed #1465: added support for regex lookups. Thanks, Tom Tobin.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5555 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/lookup/models.py94
-rw-r--r--tests/regressiontests/templates/tests.py3
2 files changed, 97 insertions, 0 deletions
diff --git a/tests/modeltests/lookup/models.py b/tests/modeltests/lookup/models.py
index 6af70f8351..60ed6f7685 100644
--- a/tests/modeltests/lookup/models.py
+++ b/tests/modeltests/lookup/models.py
@@ -251,4 +251,98 @@ Traceback (most recent call last):
...
TypeError: Cannot resolve keyword 'headline__starts' into field. Choices are: id, headline, pub_date
+# Create some articles with a bit more interesting headlines for testing field lookups:
+>>> now = datetime.now()
+>>> for a in Article.objects.all():
+... a.delete()
+>>> a1 = Article(pub_date=now, headline='f')
+>>> a1.save()
+>>> a2 = Article(pub_date=now, headline='fo')
+>>> a2.save()
+>>> a3 = Article(pub_date=now, headline='foo')
+>>> a3.save()
+>>> a4 = Article(pub_date=now, headline='fooo')
+>>> a4.save()
+>>> a5 = Article(pub_date=now, headline='Foo')
+>>> a5.save()
+
+# zero-or-more
+>>> Article.objects.filter(headline__regex=r'fo*')
+[<Article: f>, <Article: fo>, <Article: foo>, <Article: fooo>]
+>>> Article.objects.filter(headline__iregex=r'fo*')
+[<Article: Foo>, <Article: f>, <Article: fo>, <Article: foo>, <Article: fooo>]
+
+# one-or-more
+>>> Article.objects.filter(headline__regex=r'fo+')
+[<Article: fo>, <Article: foo>, <Article: fooo>]
+
+# wildcard
+>>> Article.objects.filter(headline__regex=r'fooo?')
+[<Article: foo>, <Article: fooo>]
+
+# and some more:
+>>> a6 = Article(pub_date=now, headline='bar')
+>>> a6.save()
+>>> a7 = Article(pub_date=now, headline='Bar')
+>>> a7.save()
+>>> a8 = Article(pub_date=now, headline='baz')
+>>> a8.save()
+>>> a9 = Article(pub_date=now, headline='baZ')
+>>> a9.save()
+
+# leading anchor
+>>> Article.objects.filter(headline__regex=r'^b')
+[<Article: baZ>, <Article: bar>, <Article: baz>]
+>>> Article.objects.filter(headline__iregex=r'^b')
+[<Article: Bar>, <Article: baZ>, <Article: bar>, <Article: baz>]
+
+# trailing anchor
+>>> Article.objects.filter(headline__regex=r'z$')
+[<Article: baz>]
+>>> Article.objects.filter(headline__iregex=r'z$')
+[<Article: baZ>, <Article: baz>]
+
+# character sets
+>>> Article.objects.filter(headline__regex=r'ba[rz]')
+[<Article: bar>, <Article: baz>]
+>>> Article.objects.filter(headline__regex=r'ba[RZ]')
+[<Article: baZ>]
+>>> Article.objects.filter(headline__iregex=r'ba[RZ]')
+[<Article: Bar>, <Article: baZ>, <Article: bar>, <Article: baz>]
+
+# and yet more:
+>>> a10 = Article(pub_date=now, headline='foobar')
+>>> a10.save()
+>>> a11 = Article(pub_date=now, headline='foobaz')
+>>> a11.save()
+>>> a12 = Article(pub_date=now, headline='FooBarBaz')
+>>> a12.save()
+>>> a13 = Article(pub_date=now, headline='foobarbaz')
+>>> a13.save()
+>>> a14 = Article(pub_date=now, headline='zoocarfaz')
+>>> a14.save()
+>>> a15 = Article(pub_date=now, headline='barfoobaz')
+>>> a15.save()
+>>> a16 = Article(pub_date=now, headline='BAZBARFOO')
+>>> a16.save()
+
+# alternation
+>>> Article.objects.filter(headline__regex=r'foo(bar|baz)')
+[<Article: barfoobaz>, <Article: foobar>, <Article: foobarbaz>, <Article: foobaz>]
+>>> Article.objects.filter(headline__iregex=r'foo(bar|baz)')
+[<Article: FooBarBaz>, <Article: barfoobaz>, <Article: foobar>, <Article: foobarbaz>, <Article: foobaz>]
+>>> Article.objects.filter(headline__regex=r'^foo(bar|baz)')
+[<Article: foobar>, <Article: foobarbaz>, <Article: foobaz>]
+
+# greedy matching
+>>> Article.objects.filter(headline__regex=r'f.*z')
+[<Article: barfoobaz>, <Article: foobarbaz>, <Article: foobaz>, <Article: zoocarfaz>]
+>>> Article.objects.filter(headline__iregex=r'f.*z')
+[<Article: FooBarBaz>, <Article: barfoobaz>, <Article: foobarbaz>, <Article: foobaz>, <Article: zoocarfaz>]
+
+# grouping and backreferences
+>>> Article.objects.filter(headline__regex=r'b(.).*b\1')
+[<Article: barfoobaz>, <Article: foobarbaz>]
+>>> Article.objects.filter(headline__iregex=r'b(.).*b\1')
+[<Article: BAZBARFOO>, <Article: FooBarBaz>, <Article: barfoobaz>, <Article: foobarbaz>]
"""}
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 8c2389b28a..8801100bcc 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -219,6 +219,9 @@ class Templates(unittest.TestCase):
# value will be converted to a bytestring.
'filter-syntax18': (r'{{ var }}', {'var': UnicodeInStrClass()}, '\xc5\xa0\xc4\x90\xc4\x86\xc5\xbd\xc4\x87\xc5\xbe\xc5\xa1\xc4\x91'),
+ # Numbers as filter arguments should work
+ 'filter-syntax19': ('{{ var|truncatewords:1 }}', {"var": "hello world"}, "hello ..."),
+
### COMMENT SYNTAX ########################################################
'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"),
'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"),