From 89f40e36246100df6a11316c31a76712ebc6c501 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Tue, 26 Feb 2013 09:53:47 +0100 Subject: Merged regressiontests and modeltests into the test root. --- tests/str/__init__.py | 0 tests/str/models.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ tests/str/tests.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 tests/str/__init__.py create mode 100644 tests/str/models.py create mode 100644 tests/str/tests.py (limited to 'tests/str') diff --git a/tests/str/__init__.py b/tests/str/__init__.py new file mode 100644 index 0000000000..e69de29bb2 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('')) + + @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') -- cgit v1.2.1