vendor/CMF/1.6.3/CMFCore

view tests/test_ContentTypeRegistry.py @ 0:587011552858

import CMF 1.6.3
author bdelbosc
date Mon, 23 Apr 2007 13:58:01 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Unit tests for ContentTypeRegistry module.
15 $Id$
16 """
18 from unittest import TestCase, TestSuite, makeSuite, main
19 import Testing
20 try:
21 import Zope2
22 except ImportError: # BBB: for Zope 2.7
23 import Zope as Zope2
24 Zope2.startup()
27 class MajorMinorPredicateTests( TestCase ):
29 def _makeOne(self, *args, **kw):
30 from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate
32 return MajorMinorPredicate(*args, **kw)
34 def test_z2interfaces(self):
35 from Interface.Verify import verifyClass
36 from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate
37 from Products.CMFCore.interfaces.ContentTypeRegistry \
38 import ContentTypeRegistryPredicate \
39 as IContentTypeRegistryPredicate
41 verifyClass(IContentTypeRegistryPredicate, MajorMinorPredicate)
43 def test_z3interfaces(self):
44 try:
45 from zope.interface.verify import verifyClass
46 from Products.CMFCore.interfaces \
47 import IContentTypeRegistryPredicate
48 except ImportError:
49 # BBB: for Zope 2.7
50 return
51 from Products.CMFCore.ContentTypeRegistry import MajorMinorPredicate
53 verifyClass(IContentTypeRegistryPredicate, MajorMinorPredicate)
55 def test_empty( self ):
56 pred = self._makeOne('empty')
57 assert pred.getMajorType() == 'None'
58 assert pred.getMinorType() == 'None'
59 assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
61 def test_simple( self ):
62 pred = self._makeOne('plaintext')
63 pred.edit( 'text', 'plain' )
64 assert pred.getMajorType() == 'text'
65 assert pred.getMinorType() == 'plain'
66 assert pred( 'foo', 'text/plain', 'asdfljksadf' )
67 assert not pred( 'foo', 'text/html', 'asdfljksadf' )
68 assert not pred( '', '', '' )
69 assert not pred( '', 'asdf', '' )
71 def test_wildcard( self ):
72 pred = self._makeOne('alltext')
73 pred.edit( 'text', '' )
74 assert pred.getMajorType() == 'text'
75 assert pred.getMinorType() == ''
76 assert pred( 'foo', 'text/plain', 'asdfljksadf' )
77 assert pred( 'foo', 'text/html', 'asdfljksadf' )
78 assert not pred( 'foo', 'image/png', 'asdfljksadf' )
80 pred.edit( '', 'html' )
81 assert pred.getMajorType() == ''
82 assert pred.getMinorType() == 'html'
83 assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
84 assert pred( 'foo', 'text/html', 'asdfljksadf' )
85 assert not pred( 'foo', 'image/png', 'asdfljksadf' )
88 class ExtensionPredicateTests( TestCase ):
90 def _makeOne(self, *args, **kw):
91 from Products.CMFCore.ContentTypeRegistry import ExtensionPredicate
93 return ExtensionPredicate(*args, **kw)
95 def test_z2interfaces(self):
96 from Interface.Verify import verifyClass
97 from Products.CMFCore.ContentTypeRegistry import ExtensionPredicate
98 from Products.CMFCore.interfaces.ContentTypeRegistry \
99 import ContentTypeRegistryPredicate \
100 as IContentTypeRegistryPredicate
102 verifyClass(IContentTypeRegistryPredicate, ExtensionPredicate)
104 def test_z3interfaces(self):
105 try:
106 from zope.interface.verify import verifyClass
107 from Products.CMFCore.interfaces \
108 import IContentTypeRegistryPredicate
109 except ImportError:
110 # BBB: for Zope 2.7
111 return
112 from Products.CMFCore.ContentTypeRegistry import ExtensionPredicate
114 verifyClass(IContentTypeRegistryPredicate, ExtensionPredicate)
116 def test_empty( self ):
117 pred = self._makeOne('empty')
118 assert pred.getExtensions() == 'None'
119 assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
120 assert not pred( 'foo.txt', 'text/plain', 'asdfljksadf' )
121 assert not pred( 'foo.bar', 'text/html', 'asdfljksadf' )
123 def test_simple( self ):
124 pred = self._makeOne('stardottext')
125 pred.edit( 'txt' )
126 assert pred.getExtensions() == 'txt'
127 assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
128 assert pred( 'foo.txt', 'text/plain', 'asdfljksadf' )
129 assert not pred( 'foo.bar', 'text/html', 'asdfljksadf' )
131 def test_multi( self ):
132 pred = self._makeOne('stardottext')
133 pred.edit( 'txt text html htm' )
134 assert pred.getExtensions() == 'txt text html htm'
135 assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
136 assert pred( 'foo.txt', 'text/plain', 'asdfljksadf' )
137 assert pred( 'foo.text', 'text/plain', 'asdfljksadf' )
138 assert pred( 'foo.html', 'text/plain', 'asdfljksadf' )
139 assert pred( 'foo.htm', 'text/plain', 'asdfljksadf' )
140 assert not pred( 'foo.bar', 'text/html', 'asdfljksadf' )
143 class MimeTypeRegexPredicateTests( TestCase ):
145 def _makeOne(self, *args, **kw):
146 from Products.CMFCore.ContentTypeRegistry \
147 import MimeTypeRegexPredicate
149 return MimeTypeRegexPredicate(*args, **kw)
151 def test_z2interfaces(self):
152 from Interface.Verify import verifyClass
153 from Products.CMFCore.ContentTypeRegistry \
154 import MimeTypeRegexPredicate
155 from Products.CMFCore.interfaces.ContentTypeRegistry \
156 import ContentTypeRegistryPredicate \
157 as IContentTypeRegistryPredicate
159 verifyClass(IContentTypeRegistryPredicate, MimeTypeRegexPredicate)
161 def test_z3interfaces(self):
162 try:
163 from zope.interface.verify import verifyClass
164 from Products.CMFCore.interfaces \
165 import IContentTypeRegistryPredicate
166 except ImportError:
167 # BBB: for Zope 2.7
168 return
169 from Products.CMFCore.ContentTypeRegistry \
170 import MimeTypeRegexPredicate
172 verifyClass(IContentTypeRegistryPredicate, MimeTypeRegexPredicate)
174 def test_empty( self ):
175 pred = self._makeOne('empty')
176 assert pred.getPatternStr() == 'None'
177 assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
179 def test_simple( self ):
180 pred = self._makeOne('plaintext')
181 pred.edit( 'text/plain' )
182 assert pred.getPatternStr() == 'text/plain'
183 assert pred( 'foo', 'text/plain', 'asdfljksadf' )
184 assert not pred( 'foo', 'text/html', 'asdfljksadf' )
186 def test_pattern( self ):
187 pred = self._makeOne('alltext')
188 pred.edit( 'text/*' )
189 assert pred.getPatternStr() == 'text/*'
190 assert pred( 'foo', 'text/plain', 'asdfljksadf' )
191 assert pred( 'foo', 'text/html', 'asdfljksadf' )
192 assert not pred( 'foo', 'image/png', 'asdfljksadf' )
195 class NameRegexPredicateTests( TestCase ):
197 def _makeOne(self, *args, **kw):
198 from Products.CMFCore.ContentTypeRegistry import NameRegexPredicate
200 return NameRegexPredicate(*args, **kw)
202 def test_z2interfaces(self):
203 from Interface.Verify import verifyClass
204 from Products.CMFCore.ContentTypeRegistry import NameRegexPredicate
205 from Products.CMFCore.interfaces.ContentTypeRegistry \
206 import ContentTypeRegistryPredicate \
207 as IContentTypeRegistryPredicate
209 verifyClass(IContentTypeRegistryPredicate, NameRegexPredicate)
211 def test_z3interfaces(self):
212 try:
213 from zope.interface.verify import verifyClass
214 from Products.CMFCore.interfaces \
215 import IContentTypeRegistryPredicate
216 except ImportError:
217 # BBB: for Zope 2.7
218 return
219 from Products.CMFCore.ContentTypeRegistry import NameRegexPredicate
221 verifyClass(IContentTypeRegistryPredicate, NameRegexPredicate)
223 def test_empty( self ):
224 pred = self._makeOne('empty')
225 assert pred.getPatternStr() == 'None'
226 assert not pred( 'foo', 'text/plain', 'asdfljksadf' )
228 def test_simple( self ):
229 pred = self._makeOne('onlyfoo')
230 pred.edit( 'foo' )
231 assert pred.getPatternStr() == 'foo'
232 assert pred( 'foo', 'text/plain', 'asdfljksadf' )
233 assert not pred( 'fargo', 'text/plain', 'asdfljksadf' )
234 assert not pred( 'bar', 'text/plain', 'asdfljksadf' )
236 def test_pattern( self ):
237 pred = self._makeOne('allfwords')
238 pred.edit( 'f.*' )
239 assert pred.getPatternStr() == 'f.*'
240 assert pred( 'foo', 'text/plain', 'asdfljksadf' )
241 assert pred( 'fargo', 'text/plain', 'asdfljksadf' )
242 assert not pred( 'bar', 'text/plain', 'asdfljksadf' )
245 class ContentTypeRegistryTests( TestCase ):
247 def _makeOne(self, *args, **kw):
248 from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
250 return ContentTypeRegistry(*args, **kw)
252 def setUp( self ):
253 self.reg = self._makeOne()
255 def test_z2interfaces(self):
256 from Interface.Verify import verifyClass
257 from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
258 from Products.CMFCore.interfaces.ContentTypeRegistry \
259 import ContentTypeRegistry as IContentTypeRegistry
261 verifyClass(IContentTypeRegistry, ContentTypeRegistry)
263 def test_z3interfaces(self):
264 try:
265 from zope.interface.verify import verifyClass
266 from Products.CMFCore.interfaces import IContentTypeRegistry
267 except ImportError:
268 # BBB: for Zope 2.7
269 return
270 from Products.CMFCore.ContentTypeRegistry import ContentTypeRegistry
272 verifyClass(IContentTypeRegistry, ContentTypeRegistry)
274 def test_empty( self ):
275 reg=self.reg
276 assert reg.findTypeName( 'foo', 'text/plain', 'asdfljksadf' ) is None
277 assert reg.findTypeName( 'fargo', 'text/plain', 'asdfljksadf' ) is None
278 assert reg.findTypeName( 'bar', 'text/plain', 'asdfljksadf' ) is None
279 assert not reg.listPredicates()
280 self.assertRaises( KeyError, reg.removePredicate, 'xyzzy' )
282 def test_reorder( self ):
283 reg=self.reg
284 predIDs = ( 'foo', 'bar', 'baz', 'qux' )
285 for predID in predIDs:
286 reg.addPredicate( predID, 'name_regex' )
287 ids = tuple( map( lambda x: x[0], reg.listPredicates() ) )
288 assert ids == predIDs
289 reg.reorderPredicate( 'bar', 3 )
290 ids = tuple( map( lambda x: x[0], reg.listPredicates() ) )
291 assert ids == ( 'foo', 'baz', 'qux', 'bar' )
293 def test_lookup( self ):
294 reg=self.reg
295 reg.addPredicate( 'image', 'major_minor' )
296 reg.getPredicate( 'image' ).edit( 'image', '' )
297 reg.addPredicate( 'onlyfoo', 'name_regex' )
298 reg.getPredicate( 'onlyfoo' ).edit( 'foo' )
299 reg.assignTypeName( 'onlyfoo', 'Foo' )
300 assert reg.findTypeName( 'foo', 'text/plain', 'asdfljksadf' ) == 'Foo'
301 assert not reg.findTypeName( 'fargo', 'text/plain', 'asdfljksadf' )
302 assert not reg.findTypeName( 'bar', 'text/plain', 'asdfljksadf' )
303 assert reg.findTypeName( 'foo', '', '' ) == 'Foo'
304 assert reg.findTypeName( 'foo', None, None ) == 'Foo'
307 def test_suite():
308 return TestSuite((
309 makeSuite( MajorMinorPredicateTests ),
310 makeSuite( ExtensionPredicateTests ),
311 makeSuite( MimeTypeRegexPredicateTests ),
312 makeSuite( NameRegexPredicateTests ),
313 makeSuite( ContentTypeRegistryTests ),
314 ))
316 if __name__ == '__main__':
317 main(defaultTest='test_suite')