tests.py 1.22 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

4
import datetime
5
from unittest import skipIf
6 7

from django.test import TestCase
8
from django.utils import six
9

10
from .models import Article, InternationalArticle
11

12 13

class SimpleTests(TestCase):
14 15

    @skipIf(six.PY3, "tests a __str__ method returning unicode under Python 2")
16 17
    def test_basic(self):
        a = Article.objects.create(
18
            headline=b'Parrot programs in Python',
19 20
            pub_date=datetime.datetime(2005, 7, 28)
        )
21 22
        self.assertEqual(str(a), str('Parrot programs in Python'))
        self.assertEqual(repr(a), str('<Article: Parrot programs in Python>'))
23 24 25

    def test_international(self):
        a = InternationalArticle.objects.create(
26
            headline='Girl wins €12.500 in lottery',
27 28
            pub_date=datetime.datetime(2005, 7, 28)
        )
29 30 31 32 33 34 35
        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')