source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_export.py @ 7975

Last change on this file since 7975 was 7925, checked in by uli, 13 years ago

Reflect changes in applicant exporter.

File size: 5.8 KB
Line 
1import datetime
2import os
3import shutil
4import tempfile
5import unittest
6from zope.catalog.interfaces import ICatalog
7from zope.component import queryUtility, getUtility, getUtilitiesFor
8from zope.interface.verify import verifyObject, verifyClass
9from zope.intid.interfaces import IIntIds
10from waeup.kofa.applicants import ApplicantsContainer
11from waeup.kofa.applicants.export import (
12    ApplicantsContainerExporter, ApplicantsExporter)
13from waeup.kofa.applicants.interfaces import (
14    IApplicantsContainerProvider, AppCatSource, ApplicationTypeSource)
15from waeup.kofa.applicants.tests.test_batching import (
16    ApplicantImportExportSetup)
17from waeup.kofa.interfaces import ICSVExporter
18from waeup.kofa.schoolgrades import ResultEntry
19from waeup.kofa.testing import KofaUnitTestLayer, FunctionalLayer
20from waeup.kofa.utils.utils import KofaUtils
21
22class ApplicantsContainersExporterTest(unittest.TestCase):
23
24    layer = KofaUnitTestLayer
25
26    def setUp(self):
27        self.workdir = tempfile.mkdtemp()
28        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
29        return
30
31    def tearDown(self):
32        shutil.rmtree(self.workdir)
33        return
34
35    def test_ifaces(self):
36        # make sure we fullfill interface contracts
37        obj = ApplicantsContainerExporter()
38        verifyObject(ICSVExporter, obj)
39        verifyClass(ICSVExporter, ApplicantsContainerExporter)
40        return
41
42    def test_get_as_utility(self):
43        # we can get a faculty exporter as utility
44        result = queryUtility(ICSVExporter, name="applicantscontainers")
45        self.assertTrue(result is not None)
46        return
47
48    def setup_container(self, container):
49        # set all attributes of a container
50        container.code = u'dp2012'
51        container.title = u'General Studies 2012/13'
52        container.prefix = list(ApplicationTypeSource()(container))[0]
53        container.year = 2012
54        provider = [
55            x for x in getUtilitiesFor(IApplicantsContainerProvider)
56            if x[0] == 'waeup.kofa.applicants.ApplicantsContainer'][0]
57        container.provider = provider
58        container.application_category = list(AppCatSource()(container))[0]
59        container.description = u'Some Description\nwith linebreak\n'
60        container.description += u'<<de>>man spriht deutsh'
61        container.startdate = datetime.date(2012, 1, 1)
62        container.enddate = datetime.date(2012, 1, 31)
63        return container
64
65    def test_export(self):
66        # we can export a set of applicants containers (w/o applicants)
67        container = ApplicantsContainer()
68        container = self.setup_container(container)
69        exporter = ApplicantsContainerExporter()
70        exporter.export([container], self.outfile)
71        result = open(self.outfile, 'rb').read()
72        self.assertEqual(
73            result,
74            'code,title,prefix,entry_level,year,provider,application_category,'
75            'description,startdate,enddate,strict_deadline\r\n'
76
77            'dp2012,General Studies 2012/13,app,100,2012,'
78            'waeup.kofa.applicants.ApplicantsContainer,basic,'
79            '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",'
80            '2012-01-01,2012-01-31,1\r\n'
81            )
82        return
83
84class ApplicantsExporterTest(ApplicantImportExportSetup):
85
86    layer = FunctionalLayer
87
88    def setUp(self):
89        super(ApplicantsExporterTest, self).setUp()
90        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
91        self.cat = getUtility(ICatalog, name='applicants_catalog')
92        self.intids = getUtility(IIntIds)
93        return
94
95    def test_ifaces(self):
96        # make sure we fullfill interface contracts
97        obj = ApplicantsExporter()
98        verifyObject(ICSVExporter, obj)
99        verifyClass(ICSVExporter, ApplicantsExporter)
100        return
101
102    def test_get_as_utility(self):
103        # we can get an applicant exporter as utility
104        result = queryUtility(ICSVExporter, name="applicants")
105        self.assertTrue(result is not None)
106        return
107
108    def setup_applicant(self, applicant):
109        # set predictable values for `applicant`
110        applicant.reg_number = u'123456'
111        applicant.applicant_id = u'dp2011_654321'
112        applicant.firstname = u'Anna'
113        applicant.lastname = u'Tester'
114        applicant.middlename = u'M.'
115        applicant.date_of_birth = datetime.date(1981, 2, 4)
116        applicant.sex = 'f'
117        applicant.email = 'anna@sample.com'
118        applicant.phone = u'+234-123-12345'
119        applicant.course1 = self.certificate
120        applicant.course2 = self.certificate
121        applicant.course_admitted = self.certificate
122        applicant.notice = u'Some notice\nin lines.'
123        applicant.screening_score = 98
124        applicant.screening_venue = u'Exam Room'
125        result_entry = ResultEntry(
126            KofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
127            KofaUtils.EXAM_GRADES[0][0]
128            )
129        applicant.school_grades = [
130            result_entry]
131        return applicant
132
133    def test_export(self):
134        # we can really export applicants
135        # set values we can expect in export file
136        applicant = self.setup_applicant(self.applicant)
137        exporter = ApplicantsExporter()
138        exporter.export([applicant], self.outfile)
139        result = open(self.outfile, 'rb').read()
140        self.assertEqual(
141            result,
142            'applicant_id,course1,course2,course_admitted,date_of_birth,'
143            'email,firstname,lastname,lga,middlename,notice,phone,'
144            'reg_number,school_grades,screening_score,screening_venue,'
145            'sex\r\n'
146
147            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04,anna@sample.com,'
148            'Anna,Tester,foreigner,M.,"Some notice\nin lines.",'
149            '+234-123-12345,123456,'
150            '"[(\'computer_science\', \'A\')]",98,Exam Room,f\r\n'
151            )
152        return
Note: See TracBrowser for help on using the repository browser.