1 | ## $Id: test_catalog.py 8321 2012-05-02 06:24:42Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | import shutil |
---|
19 | import tempfile |
---|
20 | from zope.catalog.interfaces import ICatalog |
---|
21 | from zope.component import queryUtility |
---|
22 | from zope.component.hooks import setSite |
---|
23 | from zope.interface.verify import verifyClass, verifyObject |
---|
24 | from waeup.kofa.app import University |
---|
25 | from waeup.kofa.interfaces import IQueryResultItem |
---|
26 | from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase |
---|
27 | from waeup.kofa.accesscodes.accesscode import ( |
---|
28 | AccessCodeBatch, invalidate_accesscode, disable_accesscode) |
---|
29 | from waeup.kofa.accesscodes.workflow import INITIALIZED, USED, DISABLED |
---|
30 | from waeup.kofa.accesscodes.catalog import AccessCodeQueryResultItem, search |
---|
31 | |
---|
32 | class CatalogTestSetup(FunctionalTestCase): |
---|
33 | # A setup for testing accesscode catalog related stuff. |
---|
34 | # |
---|
35 | # sets up a site with some accesscode batches already created. |
---|
36 | layer = FunctionalLayer |
---|
37 | |
---|
38 | def setUp(self): |
---|
39 | super(CatalogTestSetup, self).setUp() |
---|
40 | |
---|
41 | # Prepopulate ZODB |
---|
42 | app = University() |
---|
43 | self.dc_root = tempfile.mkdtemp() |
---|
44 | app['datacenter'].setStoragePath(self.dc_root) |
---|
45 | |
---|
46 | # Prepopulate the ZODB... |
---|
47 | self.getRootFolder()['app'] = app |
---|
48 | self.app = self.getRootFolder()['app'] |
---|
49 | setSite(self.app) |
---|
50 | |
---|
51 | # Create batch |
---|
52 | batch = AccessCodeBatch('now', 'manfred', 'APP', 6.6, 0) |
---|
53 | self.app['accesscodes'].addBatch(batch) |
---|
54 | |
---|
55 | # Fill batch with accesscodes |
---|
56 | batch.addAccessCode(0, '11111111') |
---|
57 | batch.addAccessCode(1, '22222222') |
---|
58 | batch.addAccessCode(2, '33333333') |
---|
59 | self.ac1 = batch.getAccessCode('APP-1-11111111') |
---|
60 | self.ac2 = batch.getAccessCode('APP-1-22222222') |
---|
61 | self.ac3 = batch.getAccessCode('APP-1-33333333') |
---|
62 | return |
---|
63 | |
---|
64 | def tearDown(self): |
---|
65 | shutil.rmtree(self.dc_root) |
---|
66 | super(CatalogTestSetup, self).tearDown() |
---|
67 | return |
---|
68 | |
---|
69 | |
---|
70 | class AccessCodeCatalogTests(CatalogTestSetup): |
---|
71 | # Tests for helpers like get_access_code, disable_accesscode, ... |
---|
72 | |
---|
73 | layer = FunctionalLayer |
---|
74 | |
---|
75 | def test_get_catalog(self): |
---|
76 | # We can get an accesscodes catalog if we wish |
---|
77 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
78 | assert cat is not None |
---|
79 | |
---|
80 | def test_search_by_code(self): |
---|
81 | # We can find a certain code |
---|
82 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
83 | results = cat.searchResults(code=('APP-1-11111111', 'APP-1-11111111')) |
---|
84 | results = [x for x in results] # Turn results generator into list |
---|
85 | assert len(results) == 1 |
---|
86 | assert results[0] is self.ac1 |
---|
87 | |
---|
88 | def test_search_code_not_existent(self): |
---|
89 | # Not existent codes will not be found |
---|
90 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
91 | results = cat.searchResults(code=('APP-1-blah', 'APP-1-blah')) |
---|
92 | results = [x for x in results] # Turn results generator into list |
---|
93 | assert len(results) == 0 |
---|
94 | |
---|
95 | def test_search_history(self): |
---|
96 | # We can search for certain history entries |
---|
97 | # To update history we use `invalidate_accesscode` |
---|
98 | invalidate_accesscode( |
---|
99 | 'APP-1-11111111', comment='used by Tester') |
---|
100 | |
---|
101 | # Now we want to find the term ``Tester`` in histories of all acs |
---|
102 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
103 | results = cat.searchResults(history='Tester') |
---|
104 | results = [x for x in results] # Turn results generator into list |
---|
105 | assert len(results) == 1 |
---|
106 | assert results[0] is self.ac1 |
---|
107 | |
---|
108 | def test_search_disabled(self): |
---|
109 | # We can seach for disabled access codes |
---|
110 | disable_accesscode('APP-1-11111111') |
---|
111 | # Now we want to find the disabled access code |
---|
112 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
113 | results1 = cat.searchResults(state=(DISABLED, DISABLED)) |
---|
114 | results2 = cat.searchResults(state=(INITIALIZED, INITIALIZED)) |
---|
115 | results1 = [x for x in results1] # Turn results generator into list |
---|
116 | # We found 1 accescode disabled |
---|
117 | assert len(results1) == 1 |
---|
118 | assert results1[0] is self.ac1 |
---|
119 | # We found 2 accesscodes not disabled |
---|
120 | assert len(results2) == 2 |
---|
121 | assert [x for x in results2] == [self.ac2, self.ac3] |
---|
122 | |
---|
123 | def test_search_used(self): |
---|
124 | # We can search for used/unused access codes |
---|
125 | invalidate_accesscode('APP-1-11111111') |
---|
126 | # Now we want to find the invalidated access code |
---|
127 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
128 | results1 = cat.searchResults(state=(USED, USED)) |
---|
129 | results2 = cat.searchResults(state=(INITIALIZED, INITIALIZED)) |
---|
130 | results1 = [x for x in results1] # Turn results generator into list |
---|
131 | # We found 1 accescode used |
---|
132 | assert len(results1) == 1 |
---|
133 | assert results1[0] is self.ac1 |
---|
134 | # We found 2 accesscodes not used |
---|
135 | assert len(results2) == 2 |
---|
136 | assert [x for x in results2] == [self.ac2, self.ac3] |
---|
137 | |
---|
138 | def test_search_mixed(self): |
---|
139 | # We can ask for several attributes at the same time |
---|
140 | invalidate_accesscode('APP-1-11111111', comment='Used by Tester') |
---|
141 | disable_accesscode('APP-1-33333333', comment='Disabled by Tester') |
---|
142 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
143 | # Now we search for an applicants code that has 'Tester' |
---|
144 | # mentioned in history, is used, and has a code between |
---|
145 | # 'APP-1-11111111' and 'APP-2-11111111'. This should find |
---|
146 | # exactly one access code. |
---|
147 | results = cat.searchResults(history='Tester', |
---|
148 | state=(USED, USED), |
---|
149 | code=('APP-1-11111111', 'APP-1-22222222')) |
---|
150 | assert len(results) == 1 |
---|
151 | assert self.ac1 in results |
---|
152 | |
---|
153 | def test_ac_change(self): |
---|
154 | # When an AC is changed, that change will be reflected |
---|
155 | # immediately in catalog |
---|
156 | cat = queryUtility(ICatalog, name='accesscodes_catalog') |
---|
157 | result = cat.searchResults(state=(USED, USED)) |
---|
158 | assert len(result) == 0 |
---|
159 | invalidate_accesscode('APP-1-11111111') |
---|
160 | result = cat.searchResults(state=(USED, USED)) |
---|
161 | assert len(result) == 1 |
---|
162 | |
---|
163 | class FakeView(object): |
---|
164 | # A view we can use in tests. Provides only the neccessary methods. |
---|
165 | flashed = [] |
---|
166 | def url(self, context): |
---|
167 | pass |
---|
168 | def flash(self, msg): |
---|
169 | self.flashed.append(msg) |
---|
170 | |
---|
171 | class AccessCodeQueryResultItemTests(CatalogTestSetup): |
---|
172 | # Test query result items |
---|
173 | |
---|
174 | layer = FunctionalLayer |
---|
175 | |
---|
176 | def test_ifaces(self): |
---|
177 | # Make sure we implement the interfaces correctly |
---|
178 | view = FakeView() |
---|
179 | self.assertTrue(verifyClass( |
---|
180 | IQueryResultItem, AccessCodeQueryResultItem)) |
---|
181 | item = AccessCodeQueryResultItem(self.ac1, view) |
---|
182 | self.assertTrue(verifyObject( |
---|
183 | IQueryResultItem, item)) |
---|
184 | return |
---|
185 | |
---|
186 | class SearchTests(CatalogTestSetup): |
---|
187 | # Tests for the search() function |
---|
188 | |
---|
189 | layer = FunctionalLayer |
---|
190 | |
---|
191 | def setUp(self): |
---|
192 | super(SearchTests, self).setUp() |
---|
193 | self.view = FakeView() |
---|
194 | return |
---|
195 | |
---|
196 | def test_search_none(self): |
---|
197 | # if we search none we will get it. Also a warning is displayed. |
---|
198 | result = search(view=self.view) |
---|
199 | self.assertTrue(result is None) |
---|
200 | self.assertEqual(self.view.flashed, ['Empty search string.']) |
---|
201 | return |
---|
202 | |
---|
203 | def test_search_history(self): |
---|
204 | # we can search for history entries |
---|
205 | result = search( |
---|
206 | query='initialized', # A word that appears in all items' hist |
---|
207 | searchtype='history', view=self.view) |
---|
208 | self.assertEqual(len(result), 3) |
---|
209 | return |
---|
210 | |
---|
211 | def test_search_batch_serial(self): |
---|
212 | # we can search for batch serials |
---|
213 | result = search( |
---|
214 | query=2, |
---|
215 | searchtype='batch_serial', view=self.view) |
---|
216 | self.assertEqual(len(result), 1) |
---|
217 | self.assertEqual(result[0].code, 'APP-1-33333333') |
---|
218 | return |
---|
219 | |
---|
220 | def test_search_freestyle(self): |
---|
221 | # we can even search for arbitrary fields |
---|
222 | result = search( |
---|
223 | query='APP-1-11111111', |
---|
224 | searchtype='code', view=self.view) |
---|
225 | self.assertEqual(len(result), 1) |
---|
226 | self.assertEqual(result[0].code, 'APP-1-11111111') |
---|
227 | return |
---|