summaryrefslogtreecommitdiff
path: root/tests/str
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2013-02-26 09:53:47 +0100
committerFlorian Apolloner <florian@apolloner.eu>2013-02-26 14:36:57 +0100
commit89f40e36246100df6a11316c31a76712ebc6c501 (patch)
tree6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/str
parentb3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff)
downloaddjango-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/str')
-rw-r--r--tests/str/__init__.py0
-rw-r--r--tests/str/models.py44
-rw-r--r--tests/str/tests.py45
3 files changed, 89 insertions, 0 deletions
diff --git a/tests/str/__init__.py b/tests/str/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/str/__init__.py
diff --git a/tests/str/models.py b/tests/str/models.py
new file mode 100644
index 0000000000..1c158ea008
--- /dev/null
+++ b/tests/str/models.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+"""
+2. Adding __str__() or __unicode__() to models
+
+Although it's not a strict requirement, each model should have a
+``_str__()`` or ``__unicode__()`` method to return a "human-readable"
+representation of the object. Do this not only for your own sanity when dealing
+with the interactive prompt, but also because objects' representations are used
+throughout Django's automatically-generated admin.
+
+Normally, you should write ``__unicode__()`` method, since this will work for
+all field types (and Django will automatically provide an appropriate
+``__str__()`` method). However, you can write a ``__str__()`` method directly,
+if you prefer. You must be careful to encode the results correctly, though.
+"""
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+class Article(models.Model):
+ headline = models.CharField(max_length=100)
+ pub_date = models.DateTimeField()
+
+ def __str__(self):
+ # Caution: this is only safe if you are certain that headline will be
+ # in ASCII.
+ return self.headline
+
+@python_2_unicode_compatible
+class BrokenArticle(models.Model):
+ headline = models.CharField(max_length=100)
+ pub_date = models.DateTimeField()
+
+ def __unicode__(self): # instead of __str__
+ return self.headline
+
+@python_2_unicode_compatible
+class InternationalArticle(models.Model):
+ headline = models.CharField(max_length=100)
+ pub_date = models.DateTimeField()
+
+ def __str__(self):
+ return self.headline
diff --git a/tests/str/tests.py b/tests/str/tests.py
new file mode 100644
index 0000000000..bd85c48d05
--- /dev/null
+++ b/tests/str/tests.py
@@ -0,0 +1,45 @@
+ # -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+
+import datetime
+
+from django.test import TestCase
+from django.utils import six
+from django.utils.unittest import skipIf
+
+from .models import Article, BrokenArticle, InternationalArticle
+
+
+class SimpleTests(TestCase):
+
+ @skipIf(six.PY3, "tests a __str__ method returning unicode under Python 2")
+ def test_basic(self):
+ a = Article.objects.create(
+ headline=b'Area man programs in Python',
+ pub_date=datetime.datetime(2005, 7, 28)
+ )
+ self.assertEqual(str(a), str('Area man programs in Python'))
+ self.assertEqual(repr(a), str('<Article: Area man programs in Python>'))
+
+ @skipIf(six.PY3, "tests Model's default __str__ method under Python 2")
+ def test_broken(self):
+ # Regression test for #19362.
+ a = BrokenArticle.objects.create(
+ headline='Girl wins €12.500 in lottery',
+ pub_date=datetime.datetime(2005, 7, 28)
+ )
+ six.assertRaisesRegex(self, RuntimeError, "Did you apply "
+ "@python_2_unicode_compatible without defining __str__\?", str, a)
+
+ def test_international(self):
+ a = InternationalArticle.objects.create(
+ headline='Girl wins €12.500 in lottery',
+ pub_date=datetime.datetime(2005, 7, 28)
+ )
+ if six.PY3:
+ self.assertEqual(str(a), 'Girl wins €12.500 in lottery')
+ else:
+ # On Python 2, the default str() output will be the UTF-8 encoded
+ # output of __unicode__() -- or __str__() when the
+ # python_2_unicode_compatible decorator is used.
+ self.assertEqual(str(a), b'Girl wins \xe2\x82\xac12.500 in lottery')