Changeset 17782


Ignore:
Timestamp:
14 May 2024, 07:27:45 (6 months ago)
Author:
Henrik Bettermann
Message:

ConfigurationContainerExporter? and tests.

Location:
main/waeup.kofa/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/CHANGES.txt

    r17772 r17782  
    441.8.2.dev0 (unreleased)
    55=======================
     6
     7* Add `ConfigurationContainerExporter`.
    68
    79* Add `ApplicantRefereeReportProcessor`.
  • main/waeup.kofa/trunk/src/waeup/kofa/configuration.py

    r17755 r17782  
    8080        return implementedBy(SessionConfiguration)
    8181
     82class ConfigurationContainerExporter(grok.GlobalUtility, ExporterBase):
     83    """The Configuration Container Exporter exports all configuration base data.
     84    It also exports the last used student id.
     85    """
     86    grok.implements(ICSVExporter)
     87    grok.name('base_configuration')
     88
     89    title = _(u'Base Configuration')
     90    fields = tuple(sorted(iface_names(IConfigurationContainer))) + ('curr_stud_id',)
     91
     92    def mangle_value(self, value, name, context=None):
     93        if name == 'curr_stud_id':
     94            value = context.__parent__['students']._curr_stud_id
     95        return super(
     96            ConfigurationContainerExporter, self).mangle_value(
     97            value, name, context=context)
     98
     99    def export_all(self, site, filepath=None):
     100        """Export base configuration into filepath as CSV data.
     101        If `filepath` is ``None``, a raw string with CSV data is returned.
     102        """
     103        writer, outfile = self.get_csv_writer(filepath)
     104        configuration = site.get('configuration')
     105        self.write_item(configuration, writer)
     106        return self.close_outfile(filepath, outfile)
     107
    82108class ConfigurationExporter(grok.GlobalUtility, ExporterBase):
    83109    """The Configuration Exporter exports all configuration data. It iterates over all
  • main/waeup.kofa/trunk/src/waeup/kofa/tests/test_configuration.py

    r7811 r17782  
    1818"""Tests for configuration containers and related.
    1919"""
     20import os
     21import shutil
     22import tempfile
     23import unittest
     24from zope.component import queryUtility
     25from zope.component.hooks import setSite, clearSite
    2026from zope.component.interfaces import IFactory
    2127from zope.interface import verify
    2228from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
     29from waeup.kofa.app import University
    2330from waeup.kofa.configuration import (
    24     ConfigurationContainer, SessionConfiguration, SessionConfigurationFactory)
    25 from waeup.kofa.interfaces import(
     31    ConfigurationContainer, SessionConfiguration, SessionConfigurationFactory,
     32    ConfigurationContainerExporter, ConfigurationExporter)
     33from waeup.kofa.interfaces import(ICSVExporter,
    2634    IConfigurationContainer, ISessionConfiguration, ISessionConfigurationAdd)
    2735
     
    7078        implemented_by = self.factory.getInterfaces()
    7179        assert implemented_by.isOrExtends(ISessionConfiguration)
     80
     81
     82class ConfigurationImportExportSetup(FunctionalTestCase):
     83
     84    layer = FunctionalLayer
     85
     86    def setUp(self):
     87        super(ConfigurationImportExportSetup, self).setUp()
     88        # Setup a sample site for each test
     89        app = University()
     90        self.dc_root = tempfile.mkdtemp()
     91        app['datacenter'].setStoragePath(self.dc_root)
     92        self.getRootFolder()['app'] = app
     93        self.app = self.getRootFolder()['app']
     94        setSite(app)
     95        self.workdir = tempfile.mkdtemp()
     96        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
     97        return
     98
     99    def tearDown(self):
     100        super(ConfigurationImportExportSetup, self).tearDown()
     101        shutil.rmtree(self.workdir)
     102        shutil.rmtree(self.dc_root)
     103        clearSite()
     104        return
     105
     106class BaseConfigurationExportTests(ConfigurationImportExportSetup):
     107
     108    def test_ifaces(self):
     109        # make sure we fullfill interface contracts
     110        obj = ConfigurationContainerExporter()
     111        verify.verifyObject(ICSVExporter, obj)
     112        verify.verifyClass(ICSVExporter, ConfigurationContainerExporter)
     113        return
     114
     115    def test_get_as_utility(self):
     116        # we can get a faculty exporter as utility
     117        result = queryUtility(ICSVExporter, name="base_configuration")
     118        self.assertTrue(result is not None)
     119        return
     120
     121    def test_export_all(self):
     122        exporter = ConfigurationContainerExporter()
     123        exporter.export_all(self.app, self.outfile)
     124        result = open(self.outfile, 'rb').read()
     125        self.assertTrue(
     126            'acronym,captcha,carry_over,current_academic_session,email_admin,'
     127            'email_subject,export_disabled_message,frontpage,frontpage_dict,'
     128            'maintmode_enabled_by,name,name_admin,next_matric_integer,'
     129            'next_matric_integer_2,next_matric_integer_3,'
     130            'next_matric_integer_4,smtp_mailer,curr_stud_id\r\n'
     131            'WAeUP.Kofa,No captcha,0,,contact@waeup.org,Kofa Contact,,'
     132            '"<h1>Welcome to WAeUP.Kofa\n<br>' in result
     133            )
     134        return
     135
     136class SessionConfigurationExportTests(ConfigurationImportExportSetup):
     137
     138    def test_ifaces(self):
     139        # make sure we fullfill interface contracts
     140        obj = ConfigurationExporter()
     141        verify.verifyObject(ICSVExporter, obj)
     142        verify.verifyClass(ICSVExporter, ConfigurationExporter)
     143        return
     144
     145    def test_get_as_utility(self):
     146        # we can get a faculty exporter as utility
     147        result = queryUtility(ICSVExporter, name="configurations")
     148        self.assertTrue(result is not None)
     149        return
     150
     151    def test_export_all(self):
     152        self.sessionconfig = SessionConfiguration()
     153        self.sessionconfig.academic_session = 2022
     154        self.sessionconfig.clearance_fee = 3000.6
     155        self.app['configuration'].addSessionConfiguration(self.sessionconfig)
     156        exporter = ConfigurationExporter()
     157        exporter.export_all(self.app, self.outfile)
     158        result = open(self.outfile, 'rb').read()
     159        self.assertEqual(result,
     160            'academic_session,booking_fee,clearance_enabled,clearance_fee,'
     161            'coursereg_deadline,late_registration_fee,maint_fee,'
     162            'payment_disabled,transcript_fee,transfer_fee\r\n'
     163            '2022,0.0,0,3000.6,,0.0,0.0,[],0.0,0.0\r\n'
     164            )
     165        return
Note: See TracChangeset for help on using the changeset viewer.