2014-03-30 19:11:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-09-13 05:28:38 +00:00
|
|
|
import datetime
|
2013-07-01 12:22:27 +00:00
|
|
|
from unittest import skipIf
|
2010-09-13 05:28:38 +00:00
|
|
|
|
2015-11-11 06:30:19 +00:00
|
|
|
from django.db import models
|
2010-09-13 05:28:38 +00:00
|
|
|
from django.test import TestCase
|
2015-11-17 05:39:28 +00:00
|
|
|
from django.test.utils import isolate_apps
|
2012-08-16 07:44:00 +00:00
|
|
|
from django.utils import six
|
2010-09-13 05:28:38 +00:00
|
|
|
|
2013-10-13 16:06:58 +00:00
|
|
|
from .models import Article, InternationalArticle
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-09-13 05:28:38 +00:00
|
|
|
|
|
|
|
class SimpleTests(TestCase):
|
2012-08-16 07:44:00 +00:00
|
|
|
|
|
|
|
@skipIf(six.PY3, "tests a __str__ method returning unicode under Python 2")
|
2010-09-13 05:28:38 +00:00
|
|
|
def test_basic(self):
|
|
|
|
a = Article.objects.create(
|
2015-08-22 18:05:54 +00:00
|
|
|
headline=b'Parrot programs in Python',
|
2010-09-13 05:28:38 +00:00
|
|
|
pub_date=datetime.datetime(2005, 7, 28)
|
|
|
|
)
|
2015-08-22 18:05:54 +00:00
|
|
|
self.assertEqual(str(a), str('Parrot programs in Python'))
|
|
|
|
self.assertEqual(repr(a), str('<Article: Parrot programs in Python>'))
|
2010-09-13 05:28:38 +00:00
|
|
|
|
|
|
|
def test_international(self):
|
|
|
|
a = InternationalArticle.objects.create(
|
2012-06-07 16:08:47 +00:00
|
|
|
headline='Girl wins €12.500 in lottery',
|
2010-09-13 05:28:38 +00:00
|
|
|
pub_date=datetime.datetime(2005, 7, 28)
|
|
|
|
)
|
2012-08-16 07:44:00 +00:00
|
|
|
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')
|
2015-11-11 06:30:19 +00:00
|
|
|
|
2015-11-17 05:39:28 +00:00
|
|
|
@isolate_apps('str')
|
2015-11-11 06:30:19 +00:00
|
|
|
def test_defaults(self):
|
|
|
|
"""
|
|
|
|
The default implementation of __str__ and __repr__ should return
|
|
|
|
instances of str.
|
|
|
|
"""
|
|
|
|
class Default(models.Model):
|
2015-11-17 05:39:28 +00:00
|
|
|
pass
|
2015-11-11 06:30:19 +00:00
|
|
|
|
|
|
|
obj = Default()
|
|
|
|
# Explicit call to __str__/__repr__ to make sure str()/repr() don't
|
|
|
|
# coerce the returned value.
|
|
|
|
self.assertIsInstance(obj.__str__(), str)
|
|
|
|
self.assertIsInstance(obj.__repr__(), str)
|
|
|
|
self.assertEqual(str(obj), str('Default object'))
|
|
|
|
self.assertEqual(repr(obj), str('<Default: Default object>'))
|