source: main/waeup.sirp/trunk/src/waeup/sirp/hostels/tests.py @ 7000

Last change on this file since 7000 was 6988, checked in by Henrik Bettermann, 13 years ago

Implement logging for reservation switch.

Fix switchReservation.

Improve testing.

  • Property svn:keywords set to Id
File size: 10.1 KB
RevLine 
[6951]1## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
2## This program is free software; you can redistribute it and/or modify
3## it under the terms of the GNU General Public License as published by
4## the Free Software Foundation; either version 2 of the License, or
5## (at your option) any later version.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## You should have received a copy of the GNU General Public License
13## along with this program; if not, write to the Free Software
14## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15##
16"""
[6988]17Tests for hostels and their UI components.
[6951]18"""
[6961]19import shutil
20import tempfile
[6951]21from zope.interface.verify import verifyClass, verifyObject
[6961]22from zope.component.hooks import setSite, clearSite
23from zope.testbrowser.testing import Browser
24from zope.security.interfaces import Unauthorized
[6972]25from zope.catalog.interfaces import ICatalog
26from zope.component import queryUtility
[6961]27from waeup.sirp.app import University
[6951]28from waeup.sirp.hostels.interfaces import (
[6963]29    IHostelsContainer, IHostel, IBed)
[6951]30from waeup.sirp.hostels.container import HostelsContainer
[6963]31from waeup.sirp.hostels.hostel import Hostel, Bed
[6951]32from waeup.sirp.testing import (FunctionalLayer, FunctionalTestCase)
33
34class HostelsContainerTestCase(FunctionalTestCase):
35
36    layer = FunctionalLayer
37
38    def test_interfaces(self):
39        # Make sure the correct interfaces are implemented.
40        self.assertTrue(
41            verifyClass(
42                IHostelsContainer, HostelsContainer)
43            )
44        self.assertTrue(
45            verifyObject(
46                IHostelsContainer, HostelsContainer())
47            )
48        self.assertTrue(
49            verifyClass(
50                IHostel, Hostel)
51            )
52        self.assertTrue(
53            verifyObject(
54                IHostel, Hostel())
55            )
[6963]56        self.assertTrue(
57            verifyClass(
58                IBed, Bed)
59            )
60        self.assertTrue(
61            verifyObject(
62                IBed, Bed())
63            )
[6951]64        return
65
66    def test_base(self):
67        # We cannot call the fundamental methods of a base in that case
68        container = HostelsContainer()
69        self.assertRaises(
70            NotImplementedError, container.archive)
71        self.assertRaises(
72            NotImplementedError, container.clear)
[6961]73
74class HostelsFullSetup(FunctionalTestCase):
75
76    def setUp(self):
77        super(HostelsFullSetup, self).setUp()
78
79        # Setup a sample site for each test
80        app = University()
81        self.dc_root = tempfile.mkdtemp()
82        app['datacenter'].setStoragePath(self.dc_root)
83
84        # Prepopulate the ZODB...
85        self.getRootFolder()['app'] = app
86        # we add the site immediately after creation to the
87        # ZODB. Catalogs and other local utilities are not setup
88        # before that step.
89        self.app = self.getRootFolder()['app']
90        # Set site here. Some of the following setup code might need
91        # to access grok.getSite() and should get our new app then
92        setSite(app)
93
[6972]94        # Create a hostel
95        hostel = Hostel()
[6973]96        hostel.hostel_id = u'hall-x'
[6972]97        self.app['hostels'][hostel.hostel_id] = hostel
98
99        # Create a bed
100        bed = Bed()
101        bed.bed_id = u'xyz'
102        bed.bed_number = 1
103        bed.bed_type = u'abc'
104        self.app['hostels'][hostel.hostel_id][bed.bed_id] = bed
105
[6961]106        self.container_path = 'http://localhost/app/hostels'
107        self.manage_container_path = self.container_path + '/@@manage'
108        self.add_hostel_path = self.container_path + '/addhostel'
109
110        # Put the prepopulated site into test ZODB and prepare test
111        # browser
112        self.browser = Browser()
113        self.browser.handleErrors = False
114
115    def tearDown(self):
116        super(HostelsFullSetup, self).tearDown()
117        clearSite()
118        shutil.rmtree(self.dc_root)
119
[6972]120class BedCatalogTests(HostelsFullSetup):
121
122    layer = FunctionalLayer
123
124    def test_get_catalog(self):
125        # We can get an students catalog if we wish
126        cat = queryUtility(ICatalog, name='beds_catalog')
127        assert cat is not None
128
129    def test_search_by_type(self):
130        # We can find a certain bed
131        cat = queryUtility(ICatalog, name='beds_catalog')
132        results = cat.searchResults(bed_type=('abc', 'abc'))
133        results = [x for x in results] # Turn results generator into list
134        assert len(results) == 1
[6973]135        assert results[0] is self.app['hostels']['hall-x']['xyz']
[6972]136
[6961]137class HostelsUITests(HostelsFullSetup):
138
139    layer = FunctionalLayer
140
141    def test_anonymous_access(self):
142        # Anonymous users can't access hostels containers
143        self.assertRaises(
144            Unauthorized, self.browser.open, self.manage_container_path)
145        return
146
[6985]147    def test_add_search_edit_delete_manage_hostels(self):
[6961]148        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
149        self.browser.open(self.container_path)
150        self.browser.getLink("Manage accommodation").click()
151        self.assertEqual(self.browser.headers['Status'], '200 Ok')
152        self.assertEqual(self.browser.url, self.manage_container_path)
153        self.browser.getControl("Add hostel").click()
154        self.assertEqual(self.browser.headers['Status'], '200 Ok')
155        self.assertEqual(self.browser.url, self.add_hostel_path)
156        self.browser.getControl("Create hostel").click()
157        self.assertEqual(self.browser.headers['Status'], '200 Ok')
158        self.assertTrue('Hostel created' in self.browser.contents)
[6988]159        self.browser.open(self.container_path + '/addhostel')
160        self.browser.getControl("Create hostel").click()
161        self.assertTrue('The hostel already exists' in self.browser.contents)
[6973]162        hall = self.app['hostels']['hall-1']
[6961]163        hall.blocks_for_female = ['A','B']
[6973]164        self.browser.open(self.container_path + '/hall-1')
[6961]165        expected = '''...<ul id="form.blocks_for_female" ><li>Block A</li>
166<li>Block B</li></ul>...'''
167        self.assertMatches(expected,self.browser.contents)
[6973]168        self.browser.open(self.container_path + '/hall-1/manage')
[6985]169        self.browser.getControl(name="form.rooms_per_floor").value = '1'
[6962]170        self.browser.getControl("Save").click()
171        self.assertTrue('Form has been saved' in self.browser.contents)
[6985]172        # Since the testbrowser does not support Javascrip the
173        # save action cleared the settings above and we have to set them
174        # again
175        self.assertTrue(len(hall.blocks_for_female) == 0)
176        hall.blocks_for_female = ['A','B']
[6988]177        hall.beds_for_fresh = ['A']
178        hall.beds_for_returning = ['B']
179        hall.beds_for_final = ['C']
[6985]180        hall.beds_for_all = ['D','E']
181        self.browser.getControl("Update all beds").click()
[6988]182        expected = '...0 empty beds removed, 10 beds added, 0 occupied beds modified ()...'
[6985]183        self.assertMatches(expected,self.browser.contents)
184        cat = queryUtility(ICatalog, name='beds_catalog')
185        results = cat.searchResults(
186            bed_type=('regular_female_all', 'regular_female_all'))
187        results = [x for x in results]
188        assert len(results) == 4
[6988]189        # Reserve bed
190        self.browser.getControl("Switch reservation", index=0).click()
191        self.assertTrue('No item selected' in self.browser.contents)
[6985]192        ctrl = self.browser.getControl(name='val_id')
[6988]193        ctrl.getControl(value='hall-1_A_101_A').selected = True
194        ctrl.getControl(value='hall-1_A_101_B').selected = True
195        ctrl.getControl(value='hall-1_A_101_C').selected = True
[6985]196        ctrl.getControl(value='hall-1_A_101_D').selected = True
197        self.browser.getControl("Switch reservation", index=0).click()
[6988]198        self.assertTrue('Successfully switched beds: hall-1_A_101_A (reserved)'
[6985]199            in self.browser.contents)
[6988]200        assert self.app['hostels']['hall-1'][
[6985]201            'hall-1_A_101_D'].bed_type == 'regular_female_reserved'
[6988]202        expected = 'name="form.beds_reserved.0." size="20" type="text" value="A_101_A"  />'
[6985]203        self.assertTrue(expected in self.browser.contents)
[6988]204        # Provoke exception
205        self.app['hostels']['hall-1']['hall-1_A_101_D'].bed_type = u'nonsense'
[6985]206        ctrl = self.browser.getControl(name='val_id')
207        ctrl.getControl(value='hall-1_A_101_D').selected = True
208        self.browser.getControl("Switch reservation", index=0).click()
[6988]209        self.assertMatches(
210            '...need more than 1 value to unpack...', self.browser.contents)
211        self.app['hostels']['hall-1'][
212            'hall-1_A_101_D'].bed_type = u'regular_female_reserved'
[6985]213        # Change hostel configuration
214        hall.beds_for_all = ['D']
215        self.browser.getControl("Update all beds").click()
[6988]216        expected = '...10 empty beds removed, 8 beds added, 0 occupied beds modified...'
[6985]217        self.assertMatches(expected,self.browser.contents)
218        results = cat.searchResults(
219            bed_type=('regular_female_all', 'regular_female_all'))
220        results = [x for x in results]
[6988]221        assert len(results) == 1
222        # Unreserve bed
223        ctrl = self.browser.getControl(name='val_id')
224        ctrl.getControl(value='hall-1_A_101_A').selected = True
225        ctrl.getControl(value='hall-1_A_101_B').selected = True
226        ctrl.getControl(value='hall-1_A_101_C').selected = True
227        ctrl.getControl(value='hall-1_A_101_D').selected = True
228        self.browser.getControl("Switch reservation", index=0).click()
229        assert self.app['hostels']['hall-1'][
230            'hall-1_A_101_D'].bed_type == 'regular_female_all'
231        self.assertFalse(expected in self.browser.contents)
[6985]232        # Remove entire hostel
[6962]233        self.browser.open(self.manage_container_path)
234        ctrl = self.browser.getControl(name='val_id')
235        value = ctrl.options[0]
236        ctrl.getControl(value=value).selected = True
237        self.browser.getControl("Remove selected", index=0).click()
238        self.assertTrue('Successfully removed' in self.browser.contents)
[6985]239        # Catalog is empty
240        results = cat.searchResults(
241            bed_type=('regular_female_all', 'regular_female_all'))
242        results = [x for x in results]
243        assert len(results) == 0
Note: See TracBrowser for help on using the repository browser.