Changeset 7857 for main/waeup.kofa/trunk


Ignore:
Timestamp:
13 Mar 2012, 01:14:45 (13 years ago)
Author:
uli
Message:

Add tests for ExporterBase?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_batching.py

    r7811 r7857  
    2121import shutil
    2222import tempfile
     23import unittest
    2324from zope import schema
    2425from zope.component import provideUtility
     
    2627from zope.component.hooks import clearSite
    2728from zope.component.interfaces import IFactory
    28 from zope.interface import Interface, implements
     29from zope.interface import Interface, implements, verify
    2930from waeup.kofa.app import University
     31from waeup.kofa.interfaces import ICSVExporter
    3032from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
     33from waeup.kofa.utils.batching import ExporterBase
    3134
    3235optionflags = (
     
    231234        return
    232235
    233 #def test_suite():
    234 #    return unittest.TestSuite()
     236class ExporterBaseTests(unittest.TestCase):
     237
     238    def setUp(self):
     239        self.workdir = tempfile.mkdtemp()
     240        self.workfile = os.path.join(self.workdir, 'testfile.csv')
     241        return
     242
     243    def tearDown(self):
     244        shutil.rmtree(self.workdir)
     245        return
     246
     247    def test_iface(self):
     248        # ExporterBase really implements the promised interface.
     249        obj = ExporterBase()
     250        verify.verifyClass(ICSVExporter, ExporterBase)
     251        verify.verifyObject(ICSVExporter, obj)
     252        return
     253
     254    def test_unimplemented(self):
     255        # make sure the not implemented methods signal that.
     256        exporter = ExporterBase()
     257        self.assertRaises(NotImplementedError, exporter.export_all, None)
     258        self.assertRaises(NotImplementedError, exporter.export, None)
     259        return
     260
     261    def test_mangle_value(self):
     262        # some basic types are mangled correctly
     263        exporter = ExporterBase()
     264        result1 = exporter.mangle_value(True, 'foo')
     265        result2 = exporter.mangle_value(False, 'foo')
     266        result3 = exporter.mangle_value('string', 'foo')
     267        result4 = exporter.mangle_value(u'string', 'foo')
     268        result5 = exporter.mangle_value(None, 'foo')
     269        self.assertEqual(
     270            (result1, result2, result3, result4, result5),
     271            ('1', '0', u'string', u'string', ''))
     272        self.assertEqual(type(result3), type('string'))
     273        self.assertEqual(type(result4), type('string'))
     274        return
     275
     276    def test_get_csv_writer(self):
     277        # we can get a CSV writer to a memory file
     278        exporter = ExporterBase()
     279        writer, outfile = exporter.get_csv_writer()
     280        writer.writerow(dict(code='A', title='B', title_prefix='C'))
     281        outfile.seek(0)
     282        self.assertEqual(
     283            outfile.read(),
     284            'code,title,title_prefix\r\nA,B,C\r\n')
     285        return
     286
     287    def test_get_csv_writer_with_file(self):
     288        # we can get CSV writer that writes to a real file
     289        exporter = ExporterBase()
     290        writer, outfile = exporter.get_csv_writer(filepath=self.workfile)
     291        writer.writerow(dict(code='A', title='B', title_prefix='C'))
     292        outfile.close()
     293        resultfile = open(self.workfile, 'rb')
     294        self.assertEqual(
     295            resultfile.read(),
     296            'code,title,title_prefix\r\nA,B,C\r\n')
     297        return
     298
     299    def test_write_item(self):
     300        # we can write items to opened exporter files.
     301        exporter = ExporterBase()
     302        writer, outfile = exporter.get_csv_writer()
     303        class Sample(object):
     304            code = 'A'
     305            title = u'B'
     306            title_prefix = True
     307        exporter.write_item(Sample(), writer)
     308        outfile.seek(0)
     309        self.assertEqual(
     310            outfile.read(),
     311            'code,title,title_prefix\r\nA,B,1\r\n')
     312        return
     313
     314    def test_close_outfile(self):
     315        # exporters can help to close outfiles.
     316        exporter = ExporterBase()
     317        writer, outfile = exporter.get_csv_writer()
     318        result = exporter.close_outfile(None, outfile)
     319        self.assertEqual(result, 'code,title,title_prefix\r\n')
     320        return
     321
     322    def test_close_outfile(self):
     323        # we can also close outfiles in real files.
     324        exporter = ExporterBase()
     325        writer, outfile = exporter.get_csv_writer(filepath=self.workfile)
     326        result = exporter.close_outfile(self.workfile, outfile)
     327        self.assertEqual(result, None)
     328        return
Note: See TracChangeset for help on using the changeset viewer.