1 | ## $Id: test_catalog.py 10552 2013-08-28 14:33:33Z 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 grok |
---|
19 | import shutil |
---|
20 | import tempfile |
---|
21 | from zope.event import notify |
---|
22 | from zope.catalog.interfaces import ICatalog |
---|
23 | from zope.component import queryUtility, createObject |
---|
24 | from zope.component.hooks import setSite |
---|
25 | from waeup.kofa.app import University |
---|
26 | from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase |
---|
27 | from waeup.kofa.students.student import Student |
---|
28 | from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket |
---|
29 | from waeup.kofa.university.faculty import Faculty |
---|
30 | from waeup.kofa.university.department import Department |
---|
31 | |
---|
32 | class CatalogTestSetup(FunctionalTestCase): |
---|
33 | # A setup for testing catalog related stuff. |
---|
34 | # |
---|
35 | # sets up a site with some student already created. |
---|
36 | layer = FunctionalLayer |
---|
37 | |
---|
38 | def create_cert(self, facname, deptname, certname): |
---|
39 | # helper: create faculty, dept, and cert |
---|
40 | self.app['faculties'][facname] = Faculty(code=facname) |
---|
41 | self.app['faculties'][facname][deptname] = Department(code=deptname) |
---|
42 | cert = createObject('waeup.Certificate') |
---|
43 | cert.start_level = 100 |
---|
44 | cert.end_level = 500 |
---|
45 | cert.code = certname |
---|
46 | self.app['faculties'][facname][deptname].certificates.addCertificate( |
---|
47 | cert) |
---|
48 | return cert |
---|
49 | |
---|
50 | def setUp(self): |
---|
51 | super(CatalogTestSetup, self).setUp() |
---|
52 | |
---|
53 | # Prepopulate ZODB |
---|
54 | app = University() |
---|
55 | self.dc_root = tempfile.mkdtemp() |
---|
56 | app['datacenter'].setStoragePath(self.dc_root) |
---|
57 | |
---|
58 | # Prepopulate the ZODB... |
---|
59 | self.getRootFolder()['app'] = app |
---|
60 | self.app = self.getRootFolder()['app'] |
---|
61 | setSite(self.app) |
---|
62 | self.certificate = self.create_cert(u'fac1', u'dep1', u'CERT1') |
---|
63 | self.certificate.study_mode = u'ug_ft' |
---|
64 | |
---|
65 | # Create student with subobjects |
---|
66 | student = Student() |
---|
67 | student.firstname = u'Bob' |
---|
68 | student.lastname = u'Tester' |
---|
69 | student.matric_number = u'1234' |
---|
70 | self.app['students'].addStudent(student) |
---|
71 | self.student_id = student.student_id |
---|
72 | self.student = self.app['students'][self.student_id] |
---|
73 | self.student['studycourse'].certificate = self.certificate |
---|
74 | self.student['studycourse'].current_session = 2010 |
---|
75 | self.student['studycourse'].current_level = 100 |
---|
76 | # Update the students_catalog |
---|
77 | notify(grok.ObjectModifiedEvent(self.student)) |
---|
78 | studylevel = StudentStudyLevel() |
---|
79 | studylevel.level = 100 |
---|
80 | studylevel.level_session = 2010 |
---|
81 | self.student['studycourse']['100'] = studylevel |
---|
82 | ticket = CourseTicket() |
---|
83 | ticket.code = 'Course1' |
---|
84 | ticket.credits = 30 |
---|
85 | ticket.score = 88 |
---|
86 | self.student['studycourse']['100']['Course1'] = ticket |
---|
87 | payment = createObject(u'waeup.StudentOnlinePayment') |
---|
88 | payment.p_id = 'p1234567890' |
---|
89 | payment.p_item = u'any item' |
---|
90 | payment.p_session = 2010 |
---|
91 | payment.p_category = 'schoolfee' |
---|
92 | payment.p_state = 'paid' |
---|
93 | self.student['payments'][payment.p_id] = payment |
---|
94 | return |
---|
95 | |
---|
96 | def tearDown(self): |
---|
97 | shutil.rmtree(self.dc_root) |
---|
98 | super(CatalogTestSetup, self).tearDown() |
---|
99 | return |
---|
100 | |
---|
101 | class StudentCatalogTests(CatalogTestSetup): |
---|
102 | |
---|
103 | layer = FunctionalLayer |
---|
104 | |
---|
105 | def test_get_catalog(self): |
---|
106 | # We can get an students catalog if we wish |
---|
107 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
108 | assert cat is not None |
---|
109 | |
---|
110 | def test_search_by_id(self): |
---|
111 | # We can find a certain student id |
---|
112 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
113 | results = cat.searchResults(student_id=(self.student_id, |
---|
114 | self.student_id)) |
---|
115 | results = [x for x in results] # Turn results generator into list |
---|
116 | assert len(results) == 1 |
---|
117 | assert results[0] is self.app['students'][self.student_id] |
---|
118 | |
---|
119 | def test_search_by_name(self): |
---|
120 | # We can find a certain name |
---|
121 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
122 | results = cat.searchResults(fullname='Bob Tester') |
---|
123 | results = [x for x in results] # Turn results generator into list |
---|
124 | assert len(results) == 1 |
---|
125 | assert results[0] is self.app['students'][self.student_id] |
---|
126 | |
---|
127 | def test_search_by_department(self): |
---|
128 | # We can find a student studying in a certain department |
---|
129 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
130 | results = cat.searchResults(depcode=('dep1','dep1')) |
---|
131 | results = [x for x in results] # Turn results generator into list |
---|
132 | assert len(results) == 1 |
---|
133 | assert results[0] is self.app['students'][self.student_id] |
---|
134 | |
---|
135 | def test_search_by_faculty(self): |
---|
136 | # We can find a student studying in a certain faculty |
---|
137 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
138 | results = cat.searchResults(faccode=('fac1','fac1')) |
---|
139 | results = [x for x in results] # Turn results generator into list |
---|
140 | assert len(results) == 1 |
---|
141 | assert results[0] is self.app['students'][self.student_id] |
---|
142 | |
---|
143 | def test_search_by_session(self): |
---|
144 | # We can find a student in a certain session |
---|
145 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
146 | results = cat.searchResults(current_session=(2010,2010)) |
---|
147 | results = [x for x in results] # Turn results generator into list |
---|
148 | assert len(results) == 1 |
---|
149 | assert results[0] is self.app['students'][self.student_id] |
---|
150 | |
---|
151 | def test_search_by_level(self): |
---|
152 | # We can find a student in a certain level |
---|
153 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
154 | results = cat.searchResults(current_level=(100, 100)) |
---|
155 | results = [x for x in results] # Turn results generator into list |
---|
156 | assert len(results) == 1 |
---|
157 | assert results[0] is self.app['students'][self.student_id] |
---|
158 | return |
---|
159 | |
---|
160 | def test_search_by_mode(self): |
---|
161 | # We can find a student in a certain mode |
---|
162 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
163 | results = cat.searchResults(current_mode=('ug_ft', 'ug_ft')) |
---|
164 | results = [x for x in results] # Turn results generator into list |
---|
165 | assert len(results) == 1 |
---|
166 | assert results[0] is self.app['students'][self.student_id] |
---|
167 | # Attention: A change of the certificate's study mode |
---|
168 | # is not reflected by the students catalog. The students_catalog |
---|
169 | # must be reindexed manually. |
---|
170 | self.certificate.study_mode = u'ug_pt' |
---|
171 | results = cat.searchResults(current_mode=('ug_ft', 'ug_ft')) |
---|
172 | assert len(results) == 1 |
---|
173 | results = cat.searchResults(current_mode=('pg_ft', 'pg_ft')) |
---|
174 | assert len(results) == 0 |
---|
175 | return |
---|
176 | |
---|
177 | |
---|
178 | class CourseTicketCatalogTests(CatalogTestSetup): |
---|
179 | |
---|
180 | layer = FunctionalLayer |
---|
181 | |
---|
182 | def test_get_catalog(self): |
---|
183 | # We can get an students catalog if we wish |
---|
184 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
185 | assert cat is not None |
---|
186 | |
---|
187 | def test_search_by_code(self): |
---|
188 | # We can find a certain course ticket by its code |
---|
189 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
190 | results = cat.searchResults(code=('Course1', 'Course1')) |
---|
191 | results = [x for x in results] # Turn results generator into list |
---|
192 | assert len(results) == 1 |
---|
193 | assert results[0] is self.app['students'][self.student_id][ |
---|
194 | 'studycourse']['100']['Course1'] |
---|
195 | |
---|
196 | def test_search_by_level(self): |
---|
197 | # We can find a certain course ticket by the level |
---|
198 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
199 | results = cat.searchResults(level=(100, 100)) |
---|
200 | results = [x for x in results] # Turn results generator into list |
---|
201 | assert len(results) == 1 |
---|
202 | assert results[0] is self.app['students'][self.student_id][ |
---|
203 | 'studycourse']['100']['Course1'] |
---|
204 | |
---|
205 | def test_search_by_session(self): |
---|
206 | # We can find a certain course ticket by the level session |
---|
207 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
208 | results = cat.searchResults(session=(2010, 2010)) |
---|
209 | results = [x for x in results] # Turn results generator into list |
---|
210 | assert len(results) == 1 |
---|
211 | assert results[0] is self.app['students'][self.student_id][ |
---|
212 | 'studycourse']['100']['Course1'] |
---|
213 | |
---|
214 | class PaymentCatalogTests(CatalogTestSetup): |
---|
215 | |
---|
216 | layer = FunctionalLayer |
---|
217 | |
---|
218 | def test_get_catalog(self): |
---|
219 | # We can get a students catalog if we wish |
---|
220 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
221 | assert cat is not None |
---|
222 | |
---|
223 | def test_search_by_session(self): |
---|
224 | # We can find a payment ticket by the payment session |
---|
225 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
226 | results = cat.searchResults(p_session=(2010, 2010)) |
---|
227 | results = [x for x in results] # Turn results generator into list |
---|
228 | assert len(results) == 1 |
---|
229 | assert results[0] is self.app['students'][self.student_id][ |
---|
230 | 'payments']['p1234567890'] |
---|
231 | |
---|
232 | def test_search_by_category(self): |
---|
233 | # We can find a payment ticket by the payment category |
---|
234 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
235 | results = cat.searchResults(p_category=('schoolfee','schoolfee')) |
---|
236 | results = [x for x in results] # Turn results generator into list |
---|
237 | assert len(results) == 1 |
---|
238 | assert results[0] is self.app['students'][self.student_id][ |
---|
239 | 'payments']['p1234567890'] |
---|
240 | |
---|
241 | def test_search_by_item(self): |
---|
242 | # We can find a payment ticket by the payment item |
---|
243 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
244 | results = cat.searchResults(p_item=('any item','any item')) |
---|
245 | results = [x for x in results] # Turn results generator into list |
---|
246 | assert len(results) == 1 |
---|
247 | assert results[0] is self.app['students'][self.student_id][ |
---|
248 | 'payments']['p1234567890'] |
---|
249 | |
---|
250 | def test_search_by_state(self): |
---|
251 | # We can find a payment ticket by the payment state |
---|
252 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
253 | results = cat.searchResults(p_state=('paid','paid')) |
---|
254 | results = [x for x in results] # Turn results generator into list |
---|
255 | assert len(results) == 1 |
---|
256 | assert results[0] is self.app['students'][self.student_id][ |
---|
257 | 'payments']['p1234567890'] |
---|
258 | |
---|
259 | def test_reindex(self): |
---|
260 | # We can reindex any kind of catalog with the updateIndexes method. |
---|
261 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
262 | results = cat.searchResults(p_state=('failed','failed')) |
---|
263 | assert len(results) == 0 |
---|
264 | results = cat.searchResults(p_state=('paid','paid')) |
---|
265 | assert len(results) == 1 |
---|
266 | results = [x for x in results] # Turn results generator into list |
---|
267 | assert results[0] is self.app['students'][self.student_id][ |
---|
268 | 'payments']['p1234567890'] |
---|
269 | results[0].p_state = 'failed' |
---|
270 | # Since we did not fire an ObjectModifiedEvent the catalog remained |
---|
271 | # unchanged |
---|
272 | results = cat.searchResults(p_state=('paid','paid')) |
---|
273 | assert len(results) == 1 |
---|
274 | # Reindexing the catalog will lead to correct search results |
---|
275 | #reindexPayments() |
---|
276 | cat.clear() # The test works with and without clearing the catalog |
---|
277 | cat.updateIndexes() |
---|
278 | results = cat.searchResults(p_state=('paid','paid')) |
---|
279 | assert len(results) == 0 |
---|
280 | results = cat.searchResults(p_state=('failed','failed')) |
---|
281 | assert len(results) == 1 |
---|
282 | results = [x for x in results] # Turn results generator into list |
---|
283 | assert len(results) == 1 |
---|
284 | assert results[0] is self.app['students'][self.student_id][ |
---|
285 | 'payments']['p1234567890'] |
---|