Ignore:
Timestamp:
20 Sep 2012, 08:22:52 (12 years ago)
Author:
uli
Message:

Merge changes from trunk r9171:9207.

Location:
main/waeup.kofa/branches/uli-async-update
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/branches/uli-async-update

  • main/waeup.kofa/branches/uli-async-update/src/waeup/kofa/hostels/tests.py

    r8686 r9208  
    1919Tests for hostels and their UI components.
    2020"""
     21import os
    2122import shutil
    2223import tempfile
     
    3637from waeup.kofa.hostels.container import HostelsContainer
    3738from waeup.kofa.hostels.hostel import Hostel, Bed
     39from waeup.kofa.hostels.batching import HostelProcessor
     40from waeup.kofa.hostels.export import BedExporter, HostelExporter
    3841from waeup.kofa.testing import (FunctionalLayer, FunctionalTestCase)
    3942from waeup.kofa.students.student import Student
    4043from waeup.kofa.students.accommodation import BedTicket
    4144from waeup.kofa.university.department import Department
     45
     46HOSTEL_SAMPLE_DATA = open(
     47    os.path.join(os.path.dirname(__file__), 'sample_hostel_data.csv'),
     48    'rb').read()
     49
     50HOSTEL_HEADER_FIELDS = HOSTEL_SAMPLE_DATA.split(
     51    '\n')[0].split(',')
    4252
    4353class HostelsContainerTestCase(FunctionalTestCase):
     
    142152        # Create a bed
    143153        bed = Bed()
    144         bed.bed_id = u'xyz'
     154        bed.bed_id = u'hall_block_room_bed'
    145155        bed.bed_number = 1
    146         bed.bed_type = u'abc'
     156        bed.bed_type = u'a_b_c'
    147157        self.app['hostels'][hostel.hostel_id][bed.bed_id] = bed
    148158
     
    157167        self.browser.handleErrors = False
    158168
     169        self.logfile = os.path.join(
     170            self.app['datacenter'].storage, 'logs', 'hostels.log')
     171
    159172    def tearDown(self):
    160173        super(HostelsFullSetup, self).tearDown()
     
    174187        # We can find a certain bed
    175188        cat = queryUtility(ICatalog, name='beds_catalog')
    176         results = cat.searchResults(bed_type=(u'abc', u'abc'))
     189        results = cat.searchResults(bed_type=(u'a_b_c', u'a_b_c'))
    177190        results = [x for x in results] # Turn results generator into list
    178191        assert len(results) == 1
    179         assert results[0] is self.app['hostels']['hall-x']['xyz']
     192        assert results[0] is self.app['hostels']['hall-x']['hall_block_room_bed']
    180193
    181194    def test_search_by_owner(self):
    182195        # We can find a certain bed
    183         myobj = self.app['hostels']['hall-x']['xyz']
     196        myobj = self.app['hostels']['hall-x']['hall_block_room_bed']
    184197        myobj.owner = u'abc'
    185198        notify(grok.ObjectModifiedEvent(myobj))
     
    188201        results = [x for x in results] # Turn results generator into list
    189202        assert len(results) == 1
    190         assert results[0] is self.app['hostels']['hall-x']['xyz']
     203        assert results[0] is self.app['hostels']['hall-x']['hall_block_room_bed']
    191204
    192205class HostelsUITests(HostelsFullSetup):
     
    300313        self.assertMatches(
    301314          '...No allocated bed selected...', self.browser.contents)
    302         # Managers can manually allocate studenst after cancellation
     315        # Managers can manually allocate students after cancellation
    303316        self.browser.open(self.container_path + '/hall-1/hall-1_A_101_A')
    304317        self.browser.getControl(name="form.owner").value = [self.student_id]
     
    334347        # Also the number of the bed has changed.
    335348        self.assertFalse(new_number == old_number)
     349        # The number of occupied bed are displayed on container page.
     350        self.browser.open(self.container_path)
     351        self.assertTrue('1 of 8' in self.browser.contents)
    336352        # Remove entire hostel
    337353        self.browser.open(self.manage_container_path)
     
    346362        results = [x for x in results]
    347363        assert len(results) == 0
     364
     365    def test_clear_hostels(self):
     366        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     367        self.browser.open(self.container_path)
     368        self.browser.getLink("Manage accommodation").click()
     369        self.browser.getControl("Add hostel").click()
     370        self.browser.getControl("Create hostel").click()
     371        hall = self.app['hostels']['hall-1']
     372        hall.blocks_for_female = ['A','B']
     373        hall.rooms_per_floor = 1
     374        hall.beds_for_fresh = ['A']
     375        hall.beds_for_returning = ['B']
     376        hall.beds_for_final = ['C']
     377        hall.beds_for_all = ['D','E']
     378        self.browser.open(self.container_path + '/hall-1/manage')
     379        self.browser.getControl("Update all beds").click()
     380        cat = queryUtility(ICatalog, name='beds_catalog')
     381        results = cat.searchResults(bed_type=(None, None))
     382        self.assertEqual(len(results), 11)
     383        self.browser.getControl("Clear hostel").click()
     384        self.assertEqual(len(self.app['hostels']['hall-1']), 0)
     385        # Only the bed in hall-x remains in the catalog.
     386        results = cat.searchResults(bed_type=(None, None))
     387        self.assertEqual(len(results), 1)
     388        # We can clear all hostels at the same time.
     389        self.browser.open(self.manage_container_path)
     390        self.browser.getControl("Clear all hostels").click()
     391        results = cat.searchResults(bed_type=(None, None))
     392        self.assertEqual(len(results), 0)
     393        # Both actions have been logged.
     394        logcontent = open(self.logfile).read()
     395        self.assertTrue('INFO - zope.mgr - hostels.browser.HostelManageFormPage'
     396                        ' - hall-1 - cleared' in logcontent)
     397        self.assertTrue('zope.mgr - hostels.browser.HostelsContainerManagePage'
     398                        ' - hostels - all hostels cleared' in logcontent)
     399
     400class ExportTests(HostelsFullSetup):
     401
     402    layer = FunctionalLayer
     403
     404    def setUp(self):
     405        super(ExportTests, self).setUp()
     406        self.workdir = tempfile.mkdtemp()
     407        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
     408        return
     409
     410    def test_export_hostels(self):
     411        exporter = HostelExporter()
     412        exporter.export_all(self.app, self.outfile)
     413        result = open(self.outfile, 'rb').read()
     414        self.assertEqual(
     415            result,
     416            'beds_for_all,beds_for_final,beds_for_fresh,beds_for_pre,'
     417            'beds_for_returning,beds_reserved,blocks_for_female,'
     418            'blocks_for_male,floors_per_block,hostel_id,hostel_name,'
     419            'rooms_per_floor,sort_id,special_handling\r\n,,,,,[],,,1,'
     420            'hall-x,Hall 1,2,10,regular\r\n'
     421            )
     422        return
     423
     424    def test_export_beds(self):
     425        exporter = BedExporter()
     426        exporter.export_all(self.app, self.outfile)
     427        result = open(self.outfile, 'rb').read()
     428        self.assertEqual(
     429            result,
     430            'bed_id,bed_number,bed_type,owner,hall,block,room,bed,'
     431            'special_handling,sex,bt\r\nhall_block_room_bed,1,a_b_c,,'
     432            'hall,block,room,bed,a,b,c\r\n'
     433            )
     434        return
     435
     436class HostelProcessorTest(HostelsFullSetup):
     437
     438    layer = FunctionalLayer
     439
     440    def test_import(self):
     441        self.processor = HostelProcessor()
     442        self.workdir = tempfile.mkdtemp()
     443        self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv')
     444        open(self.csv_file, 'wb').write(HOSTEL_SAMPLE_DATA)
     445        num, num_warns, fin_file, fail_file = self.processor.doImport(
     446            self.csv_file, HOSTEL_HEADER_FIELDS)
     447        self.assertEqual(num_warns,0)
     448        self.assertEqual(len(self.app['hostels'].keys()), 11) # including hall-x
     449        self.assertEqual(self.app['hostels'][
     450            'block-a-upper-hostel'].hostel_id,'block-a-upper-hostel')
     451        self.assertEqual(self.app['hostels'][
     452            'block-a-upper-hostel'].beds_for_final, ['A', 'B'])
     453        logcontent = open(self.logfile).read()
     454        self.assertTrue(
     455            "Hostel Processor - block-a-upper-hostel - "
     456            "Record updated: beds_for_pre=['G'], floors_per_block=1, "
     457            "beds_for_final=['A', 'B'], rooms_per_floor=32, "
     458            "blocks_for_male=[], hostel_id=block-a-upper-hostel, "
     459            "sort_id=20, beds_for_returning=['C', 'D'], "
     460            "hostel_name=Block A Upper Hostel, beds_for_fresh=['E', 'F'], "
     461            "blocks_for_female=['A'], beds_for_all=[], beds_reserved=[]"
     462            in logcontent)
     463        shutil.rmtree(os.path.dirname(fin_file))
     464        return
Note: See TracChangeset for help on using the changeset viewer.