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