Back to index

plone3  3.1.7
test_marshaller.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 Unittests for marshaller
00027 
00028 $Id: test_marshaller.py 7768 2007-03-18 23:38:08Z nouri $
00029 """
00030 
00031 import os
00032 from unittest import TestCase
00033 from OFS.Image import Pdata
00034 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00035 from Products.Archetypes.tests.utils import makeContent
00036 from Products.Archetypes.tests.utils import aputrequest
00037 from Products.Archetypes.tests.utils import PACKAGE_HOME
00038 from Products.Archetypes.atapi import *
00039 from Products.Archetypes.WebDAVSupport import PdataStreamIterator
00040 from Products.Archetypes.examples.DDocument import DDocument
00041 
00042 class MarshallerTests(ATSiteTestCase):
00043 
00044     # XXX this test is fu... up the machine by eating all memory
00045     def XXX_test_textFieldObjectWordReplace(self):
00046         #test that uploading to an existing object works
00047         obj1 = makeContent(self.folder, portal_type='DDocument', id='obj1')
00048 
00049         wordFilePath = os.path.join(PACKAGE_HOME, "input", "word.doc")
00050         wordFile = open(wordFilePath, 'r')
00051         data = wordFile.read()
00052         wordFile.seek(0)
00053 
00054         # put requests don't have the luxury of being able to specify a
00055         # content type
00056         request = aputrequest(wordFile, 'application/msword')
00057         request.processInputs()
00058         word = self.folder.obj1
00059         word.PUT(request, request.RESPONSE)
00060 
00061         #and we can get the stuff back
00062         self.assertEqual(word.getContentType('body'), 'application/msword')
00063         self.assertEqual(word.getRawBody(), data)
00064 
00065     def test_textFieldObjectRSTreplace(self):
00066         ## And again with an RST
00067         obj1 = makeContent(self.folder, portal_type='DDocument', id='obj1')
00068 
00069         rstFilePath = os.path.join(PACKAGE_HOME, "input", "rest1.rst")
00070         rstFile = open(rstFilePath, 'r')
00071         data = rstFile.read()
00072         rstFile.seek(0)
00073 
00074         request = aputrequest(rstFile, 'text/x-rst')
00075         # XXX PUT factory is using PARENTS to get an acquisition context
00076         request['PARENTS'] = (self.folder, self.portal)
00077         request.processInputs()
00078         rst = self.folder.obj1
00079         rst.PUT(request, request.RESPONSE)
00080 
00081         #and we can get the stuff back
00082         self.assertEqual(rst.getContentType('body'), 'text/x-rst')
00083         self.assertEqual(rst.getRawBody().strip(), data.strip())
00084 
00085     def test_fileFieldObjectWordReplace(self):
00086         #test that uploading to an existing object works
00087         obj1 = makeContent(self.folder, portal_type='SimpleFile', id='obj1')
00088 
00089         wordFilePath = os.path.join(PACKAGE_HOME, "input", "word.doc")
00090         wordFile = open(wordFilePath, 'r')
00091         data = wordFile.read()
00092         wordFile.seek(0)
00093 
00094         request = aputrequest(wordFile, 'application/msword')
00095         request.processInputs()
00096         word = self.folder.obj1
00097         word.PUT(request, request.RESPONSE)
00098 
00099         #and we can get the stuff back
00100         self.assertEqual(word.getContentType('body'), 'application/msword')
00101         self.assertEqual(str(word.getRawBody()), data)
00102 
00103     def setupCTR(self):
00104         #Modify the CTR to point to SimpleType
00105         ctr = self.portal.content_type_registry
00106         if ctr.getPredicate('text'):
00107             # ATCT has a predict
00108             ctr.removePredicate('text')
00109         ctr.addPredicate('text', 'major_minor' )
00110         ctr.getPredicate('text' ).edit('text', '' )
00111         ctr.assignTypeName('text', 'DDocument')
00112         ctr.reorderPredicate('text', 0)
00113 
00114         ctr.addPredicate('msword', 'major_minor' )
00115         ctr.getPredicate('msword' ).edit( 'application', 'msword' )
00116         ctr.assignTypeName('msword', 'DDocument')
00117         ctr.reorderPredicate('msword', 1)
00118 
00119         return ctr
00120 
00121     # XXX this test is fu... up the machine by eating all memory
00122     def XXX_test_objectCreate(self):
00123         #create the correct object on upload
00124         #one day, but this will need a change to the factory
00125         ctr = self.setupCTR()
00126 
00127         #now trigger the creation of a content type akin to DAV
00128         wordFilePath = os.path.join(PACKAGE_HOME, "input", "word.doc")
00129         wordFile = open(wordFilePath, 'r')
00130 
00131         obj = self.folder.PUT_factory('test', 'application/msword', wordFile)
00132         self.folder._setObject('test', obj)
00133         word = self.folder.test
00134 
00135         request = aputrequest(wordFile, 'application/msword')
00136         request.processInputs()
00137         word.PUT(request, request.RESPONSE)
00138 
00139         wordFile.seek(0)
00140         data = wordFile.read()
00141 
00142         self.assertEqual(word.archetype_name, DDocument.archetype_name)
00143         self.assertEqual(str(word.getBody(raw=1)), data)
00144         self.assertEqual(word.getContentType('body'), 'application/msword')
00145 
00146 class PdataStreamTests(TestCase):
00147 
00148     def test_iterator(self):
00149         start = pdata = Pdata('blob')
00150         for i in range(0, 5):
00151             pdata.next = Pdata('bla')
00152             pdata = pdata.next
00153         iterator = PdataStreamIterator(start, size=19, streamsize=6)
00154         expected = ['blobbl', 'ablabl', 'ablabl', 'a']
00155         self.assertEquals(list(iterator), expected)
00156 
00157 def test_suite():
00158     from unittest import TestSuite, makeSuite
00159     suite = TestSuite()
00160     suite.addTest(makeSuite(MarshallerTests))
00161     suite.addTest(makeSuite(PdataStreamTests))
00162     return suite