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

Last change on this file since 13213 was 13080, checked in by Henrik Bettermann, 9 years ago

Improve interfaces for documentation.

  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1import datetime
2import os
3import pytz
4import shutil
5import tempfile
6import unittest
7from zope.catalog.interfaces import ICatalog
8from zope.component import queryUtility, getUtility
9from zope.interface.verify import verifyObject, verifyClass
10from zope.intid.interfaces import IIntIds
11from waeup.kofa.applicants import ApplicantsContainer
12from waeup.kofa.applicants.export import (
13    ApplicantsContainerExporter, ApplicantExporter)
14from waeup.kofa.applicants.interfaces import (
15    AppCatSource, ApplicationTypeSource)
16from waeup.kofa.applicants.tests.test_batching import (
17    ApplicantImportExportSetup)
18from waeup.kofa.interfaces import ICSVExporter
19from waeup.kofa.schoolgrades import ResultEntry
20from waeup.kofa.testing import KofaUnitTestLayer, FunctionalLayer
21from waeup.kofa.utils.utils import KofaUtils
22
23class ApplicantsContainersExporterTest(unittest.TestCase):
24
25    layer = KofaUnitTestLayer
26
27    def setUp(self):
28        self.workdir = tempfile.mkdtemp()
29        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
30        return
31
32    def tearDown(self):
33        shutil.rmtree(self.workdir)
34        return
35
36    def test_ifaces(self):
37        # make sure we fullfill interface contracts
38        obj = ApplicantsContainerExporter()
39        verifyObject(ICSVExporter, obj)
40        verifyClass(ICSVExporter, ApplicantsContainerExporter)
41        return
42
43    def test_get_as_utility(self):
44        # we can get a faculty exporter as utility
45        result = queryUtility(ICSVExporter, name="applicantscontainers")
46        self.assertTrue(result is not None)
47        return
48
49    def setup_container(self, container):
50        # set all attributes of a container
51        container.code = u'dp2015'
52        container.title = u'General Studies'
53        container.prefix = list(ApplicationTypeSource()(container))[0]
54        container.year = 2015
55        container.application_category = list(AppCatSource()(container))[0]
56        container.description = u'Some Description\nwith linebreak\n'
57        container.description += u'<<de>>man spriht deutsh'
58        container.startdate = datetime.datetime(
59            2015, 1, 1, 12, 0, 0, tzinfo=pytz.utc)
60        container.enddate = datetime.datetime(
61            2015, 1, 31, 23, 0, 0, tzinfo=pytz.utc)
62        return container
63
64    def test_export(self):
65        # we can export a set of applicants containers (w/o applicants)
66        container = ApplicantsContainer()
67        container = self.setup_container(container)
68        exporter = ApplicantsContainerExporter()
69        exporter.export([container], self.outfile)
70        result = open(self.outfile, 'rb').read()
71        self.assertEqual(
72            result,
73            'application_category,application_fee,application_slip_notice,code,description,'
74            'enddate,hidden,mode,prefix,startdate,strict_deadline,title,year\r\n'
75
76            'basic,0.0,,dp2015,'
77            '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",'
78            '2015-01-31 23:00:00+00:00#,0,,app,2015-01-01 12:00:00+00:00#,1,'
79            'General Studies,2015\r\n'
80            )
81        return
82
83class ApplicantExporterTest(ApplicantImportExportSetup):
84
85    layer = FunctionalLayer
86
87    def setUp(self):
88        super(ApplicantExporterTest, self).setUp()
89        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
90        self.cat = getUtility(ICatalog, name='applicants_catalog')
91        self.intids = getUtility(IIntIds)
92        return
93
94    def test_ifaces(self):
95        # make sure we fullfill interface contracts
96        obj = ApplicantExporter()
97        verifyObject(ICSVExporter, obj)
98        verifyClass(ICSVExporter, ApplicantExporter)
99        return
100
101    def test_get_as_utility(self):
102        # we can get an applicant exporter as utility
103        result = queryUtility(ICSVExporter, name="applicants")
104        self.assertTrue(result is not None)
105        return
106
107    def setup_applicant(self, applicant):
108        # set predictable values for `applicant`
109        applicant.reg_number = u'123456'
110        applicant.applicant_id = u'dp2011_654321'
111        applicant.firstname = u'Anna'
112        applicant.lastname = u'Tester'
113        applicant.middlename = u'M.'
114        applicant.date_of_birth = datetime.date(1981, 2, 4)
115        applicant.sex = 'f'
116        applicant.email = 'anna@sample.com'
117        applicant.phone = u'+234-123-12345'
118        applicant.course1 = self.certificate
119        applicant.course2 = self.certificate
120        applicant.course_admitted = self.certificate
121        applicant.notice = u'Some notice\nin lines.'
122        applicant.password = 'any password'
123        result_entry = ResultEntry(
124            KofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
125            KofaUtils.EXAM_GRADES[0][0]
126            )
127        applicant.school_grades = [
128            result_entry]
129        return applicant
130
131    def test_export_emtpy(self):
132        # we can export nearly empty applicants
133        self.applicant.applicant_id = u'dp2011_654321'
134        exporter = ApplicantExporter()
135        exporter.export([self.applicant], self.outfile)
136        result = open(self.outfile, 'rb').read()
137        # The exported records do contain a real date in their
138        # history dict. We skip the date and split the comparison
139        # into two parts.
140        self.assertTrue(
141            'applicant_id,course1,course2,course_admitted,date_of_birth,'
142            'email,firstname,lastname,locked,middlename,notice,phone,'
143            'reg_number,sex,special_application,student_id,suspended,'
144            'password,state,history,container_code,application_number,'
145            'display_fullname,application_date\r\n'
146            'dp2011_654321,,,,,,Anna,Tester,0'
147            in result)
148        self.assertTrue(
149            'Application initialized by system\'],dp2011,654321,Anna Tester'
150            in result)
151        return
152
153    def test_export(self):
154        # we can really export applicants
155        # set values we can expect in export file
156        applicant = self.setup_applicant(self.applicant)
157        exporter = ApplicantExporter()
158        exporter.export([applicant], self.outfile)
159        result = open(self.outfile, 'rb').read()
160        # The exported records do contain a real date in their
161        # history dict. We skip the date and split the comparison
162        # into two parts.
163        self.assertTrue(
164            'applicant_id,course1,course2,course_admitted,date_of_birth,'
165            'email,firstname,lastname,locked,middlename,notice,phone,'
166            'reg_number,sex,special_application,student_id,suspended,'
167            'password,state,history,container_code,application_number,'
168            'display_fullname,application_date\r\n'
169            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,'
170            'anna@sample.com,Anna,Tester,'
171            in result)
172        self.assertTrue(
173            'Application initialized by system\'],dp2011,654321,'
174            'Anna M. Tester,\r\n'
175            in result)
176
177        return
178
179    def test_export_all(self):
180        # we can export all applicants in a portal
181        # set values we can expect in export file
182        self.applicant = self.setup_applicant(self.applicant)
183        exporter = ApplicantExporter()
184        exporter.export_all(self.app, self.outfile)
185        result = open(self.outfile, 'rb').read()
186        self.assertTrue(
187            'applicant_id,course1,course2,course_admitted,date_of_birth,'
188            'email,firstname,lastname,locked,middlename,notice,phone,'
189            'reg_number,sex,special_application,student_id,suspended,'
190            'password,state,history,container_code,application_number,'
191            'display_fullname,application_date\r\n'
192            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,'
193            'anna@sample.com,Anna,Tester,'
194            in result)
195        self.assertTrue(
196            'Application initialized by system\'],dp2011,654321,'
197            'Anna M. Tester,\r\n'
198            in result)
199        return
200
201    def test_export_filtered(self):
202        self.applicant = self.setup_applicant(self.applicant)
203        exporter = ApplicantExporter()
204        exporter.export_filtered(
205            self.app, self.outfile, container=self.container.code)
206        result = open(self.outfile, 'rb').read()
207        self.assertTrue(
208            'applicant_id,course1,course2,course_admitted,date_of_birth,'
209            'email,firstname,lastname,locked,middlename,notice,phone,'
210            'reg_number,sex,special_application,student_id,suspended,'
211            'password,state,history,container_code,application_number,'
212            'display_fullname,application_date\r\n'
213            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04#,'
214            'anna@sample.com,Anna,Tester,'
215            in result)
216        self.assertTrue(
217            'Application initialized by system\'],dp2011,654321,'
218            'Anna M. Tester,\r\n'
219            in result)
220        # In empty container no applicants are exported
221        container = ApplicantsContainer()
222        container.code = u'anything'
223        self.app['applicants']['anything'] = self.container
224        exporter.export_filtered(
225            self.app, self.outfile, container=container.code)
226        result = open(self.outfile, 'rb').read()
227        self.assertTrue(
228            'applicant_id,course1,course2,course_admitted,date_of_birth,'
229            'email,firstname,lastname,locked,middlename,notice,phone,'
230            'reg_number,sex,special_application,student_id,suspended,'
231            'password,state,history,container_code,application_number,'
232            'display_fullname,application_date\r\n'
233            in result)
234        return
Note: See TracBrowser for help on using the repository browser.