[Zope3-checkins] CVS: Zope3/src/zope/app/i18nfile/tests - __init__.py:1.2 test_i18nfile.py:1.2 test_i18nimage.py:1.2

Philipp von Weitershausen philikon at philikon.de
Tue Feb 24 11:50:10 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/i18nfile/tests
In directory cvs.zope.org:/tmp/cvs-serv26556/src/zope/app/i18nfile/tests

Added Files:
	__init__.py test_i18nfile.py test_i18nimage.py 
Log Message:


Moved I18nFile and I18nImage to their own package below zope.app,
including their interfaces and browser views.




=== Zope3/src/zope/app/i18nfile/tests/__init__.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:50:10 2004
+++ Zope3/src/zope/app/i18nfile/tests/__init__.py	Tue Feb 24 11:50:09 2004
@@ -0,0 +1,2 @@
+#
+# This file is necessary to make this directory a package.


=== Zope3/src/zope/app/i18nfile/tests/test_i18nfile.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:50:10 2004
+++ Zope3/src/zope/app/i18nfile/tests/test_i18nfile.py	Tue Feb 24 11:50:09 2004
@@ -0,0 +1,189 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+
+import unittest
+from zope.interface.verify import verifyClass
+from zope.i18n.tests.testii18naware import TestII18nAware
+from zope.app.file.file import FileChunk
+from zope.app.i18nfile import I18nFile
+
+def sorted(list):
+    list.sort()
+    return list
+
+class Test(TestII18nAware):
+
+    def _makeFile(self, *args, **kw):
+        return I18nFile(*args, **kw)
+
+    def _createObject(self):
+        obj = self._makeFile(defaultLanguage='fr')
+        obj.setData('', 'lt')
+        obj.setData('', 'en')
+        return obj
+
+    def testEmpty(self):
+        file = self._makeFile()
+
+        self.assertEqual(file.getContentType(), '')
+        self.assertEqual(file.getData(), '')
+        self.assertEqual(file.getDefaultLanguage(), 'en')
+
+    def testConstructor(self):
+        file = self._makeFile('Foobar')
+        self.assertEqual(file.getContentType(), '')
+        self.assertEqual(file.getData(), 'Foobar')
+        self.assertEqual(file.getData('en'), 'Foobar')
+        self.assertEqual(file.getData('nonexistent'), 'Foobar')
+        self.assertEqual(file.getDefaultLanguage(), 'en')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+
+        file = self._makeFile('Foobar', 'text/plain')
+        self.assertEqual(file.getContentType(), 'text/plain')
+        self.assertEqual(file.getData(), 'Foobar')
+        self.assertEqual(file.getData('en'), 'Foobar')
+        self.assertEqual(file.getData('nonexistent'), 'Foobar')
+        self.assertEqual(file.getDefaultLanguage(), 'en')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+
+        file = self._makeFile(data='Foobar', contentType='text/plain')
+        self.assertEqual(file.getContentType(), 'text/plain')
+        self.assertEqual(file.getData(), 'Foobar')
+        self.assertEqual(file.getData('en'), 'Foobar')
+        self.assertEqual(file.getData('nonexistent'), 'Foobar')
+        self.assertEqual(file.getDefaultLanguage(), 'en')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+        file = self._makeFile(data='Foobar', contentType='text/plain',
+                              defaultLanguage='fr')
+        self.assertEqual(file.getContentType(), 'text/plain')
+        self.assertEqual(file.getData(), 'Foobar')
+        self.assertEqual(file.getData('en'), 'Foobar')
+        self.assertEqual(file.getData('nonexistent'), 'Foobar')
+        self.assertEqual(file.getDefaultLanguage(), 'fr')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['fr'])
+
+    def testMutators(self):
+        file = self._makeFile()
+
+        file.setContentType('text/plain')
+        self.assertEqual(file.getContentType(), 'text/plain')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+        file.setData('Foobar')
+        self.assertEqual(file.getData(), 'Foobar')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+        file.setData('Barbaz', language='fr')
+        self.assertEqual(file.getData(), 'Foobar')
+        self.assertEqual(file.getData('fr'), 'Barbaz')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
+
+        file.edit('Blah', 'text/html')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('fr'), 'Barbaz')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
+
+        file.edit('Quux', 'text/html', 'lt')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('fr'), 'Barbaz')
+        self.assertEqual(file.getData('lt'), 'Quux')
+        self.assertEqual(file.getSize(), len('Blah'))
+        self.assertEqual(file.getSize('fr'), len('Barbaz'))
+        self.assertEqual(file.getSize('lt'), len('Quux'))
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr', 'lt'])
+
+        file.removeLanguage('lt')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('fr'), 'Barbaz')
+        self.assertEqual(file.getSize(), len('Blah'))
+        self.assertEqual(file.getSize('fr'), len('Barbaz'))
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
+        self.assertEqual(file.getData('lt'), 'Blah')
+        self.assertEqual(file.getSize('lt'), len('Blah'))
+
+        file.removeLanguage('nonexistent')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('fr'), 'Barbaz')
+        self.assertEqual(file.getSize(), len('Blah'))
+        self.assertEqual(file.getSize('fr'), len('Barbaz'))
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
+        self.assertEqual(file.getData('lt'), 'Blah')
+        self.assertEqual(file.getSize('lt'), len('Blah'))
+
+        self.assertRaises(ValueError, file.removeLanguage, file.getDefaultLanguage())
+        self.assertRaises(ValueError, file.setDefaultLanguage, 'nonexistent')
+
+    def testLargeDataInput(self):
+        file = self._makeFile()
+
+        # Insert as string
+        file.setData('Foobar'*60000, 'en')
+        self.assertEqual(file.getSize('en'), 6*60000)
+        self.assertEqual(file.getData('en'), 'Foobar'*60000)
+
+        # Insert data as FileChunk
+        fc = FileChunk('Foobar'*4000)
+        file.setData(fc, 'lt')
+        self.assertEqual(file.getSize('lt'), 6*4000)
+        self.assertEqual(file.getData('lt'), 'Foobar'*4000)
+
+        # Insert data from file object
+        import cStringIO
+        sio = cStringIO.StringIO()
+        sio.write('Foobar'*100000)
+        sio.seek(0)
+        file.setData(sio, 'fr')
+        self.assertEqual(file.getSize('fr'), 6*100000)
+        self.assertEqual(file.getData('fr'), 'Foobar'*100000)
+
+    def testInterface(self):
+        from zope.app.file.interfaces import IFile
+        from zope.app.i18nfile.interfaces import II18nFile
+        from zope.i18n.interfaces import II18nAware
+
+        self.failUnless(IFile.isImplementedByInstancesOf(I18nFile))
+        self.failUnless(verifyClass(IFile, I18nFile))
+
+        self.failUnless(II18nAware.isImplementedByInstancesOf(I18nFile))
+        self.failUnless(verifyClass(II18nAware, I18nFile))
+
+        self.failUnless(II18nFile.isImplementedByInstancesOf(I18nFile))
+        self.failUnless(verifyClass(II18nFile, I18nFile))
+
+    def testSetDefaultLanguage(self):
+        # getDefaultLanguage and getAvailableLanguages are tested in the
+        # above tests
+        file = self._makeFile()
+
+        file.setData('', language='lt')
+        file.setDefaultLanguage('lt')
+        self.assertEqual(file.getDefaultLanguage(), 'lt')
+
+
+def test_suite():
+    loader = unittest.TestLoader()
+    return loader.loadTestsFromTestCase(Test)
+
+if __name__=='__main__':
+    unittest.TextTestRunner().run(test_suite())


=== Zope3/src/zope/app/i18nfile/tests/test_i18nimage.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:50:10 2004
+++ Zope3/src/zope/app/i18nfile/tests/test_i18nimage.py	Tue Feb 24 11:50:09 2004
@@ -0,0 +1,166 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+
+$Id$
+"""
+
+import unittest
+from zope.interface.verify import verifyClass
+from zope.i18n.tests.testii18naware import TestII18nAware
+
+from zope.app.i18nfile.i18nimage import I18nImage
+
+def sorted(list):
+    list.sort()
+    return list
+
+class Test(TestII18nAware):
+
+    def _makeImage(self, *args, **kw):
+        return I18nImage(*args, **kw)
+
+    def _createObject(self):
+        obj = self._makeImage(defaultLanguage='fr')
+        obj.setData('', 'lt')
+        obj.setData('', 'en')
+        return obj
+
+    def testEmpty(self):
+        file = self._makeImage()
+
+        self.assertEqual(file.getContentType(), '')
+        self.assertEqual(file.getData(), '')
+        self.assertEqual(file.getDefaultLanguage(), 'en')
+
+
+    def testConstructor(self):
+        file = self._makeImage('Data')
+        self.assertEqual(file.getContentType(), '')
+        self.assertEqual(file.getData(), 'Data')
+        self.assertEqual(file.getData('en'), 'Data')
+        self.assertEqual(file.getData('nonexistent'), 'Data')
+        self.assertEqual(file.getDefaultLanguage(), 'en')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+        file = self._makeImage('Data', defaultLanguage='fr')
+        self.assertEqual(file.getContentType(), '')
+        self.assertEqual(file.getData(), 'Data')
+        self.assertEqual(file.getData('en'), 'Data')
+        self.assertEqual(file.getData('nonexistent'), 'Data')
+        self.assertEqual(file.getDefaultLanguage(), 'fr')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['fr'])
+
+    def testMutators(self):
+        # XXX What's the point of this test? Does it test that data
+        # contents override content-type? Or not? If the former, then
+        # real image data should be used.
+        file = self._makeImage()
+
+        file.setContentType('text/plain')
+        self.assertEqual(file.getContentType(), 'text/plain')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+        file.setData('Foobar')
+        self.assertEqual(file.getData(), 'Foobar')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+        file.edit('Blah', 'text/html')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
+
+        file.setData('Foobar in lt', 'lt')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('lt'), 'Foobar in lt')
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'lt'])
+
+        file.edit('Blah in fr', 'text/html', 'fr')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('lt'), 'Foobar in lt')
+        self.assertEqual(file.getData('fr'), 'Blah in fr')
+        self.assertEqual(sorted(file.getAvailableLanguages()),
+                         ['en', 'fr', 'lt'])
+
+        file.removeLanguage('lt')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('fr'), 'Blah in fr')
+        self.assertEqual(file.getSize(), len('Blah'))
+        self.assertEqual(file.getSize('fr'), len('Blah in fr'))
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
+        self.assertEqual(file.getData('lt'), 'Blah')
+        self.assertEqual(file.getSize('lt'), len('Blah'))
+
+        file.removeLanguage('nonexistent')
+        self.assertEqual(file.getContentType(), 'text/html')
+        self.assertEqual(file.getData(), 'Blah')
+        self.assertEqual(file.getData('fr'), 'Blah in fr')
+        self.assertEqual(file.getSize(), len('Blah'))
+        self.assertEqual(file.getSize('fr'), len('Blah in fr'))
+        self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
+        self.assertEqual(file.getData('lt'), 'Blah')
+        self.assertEqual(file.getSize('lt'), len('Blah'))
+
+        self.assertRaises(ValueError, file.removeLanguage,
+                          file.getDefaultLanguage())
+
+        self.assertRaises(ValueError, file.setDefaultLanguage, 'nonexistent')
+
+        # Check that setData updates content type only when updating the
+        # default language.  Need some real images or at least headers
+        # for that.
+
+        gifHdr = 'GIF87a\x20\x00\x10\x00'
+        file.setData(gifHdr)
+        self.assertEqual(file.getContentType(), 'image/gif')
+
+        pngHdr = '\211PNG\r\n\032\n\0\0\0\x20\0\0\0\x10'
+        file.setData(pngHdr, 'fr')
+        self.assertEqual(file.getContentType(), 'image/gif')
+
+        file.setData(pngHdr, 'en')
+        self.assertEqual(file.getContentType(), 'image/png')
+
+    def testInterface(self):
+        from zope.app.file.interfaces import IImage
+        from zope.app.i18nfile.interfaces import II18nFile
+        from zope.i18n.interfaces import II18nAware
+
+        self.failUnless(IImage.isImplementedByInstancesOf(I18nImage))
+        self.failUnless(verifyClass(IImage, I18nImage))
+
+        self.failUnless(II18nAware.isImplementedByInstancesOf(I18nImage))
+        self.failUnless(verifyClass(II18nAware, I18nImage))
+
+        self.failUnless(II18nFile.isImplementedByInstancesOf(I18nImage))
+        self.failUnless(verifyClass(II18nFile, I18nImage))
+
+    def testSetDefaultLanguage(self):
+        # getDefaultLanguage and getAvailableLanguages are tested in the
+        # above tests
+
+        file = self._makeImage()
+
+        file.setData('', language='lt')
+        file.setDefaultLanguage('lt')
+        self.assertEqual(file.getDefaultLanguage(), 'lt')
+
+def test_suite():
+    loader = unittest.TestLoader()
+    return loader.loadTestsFromTestCase(Test)
+
+if __name__=='__main__':
+    unittest.TextTestRunner().run(test_suite())




More information about the Zope3-Checkins mailing list