summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/lookup/models.py94
-rw-r--r--tests/regressiontests/datatypes/__init__.py0
-rw-r--r--tests/regressiontests/datatypes/models.py59
-rw-r--r--tests/regressiontests/defaultfilters/tests.py13
-rw-r--r--tests/regressiontests/forms/tests.py24
-rw-r--r--tests/regressiontests/serializers_regress/tests.py16
-rw-r--r--tests/regressiontests/templates/tests.py5
-rw-r--r--tests/regressiontests/templates/urls.py2
8 files changed, 206 insertions, 7 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/datatypes/__init__.py b/tests/regressiontests/datatypes/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/datatypes/__init__.py
diff --git a/tests/regressiontests/datatypes/models.py b/tests/regressiontests/datatypes/models.py
new file mode 100644
index 0000000000..8c5c8c285d
--- /dev/null
+++ b/tests/regressiontests/datatypes/models.py
@@ -0,0 +1,59 @@
+"""
+This is a basic model to test saving and loading boolean and date-related
+types, which in the past were problematic for some database backends.
+"""
+
+from django.db import models
+from django.conf import settings
+
+class Donut(models.Model):
+ name = models.CharField(maxlength=100)
+ is_frosted = models.BooleanField(default=False)
+ has_sprinkles = models.NullBooleanField()
+ baked_date = models.DateField(null=True)
+ baked_time = models.TimeField(null=True)
+ consumed_at = models.DateTimeField(null=True)
+
+ class Meta:
+ ordering = ('consumed_at',)
+
+ def __str__(self):
+ return self.name
+
+__test__ = {'API_TESTS': """
+# No donuts are in the system yet.
+>>> Donut.objects.all()
+[]
+
+>>> d = Donut(name='Apple Fritter')
+
+# Ensure we're getting True and False, not 0 and 1
+>>> d.is_frosted
+False
+>>> d.has_sprinkles
+>>> d.has_sprinkles = True
+>>> d.has_sprinkles == True
+True
+>>> d.save()
+>>> d2 = Donut.objects.all()[0]
+>>> d2
+<Donut: Apple Fritter>
+>>> d2.is_frosted == False
+True
+>>> d2.has_sprinkles == True
+True
+
+>>> import datetime
+>>> d2.baked_date = datetime.date(year=1938, month=6, day=4)
+>>> d2.baked_time = datetime.time(hour=5, minute=30)
+>>> d2.consumed_at = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59)
+>>> d2.save()
+
+>>> d3 = Donut.objects.all()[0]
+>>> d3.baked_date
+datetime.date(1938, 6, 4)
+>>> d3.baked_time
+datetime.time(5, 30)
+>>> d3.consumed_at
+datetime.datetime(2007, 4, 20, 16, 19, 59)
+"""}
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
index 4a2e9432b0..b35636aba6 100644
--- a/tests/regressiontests/defaultfilters/tests.py
+++ b/tests/regressiontests/defaultfilters/tests.py
@@ -121,7 +121,18 @@ u'\xcb'
'<a href="http://short.com/" rel="nofollow">http://short.com/</a>'
>>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
-'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google.co...</a>'
+'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>'
+
+# Check truncating of URIs which are the exact length
+>>> uri = 'http://31characteruri.com/test/'
+>>> len(uri)
+31
+>>> urlizetrunc(uri, 31)
+'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/test/</a>'
+>>> urlizetrunc(uri, 30)
+'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/t...</a>'
+>>> urlizetrunc(uri, 2)
+'<a href="http://31characteruri.com/test/" rel="nofollow">...</a>'
>>> wordcount('')
0
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index 4114919c2e..892498c3ae 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -1860,8 +1860,12 @@ ValidationError: [u'Enter a valid date.']
>>> f = SplitDateTimeField(required=False)
>>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])
datetime.datetime(2006, 1, 10, 7, 30)
+>>> f.clean(['2006-01-10', '07:30'])
+datetime.datetime(2006, 1, 10, 7, 30)
>>> f.clean(None)
>>> f.clean('')
+>>> f.clean([''])
+>>> f.clean(['', ''])
>>> f.clean('hello')
Traceback (most recent call last):
...
@@ -1878,6 +1882,18 @@ ValidationError: [u'Enter a valid time.']
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid date.']
+>>> f.clean(['2006-01-10', ''])
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid time.']
+>>> f.clean(['2006-01-10'])
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid time.']
+>>> f.clean(['', '07:30'])
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a valid date.']
#########
# Forms #
@@ -1959,11 +1975,11 @@ AttributeError: 'Person' object has no attribute 'cleaned_data'
<li><ul class="errorlist"><li>This field is required.</li></ul><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
<li><ul class="errorlist"><li>This field is required.</li></ul><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>
>>> print p.as_p()
-<p><ul class="errorlist"><li>This field is required.</li></ul></p>
+<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
-<p><ul class="errorlist"><li>This field is required.</li></ul></p>
+<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
-<p><ul class="errorlist"><li>This field is required.</li></ul></p>
+<ul class="errorlist"><li>This field is required.</li></ul>
<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
If you don't pass any values to the Form's __init__(), or if you pass None,
@@ -2669,7 +2685,7 @@ its field's order in the form.
<li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>
>>> print p.as_p()
-<p><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></p>
+<ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul>
<p>First name: <input type="text" name="first_name" value="John" /></p>
<p>Last name: <input type="text" name="last_name" value="Lennon" /></p>
<p>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>
diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
index 1a144c8356..febcfa822e 100644
--- a/tests/regressiontests/serializers_regress/tests.py
+++ b/tests/regressiontests/serializers_regress/tests.py
@@ -15,6 +15,7 @@ from django.utils.functional import curry
from django.core import serializers
from django.db import transaction
from django.core import management
+from django.conf import settings
from models import *
try:
@@ -116,10 +117,13 @@ test_data = [
(data_obj, 31, DateTimeData, None),
(data_obj, 40, EmailData, "hovercraft@example.com"),
(data_obj, 41, EmailData, None),
+ (data_obj, 42, EmailData, ""),
(data_obj, 50, FileData, 'file:///foo/bar/whiz.txt'),
(data_obj, 51, FileData, None),
+ (data_obj, 52, FileData, ""),
(data_obj, 60, FilePathData, "/foo/bar/whiz.txt"),
(data_obj, 61, FilePathData, None),
+ (data_obj, 62, FilePathData, ""),
(data_obj, 70, DecimalData, decimal.Decimal('12.345')),
(data_obj, 71, DecimalData, decimal.Decimal('-12.345')),
(data_obj, 72, DecimalData, decimal.Decimal('0.0')),
@@ -146,6 +150,7 @@ test_data = [
(data_obj, 131, PositiveSmallIntegerData, None),
(data_obj, 140, SlugData, "this-is-a-slug"),
(data_obj, 141, SlugData, None),
+ (data_obj, 142, SlugData, ""),
(data_obj, 150, SmallData, 12),
(data_obj, 151, SmallData, -12),
(data_obj, 152, SmallData, 0),
@@ -160,8 +165,10 @@ The end."""),
(data_obj, 171, TimeData, None),
(data_obj, 180, USStateData, "MA"),
(data_obj, 181, USStateData, None),
+ (data_obj, 182, USStateData, ""),
(data_obj, 190, XMLData, "<foo></foo>"),
(data_obj, 191, XMLData, None),
+ (data_obj, 192, XMLData, ""),
(generic_obj, 200, GenericData, ['Generic Object 1', 'tag1', 'tag2']),
(generic_obj, 201, GenericData, ['Generic Object 2', 'tag2', 'tag3']),
@@ -241,6 +248,15 @@ The end."""),
# (pk_obj, 790, XMLPKData, "<foo></foo>"),
]
+# Because Oracle treats the empty string as NULL, Oracle is expected to fail
+# when field.empty_strings_allowed is True and the value is None; skip these
+# tests.
+if settings.DATABASE_ENGINE == 'oracle':
+ test_data = [data for data in test_data
+ if not (data[0] == data_obj and
+ data[2]._meta.get_field('data').empty_strings_allowed and
+ data[3] is None)]
+
# Dynamically create serializer tests to ensure that all
# registered serializers are automatically tested.
class SerializerTests(unittest.TestCase):
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index e983a539ae..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"),
@@ -725,7 +728,7 @@ class Templates(unittest.TestCase):
'url01' : ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),
'url02' : ('{% url regressiontests.templates.views.client_action client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'),
'url03' : ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'),
- 'url04' : ('{% url named-client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
+ 'url04' : ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'),
# Failures
'url-fail01' : ('{% url %}', {}, template.TemplateSyntaxError),
diff --git a/tests/regressiontests/templates/urls.py b/tests/regressiontests/templates/urls.py
index eaa9fd5d9f..5fbade5c58 100644
--- a/tests/regressiontests/templates/urls.py
+++ b/tests/regressiontests/templates/urls.py
@@ -7,5 +7,5 @@ urlpatterns = patterns('',
(r'^$', views.index),
(r'^client/(\d+)/$', views.client),
(r'^client/(\d+)/(?P<action>[^/]+)/$', views.client_action),
- url(r'^named-client/(\d+)/$', views.client, name="named-client"),
+ url(r'^named-client/(\d+)/$', views.client, name="named.client"),
)