source: main/waeup.kofa/branches/uli-rm-bootstrap/src/waeup/kofa/tests/test_maintenance.py @ 15505

Last change on this file since 15505 was 12110, checked in by Henrik Bettermann, 10 years ago

svn propset svn:keywords "Id"

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1import shutil
2import tempfile
3from grokcore.site.interfaces import IUtilityInstaller
4from zope import schema
5from zope.catalog.catalog import Catalog
6from zope.catalog.field import FieldIndex
7from zope.catalog.interfaces import ICatalog
8from zope.component import getUtility
9from zope.component.hooks import setSite
10from zope.interface import Interface, implements
11from waeup.kofa.app import University
12from waeup.kofa.maintenance import update_catalog
13from waeup.kofa.testing import FunctionalTestCase, FunctionalLayer
14
15class ISampleContent(Interface):
16
17    age = schema.Int(
18        title=u'Age')
19    name = schema.TextLine(
20        title=u'Name')
21
22class SampleContent(object):
23    implements(ISampleContent)
24
25    def __init__(self, age=None, name=None):
26        self.age = age
27        self.name = name
28        return
29
30    def __eq__(self, ob):
31        # make sample contents comparable
32        if not hasattr(ob, 'name') or not hasattr(ob, 'age'):
33            return False
34        return self.age == ob.age and self.name == ob.name
35
36class UpdateCatalogTests(FunctionalTestCase):
37
38    layer = FunctionalLayer
39
40    def setUp(self):
41        super(UpdateCatalogTests, self).setUp()
42        # Setup a sample site for each test
43        app = University()
44        self.dc_root = tempfile.mkdtemp()
45        app['datacenter'].setStoragePath(self.dc_root)
46
47        # Prepopulate the ZODB...
48        self.getRootFolder()['app'] = app
49        self.app = self.getRootFolder()['app']
50        setSite(self.app)
51        return
52
53    def tearDown(self):
54        super(UpdateCatalogTests, self).tearDown()
55        shutil.rmtree(self.dc_root)
56        return
57
58    def create_catalog(self):
59        # install catalog in site
60        self.catalog = Catalog()
61        install = getUtility(IUtilityInstaller)
62        install(self.app, self.catalog, ICatalog, 'my_catalog')
63        self.create_indexes()
64        self.catalog.clear() # make sure cat is empty
65        return
66
67    def create_indexes(self):
68        # create 'age' and 'name' field index in cat if they do not
69        # exist already.
70        for name in ('age', 'name'):
71            if name in self.catalog.keys():
72                continue
73            self.catalog[name] = FieldIndex(
74                field_name=name, interface=ISampleContent,
75                field_callable=False)
76        return
77
78    def add_sample_content(self):
79        # Add sample content in ZODB
80        manfred = SampleContent(42, 'Manfred')
81        self.app['manfred'] = manfred
82        self.manfred = self.app['manfred'] # this one has __parent__ etc.
83        return
84
85    def test_update_catalog_by_list(self):
86        ## We can catalog new objects passed in as list
87        # create object in ZODB before catalog exists
88        self.add_sample_content()
89        self.create_catalog()
90
91        # right now we can't find manfred
92        result1 = list(self.catalog.searchResults(age=(40,43)))
93        self.assertEqual(len(result1), 0)
94
95        # update catalog. We have to feed 'located' objects, which is
96        # normally the case for objects from ZODB. In our case
97        # 'manfred' could not be catalogued while the located version
98        # 'manfred_located' (with a __parent__ and __name__ attribute)
99        # can.
100        update_catalog(self.app, 'my_catalog', [self.manfred])
101
102        # now we can find manfred in catalog
103        result2 = list(self.catalog.searchResults(age=(40,43)))
104        self.assertEqual(result2[0], self.manfred)
105        return
106
107    def test_update_catalog_iterator(self):
108        ## We can catalog new objects via function
109        # create object in ZODB before catalog exists
110        self.add_sample_content()
111        self.create_catalog()
112
113        # right now we can't find manfred
114        result1 = list(self.catalog.searchResults(age=(40,43)))
115        self.assertEqual(len(result1), 0)
116
117        # update catalog. We have to feed 'located' objects, which is
118        # normally the case for objects from ZODB. In our case
119        # 'manfred' could not be catalogued while the located version
120        # 'manfred_located' (with a __parent__ and __name__ attribute)
121        # can.
122        def func():
123            for x in [self.manfred]:
124                yield x
125        update_catalog(self.app, 'my_catalog', func=func)
126
127        # now we can find manfred in catalog
128        result2 = list(self.catalog.searchResults(age=(40,43)))
129        self.assertEqual(result2[0], self.manfred)
130        return
Note: See TracBrowser for help on using the repository browser.