Hi all,

I'm writing a doctest for a simple model and I can't get it to pass.

The problem seems to be that no matter how I enter the data into the
test db, whether as a Unicode string or a utf-8 bytestring, and no
matter what I tell my test to expect as a return value, the expected
value is always rendered as a Unicode string (in this case a rendered
Chinese character), whereas the value actually returned by the test is
always a utf-8 bytestring.

I'm using the latest django revision (6628), os x 10.4.10, python 2.5,
a postgres 8.2, my test database is set to: TEST_DATABASE_CHARSET =
'utf8' in settings.py, and I've verified that the test database is
indeed UTF8 after its been created.  The models.py file has been saved
in bbedit as a Unix file with UTF-8 encoding, and I put an explicit
UTF-8 tag at the top of models.py for good measure.

Here are my specific questions:

1.) Terminology: The expected result of the tests always seems to
return '\xe4\xb8...' version of the chinese characters - this is the
utf-8 bytestring, right?

2.) Am I entering the data correctly?  I believe I correctly used both
of the formats listed in the "Unicode data in Django" documentation
(http://www.djangoproject.com/documentation/unicode/).  Is there a
more correct way of entering the data?

3.) Any ideas on how I could change this simple test to make it pass?

Thanks!

If anyone wants to dig into this, the model.py and the test errors I
got back are copied below.

Here's the simplified model.py file with the tests:

# -*- coding: utf-8 -*-
from django.db import models

class City(models.Model):
    """
    A city in China with an English name and a name in Chinese
characters.

    # Create a couple of test cities
    >>> beijing = City.objects.create(
    ...     name_en='Beijing',
    ...     name_cn=u'北京',
    ... )

    >>> shanghai = City.objects.create(
    ...     name_en='Shanghai',
    ...     name_cn='\xe4\xb8\x8a\xe6\xb5\xb7',
    ... )

    # Make sure the city was saved correctly

    # Test 1 expects Unicode string back
    >>> beijing.name_cn
    '北京'

    # Test 2 expects u prefixed Unicode string back
    >>> beijing.name_cn
    u'北京'

    # Test 3 expects UTF-8 bytestring back
    >>> beijing.name_cn
    '\xe5\x8c\x97\xe4\xba\xac'

    # Test 4 expects u prefixed UTF-8 bytestring back
    >>> beijing.name_cn
    u'\xe5\x8c\x97\xe4\xba\xac'

    # Test 5 expects Unicode string back
    >>> shanghai.name_cn
    '上海'

        # Test 6 expects u prefixed Unicode string back
    >>> shanghai.name_cn
    u'上海'

    # Test 7 expects UTF-8 bytestring back
    >>> shanghai.name_cn
    '\xe4\xb8\x8a\xe6\xb5\xb7'

    # Test 8 expects u prefixed UTF-8 bytestring back
    >>> shanghai.name_cn
    u'\xe4\xb8\x8a\xe6\xb5\xb7'
    """

    name_en = models.CharField(u'English',
        maxlength=50,
        blank=True,
        null=True,
        help_text='The city display name in English',
    )

    name_cn = models.CharField(u'Chinese',
        maxlength=50,
        blank=True,
        null=True,
        help_text='The city display name in Chinese',
    )

Here's the output from the 8 tests enumerated in the doctest string:
======================================================================
FAIL: Doctest: chinabites.city.models.City
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/test/_doctest.py", line 2169, in
runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for chinabites.city.models.City
  File "/home/django/projects/chinabites/../chinabites/city/
models.py", line 4, in City

----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 22, in chinabites.city.models.City
Failed example:
    beijing.name_cn
Expected:
    '北京'
Got:
    u'\xe5\x8c\x97\xe4\xba\xac'
----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 26, in chinabites.city.models.City
Failed example:
    beijing.name_cn
Expected:
    u'北京'
Got:
    u'\xe5\x8c\x97\xe4\xba\xac'
----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 30, in chinabites.city.models.City
Failed example:
    beijing.name_cn
Expected:
    '北京'
Got:
    u'\xe5\x8c\x97\xe4\xba\xac'
----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 34, in chinabites.city.models.City
Failed example:
    beijing.name_cn
Expected:
    u'北京'
Got:
    u'\xe5\x8c\x97\xe4\xba\xac'
----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 38, in chinabites.city.models.City
Failed example:
    shanghai.name_cn
Expected:
    '上海'
Got:
    '\xe4\xb8\x8a\xe6\xb5\xb7'
----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 42, in chinabites.city.models.City
Failed example:
    shanghai.name_cn
Expected:
    u'上海'
Got:
    '\xe4\xb8\x8a\xe6\xb5\xb7'
----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 46, in chinabites.city.models.City
Failed example:
    shanghai.name_cn
Expected:
    '上海'
Got:
    '\xe4\xb8\x8a\xe6\xb5\xb7'
----------------------------------------------------------------------
File "/home/django/projects/chinabites/../chinabites/city/models.py",
line 50, in chinabites.city.models.City
Failed example:
    shanghai.name_cn
Expected:
    u'上海'
Got:
    '\xe4\xb8\x8a\xe6\xb5\xb7'


----------------------------------------------------------------------
Ran 1 test in 0.013s


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to