Back to index

plone3  3.1.7
test_filename.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 """
00026 """
00027 
00028 import os
00029 from StringIO import StringIO
00030 
00031 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00032 from Products.Archetypes.tests.utils import PACKAGE_HOME
00033 
00034 from Products.Archetypes.tests.test_classgen import Dummy
00035 from Products.Archetypes.tests.test_classgen import gen_dummy
00036 from Products.Archetypes.tests.test_classgen import default_text
00037 
00038 from Products.Archetypes.atapi import *
00039 
00040 
00041 class GetFilenameTest(ATSiteTestCase):
00042 
00043     def afterSetUp(self):
00044         gen_dummy()
00045         self._dummy = dummy = Dummy(oid='dummy')
00046         self._dummy.initializeArchetype()
00047 
00048     def test_textfieldwithfilename(self):
00049         obj = self._dummy
00050         field = obj.getField('atextfield')
00051         self.assertEqual(field.getFilename(obj), '')
00052         self.assertEqual(field.getRaw(obj), default_text)
00053         obj.setAtextfield('Bla', filename='name.rst')
00054         self.assertEqual(field.getFilename(obj), 'name.rst')
00055         self.assertEqual(field.getRaw(obj), 'Bla')
00056 
00057     def test_textfieldwithfilename2(self):
00058         obj = self._dummy
00059         field = obj.getField('atextfield')
00060         obj.setAtextfield('Ble', filename='eitadiacho.txt')
00061         self.assertEqual(field.getRaw(obj), 'Ble')
00062         self.assertEqual(field.getFilename(obj), 'eitadiacho.txt')
00063 
00064     def test_textfieldwithoutfilename(self):
00065         obj = self._dummy
00066         field = obj.getField('atextfield')
00067         obj.setAtextfield('Bli')
00068         self.assertEqual(str(field.getRaw(obj)), 'Bli')
00069         self.assertEqual(field.getFilename(obj), '')
00070 
00071     def test_textfielduploadwithoutfilename(self):
00072         obj = self._dummy
00073         file = open(os.path.join(PACKAGE_HOME, 'input', 'rest1.tgz'), 'r')
00074         field = obj.getField('atextfield')
00075         obj.setAtextfield(file)
00076         file.close()
00077         self.assertEqual(field.getFilename(obj), 'rest1.tgz')
00078 
00079     def test_filefieldwithfilename(self):
00080         obj = self._dummy
00081         field = obj.getField('afilefield')
00082         obj.setAfilefield('Blo', filename='beleza.jpg')
00083         self.assertEqual(str(obj.getAfilefield()), 'Blo')
00084         self.assertEqual(field.getFilename(obj), 'beleza.jpg')
00085 
00086     def test_filefieldwithfilename2(self):
00087         obj = self._dummy
00088         field = obj.getField('afilefield')
00089         obj.setAfilefield('Blu', filename='juca.avi')
00090         self.assertEqual(str(obj.getAfilefield()), 'Blu')
00091         self.assertEqual(field.getFilename(obj), 'juca.avi')
00092 
00093     def test_filefieldwithoutfilename(self):
00094         obj = self._dummy
00095         field = obj.getField('afilefield')
00096         obj.setAfilefield('Blao')
00097         self.assertEqual(str(obj.getAfilefield()), 'Blao')
00098         self.assertEqual(field.getFilename(obj), '')
00099 
00100     def test_filefielduploadwithoutfilename(self):
00101         obj = self._dummy
00102         file = open(os.path.join(PACKAGE_HOME, 'input', 'rest1.tgz'), 'r')
00103         field = obj.getField('afilefield')
00104         obj.setAfilefield(file)
00105         file.close()
00106         self.assertEqual(field.getFilename(obj), 'rest1.tgz')
00107 
00108 
00109 class SetFilenameTest(ATSiteTestCase):
00110 
00111     def afterSetUp(self):
00112         gen_dummy()
00113         self._dummy = dummy = Dummy(oid='dummy')
00114         self._dummy.initializeArchetype()
00115         file1 = open(os.path.join(PACKAGE_HOME, 'input', 'rest1.tgz'), 'r')
00116         file2 = open(os.path.join(PACKAGE_HOME, 'input', 'word.doc'), 'r')
00117         # afilefield is the primary field
00118         dummy.setAfilefield(file1)
00119         dummy.setAnotherfilefield(file2)
00120         file1.close()
00121         file2.close()
00122 
00123     def testMutatorSetFilename(self):
00124         obj = self._dummy
00125         field1 = obj.getField('afilefield')
00126         field2 = obj.getField('anotherfilefield')
00127         filename1 = 'rest1.tgz'
00128         filename2 = 'word.doc'
00129         self.assertEqual(field1.getFilename(obj), filename1)
00130         self.assertEqual(field2.getFilename(obj), filename2)
00131 
00132     def testBaseObjectPrimaryFieldSetFilename(self):
00133         obj = self._dummy
00134         filename1 = 'eitadiacho.mid'
00135         filename2 = 'ehagoraoununca.pdf'
00136         obj.setFilename(filename1)
00137         obj.setFilename(filename2, 'anotherfilefield')
00138         self.assertEqual(obj.getFilename(), filename1)
00139         self.assertEqual(obj.getFilename('afilefield'), filename1)
00140         self.assertEqual(obj.getFilename('anotherfilefield'), filename2)
00141 
00142     def testBaseObjectSetFilename(self):
00143         obj = self._dummy
00144         filename1 = 'masbahtche.avi'
00145         filename2 = 'guasco.mpg'
00146         obj.setFilename(filename1, 'afilefield')
00147         obj.setFilename(filename2, 'anotherfilefield')
00148         self.assertEqual(obj.getFilename(), filename1)
00149         self.assertEqual(obj.getFilename('afilefield'), filename1)
00150         self.assertEqual(obj.getFilename('anotherfilefield'), filename2)
00151 
00152     def testFieldSetFilename(self):
00153         obj = self._dummy
00154         field1 = obj.getField('afilefield')
00155         field2 = obj.getField('anotherfilefield')
00156         filename1 = 'muamba.gif'
00157         filename2 = 'noruega.jpg'
00158         field1.setFilename(obj, filename1)
00159         field2.setFilename(obj, filename2)
00160         self.assertEqual(field1.getFilename(obj), filename1)
00161         self.assertEqual(field2.getFilename(obj), filename2)
00162 
00163 
00164 class LargeFileTest(ATSiteTestCase):
00165     def testSetFilenameOfLargeFile(self):
00166         """Test for https://dev.plone.org/archetypes/ticket/635
00167         """
00168         self.folder.invokeFactory('SimpleFile', 'sf')
00169         obj = self.folder.sf
00170         contents = StringIO('largest file, ' * 10000)
00171         obj.setBody(contents)
00172         self.assertEqual(obj.getBody().size, 140000)
00173         obj.getBody().setFilename('Spam.txt')
00174         self.assertEqual(obj.getBody().size, 140000)
00175         
00176 
00177 class StrangeIdTest(ATSiteTestCase):
00178     def test_strangeUnallowedIds(self):
00179         """ Certain IDs used to give an error and are unusable
00180 
00181         They're set in zope's lib/python/App/Product.py. Examples:
00182         home, version. This test used to include 'icon', too, but that's
00183         apparently really an id that's already been taken (instead of
00184         a bug).
00185         """
00186         strangeIds = ['home', 'version']
00187         for id in strangeIds:
00188             self.folder.invokeFactory('Folder', id)
00189             self.assert_(id in self.folder.objectIds())
00190 
00191 
00192 def test_suite():
00193     from unittest import TestSuite, makeSuite
00194     suite = TestSuite()
00195     suite.addTest(makeSuite(GetFilenameTest))
00196     suite.addTest(makeSuite(SetFilenameTest))
00197     suite.addTest(makeSuite(LargeFileTest))
00198     suite.addTest(makeSuite(StrangeIdTest))
00199     return suite