1 | ## $Id: test_catalog.py 14115 2016-08-23 08:54:40Z 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 = createObject('waeup.StudentStudyLevel') |
---|
79 | studylevel.level = 100 |
---|
80 | studylevel.level_session = 2010 |
---|
81 | self.student['studycourse']['100'] = studylevel |
---|
82 | ticket = createObject('waeup.CourseTicket') |
---|
83 | ticket.code = 'Course1' |
---|
84 | ticket.credits = 30 |
---|
85 | ticket.score = 70 |
---|
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.amount_auth = 12345.678 |
---|
93 | payment.p_state = 'paid' |
---|
94 | self.student['payments'][payment.p_id] = payment |
---|
95 | return |
---|
96 | |
---|
97 | def tearDown(self): |
---|
98 | shutil.rmtree(self.dc_root) |
---|
99 | super(CatalogTestSetup, self).tearDown() |
---|
100 | return |
---|
101 | |
---|
102 | class StudentCatalogTests(CatalogTestSetup): |
---|
103 | |
---|
104 | layer = FunctionalLayer |
---|
105 | |
---|
106 | def test_get_catalog(self): |
---|
107 | # We can get an students catalog if we wish |
---|
108 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
109 | assert cat is not None |
---|
110 | |
---|
111 | def test_search_by_id(self): |
---|
112 | # We can find a certain student id |
---|
113 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
114 | results = cat.searchResults(student_id=(self.student_id, |
---|
115 | self.student_id)) |
---|
116 | results = [x for x in results] # Turn results generator into list |
---|
117 | assert len(results) == 1 |
---|
118 | assert results[0] is self.app['students'][self.student_id] |
---|
119 | |
---|
120 | def test_search_by_name(self): |
---|
121 | # We can find a certain name |
---|
122 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
123 | results = cat.searchResults(fullname='Bob Tester') |
---|
124 | results = [x for x in results] # Turn results generator into list |
---|
125 | assert len(results) == 1 |
---|
126 | assert results[0] is self.app['students'][self.student_id] |
---|
127 | |
---|
128 | def test_search_by_department(self): |
---|
129 | # We can find a student studying in a certain department |
---|
130 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
131 | results = cat.searchResults(depcode=('dep1','dep1')) |
---|
132 | results = [x for x in results] # Turn results generator into list |
---|
133 | assert len(results) == 1 |
---|
134 | assert results[0] is self.app['students'][self.student_id] |
---|
135 | |
---|
136 | def test_search_by_faculty(self): |
---|
137 | # We can find a student studying in a certain faculty |
---|
138 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
139 | results = cat.searchResults(faccode=('fac1','fac1')) |
---|
140 | results = [x for x in results] # Turn results generator into list |
---|
141 | assert len(results) == 1 |
---|
142 | assert results[0] is self.app['students'][self.student_id] |
---|
143 | |
---|
144 | def test_search_by_session(self): |
---|
145 | # We can find a student in a certain session |
---|
146 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
147 | results = cat.searchResults(current_session=(2010,2010)) |
---|
148 | results = [x for x in results] # Turn results generator into list |
---|
149 | assert len(results) == 1 |
---|
150 | assert results[0] is self.app['students'][self.student_id] |
---|
151 | |
---|
152 | def test_search_by_level(self): |
---|
153 | # We can find a student in a certain level |
---|
154 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
155 | results = cat.searchResults(current_level=(100, 100)) |
---|
156 | results = [x for x in results] # Turn results generator into list |
---|
157 | assert len(results) == 1 |
---|
158 | assert results[0] is self.app['students'][self.student_id] |
---|
159 | return |
---|
160 | |
---|
161 | def test_search_by_mode(self): |
---|
162 | # We can find a student in a certain mode |
---|
163 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
164 | results = cat.searchResults(current_mode=('ug_ft', 'ug_ft')) |
---|
165 | results = [x for x in results] # Turn results generator into list |
---|
166 | assert len(results) == 1 |
---|
167 | assert results[0] is self.app['students'][self.student_id] |
---|
168 | # Attention: A change of the certificate's study mode |
---|
169 | # is not reflected by the students catalog. The students_catalog |
---|
170 | # must be reindexed manually. |
---|
171 | self.certificate.study_mode = u'ug_pt' |
---|
172 | results = cat.searchResults(current_mode=('ug_ft', 'ug_ft')) |
---|
173 | assert len(results) == 1 |
---|
174 | results = cat.searchResults(current_mode=('pg_ft', 'pg_ft')) |
---|
175 | assert len(results) == 0 |
---|
176 | return |
---|
177 | |
---|
178 | |
---|
179 | class CourseTicketCatalogTests(CatalogTestSetup): |
---|
180 | |
---|
181 | layer = FunctionalLayer |
---|
182 | |
---|
183 | def test_get_catalog(self): |
---|
184 | # We can get an students catalog if we wish |
---|
185 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
186 | assert cat is not None |
---|
187 | |
---|
188 | def test_search_by_code(self): |
---|
189 | # We can find a certain course ticket by its code |
---|
190 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
191 | results = cat.searchResults(code=('Course1', 'Course1')) |
---|
192 | results = [x for x in results] # Turn results generator into list |
---|
193 | assert len(results) == 1 |
---|
194 | assert results[0] is self.app['students'][self.student_id][ |
---|
195 | 'studycourse']['100']['Course1'] |
---|
196 | |
---|
197 | def test_search_by_level(self): |
---|
198 | # We can find a certain course ticket by the level |
---|
199 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
200 | results = cat.searchResults(level=(100, 100)) |
---|
201 | results = [x for x in results] # Turn results generator into list |
---|
202 | assert len(results) == 1 |
---|
203 | assert results[0] is self.app['students'][self.student_id][ |
---|
204 | 'studycourse']['100']['Course1'] |
---|
205 | |
---|
206 | def test_search_by_session(self): |
---|
207 | # We can find a certain course ticket by the level session |
---|
208 | cat = queryUtility(ICatalog, name='coursetickets_catalog') |
---|
209 | results = cat.searchResults(session=(2010, 2010)) |
---|
210 | results = [x for x in results] # Turn results generator into list |
---|
211 | assert len(results) == 1 |
---|
212 | assert results[0] is self.app['students'][self.student_id][ |
---|
213 | 'studycourse']['100']['Course1'] |
---|
214 | |
---|
215 | class PaymentCatalogTests(CatalogTestSetup): |
---|
216 | |
---|
217 | layer = FunctionalLayer |
---|
218 | |
---|
219 | def test_get_catalog(self): |
---|
220 | # We can get a students catalog if we wish |
---|
221 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
222 | assert cat is not None |
---|
223 | |
---|
224 | def test_search_by_session(self): |
---|
225 | # We can find a payment ticket by the payment session |
---|
226 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
227 | results = cat.searchResults(p_session=(2010, 2010)) |
---|
228 | results = [x for x in results] # Turn results generator into list |
---|
229 | assert len(results) == 1 |
---|
230 | assert results[0] is self.app['students'][self.student_id][ |
---|
231 | 'payments']['p1234567890'] |
---|
232 | |
---|
233 | def test_search_by_category(self): |
---|
234 | # We can find a payment ticket by the payment category |
---|
235 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
236 | results = cat.searchResults(p_category=('schoolfee','schoolfee')) |
---|
237 | results = [x for x in results] # Turn results generator into list |
---|
238 | assert len(results) == 1 |
---|
239 | assert results[0] is self.app['students'][self.student_id][ |
---|
240 | 'payments']['p1234567890'] |
---|
241 | |
---|
242 | def test_search_by_item(self): |
---|
243 | # We can find a payment ticket by the payment item |
---|
244 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
245 | results = cat.searchResults(p_item=('any item','any item')) |
---|
246 | results = [x for x in results] # Turn results generator into list |
---|
247 | assert len(results) == 1 |
---|
248 | assert results[0] is self.app['students'][self.student_id][ |
---|
249 | 'payments']['p1234567890'] |
---|
250 | |
---|
251 | def test_search_by_state(self): |
---|
252 | # We can find a payment ticket by the payment state |
---|
253 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
254 | results = cat.searchResults(p_state=('paid','paid')) |
---|
255 | results = [x for x in results] # Turn results generator into list |
---|
256 | assert len(results) == 1 |
---|
257 | assert results[0] is self.app['students'][self.student_id][ |
---|
258 | 'payments']['p1234567890'] |
---|
259 | |
---|
260 | def test_reindex(self): |
---|
261 | # We can reindex any kind of catalog with the updateIndexes method. |
---|
262 | cat = queryUtility(ICatalog, name='payments_catalog') |
---|
263 | results = cat.searchResults(p_state=('failed','failed')) |
---|
264 | assert len(results) == 0 |
---|
265 | results = cat.searchResults(p_state=('paid','paid')) |
---|
266 | assert len(results) == 1 |
---|
267 | results = [x for x in results] # Turn results generator into list |
---|
268 | assert results[0] is self.app['students'][self.student_id][ |
---|
269 | 'payments']['p1234567890'] |
---|
270 | results[0].p_state = 'failed' |
---|
271 | # Since we did not fire an ObjectModifiedEvent the catalog remained |
---|
272 | # unchanged |
---|
273 | results = cat.searchResults(p_state=('paid','paid')) |
---|
274 | assert len(results) == 1 |
---|
275 | # Reindexing the catalog will lead to correct search results |
---|
276 | #reindexPayments() |
---|
277 | cat.clear() # The test works with and without clearing the catalog |
---|
278 | cat.updateIndexes() |
---|
279 | results = cat.searchResults(p_state=('paid','paid')) |
---|
280 | assert len(results) == 0 |
---|
281 | results = cat.searchResults(p_state=('failed','failed')) |
---|
282 | assert len(results) == 1 |
---|
283 | results = [x for x in results] # Turn results generator into list |
---|
284 | assert len(results) == 1 |
---|
285 | assert results[0] is self.app['students'][self.student_id][ |
---|
286 | 'payments']['p1234567890'] |
---|