I need to test, whether the representation of my Django models are working with Unicode as it might be that users insert th9ings like ü or ¼ into it. To do so, I have this Django tests.py
# -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils import timezone
from .models import *
from .views import *
class CategoryTestCase(TestCase):
""" Test to check whether category name is printed correctly.
If there is a parent, it should be also printed seperated by a : """
def setUp(self):
self.cat1 = Category.objects.create(name=u'Category 1')
self.cat2 = Category.objects.create(name=u'Category ü', parent=self.cat1)
self.cat3 = Category.objects.create(name=u'Category 3', parent=self.cat2)
def test_category_name(self):
cat_result1 = u'Category 1'
cat_result2 = u'Category 1' + settings.PARENT_DELIMITER + u'Category ü'
cat_result3 = u'Category 1' + settings.PARENT_DELIMITER + u'Category ü' + settings.PARENT_DELIMITER + u'Category 3'
self.assertEqual(self.cat1.__str__(), cat_result1)
self.assertEqual(self.cat2.__str__(), cat_result2)
self.assertEqual(self.cat3.__str__(), cat_result3)
This is intended to test this little model:
#...
from django.utils.encoding import python_2_unicode_compatible
#....
@python_2_unicode_compatible
class Category(models.Model):
""" Representing a category a part might contains to.
E.g. resistor """
name = models.CharField(
max_length=50,
help_text=_("Name of the category.")
)
parent = models.ForeignKey(
"self",
null=True,
blank=True,
help_text=_("If having a subcateogry, the parent.")
)
description = models.TextField(
_("Description"),
blank=True,
null=True,
help_text=_("A chance to summarize usage of category.")
)
def __str__(self):
if self.parent is None:
return ('{}'.format(self.name))
else:
return ('%s%s%s' % (
self.parent.__str__(),
settings.PARENT_DELIMITER,
self.name)
)
def get_parents(self):
""" Returns a list with parants of that StoragePare incl itself"""
result = []
next = self
while True:
if next.id in result:
raise(CircleDetectedException(
_('There seems to be a circle inside ancestors of %s.' % self.id)))
else:
result.append(next.id)
if next.parent is not None:
next = next.parent
else:
break
return result
def clean(self):
pass
(stripped that a little)
When running this code via Python 3 and test or with Python2/3 executing as application it's working. Only the test with Python2 is failing so I assume it's something wrong with my idea how to test this. Based on the error message it appears that the Unicode string is somewhere not properly encoded and decoded.
======================================================================
FAIL: test_category_name (partsmanagement.tests.CategoryTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/frlan/quellen/partuniverse/partuniverse/partsmanagement/tests.py", line 31, in test_category_name
self.assertEqual(self.cat2.__str__(), cat_result2)
AssertionError: 'Category 1->Category xc3xbc' != u'Category 1->Category xfc'
So my question is: How to do proper Unicode-representation testing with Django.
Aucun commentaire:
Enregistrer un commentaire