Changeset 7731


Ignore:
Timestamp:
29 Feb 2012, 18:33:41 (13 years ago)
Author:
uli
Message:

Add tests for department exporter and reflect changes in ICSVExporter
interface.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/university/tests/test_export.py

    r7727 r7731  
    77from waeup.sirp.interfaces import ICSVExporter
    88from waeup.sirp.testing import SIRPUnitTestLayer
    9 from waeup.sirp.university import FacultiesContainer, Faculty
    10 from waeup.sirp.university.export import FacultyExporter
     9from waeup.sirp.university import FacultiesContainer, Faculty, Department
     10from waeup.sirp.university.export import FacultyExporter, DepartmentExporter
    1111
    1212class FacultyExporterTest(unittest.TestCase):
     
    3232    def test_get_as_utility(self):
    3333        # we can get a faculty exporter as utility
    34         result = queryUtility(ICSVExporter, name="Faculty Exporter")
     34        result = queryUtility(ICSVExporter, name="faculties")
    3535        self.assertTrue(result is not None)
    3636        return
    3737
    3838    def test_export(self):
    39         # we can export single faculties (for what it's worth)
     39        # we can export a set of faculties
    4040        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
    4141        exporter = FacultyExporter()
    42         exporter.export(fac, self.outfile)
     42        exporter.export([fac], self.outfile)
    4343        result = open(self.outfile, 'rb').read()
    4444        self.assertEqual(
     
    5050
    5151    def test_export_to_string(self):
    52         # we can export single faculties to a string.
     52        # we can export a set of faculties to a string.
    5353        fac = Faculty('Faculty of Cheese', 'faculty', 'F1')
    5454        exporter = FacultyExporter()
    55         result = exporter.export(fac, filepath=None)
     55        result = exporter.export([fac], filepath=None)
    5656        self.assertEqual(
    5757            result,
     
    9797            )
    9898        return
     99
     100class DepartmentExporterTest(unittest.TestCase):
     101    # Tests for DepartmentExporter
     102
     103    layer = SIRPUnitTestLayer
     104
     105    def setUp(self):
     106        self.workdir = tempfile.mkdtemp()
     107        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
     108        # create some departments in a fake site
     109        container = FacultiesContainer()
     110        self.site = {'faculties':container}
     111        self.fac1 = Faculty('Faculty of Cheese', 'faculty', 'F1')
     112        self.fac2 = Faculty('Centre of Onion', 'centre', 'F2')
     113        container.addFaculty(self.fac1)
     114        container.addFaculty(self.fac2)
     115        self.dept1 = Department('Department of Cheddar', 'department', 'D1')
     116        self.dept2 = Department('Institue of Gouda', 'institute', 'D2')
     117        self.dept3 = Department('Department of Rings', 'department', 'D3')
     118        self.fac1.addDepartment(self.dept1)
     119        self.fac1.addDepartment(self.dept2)
     120        self.fac2.addDepartment(self.dept3)
     121        return
     122
     123    def tearDown(self):
     124        shutil.rmtree(self.workdir)
     125        return
     126
     127    def test_ifaces(self):
     128        # make sure we fullfill interface contracts
     129        obj = DepartmentExporter()
     130        verifyObject(ICSVExporter, obj)
     131        verifyClass(ICSVExporter, DepartmentExporter)
     132        return
     133
     134    def test_get_as_utility(self):
     135        # we can get a faculty exporter as utility
     136        result = queryUtility(ICSVExporter, name="departments")
     137        self.assertTrue(result is not None)
     138        return
     139
     140    def test_export(self):
     141        # we can export an iterable of departments
     142        exporter = DepartmentExporter()
     143        exporter.export([self.dept1], self.outfile)
     144        result = open(self.outfile, 'rb').read()
     145        self.assertEqual(
     146            result,
     147            'code,faculty,title,title_prefix\r\n'
     148            'D1,F1,Department of Cheddar,department\r\n'
     149            )
     150        return
     151
     152    def test_export_to_string(self):
     153        # we can export an iterable of departments to a string.
     154        exporter = DepartmentExporter()
     155        result = exporter.export([self.dept1, self.dept2], filepath=None)
     156        self.assertEqual(
     157            result,
     158            'code,faculty,title,title_prefix\r\n'
     159            'D1,F1,Department of Cheddar,department\r\n'
     160            'D2,F1,Institue of Gouda,institute\r\n'
     161            )
     162        return
     163
     164    def test_export_all(self):
     165        # we can export all depts in a site
     166        exporter = DepartmentExporter()
     167        exporter.export_all(self.site, self.outfile)
     168        result = open(self.outfile, 'rb').read()
     169        self.assertEqual(
     170            result,
     171            'code,faculty,title,title_prefix\r\n'
     172            'D1,F1,Department of Cheddar,department\r\n'
     173            'D2,F1,Institue of Gouda,institute\r\n'
     174            'D3,F2,Department of Rings,department\r\n'
     175            )
     176        return
     177
     178    def test_export_all_to_string(self):
     179        # we can export all depts in a site to a string
     180        exporter = DepartmentExporter()
     181        result = exporter.export_all(self.site, filepath=None)
     182        self.assertEqual(
     183            result,
     184            'code,faculty,title,title_prefix\r\n'
     185            'D1,F1,Department of Cheddar,department\r\n'
     186            'D2,F1,Institue of Gouda,institute\r\n'
     187            'D3,F2,Department of Rings,department\r\n'
     188            )
     189        return
Note: See TracChangeset for help on using the changeset viewer.