Skip to content

Commit 521be8c

Browse files
committed
Migrated i18n and field_defaults doctests. Thanks to Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13779 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 3fe5c61 commit 521be8c

File tree

4 files changed

+34
-47
lines changed

4 files changed

+34
-47
lines changed

tests/modeltests/field_defaults/models.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,3 @@ class Article(models.Model):
1919

2020
def __unicode__(self):
2121
return self.headline
22-
23-
__test__ = {'API_TESTS': u"""
24-
>>> from datetime import datetime
25-
26-
# No articles are in the system yet.
27-
>>> Article.objects.all()
28-
[]
29-
30-
# Create an Article.
31-
>>> a = Article(id=None)
32-
33-
# Grab the current datetime it should be very close to the default that just
34-
# got saved as a.pub_date
35-
>>> now = datetime.now()
36-
37-
# Save it into the database. You have to call save() explicitly.
38-
>>> a.save()
39-
40-
# Now it has an ID. Note it's a long integer, as designated by the trailing "L".
41-
>>> a.id
42-
1L
43-
44-
# Access database columns via Python attributes.
45-
>>> a.headline
46-
u'Default headline'
47-
48-
# make sure the two dates are sufficiently close
49-
>>> d = now - a.pub_date
50-
>>> d.seconds < 5
51-
True
52-
53-
# make sure that SafeString/SafeUnicode fields work
54-
>>> from django.utils.safestring import SafeUnicode, SafeString
55-
>>> a.headline = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
56-
>>> a.save()
57-
>>> a.headline = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
58-
>>> a.save()
59-
"""}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from datetime import datetime
2+
3+
from django.test import TestCase
4+
5+
from models import Article
6+
7+
8+
class DefaultTests(TestCase):
9+
def test_field_defaults(self):
10+
a = Article()
11+
now = datetime.now()
12+
a.save()
13+
14+
self.assertTrue(isinstance(a.id, (int, long)))
15+
self.assertEqual(a.headline, "Default headline")
16+
self.assertTrue((now - a.pub_date).seconds < 5)

tests/regressiontests/i18n/models.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@ class Company(models.Model):
1010
date_added = models.DateTimeField(default=datetime(1799,1,31,23,59,59,0))
1111
cents_payed = models.DecimalField(max_digits=4, decimal_places=2)
1212
products_delivered = models.IntegerField()
13-
14-
__test__ = {'API_TESTS': '''
15-
>>> tm = TestModel()
16-
>>> tm.save()
17-
'''
18-
}

tests/regressiontests/i18n/tests.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
import sys
66
import pickle
77

8-
from django.template import Template, Context
98
from django.conf import settings
9+
from django.template import Template, Context
10+
from django.test import TestCase
1011
from django.utils.formats import get_format, date_format, time_format, localize, localize_input
1112
from django.utils.numberformat import format as nformat
12-
from django.test import TestCase
13+
from django.utils.safestring import mark_safe, SafeString, SafeUnicode
1314
from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale
1415

16+
1517
from forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm
18+
from models import Company, TestModel
1619

1720

1821
class TranslationTests(TestCase):
@@ -59,7 +62,6 @@ def test_safe_status(self):
5962
"""
6063
Translating a string requiring no auto-escaping shouldn't change the "safe" status.
6164
"""
62-
from django.utils.safestring import mark_safe, SafeString, SafeUnicode
6365
s = mark_safe('Password')
6466
self.assertEqual(SafeString, type(s))
6567
activate('de')
@@ -620,3 +622,16 @@ class DjangoFallbackResolutionOrderI18NTests(ResolutionOrderI18NTests):
620622

621623
def test_django_fallback(self):
622624
self.assertUgettext('Date/time', 'Datum/Zeit')
625+
626+
627+
class TestModels(TestCase):
628+
def test_lazy(self):
629+
tm = TestModel()
630+
tm.save()
631+
632+
def test_safestr(self):
633+
c = Company(cents_payed=12, products_delivered=1)
634+
c.name = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
635+
c.save()
636+
c.name = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
637+
c.save()

0 commit comments

Comments
 (0)