1 | ## $Id: tests.py 13346 2015-10-26 08:57:30Z 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 | """ |
---|
19 | Tests for hostels and their UI components. |
---|
20 | """ |
---|
21 | import os |
---|
22 | import shutil |
---|
23 | import tempfile |
---|
24 | import grok |
---|
25 | import pytz |
---|
26 | from datetime import datetime, timedelta |
---|
27 | from zope.event import notify |
---|
28 | from zope.interface.verify import verifyClass, verifyObject |
---|
29 | from zope.component.hooks import setSite, clearSite |
---|
30 | from zope.testbrowser.testing import Browser |
---|
31 | from zope.security.interfaces import Unauthorized |
---|
32 | from zope.catalog.interfaces import ICatalog |
---|
33 | from zope.component import queryUtility |
---|
34 | from waeup.kofa.app import University |
---|
35 | from waeup.kofa.hostels.interfaces import ( |
---|
36 | IHostelsContainer, IHostel, IBed) |
---|
37 | from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED |
---|
38 | from waeup.kofa.hostels.container import HostelsContainer |
---|
39 | from waeup.kofa.hostels.hostel import Hostel, Bed |
---|
40 | from waeup.kofa.hostels.batching import HostelProcessor |
---|
41 | from waeup.kofa.hostels.export import BedExporter, HostelExporter |
---|
42 | from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase) |
---|
43 | from waeup.kofa.students.student import Student |
---|
44 | from waeup.kofa.students.accommodation import BedTicket |
---|
45 | from waeup.kofa.university.department import Department |
---|
46 | |
---|
47 | HOSTEL_SAMPLE_DATA = open( |
---|
48 | os.path.join(os.path.dirname(__file__), 'sample_hostel_data.csv'), |
---|
49 | 'rb').read() |
---|
50 | |
---|
51 | HOSTEL_HEADER_FIELDS = HOSTEL_SAMPLE_DATA.split( |
---|
52 | '\n')[0].split(',') |
---|
53 | |
---|
54 | class HostelsContainerTestCase(FunctionalTestCase): |
---|
55 | |
---|
56 | layer = FunctionalLayer |
---|
57 | |
---|
58 | def test_interfaces(self): |
---|
59 | # Make sure the correct interfaces are implemented. |
---|
60 | self.assertTrue( |
---|
61 | verifyClass( |
---|
62 | IHostelsContainer, HostelsContainer) |
---|
63 | ) |
---|
64 | self.assertTrue( |
---|
65 | verifyObject( |
---|
66 | IHostelsContainer, HostelsContainer()) |
---|
67 | ) |
---|
68 | self.assertTrue( |
---|
69 | verifyClass( |
---|
70 | IHostel, Hostel) |
---|
71 | ) |
---|
72 | self.assertTrue( |
---|
73 | verifyObject( |
---|
74 | IHostel, Hostel()) |
---|
75 | ) |
---|
76 | self.assertTrue( |
---|
77 | verifyClass( |
---|
78 | IBed, Bed) |
---|
79 | ) |
---|
80 | bed = Bed() |
---|
81 | bed.bed_id = u'a_b_c_d' |
---|
82 | bed.bed_type = u'a_b_c' |
---|
83 | self.assertTrue( |
---|
84 | verifyObject( |
---|
85 | IBed, bed) |
---|
86 | ) |
---|
87 | return |
---|
88 | |
---|
89 | def test_base(self): |
---|
90 | # We cannot call the fundamental methods of a base in that case |
---|
91 | container = HostelsContainer() |
---|
92 | hostel = Hostel() |
---|
93 | # We cannot add arbitrary objects |
---|
94 | department = Department() |
---|
95 | self.assertRaises( |
---|
96 | TypeError, container.addHostel, department) |
---|
97 | self.assertRaises( |
---|
98 | TypeError, hostel.addBed, department) |
---|
99 | # Application is expired if startdate or enddate are not set |
---|
100 | # or current datetime is outside application period. |
---|
101 | self.assertTrue(container.expired) |
---|
102 | delta = timedelta(days=10) |
---|
103 | container.startdate = datetime.now(pytz.utc) - delta |
---|
104 | self.assertTrue(container.expired) |
---|
105 | container.enddate = datetime.now(pytz.utc) + delta |
---|
106 | self.assertFalse(container.expired) |
---|
107 | |
---|
108 | class HostelsFullSetup(FunctionalTestCase): |
---|
109 | |
---|
110 | def setUp(self): |
---|
111 | super(HostelsFullSetup, self).setUp() |
---|
112 | |
---|
113 | # Setup a sample site for each test |
---|
114 | app = University() |
---|
115 | self.dc_root = tempfile.mkdtemp() |
---|
116 | app['datacenter'].setStoragePath(self.dc_root) |
---|
117 | |
---|
118 | # Prepopulate the ZODB... |
---|
119 | self.getRootFolder()['app'] = app |
---|
120 | # we add the site immediately after creation to the |
---|
121 | # ZODB. Catalogs and other local utilities are not setup |
---|
122 | # before that step. |
---|
123 | self.app = self.getRootFolder()['app'] |
---|
124 | # Set site here. Some of the following setup code might need |
---|
125 | # to access grok.getSite() and should get our new app then |
---|
126 | setSite(app) |
---|
127 | |
---|
128 | # Add student with subobjects |
---|
129 | student = Student() |
---|
130 | student.firstname = u'Anna' |
---|
131 | student.lastname = u'Tester' |
---|
132 | student.reg_number = u'123' |
---|
133 | student.matric_number = u'234' |
---|
134 | student.sex = u'f' |
---|
135 | self.app['students'].addStudent(student) |
---|
136 | self.student_id = student.student_id |
---|
137 | self.student = self.app['students'][self.student_id] |
---|
138 | self.student['studycourse'].current_session = 2004 |
---|
139 | self.student['studycourse'].entry_session = 2004 |
---|
140 | # The students_catalog must be informed that the |
---|
141 | # session attribute has changed |
---|
142 | notify(grok.ObjectModifiedEvent(self.student)) |
---|
143 | |
---|
144 | # Set accommodation_session |
---|
145 | self.app['hostels'].accommodation_session = 2004 |
---|
146 | |
---|
147 | # Create a hostel |
---|
148 | hostel = Hostel() |
---|
149 | hostel.hostel_id = u'hall-x' |
---|
150 | self.app['hostels'][hostel.hostel_id] = hostel |
---|
151 | |
---|
152 | # Create a bed |
---|
153 | bed = Bed() |
---|
154 | bed.bed_id = u'hall_block_room_bed' |
---|
155 | bed.bed_number = 1 |
---|
156 | bed.bed_type = u'a_b_c' |
---|
157 | self.app['hostels'][hostel.hostel_id][bed.bed_id] = bed |
---|
158 | |
---|
159 | self.container_path = 'http://localhost/app/hostels' |
---|
160 | self.student_path = 'http://localhost/app/students/%s' % self.student_id |
---|
161 | self.manage_container_path = self.container_path + '/@@manage' |
---|
162 | self.add_hostel_path = self.container_path + '/addhostel' |
---|
163 | |
---|
164 | # Put the prepopulated site into test ZODB and prepare test |
---|
165 | # browser |
---|
166 | self.browser = Browser() |
---|
167 | self.browser.handleErrors = False |
---|
168 | |
---|
169 | self.logfile = os.path.join( |
---|
170 | self.app['datacenter'].storage, 'logs', 'hostels.log') |
---|
171 | |
---|
172 | def tearDown(self): |
---|
173 | super(HostelsFullSetup, self).tearDown() |
---|
174 | clearSite() |
---|
175 | shutil.rmtree(self.dc_root) |
---|
176 | |
---|
177 | class HostelsContainerTests(HostelsFullSetup): |
---|
178 | |
---|
179 | layer = FunctionalLayer |
---|
180 | |
---|
181 | def test_release_expired_allocations(self): |
---|
182 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
183 | cat = queryUtility(ICatalog, name='beds_catalog') |
---|
184 | bedticket = BedTicket() |
---|
185 | bedticket.booking_session = 2004 |
---|
186 | bedticket.bed_coordinates = u'anything' |
---|
187 | self.student['accommodation'].addBedTicket(bedticket) |
---|
188 | self.app[ |
---|
189 | 'hostels']['hall-x']['hall_block_room_bed'].owner = self.student_id |
---|
190 | notify(grok.ObjectModifiedEvent( |
---|
191 | self.app['hostels']['hall-x']['hall_block_room_bed'])) |
---|
192 | results = cat.searchResults(owner=(self.student_id, self.student_id)) |
---|
193 | self.assertEqual(len(results), 1) |
---|
194 | self.browser.open(self.container_path + '/releaseexpired') |
---|
195 | self.assertTrue('No bed released' in self.browser.contents) |
---|
196 | delta = timedelta(days=10) |
---|
197 | bedticket.booking_date = datetime.utcnow() - delta |
---|
198 | self.browser.open(self.container_path + '/releaseexpired') |
---|
199 | self.assertTrue( |
---|
200 | 'Successfully released beds: hall_block_room_bed (K1000000)' |
---|
201 | in self.browser.contents) |
---|
202 | results = cat.searchResults(owner=(self.student_id, self.student_id)) |
---|
203 | self.assertEqual(len(results), 0) |
---|
204 | self.assertMatches(bedticket.display_coordinates, |
---|
205 | '-- booking expired (2015-10-14 08:35:38 UTC) --') |
---|
206 | self.assertEqual( |
---|
207 | self.app['hostels']['hall-x']['hall_block_room_bed'].owner, |
---|
208 | NOT_OCCUPIED) |
---|
209 | # Releasing is logged. |
---|
210 | logcontent = open(self.logfile).read() |
---|
211 | self.assertTrue( |
---|
212 | 'hostels.browser.ReleaseExpiredAllocationsPage - hostels - ' |
---|
213 | 'released: hall_block_room_bed (K1000000)' |
---|
214 | in logcontent) |
---|
215 | return |
---|
216 | |
---|
217 | class BedCatalogTests(HostelsFullSetup): |
---|
218 | |
---|
219 | layer = FunctionalLayer |
---|
220 | |
---|
221 | def test_get_catalog(self): |
---|
222 | # We can get a beds catalog if we wish |
---|
223 | cat = queryUtility(ICatalog, name='beds_catalog') |
---|
224 | assert cat is not None |
---|
225 | |
---|
226 | def test_search_by_type(self): |
---|
227 | # We can find a certain bed |
---|
228 | cat = queryUtility(ICatalog, name='beds_catalog') |
---|
229 | results = cat.searchResults(bed_type=(u'a_b_c', u'a_b_c')) |
---|
230 | results = [x for x in results] # Turn results generator into list |
---|
231 | assert len(results) == 1 |
---|
232 | assert results[0] is self.app['hostels']['hall-x']['hall_block_room_bed'] |
---|
233 | |
---|
234 | def test_search_by_owner(self): |
---|
235 | # We can find a certain bed |
---|
236 | myobj = self.app['hostels']['hall-x']['hall_block_room_bed'] |
---|
237 | myobj.owner = u'abc' |
---|
238 | notify(grok.ObjectModifiedEvent(myobj)) |
---|
239 | cat = queryUtility(ICatalog, name='beds_catalog') |
---|
240 | results = cat.searchResults(owner=(u'abc', u'abc')) |
---|
241 | results = [x for x in results] # Turn results generator into list |
---|
242 | assert len(results) == 1 |
---|
243 | assert results[0] is self.app['hostels']['hall-x']['hall_block_room_bed'] |
---|
244 | |
---|
245 | class HostelsUITests(HostelsFullSetup): |
---|
246 | |
---|
247 | layer = FunctionalLayer |
---|
248 | |
---|
249 | def test_anonymous_access(self): |
---|
250 | # Anonymous users can't access hostels containers |
---|
251 | self.assertRaises( |
---|
252 | Unauthorized, self.browser.open, self.manage_container_path) |
---|
253 | return |
---|
254 | |
---|
255 | def test_add_search_edit_delete_manage_hostels(self): |
---|
256 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
257 | self.browser.open(self.container_path) |
---|
258 | self.browser.getLink("Manage accommodation").click() |
---|
259 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
260 | self.assertEqual(self.browser.url, self.manage_container_path) |
---|
261 | self.browser.getControl("Add hostel").click() |
---|
262 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
263 | self.assertEqual(self.browser.url, self.add_hostel_path) |
---|
264 | self.browser.getControl("Create hostel").click() |
---|
265 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
266 | self.assertTrue('Hostel created' in self.browser.contents) |
---|
267 | self.browser.open(self.container_path + '/addhostel') |
---|
268 | self.browser.getControl("Create hostel").click() |
---|
269 | self.assertTrue('The hostel already exists' in self.browser.contents) |
---|
270 | hall = self.app['hostels']['hall-1'] |
---|
271 | hall.blocks_for_female = ['A','B'] |
---|
272 | self.browser.open(self.container_path + '/hall-1') |
---|
273 | expected = '...<ul id="form.blocks_for_female" ><li>Block A</li>...' |
---|
274 | self.assertMatches(expected,self.browser.contents) |
---|
275 | self.browser.open(self.container_path + '/hall-1/manage') |
---|
276 | self.browser.getControl(name="form.rooms_per_floor").value = '1' |
---|
277 | self.browser.getControl("Save").click() |
---|
278 | self.assertTrue('Form has been saved' in self.browser.contents) |
---|
279 | # Since the testbrowser does not support Javascrip the |
---|
280 | # save action cleared the settings above and we have to set them |
---|
281 | # again. |
---|
282 | self.assertTrue(len(hall.blocks_for_female) == 0) |
---|
283 | hall.blocks_for_female = ['A','B'] |
---|
284 | hall.beds_for_fresh = ['A'] |
---|
285 | hall.beds_for_returning = ['B'] |
---|
286 | hall.beds_for_final = ['C'] |
---|
287 | hall.beds_for_all = ['D','E'] |
---|
288 | self.browser.getControl("Update all beds").click() |
---|
289 | expected = '...0 empty beds removed, 10 beds added, 0 occupied beds modified ()...' |
---|
290 | self.assertMatches(expected,self.browser.contents) |
---|
291 | cat = queryUtility(ICatalog, name='beds_catalog') |
---|
292 | results = cat.searchResults( |
---|
293 | bed_type=('regular_female_all', 'regular_female_all')) |
---|
294 | results = [(x.bed_id, x.bed_type) for x in results] |
---|
295 | self.assertEqual(results, |
---|
296 | [(u'hall-1_A_101_D', u'regular_female_all'), |
---|
297 | (u'hall-1_A_101_E', u'regular_female_all'), |
---|
298 | (u'hall-1_B_101_D', u'regular_female_all'), |
---|
299 | (u'hall-1_B_101_E', u'regular_female_all')]) |
---|
300 | # Reserve beds. |
---|
301 | self.browser.getControl("Switch reservation", index=0).click() |
---|
302 | self.assertTrue('No item selected' in self.browser.contents) |
---|
303 | ctrl = self.browser.getControl(name='val_id') |
---|
304 | ctrl.getControl(value='hall-1_A_101_A').selected = True |
---|
305 | ctrl.getControl(value='hall-1_A_101_B').selected = True |
---|
306 | ctrl.getControl(value='hall-1_A_101_C').selected = True |
---|
307 | ctrl.getControl(value='hall-1_A_101_D').selected = True |
---|
308 | self.browser.getControl("Switch reservation", index=0).click() |
---|
309 | self.assertTrue('Successfully switched beds: hall-1_A_101_A (reserved)' |
---|
310 | in self.browser.contents) |
---|
311 | self.assertEqual(self.app['hostels']['hall-1'][ |
---|
312 | 'hall-1_A_101_D'].bed_type, 'regular_female_reserved') |
---|
313 | # The catalog has been updated. |
---|
314 | results = cat.searchResults( |
---|
315 | bed_type=('regular_female_all', 'regular_female_all')) |
---|
316 | results = [(x.bed_id, x.bed_type) for x in results] |
---|
317 | self.assertEqual(results, |
---|
318 | [(u'hall-1_A_101_E', u'regular_female_all'), |
---|
319 | (u'hall-1_B_101_D', u'regular_female_all'), |
---|
320 | (u'hall-1_B_101_E', u'regular_female_all')]) |
---|
321 | results = cat.searchResults( |
---|
322 | bed_type=('regular_female_reserved', 'regular_female_reserved')) |
---|
323 | results = [(x.bed_id, x.bed_type) for x in results] |
---|
324 | self.assertEqual(results, |
---|
325 | [(u'hall-1_A_101_A', u'regular_female_reserved'), |
---|
326 | (u'hall-1_A_101_B', u'regular_female_reserved'), |
---|
327 | (u'hall-1_A_101_C', u'regular_female_reserved'), |
---|
328 | (u'hall-1_A_101_D', u'regular_female_reserved')]) |
---|
329 | # Change hostel configuration with one bed booked. |
---|
330 | hall['hall-1_A_101_E'].owner = u'anyid' |
---|
331 | notify(grok.ObjectModifiedEvent(hall['hall-1_A_101_E'])) |
---|
332 | hall.beds_for_fresh = ['A', 'E'] |
---|
333 | hall.beds_for_all = ['D'] |
---|
334 | self.browser.getControl("Update all beds").click() |
---|
335 | expected = '...9 empty beds removed, 9 beds added, 1 occupied beds modified...' |
---|
336 | self.assertMatches(expected,self.browser.contents) |
---|
337 | # Updating beds (including booked beds!) does update catalog. |
---|
338 | results = cat.searchResults( |
---|
339 | bed_type=('regular_female_all', 'regular_female_all')) |
---|
340 | results = [(x.bed_id, x.bed_type) for x in results] |
---|
341 | # The reservation of hall-1_A_101_D has been cancelled. |
---|
342 | self.assertEqual(results, |
---|
343 | [(u'hall-1_A_101_D', u'regular_female_all'), |
---|
344 | (u'hall-1_B_101_D', u'regular_female_all')]) |
---|
345 | # Release bed which has previously been booked. |
---|
346 | bedticket = BedTicket() |
---|
347 | bedticket.booking_session = 2004 |
---|
348 | bedticket.bed_coordinates = u'anything' |
---|
349 | self.student['accommodation'].addBedTicket(bedticket) |
---|
350 | self.app['hostels']['hall-1']['hall-1_A_101_D'].owner = self.student_id |
---|
351 | notify(grok.ObjectModifiedEvent(self.app['hostels']['hall-1']['hall-1_A_101_D'])) |
---|
352 | self.browser.open(self.container_path + '/hall-1/manage') |
---|
353 | ctrl = self.browser.getControl(name='val_id') |
---|
354 | self.browser.getControl("Release selected beds", index=0).click() |
---|
355 | self.assertMatches("...No item selected...", self.browser.contents) |
---|
356 | ctrl = self.browser.getControl(name='val_id') |
---|
357 | ctrl.getControl(value='hall-1_A_101_D').selected = True |
---|
358 | self.browser.getControl("Release selected beds", index=0).click() |
---|
359 | self.assertMatches( |
---|
360 | '...Successfully released beds: hall-1_A_101_D (%s)...' % self.student_id, |
---|
361 | self.browser.contents) |
---|
362 | self.assertMatches(bedticket.bed_coordinates, |
---|
363 | u' -- booking cancelled on <YYYY-MM-DD hh:mm:ss> UTC --') |
---|
364 | # The catalog has been updated. |
---|
365 | results = cat.searchResults(owner=(self.student_id, self.student_id)) |
---|
366 | assert len(results) == 0 |
---|
367 | # If we release a free bed, nothing will happen. |
---|
368 | ctrl = self.browser.getControl(name='val_id') |
---|
369 | ctrl.getControl(value='hall-1_A_101_D').selected = True |
---|
370 | self.browser.getControl("Release selected beds", index=0).click() |
---|
371 | self.assertMatches( |
---|
372 | '...No allocated bed selected...', self.browser.contents) |
---|
373 | # Managers can manually allocate eligible students after cancellation. |
---|
374 | self.browser.open(self.container_path + '/hall-1/hall-1_A_101_A') |
---|
375 | # 'not occupied' is not accepted. |
---|
376 | self.browser.getControl("Save").click() |
---|
377 | self.assertMatches( |
---|
378 | "...No valid student id...", |
---|
379 | self.browser.contents) |
---|
380 | # Invalid student ids are not accepted. |
---|
381 | self.browser.getControl(name="form.owner").value = 'nonsense' |
---|
382 | self.browser.getControl("Save").click() |
---|
383 | self.assertMatches( |
---|
384 | "...Either student does not exist or student " |
---|
385 | "is not in accommodation session...", |
---|
386 | self.browser.contents) |
---|
387 | self.browser.getControl(name="form.owner").value = self.student_id |
---|
388 | self.browser.getControl("Save").click() |
---|
389 | self.assertMatches("...Form has been saved...", self.browser.contents) |
---|
390 | # Students can only be allocated once. |
---|
391 | self.browser.open(self.container_path + '/hall-1/hall-1_A_101_B') |
---|
392 | self.browser.getControl(name="form.owner").value = self.student_id |
---|
393 | self.browser.getControl("Save").click() |
---|
394 | self.assertMatches( |
---|
395 | "...This student resides in bed hall-1_A_101_A...", |
---|
396 | self.browser.contents) |
---|
397 | # If we open the same form again, we will be redirected to hostel |
---|
398 | # manage page. Beds must be released first before they can be |
---|
399 | # allocated to other students. |
---|
400 | self.browser.open(self.container_path + '/hall-1/hall-1_A_101_A') |
---|
401 | self.assertEqual(self.browser.url, |
---|
402 | self.container_path + '/hall-1/@@manage#tab2') |
---|
403 | # Updating the beds again will not affect the allocation and also |
---|
404 | # the bed numbering remains the same. |
---|
405 | old_number = self.app['hostels']['hall-1']['hall-1_A_101_A'].bed_number |
---|
406 | old_owner = self.app['hostels']['hall-1']['hall-1_A_101_A'].owner |
---|
407 | self.browser.getControl("Update all beds").click() |
---|
408 | # 8 beds have been removed and re-added, 2 beds remains untouched |
---|
409 | # because they are occupied. |
---|
410 | expected = '...8 empty beds removed, 8 beds added, 0 occupied beds modified...' |
---|
411 | self.assertMatches(expected,self.browser.contents) |
---|
412 | new_number = self.app['hostels']['hall-1']['hall-1_A_101_A'].bed_number |
---|
413 | new_owner = self.app['hostels']['hall-1']['hall-1_A_101_A'].owner |
---|
414 | self.assertEqual(new_number, old_number) |
---|
415 | self.assertEqual(new_owner, old_owner) |
---|
416 | # If we change the bed type of an allocated bed, the modification will |
---|
417 | # be indicated. |
---|
418 | hall.blocks_for_female = ['B'] |
---|
419 | hall.blocks_for_male = ['A'] |
---|
420 | self.browser.getControl("Update all beds").click() |
---|
421 | expected = '...8 empty beds removed, 8 beds added, ' + \ |
---|
422 | '2 occupied beds modified (hall-1_A_101_A, hall-1_A_101_E, )...' |
---|
423 | self.assertMatches(expected,self.browser.contents) |
---|
424 | new_number = self.app['hostels']['hall-1']['hall-1_A_101_A'].bed_number |
---|
425 | # Also the number of the bed has changed. |
---|
426 | self.assertFalse(new_number == old_number) |
---|
427 | # The number of occupied beds are displayed on container page. |
---|
428 | self.browser.open(self.container_path) |
---|
429 | self.assertTrue('2 of 10' in self.browser.contents) |
---|
430 | # Remove entire hostel. |
---|
431 | self.browser.open(self.manage_container_path) |
---|
432 | ctrl = self.browser.getControl(name='val_id') |
---|
433 | value = ctrl.options[0] |
---|
434 | ctrl.getControl(value=value).selected = True |
---|
435 | self.browser.getControl("Remove selected", index=0).click() |
---|
436 | self.assertTrue('Successfully removed' in self.browser.contents) |
---|
437 | # Catalog is empty. |
---|
438 | results = cat.searchResults( |
---|
439 | bed_type=('regular_female_all', 'regular_female_all')) |
---|
440 | results = [x for x in results] |
---|
441 | assert len(results) == 0 |
---|
442 | # Actions are logged. |
---|
443 | logcontent = open(self.logfile).read() |
---|
444 | self.assertTrue( |
---|
445 | 'hall-1 - 9 empty beds removed, 9 beds added, 1 occupied ' |
---|
446 | 'beds modified (hall-1_A_101_E, )' |
---|
447 | in logcontent) |
---|
448 | |
---|
449 | def test_clear_hostels(self): |
---|
450 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
451 | self.browser.open(self.container_path) |
---|
452 | self.browser.getLink("Manage accommodation").click() |
---|
453 | self.browser.getControl("Add hostel").click() |
---|
454 | self.browser.getControl("Create hostel").click() |
---|
455 | hall = self.app['hostels']['hall-1'] |
---|
456 | hall.blocks_for_female = ['A','B'] |
---|
457 | hall.rooms_per_floor = 1 |
---|
458 | hall.beds_for_fresh = ['A'] |
---|
459 | hall.beds_for_returning = ['B'] |
---|
460 | hall.beds_for_final = ['C'] |
---|
461 | hall.beds_for_all = ['D','E'] |
---|
462 | self.browser.open(self.container_path + '/hall-1/manage') |
---|
463 | self.browser.getControl("Update all beds").click() |
---|
464 | cat = queryUtility(ICatalog, name='beds_catalog') |
---|
465 | results = cat.searchResults(bed_type=(None, None)) |
---|
466 | self.assertEqual(len(results), 11) |
---|
467 | self.browser.getControl("Clear hostel").click() |
---|
468 | self.assertEqual(len(self.app['hostels']['hall-1']), 0) |
---|
469 | # Only the bed in hall-x remains in the catalog. |
---|
470 | results = cat.searchResults(bed_type=(None, None)) |
---|
471 | self.assertEqual(len(results), 1) |
---|
472 | # We can clear all hostels at the same time. |
---|
473 | self.browser.open(self.manage_container_path) |
---|
474 | self.browser.getControl("Clear all hostels").click() |
---|
475 | results = cat.searchResults(bed_type=(None, None)) |
---|
476 | self.assertEqual(len(results), 0) |
---|
477 | # Both actions have been logged. |
---|
478 | logcontent = open(self.logfile).read() |
---|
479 | self.assertTrue('INFO - zope.mgr - hostels.browser.HostelManageFormPage' |
---|
480 | ' - hall-1 - cleared' in logcontent) |
---|
481 | self.assertTrue('zope.mgr - hostels.browser.HostelsContainerManagePage' |
---|
482 | ' - hostels - all hostels cleared' in logcontent) |
---|
483 | |
---|
484 | class ExportTests(HostelsFullSetup): |
---|
485 | |
---|
486 | layer = FunctionalLayer |
---|
487 | |
---|
488 | def setUp(self): |
---|
489 | super(ExportTests, self).setUp() |
---|
490 | self.workdir = tempfile.mkdtemp() |
---|
491 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
492 | return |
---|
493 | |
---|
494 | def test_export_hostels(self): |
---|
495 | exporter = HostelExporter() |
---|
496 | exporter.export_all(self.app, self.outfile) |
---|
497 | result = open(self.outfile, 'rb').read() |
---|
498 | self.assertEqual( |
---|
499 | result, |
---|
500 | 'beds_for_all,beds_for_final,beds_for_fresh,beds_for_pre,' |
---|
501 | 'beds_for_returning,blocks_for_female,' |
---|
502 | 'blocks_for_male,floors_per_block,hostel_id,hostel_name,maint_fee,' |
---|
503 | 'rooms_per_floor,sort_id,special_handling\r\n[],[],[],[],[],[],[],' |
---|
504 | '1,hall-x,Hall 1,0.0,2,10,regular\r\n' |
---|
505 | ) |
---|
506 | return |
---|
507 | |
---|
508 | def test_export_beds(self): |
---|
509 | exporter = BedExporter() |
---|
510 | exporter.export_all(self.app, self.outfile) |
---|
511 | result = open(self.outfile, 'rb').read() |
---|
512 | self.assertEqual( |
---|
513 | result, |
---|
514 | 'bed_id,bed_number,bed_type,owner,hall,block,room,bed,' |
---|
515 | 'special_handling,sex,bt\r\nhall_block_room_bed,1,a_b_c,,' |
---|
516 | 'hall,block,room,bed,a,b,c\r\n' |
---|
517 | ) |
---|
518 | return |
---|
519 | |
---|
520 | def tearDown(self): |
---|
521 | super(ExportTests, self).tearDown() |
---|
522 | clearSite() |
---|
523 | shutil.rmtree(os.path.dirname(self.outfile)) |
---|
524 | |
---|
525 | class HostelProcessorTest(HostelsFullSetup): |
---|
526 | |
---|
527 | layer = FunctionalLayer |
---|
528 | |
---|
529 | def test_import(self): |
---|
530 | self.processor = HostelProcessor() |
---|
531 | self.workdir = tempfile.mkdtemp() |
---|
532 | self.csv_file = os.path.join(self.workdir, 'sample_hostel_data.csv') |
---|
533 | open(self.csv_file, 'wb').write(HOSTEL_SAMPLE_DATA) |
---|
534 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
535 | self.csv_file, HOSTEL_HEADER_FIELDS) |
---|
536 | self.assertEqual(num_warns,0) |
---|
537 | self.assertEqual(len(self.app['hostels'].keys()), 11) # including hall-x |
---|
538 | self.assertEqual(self.app['hostels'][ |
---|
539 | 'block-a-upper-hostel'].hostel_id,'block-a-upper-hostel') |
---|
540 | self.assertEqual(self.app['hostels'][ |
---|
541 | 'block-a-upper-hostel'].beds_for_final, ['A', 'B']) |
---|
542 | logcontent = open(self.logfile).read() |
---|
543 | self.assertTrue( |
---|
544 | "Hostel Processor - sample_hostel_data - block-a-upper-hostel - " |
---|
545 | "updated: " |
---|
546 | "beds_for_pre=['G'], floors_per_block=1, " |
---|
547 | "special_handling=regular, " |
---|
548 | "beds_for_final=['A', 'B'], rooms_per_floor=32, " |
---|
549 | "hostel_id=block-a-upper-hostel, " |
---|
550 | "sort_id=20, beds_for_returning=['C', 'D'], " |
---|
551 | "hostel_name=Block A Upper Hostel, beds_for_fresh=['E', 'F'], " |
---|
552 | "blocks_for_female=['A']" |
---|
553 | in logcontent) |
---|
554 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
555 | shutil.rmtree(self.workdir) |
---|
556 | # The manage page can be opened. |
---|
557 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
558 | self.browser.open(self.container_path + '/block-a-upper-hostel') |
---|
559 | self.browser.getLink("Manage").click() |
---|
560 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
561 | return |
---|
562 | |
---|
563 | def test_import_update(self): |
---|
564 | self.processor = HostelProcessor() |
---|
565 | self.workdir = tempfile.mkdtemp() |
---|
566 | self.csv_file = os.path.join(self.workdir, 'sample_hostel_data.csv') |
---|
567 | open(self.csv_file, 'wb').write(HOSTEL_SAMPLE_DATA) |
---|
568 | self.csv_file = os.path.join(self.workdir, 'sample_hostel_data.csv') |
---|
569 | open(self.csv_file, 'wb').write(HOSTEL_SAMPLE_DATA) |
---|
570 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
571 | self.csv_file, HOSTEL_HEADER_FIELDS) |
---|
572 | # We import the same file in update mode |
---|
573 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
574 | self.csv_file, HOSTEL_HEADER_FIELDS, 'update') |
---|
575 | self.assertEqual(num_warns,0) |
---|
576 | logcontent = open(self.logfile).read() |
---|
577 | self.assertTrue( |
---|
578 | "Hostel Processor - sample_hostel_data - block-a-upper-hostel - " |
---|
579 | "updated: " |
---|
580 | "beds_for_pre=['G'], floors_per_block=1, " |
---|
581 | "special_handling=regular, " |
---|
582 | "beds_for_final=['A', 'B'], rooms_per_floor=32, " |
---|
583 | "hostel_id=block-a-upper-hostel, " |
---|
584 | "sort_id=20, beds_for_returning=['C', 'D'], " |
---|
585 | "hostel_name=Block A Upper Hostel, beds_for_fresh=['E', 'F'], " |
---|
586 | "blocks_for_female=['A']" |
---|
587 | in logcontent) |
---|
588 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
589 | shutil.rmtree(self.workdir) |
---|
590 | return |
---|