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