Back to index

plone3  3.1.7
test_predicate.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004-2006 Enfold Systems, LLC
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 2 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 """
00019 $Id: test_predicate.py 5626 2006-01-24 02:26:47Z dreamcatcher $
00020 """
00021 
00022 import os, sys
00023 
00024 if __name__ == '__main__':
00025     execfile(os.path.join(sys.path[0], 'framework.py'))
00026 
00027 # Load fixture
00028 from Testing import ZopeTestCase
00029 from Products.Marshall.tests.base import BaseTest
00030 
00031 # Install our product
00032 ZopeTestCase.installProduct('Marshall')
00033 ZopeTestCase.installProduct('Archetypes')
00034 
00035 from Products.CMFCore.utils import getToolByName
00036 from Products.Marshall.predicates import add_predicate
00037 from Products.Marshall.config import TOOL_ID as tool_id
00038 
00039 class PredicateTest(BaseTest):
00040 
00041     def afterSetUp(self):
00042         super(PredicateTest, self).afterSetUp()
00043         self.loginPortalOwner()
00044         self.qi = self.portal.portal_quickinstaller
00045         self.qi.installProduct('Marshall')
00046         self.tool = getToolByName(self.portal, tool_id)
00047 
00048     def get(self, obj, **kw):
00049         return self.tool.getMarshallersFor(obj, **kw)
00050 
00051     def reverse(self):
00052         ids = self.tool.objectIds()
00053         for pos, id in enumerate(ids):
00054             self.tool.moveObjectToPosition(id, len(ids) - pos)
00055 
00056 class DefaultPredicateTest(PredicateTest):
00057 
00058     def test_expression_filename(self):
00059         add_predicate(self.tool, id='doc',
00060                       title='.doc extension',
00061                       predicate='default',
00062                       expression="python: filename and filename.endswith('.doc')",
00063                       component_name='primary_field')
00064         add_predicate(self.tool, id='txt',
00065                       title='.txt extension',
00066                       predicate='default',
00067                       expression="python: filename and filename.endswith('.txt')",
00068                       component_name='rfc822')
00069         add_predicate(self.tool, id='default',
00070                       title='default',
00071                       predicate='default',
00072                       expression='',
00073                       component_name='primary_field')
00074 
00075         self.assertEquals(
00076             self.get(None, filename='sample.doc'), ('primary_field',))
00077         self.assertEquals(
00078             self.get(None, filename='sample.txt'),
00079             ('rfc822', 'primary_field'))
00080 
00081         # Make sure reordering the predicates does change the order
00082         # in which ids are returned.
00083         self.reverse()
00084         self.assertEquals(
00085             self.get(None, filename='sample.txt'),
00086             ('primary_field', 'rfc822'))
00087 
00088         self.assertEquals(self.get(None, filename='sample.rot'),
00089                           ('primary_field',))
00090         # Make sure it works even if no filename kw is passed in
00091         self.assertEquals(self.get(None), ('primary_field',))
00092 
00093     def test_expression_content_type(self):
00094         add_predicate(
00095             self.tool, id='text_xml',
00096             title='text/xml content type',
00097             predicate='default',
00098             expression="python: content_type and content_type == 'text/xml'",
00099             component_name='simple_xml')
00100         add_predicate(
00101             self.tool,
00102             id='text_plain',
00103             title='text/plain content type',
00104             predicate='default',
00105             expression="python: content_type and content_type == 'text/plain'",
00106             component_name='rfc822')
00107         add_predicate(
00108             self.tool,
00109             id='default',
00110             title='default',
00111             predicate='default',
00112             expression="",
00113             component_name='primary_field')
00114 
00115         self.assertEquals(
00116             self.get(None, content_type='text/xml'),
00117             ('simple_xml', 'primary_field'))
00118         self.assertEquals(self.get(None, content_type='text/plain'),
00119                           ('rfc822', 'primary_field'))
00120 
00121         # Make sure reordering the predicates does change the order
00122         # in which ids are returned.
00123         self.reverse()
00124         self.assertEquals(self.get(None, content_type='text/plain'),
00125                           ('primary_field', 'rfc822'))
00126 
00127         # Make sure it works even if no content_type kw is passed in
00128         self.assertEquals(self.get(None), ('primary_field',))
00129 
00130     def test_expression_data(self):
00131         add_predicate(
00132             self.tool,
00133             id='data_len_3',
00134             title='data length > 3',
00135             predicate='default',
00136             expression="python: data and len(data) > 3",
00137             component_name='primary_field')
00138         add_predicate(
00139             self.tool,
00140             id='data_len_2',
00141             title='data length == 2',
00142             predicate='default',
00143             expression="python: data and len(data) == 2",
00144             component_name='rfc822')
00145         add_predicate(
00146             self.tool,
00147             id='default',
00148             title='default',
00149             predicate='default',
00150             expression='',
00151             component_name='primary_field')
00152         self.assertEquals(self.get(None, data='4242'), ('primary_field',))
00153         self.assertEquals(self.get(None, data='42'), ('rfc822', 'primary_field'))
00154 
00155         # Make sure reordering the predicates does change the order
00156         # in which ids are returned.
00157         self.reverse()
00158         self.assertEquals(self.get(None, data='42'), ('primary_field', 'rfc822'))
00159 
00160         # Make sure it works even if no data kw is passed in
00161         self.assertEquals(self.get(None), ('primary_field',))
00162 
00163 
00164 class XMLNSPredicateTest(PredicateTest):
00165 
00166     def test_xmlns_element(self):
00167         p = add_predicate(
00168             self.tool,
00169             id='xmlns_attr',
00170             title='xmlns',
00171             predicate='xmlns_attr',
00172             expression='',
00173             component_name='primary_field')
00174         p.edit(element_name='test', value='FooBar')
00175         data = """<?xml version="1.0" encoding="UTF-8"?>
00176         <test>
00177            FooBar
00178         </test>
00179         """
00180         bad_data = """<?xml version="1.0" encoding="UTF-8"?>
00181         <test>
00182            FooBaz
00183         </test>
00184         """
00185         self.assertEquals(self.get(None, data=data), ('primary_field',))
00186         self.assertEquals(self.get(None, data=bad_data), ())
00187         self.assertEquals(self.get(None, data=''), ())
00188 
00189     def test_xmlns_element(self):
00190         p = add_predicate(
00191             self.tool,
00192             id='xmlns_attr',
00193             title='xmlns',
00194             predicate='xmlns_attr',
00195             expression='',
00196             component_name='primary_field')
00197         p.edit(element_name='test', value=None)
00198         data = """<?xml version="1.0" encoding="UTF-8"?>
00199         <test>
00200            Doesn't Matter
00201         </test>
00202         """
00203         self.assertEquals(self.get(None, data=data), ('primary_field',))
00204 
00205     def test_xmlns_element_ns(self):
00206         p = add_predicate(
00207             self.tool,
00208             id='xmlns_attr',
00209             title='xmlns',
00210             predicate='xmlns_attr',
00211             expression='',
00212             component_name='primary_field')
00213         p.edit(element_ns='http://foo.com/bar', element_name='test',
00214                value='FooBar')
00215         data = """<?xml version="1.0" encoding="UTF-8"?>
00216         <t:test xmlns:t="http://foo.com/bar">
00217                 FooBar
00218         </t:test>
00219         """
00220         self.assertEquals(self.get(None, data=data), ('primary_field',))
00221 
00222     def test_xmlns_element_ns_att(self):
00223         p = add_predicate(
00224             self.tool,
00225             id='xmlns_attr',
00226             title='xmlns',
00227             predicate='xmlns_attr',
00228             expression='',
00229             component_name='primary_field')
00230         p.edit(element_ns='http://foo.com/bar', element_name='test',
00231                attr_name='wot', value='FooBar')
00232         data = """<?xml version="1.0" encoding="UTF-8"?>
00233         <t:test xmlns:t="http://foo.com/bar"
00234            wot="FooBar" />
00235         """
00236         self.assertEquals(self.get(None, data=data), ('primary_field',))
00237 
00238     def test_xmlns_element_ns_att_ns(self):
00239         p = add_predicate(
00240             self.tool,
00241             id='xmlns_attr',
00242             title='xmlns',
00243             predicate='xmlns_attr',
00244             expression='',
00245             component_name='primary_field')
00246         p.edit(element_ns='http://foo.com/bar', element_name='test',
00247                attr_ns='http://foo.com/bar', attr_name='wot',
00248                value='FooBar')
00249         data = """<?xml version="1.0" encoding="UTF-8"?>
00250         <t:test xmlns:t="http://foo.com/bar"
00251            t:wot="FooBar" />
00252         """
00253         self.assertEquals(self.get(None, data=data), ('primary_field',))
00254 
00255 
00256 def test_suite():
00257     import unittest
00258     suite = unittest.TestSuite()
00259     suite.addTest(unittest.makeSuite(DefaultPredicateTest))
00260     suite.addTest(unittest.makeSuite(XMLNSPredicateTest))
00261     return suite
00262 
00263 if __name__ == '__main__':
00264     framework(descriptions=1, verbosity=1)