source: main/waeup.kofa/trunk/src/waeup/kofa/hostels/tests.py @ 13443

Last change on this file since 13443 was 13440, checked in by Henrik Bettermann, 9 years ago

Add event handler which removes bed object from bed ticket if bed is removed.

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