source: products/qTopic/branches/plone2.1/tests/test_topictool.py @ 1

Last change on this file since 1 was 1, checked in by myroslav, 14 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 14.7 KB
Line 
1"""
2    Tests for patched 'portal_atct'
3"""
4
5__docformat__ = 'restructuredtext'
6
7import os, sys
8if __name__ == '__main__':
9    execfile(os.path.join(sys.path[0], 'framework.py'))
10
11
12
13from Testing import ZopeTestCase
14ZopeTestCase.installProduct('qTopic')
15from Products.ATContentTypes.tests import atcttestcase
16
17
18from Products.CMFCore.utils import getToolByName
19from Products.CMFPlone.CatalogTool import CatalogTool
20from Products.ATContentTypes.config import TOOLNAME
21from Products.ATContentTypes.interfaces import IATCTTopicsTool
22from Interface.Verify import verifyObject
23from Products.ATContentTypes.configuration import zconf
24from Products.ATContentTypes.interfaces import IATCTTool
25from Products.ZCatalog.ZCatalog import manage_addZCatalog
26
27# z3 imports
28from Products.ATContentTypes.interface import IATCTTopicsTool as Z3IATCTTopicsTool
29from zope.interface.verify import verifyObject as Z3verifyObject
30
31tool_config = zconf.atct_tool.topic_tool
32
33
34tests = []
35index_def = {'index'        : 'end',
36             'friendlyName' : 'End Date For Test',
37             'description'  : 'This is an end Date',
38             'criteria'     : ['ATDateCriteria','ATDateRangeCriteria']
39            }
40meta_def =  {'metadata'     : 'ModificationDate',
41             'friendlyName' : 'Modification Date For Test',
42             'description'  : ''
43            }
44
45conf_index_def = [i for i in tool_config.indexes if i.name == index_def['index']][0]
46conf_meta_def = [m for m in tool_config.metadata if m.name == meta_def['metadata']][0]
47CATALOG_NAME = 'test_catalog'
48class TestTool(atcttestcase.ATCTSiteTestCase):
49
50    def afterSetUp(self):
51        self.tool = self.portal.portal_atct
52        manage_addZCatalog(self.portal, id = CATALOG_NAME, title = 'Test catalog')
53        self.cat = self.portal[CATALOG_NAME]
54        self.cat.addIndex('end', 'DateIndex')
55        self.cat.manage_addColumn(meta_def['metadata'])
56
57    def test_interface(self):
58        self.failUnless(IATCTTopicsTool.isImplementedBy(self.tool))
59        self.failUnless(verifyObject(IATCTTopicsTool, self.tool))
60
61    def test_Z3interface(self):
62        iface = Z3IATCTTopicsTool
63        self.failUnless(Z3verifyObject(iface,self.tool))
64 
65    #Index tests
66    def test_add_index(self):
67        t = self.tool
68
69        t.addIndex(enabled = True, catalog_name=CATALOG_NAME, **index_def)
70        index = t.getIndex(index_def['index'], catalog_name=CATALOG_NAME)
71        self.failUnlessEqual(index.index, index_def['index'])
72        self.failUnlessEqual(index.friendlyName, index_def['friendlyName'])
73        self.failUnlessEqual(index.description, index_def['description'])
74        #Only need to test truth not actual value
75        self.failUnless(index.enabled)
76        self.failUnlessEqual(index.criteria, tuple(index_def['criteria']))
77
78        self.failUnless(index in t.getEnabledIndexes(catalog_name=CATALOG_NAME))
79        self.failUnless(index_def['index'] in [a[0] for a in t.getEnabledFields(catalog_name=CATALOG_NAME)])
80        self.failUnless(index_def['index'] in t.getIndexDisplay(True, catalog_name=CATALOG_NAME).keys())
81        self.failUnless(index_def['friendlyName'] in t.getIndexDisplay(True, catalog_name=CATALOG_NAME).values())
82        self.failUnless(index_def['index'] in t.getIndexes(1))
83
84    def test_disable_index(self):
85        t = self.tool
86        t.addIndex(enabled = False, catalog_name=CATALOG_NAME, **index_def)
87        index = t.getIndex(index_def['index'], catalog_name=CATALOG_NAME)
88        self.failUnlessEqual(index.index, index_def['index'])
89        self.failUnlessEqual(index.friendlyName, index_def['friendlyName'])
90        self.failUnlessEqual(index.description, index_def['description'])
91        #Only need to test truth not actual value
92        self.failIf(index.enabled)
93        self.failUnlessEqual(index.criteria, tuple(index_def['criteria']))
94
95        self.failIf(index in t.getEnabledIndexes(catalog_name=CATALOG_NAME))
96        self.failIf(index_def['index'] in [a[0] for a in t.getEnabledFields(catalog_name=CATALOG_NAME)])
97        self.failIf(index_def['index'] in t.getIndexes(1,catalog_name=CATALOG_NAME))
98        self.failIf(index_def['index'] in t.getIndexDisplay(True, catalog_name=CATALOG_NAME).keys())
99        self.failUnless(index_def['friendlyName'] not in t.getIndexDisplay(True, catalog_name=CATALOG_NAME).values())
100        #Make sure it's still in the un-limited list
101        self.failUnless(index_def['index'] in t.getIndexDisplay(False, catalog_name=CATALOG_NAME).keys())
102        self.failUnless(index_def['friendlyName'] in t.getIndexDisplay(False, catalog_name=CATALOG_NAME).values())
103        self.failUnless(index_def['index'] in t.getIndexes(catalog_name=CATALOG_NAME))
104
105    def test_add_bogus_index(self):
106        """An metdatum that's not in the catalog should be deleted automatically
107           on any call to one of the index list methods."""
108        t = self.tool
109        t.addIndex('bogosity', enabled = True, catalog_name=CATALOG_NAME)
110        error = False
111        #The methods getEnabledFields, getEnabledIndexes, getIndexes,
112        #getIndexDisplay, and getIndex all automatically restore fields
113        #from the catalog
114
115        try:
116            t.getIndex('bogosity', catalog_name=CATALOG_NAME)
117        except AttributeError:
118            error = True
119        self.failUnless(error)
120
121        #Add
122        t.addIndex('bogosity', enabled = True, catalog_name=CATALOG_NAME)
123        self.failIf('bogosity' in [a[0] for a in t.getEnabledFields(catalog_name=CATALOG_NAME)])
124        #Add
125        t.addIndex('bogosity', enabled = True, catalog_name=CATALOG_NAME)
126        self.failIf('bogosity' in t.getIndexDisplay(True, catalog_name=CATALOG_NAME).keys())
127        #Add
128        t.addIndex('bogosity', enabled = True, catalog_name=CATALOG_NAME)
129        self.failIf('bogosity' in t.getIndexes(1, catalog_name=CATALOG_NAME))
130        #Add
131        t.addIndex('bogosity', enabled = True, catalog_name=CATALOG_NAME)
132        self.failIf('bogosity' in [i.index for i in t.getEnabledIndexes(catalog_name=CATALOG_NAME)])
133
134    def test_remove_index(self):
135        t = self.tool
136        t.addIndex(catalog_name=CATALOG_NAME, **index_def)
137        t.removeIndex(index_def['index'], catalog_name=CATALOG_NAME)
138        error = None
139        try:
140            index = t.topic_indexes[CATALOG_NAME][index_def['index']]
141        except KeyError:
142            error = True
143        self.failUnless(error)
144        #Should be restored automatically by getIndex and friends
145        error = None
146        try:
147            index = t.getIndex(index_def['index'], catalog_name=CATALOG_NAME)
148        except AttributeError:
149            error = True
150        self.failIf(error)
151        #Make sure the FriendlyName is reset to default
152        self.failUnlessEqual(index.friendlyName, getattr(conf_index_def,'friendlyName'))
153
154    def test_update_index(self):
155        """An index with no criteria set should set all available criteria,
156           also changes made using updateIndex should not reset already set
157           values"""
158        t = self.tool
159        t.addIndex(enabled = True, catalog_name=CATALOG_NAME, **index_def)
160        t.updateIndex(index_def['index'], criteria = None,
161                      description = 'New Description', catalog_name=CATALOG_NAME)
162        index = t.getIndex(index_def['index'], catalog_name=CATALOG_NAME)
163        self.failUnless(index.criteria)
164        self.failUnless(index.criteria != index_def['criteria'])
165        self.failUnless(index.description == 'New Description')
166        self.failUnless(index.friendlyName == index_def['friendlyName'])
167        self.failUnless(index.enabled)
168
169    def test_all_indexes(self):
170        """Ensure that the tool includes all indexes in the catalog"""
171        t = self.tool
172        cat = getToolByName(self.tool, CATALOG_NAME)
173        indexes = [field for field in cat.indexes()]
174        init_indexes = list(t.getIndexes(catalog_name=CATALOG_NAME))
175        unique_indexes = [i for i in indexes if i not in init_indexes]
176        unique_indexes = unique_indexes + [i for i in init_indexes if i not in indexes]
177        self.failIf(unique_indexes)
178
179    def test_change_catalog_index(self):
180        """Make sure tool updates when indexes are added to or deleted from
181           the catalog"""
182        t = self.tool
183        cat = getToolByName(self.tool, CATALOG_NAME)
184        #add
185        error = False
186        cat.manage_addIndex('nonsense', 'FieldIndex')
187        try:
188            t.getIndex('nonsense', catalog_name=CATALOG_NAME)
189        except AttributeError:
190            error = True
191        self.failIf(error)
192        #remove
193        error = False
194        cat.delIndex('nonsense')
195        try:
196            t.getIndex('nonsense', catalog_name=CATALOG_NAME)
197        except AttributeError:
198            error = True
199        self.failUnless(error)
200
201
202    #Metadata tests
203    def test_add_metadata(self):
204        t = self.tool
205        t.addMetadata(enabled = True, catalog_name=CATALOG_NAME,**meta_def)
206        meta = t.getMetadata(meta_def['metadata'], catalog_name=CATALOG_NAME)
207        self.failUnlessEqual(meta.index, meta_def['metadata'])
208        self.failUnlessEqual(meta.friendlyName, meta_def['friendlyName'])
209        self.failUnlessEqual(meta.description, meta_def['description'])
210        #Only need to test truth not actual value
211        self.failUnless(meta.enabled)
212
213        self.failUnless(meta in t.getEnabledMetadata(catalog_name=CATALOG_NAME))
214        self.failUnless(meta_def['metadata'] in t.getMetadataDisplay(True, catalog_name=CATALOG_NAME).keys())
215        self.failUnless(meta_def['friendlyName'] in t.getMetadataDisplay(True, catalog_name=CATALOG_NAME).values())
216        self.failUnless(meta_def['metadata'] in t.getAllMetadata(1, catalog_name=CATALOG_NAME))
217
218    def test_disable_metadata(self):
219        t = self.tool
220        t.addMetadata(enabled = False, catalog_name=CATALOG_NAME, **meta_def)
221        meta = t.getMetadata(meta_def['metadata'], catalog_name=CATALOG_NAME)
222        self.failUnlessEqual(meta.index, meta_def['metadata'])
223        self.failUnlessEqual(meta.friendlyName, meta_def['friendlyName'])
224        self.failUnlessEqual(meta.description, meta_def['description'])
225        #Only need to test truth not actual value
226        self.failIf(meta.enabled)
227
228        self.failUnless(meta not in t.getEnabledMetadata(catalog_name=CATALOG_NAME))
229        self.failIf(meta_def['metadata'] in t.getAllMetadata(1, catalog_name=CATALOG_NAME))
230        self.failIf(meta_def['metadata'] in t.getMetadataDisplay(True, catalog_name=CATALOG_NAME).keys())
231        self.failIf(meta_def['friendlyName'] in t.getMetadataDisplay(True, catalog_name=CATALOG_NAME).values())
232        #Make sure it's still in the un-limited list
233        self.failUnless(meta_def['metadata'] in t.getMetadataDisplay(False, catalog_name=CATALOG_NAME).keys())
234        self.failUnless(meta_def['friendlyName'] in t.getMetadataDisplay(False, catalog_name=CATALOG_NAME).values())
235        self.failUnless(meta_def['metadata'] in t.getAllMetadata(catalog_name=CATALOG_NAME))
236
237    def test_add_bogus_metadata(self):
238        """An metdatum that's not in the catalog should be deleted automatically
239           on any call to one of the index list methods"""
240        t = self.tool
241        t.addMetadata('bogosity', enabled = True, catalog_name=CATALOG_NAME)
242
243        error = False
244        #The methods getEnabledMetadata, getAllMetadata, getMetadataDisplay,
245        #and getMetadata all automatically restore fields from the catalog
246        try:
247            t.getMetadata('bogosity', catalog_name=CATALOG_NAME)
248        except AttributeError:
249            error = True
250        self.assert_(error)
251
252        #Add
253        t.addMetadata('bogosity', enabled = True, catalog_name=CATALOG_NAME)
254        self.failIf('bogosity' in t.getMetadataDisplay(True, catalog_name=CATALOG_NAME).keys())
255        #Add
256        t.addMetadata('bogosity', enabled = True, catalog_name=CATALOG_NAME)
257        self.failIf('bogosity' in t.getAllMetadata(1, catalog_name=CATALOG_NAME))
258        #Add
259        t.addMetadata('bogosity', enabled = True, catalog_name=CATALOG_NAME)
260        self.failIf('bogosity' in [i.index for i in t.getEnabledMetadata(catalog_name=CATALOG_NAME)])
261
262    def test_remove_metadata(self):
263        t = self.tool
264        t.addMetadata(catalog_name=CATALOG_NAME, **meta_def)
265        t.removeMetadata(meta_def['metadata'], catalog_name=CATALOG_NAME)
266        error = None
267        try:
268            meta = t.topic_metadata[CATALOG_NAME][meta_def['metadata']]
269        except KeyError:
270            error = True
271        self.failUnless(error)
272        #Should be restored automatically by getMetadata and friends
273        error = None
274        try:
275            meta = t.getMetadata(meta_def['metadata'], catalog_name=CATALOG_NAME)
276        except AttributeError:
277            error = True
278        self.failIf(error)
279        #Make sure the FriendlyName is reset to default
280        self.failUnlessEqual(meta.friendlyName, getattr(conf_meta_def,'friendlyName'))
281
282    def test_update_metadata(self):
283        """Changes made using updateMetadata should not reset already set
284           values"""
285        t = self.tool
286        t.addMetadata(enabled = True, catalog_name=CATALOG_NAME, **meta_def)
287        t.updateMetadata(meta_def['metadata'], friendlyName = 'New Name', catalog_name=CATALOG_NAME)
288        meta = t.getMetadata(meta_def['metadata'], catalog_name=CATALOG_NAME)
289        self.failUnless(meta.friendlyName == 'New Name')
290        self.failUnless(meta.enabled)
291
292    def test_all_metadata(self):
293        """Ensure that the tool includes all metadata in the catalog"""
294        t = self.tool
295        cat = getToolByName(self.tool, CATALOG_NAME)
296        metadata = [field for field in cat.schema()]
297        init_metadata = list(t.getAllMetadata(catalog_name=CATALOG_NAME))
298        unique_metadata = [i for i in metadata if i not in init_metadata]
299        unique_metadata = unique_metadata + [i for i in init_metadata if i not in metadata]
300        self.failIf(unique_metadata)
301
302    def test_change_catalog_schema(self):
303        """Make sure tool updates when columns are added to or deleted from
304           the catalog"""
305        t = self.tool
306        cat = getToolByName(self.tool, CATALOG_NAME)
307        #add
308        error = False
309        cat.manage_addColumn('nonsense')
310        try:
311            t.getMetadata('nonsense', catalog_name=CATALOG_NAME)
312        except AttributeError:
313            error = True
314        self.failIf(error)
315        #remove
316        error = False
317        cat.delColumn('nonsense')
318        try:
319            t.getMetadata('nonsense',catalog_name=CATALOG_NAME)
320        except AttributeError:
321            error = True
322        self.assert_(error)
323
324tests.append(TestTool)
325
326if __name__ == '__main__':
327    framework()
328else:
329    # While framework.py provides its own test_suite()
330    # method the testrunner utility does not.
331    import unittest
332    def test_suite():
333        suite = unittest.TestSuite()
334        for test in tests:
335            suite.addTest(unittest.makeSuite(test))
336        return suite
Note: See TracBrowser for help on using the repository browser.