Back to index

plone3  3.1.7
test_unicode.py
Go to the documentation of this file.
00001 # -*- coding: ISO-8859-1 -*-
00002 # XXX change encoding to UTF-8
00003 ################################################################################
00004 #
00005 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00006 #                              the respective authors. All rights reserved.
00007 # For a list of Archetypes contributors see docs/CREDITS.txt.
00008 #
00009 # Redistribution and use in source and binary forms, with or without
00010 # modification, are permitted provided that the following conditions are met:
00011 #
00012 # * Redistributions of source code must retain the above copyright notice, this
00013 #   list of conditions and the following disclaimer.
00014 # * Redistributions in binary form must reproduce the above copyright notice,
00015 #   this list of conditions and the following disclaimer in the documentation
00016 #   and/or other materials provided with the distribution.
00017 # * Neither the name of the author nor the names of its contributors may be used
00018 #   to endorse or promote products derived from this software without specific
00019 #   prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00022 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00024 # FOR A PARTICULAR PURPOSE.
00025 #
00026 ################################################################################
00027 """
00028 """
00029 
00030 from Testing import ZopeTestCase
00031 
00032 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00033 from test_classgen import Dummy
00034 
00035 from Products.Archetypes.atapi import *
00036 from Products.PortalTransforms.data import datastream
00037 
00038 
00039 class FakeTransformer:
00040     def __init__(self, expected):
00041         self.expected = expected
00042 
00043     def convertTo(self, target_mimetype, orig, data=None, object=None, **kwargs):
00044         assert orig == self.expected, '????'
00045         if data is None:
00046             data = datastream('test')
00047         data.setData(orig)
00048         return data
00049 
00050 
00051 class UnicodeStringFieldTest(ATSiteTestCase):
00052 
00053     def test_set(self):
00054         instance = Dummy()
00055         f = StringField('test')
00056         f.set(instance, 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00057         self.failUnlessEqual(f.get(instance), 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00058         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), 'héhéhé')
00059         f.set(instance, 'héhéhé', encoding='ISO-8859-1')
00060         self.failUnlessEqual(f.get(instance), 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00061         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), 'héhéhé')
00062         f.set(instance, u'héhéhé')
00063         self.failUnlessEqual(f.get(instance), 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00064         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), 'héhéhé')
00065 
00066 
00067 class UnicodeLinesFieldTest(ATSiteTestCase):
00068 
00069     def test_set1(self):
00070         instance = Dummy()
00071         f = LinesField('test')
00072         f.set(instance, 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00073         out = ('h\xc3\xa9h\xc3\xa9h\xc3\xa9',)
00074         iso = ('héhéhé',)
00075         self.failUnlessEqual(f.get(instance), out)
00076         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), iso)
00077         f.set(instance, 'héhéhé', encoding='ISO-8859-1')
00078         self.failUnlessEqual(f.get(instance), out)
00079         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), iso)
00080         f.set(instance, u'héhéhé')
00081         self.failUnlessEqual(f.get(instance), out)
00082         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), iso)
00083 
00084     def test_set2(self):
00085         instance = Dummy()
00086         f = LinesField('test')
00087         f.set(instance, ['h\xc3\xa9h\xc3\xa9h\xc3\xa9'])
00088         out = ('h\xc3\xa9h\xc3\xa9h\xc3\xa9',)
00089         iso = ('héhéhé',)
00090         self.failUnlessEqual(f.get(instance), out)
00091         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), iso)
00092         f.set(instance, ['héhéhé'], encoding='ISO-8859-1')
00093         self.failUnlessEqual(f.get(instance), out)
00094         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), iso)
00095         f.set(instance, [u'héhéhé'])
00096         self.failUnlessEqual(f.get(instance), out)
00097         self.failUnlessEqual(f.get(instance, encoding="ISO-8859-1"), iso)
00098 
00099 
00100 class UnicodeTextFieldTest(ATSiteTestCase):
00101 
00102     def test_set(self):
00103         instance = Dummy()
00104         f = TextField('test')
00105         f.set(instance, 'h\xc3\xa9h\xc3\xa9h\xc3\xa9', mimetype='text/plain')
00106         self.failUnlessEqual(f.getRaw(instance), 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00107         self.failUnlessEqual(f.getRaw(instance, encoding="ISO-8859-1"), 'héhéhé')
00108         f.set(instance, 'héhéhé', encoding='ISO-8859-1', mimetype='text/plain')
00109         self.failUnlessEqual(f.getRaw(instance), 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00110         self.failUnlessEqual(f.getRaw(instance, encoding="ISO-8859-1"), 'héhéhé')
00111         f.set(instance, u'héhéhé', mimetype='text/plain')
00112         self.failUnlessEqual(f.getRaw(instance), 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00113         self.failUnlessEqual(f.getRaw(instance, encoding="ISO-8859-1"), 'héhéhé')
00114 
00115 
00116 class UnicodeBaseUnitTest(ATSiteTestCase):
00117 
00118     def afterSetUp(self):
00119         self.instance = Dummy()
00120         self.bu = BaseUnit('test', 'héhéhé', self.instance,
00121                            mimetype='text/plain', encoding='ISO-8859-1')
00122 
00123     def test_store(self):
00124         """check non binary string are stored as unicode"""
00125         self.failUnless(type(self.bu.raw) is type(u''))
00126 
00127     def test_getRaw(self):
00128         """check bu.getRaw return the ustring encoded with the default charset
00129         or the specified one if any
00130         """
00131         self.failUnlessEqual(self.bu.getRaw(), 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00132         self.failUnlessEqual(self.bu.getRaw('ISO-8859-1'), 'héhéhé')
00133 
00134     def test_transform(self):
00135         """check the string given to the transformer is encoded using its
00136         original encoding, and finally returned using the default charset
00137         """
00138         self.instance.aq_parent = None
00139         self.instance.portal_transforms = FakeTransformer('héhéhé')
00140         transformed = self.bu.transform(self.instance, 'text/plain')
00141         self.failUnlessEqual(transformed, 'h\xc3\xa9h\xc3\xa9h\xc3\xa9')
00142 
00143 
00144 def test_suite():
00145     from unittest import TestSuite, makeSuite
00146     suite = TestSuite()
00147     suite.addTest(makeSuite(UnicodeStringFieldTest))
00148     suite.addTest(makeSuite(UnicodeLinesFieldTest))
00149     suite.addTest(makeSuite(UnicodeTextFieldTest))
00150     suite.addTest(makeSuite(UnicodeBaseUnitTest))
00151     return suite