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

Last change on this file since 8062 was 8057, checked in by Henrik Bettermann, 13 years ago

Adjust copyright header and propset svn:keywords.

  • Property svn:keywords set to Id
File size: 8.0 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    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        container.entry_level = 100
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.date(2012, 1, 1)
59        container.enddate = datetime.date(2012, 1, 31)
60        return container
61
62    def test_export(self):
63        # we can export a set of applicants containers (w/o applicants)
64        container = ApplicantsContainer()
65        container = self.setup_container(container)
66        exporter = ApplicantsContainerExporter()
67        exporter.export([container], self.outfile)
68        result = open(self.outfile, 'rb').read()
69        self.assertEqual(
70            result,
71            'code,title,prefix,entry_level,year,application_category,'
72            'description,startdate,enddate,strict_deadline\r\n'
73
74            'dp2012,General Studies 2012/13,app,100,2012,basic,'
75            '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",'
76            '2012-01-01,2012-01-31,1\r\n'
77            )
78        return
79
80class ApplicantsExporterTest(ApplicantImportExportSetup):
81
82    layer = FunctionalLayer
83
84    def setUp(self):
85        super(ApplicantsExporterTest, self).setUp()
86        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
87        self.cat = getUtility(ICatalog, name='applicants_catalog')
88        self.intids = getUtility(IIntIds)
89        return
90
91    def test_ifaces(self):
92        # make sure we fullfill interface contracts
93        obj = ApplicantsExporter()
94        verifyObject(ICSVExporter, obj)
95        verifyClass(ICSVExporter, ApplicantsExporter)
96        return
97
98    def test_get_as_utility(self):
99        # we can get an applicant exporter as utility
100        result = queryUtility(ICSVExporter, name="applicants")
101        self.assertTrue(result is not None)
102        return
103
104    def setup_applicant(self, applicant):
105        # set predictable values for `applicant`
106        applicant.reg_number = u'123456'
107        applicant.applicant_id = u'dp2011_654321'
108        applicant.firstname = u'Anna'
109        applicant.lastname = u'Tester'
110        applicant.middlename = u'M.'
111        applicant.date_of_birth = datetime.date(1981, 2, 4)
112        applicant.sex = 'f'
113        applicant.email = 'anna@sample.com'
114        applicant.phone = u'+234-123-12345'
115        applicant.course1 = self.certificate
116        applicant.course2 = self.certificate
117        applicant.course_admitted = self.certificate
118        applicant.notice = u'Some notice\nin lines.'
119        applicant.screening_score = 98
120        applicant.screening_venue = u'Exam Room'
121        applicant.password = 'any password'
122        result_entry = ResultEntry(
123            KofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
124            KofaUtils.EXAM_GRADES[0][0]
125            )
126        applicant.school_grades = [
127            result_entry]
128        return applicant
129
130    def test_export_emtpy(self):
131        # we can export nearly empty applicants
132        self.applicant.applicant_id = u'dp2011_654321'
133        exporter = ApplicantsExporter()
134        exporter.export([self.applicant], self.outfile)
135        result = open(self.outfile, 'rb').read()
136        # The exported records do contain a real date in their
137        # history dict. We skip the date and split the comparison
138        # into two parts.
139        self.assertTrue(
140            'applicant_id,application_date,application_number,course1,course2,'
141            'course_admitted,date_of_birth,display_fullname,email,firstname,'
142            'history,lastname,lga,locked,middlename,notice,password,phone,'
143            'reg_number,screening_score,screening_venue,sex,state,'
144            'student_id\r\n'
145            'dp2011_654321,,654321,,,,,Anna Tester,,Anna,'
146            in result)
147        self.assertTrue(
148            'Application initialized by system\'],Tester,'
149            'foreigner,0,,,,,,,,,initialized,\r\n'
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 = ApplicantsExporter()
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,application_date,application_number,course1,course2,'
165            'course_admitted,date_of_birth,display_fullname,email,firstname,'
166            'history,lastname,lga,locked,middlename,notice,password,phone,'
167            'reg_number,screening_score,screening_venue,sex,state,'
168            'student_id\r\n'
169            'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04,'
170            'Anna M. Tester,anna@sample.com,Anna,'
171            in result)
172        self.assertTrue(
173            'Application initialized by system\'],'
174            'Tester,foreigner,0,M.,"Some notice\nin lines.",any password,'
175            '+234-123-12345,123456,98,Exam Room,f,initialized,\r\n'
176            in result)
177
178        return
179
180    def test_export_all(self):
181        # we can export all applicants in a portal
182        # set values we can expect in export file
183        self.applicant = self.setup_applicant(self.applicant)
184        exporter = ApplicantsExporter()
185        exporter.export_all(self.app, self.outfile)
186        result = open(self.outfile, 'rb').read()
187        self.assertTrue(
188            'applicant_id,application_date,application_number,course1,course2,'
189            'course_admitted,date_of_birth,display_fullname,email,firstname,'
190            'history,lastname,lga,locked,middlename,notice,password,phone,'
191            'reg_number,screening_score,screening_venue,sex,state,'
192            'student_id\r\n'
193            'dp2011_654321,,654321,CERT1,CERT1,CERT1,1981-02-04,'
194            'Anna M. Tester,anna@sample.com,Anna,'
195            in result)
196        self.assertTrue(
197            'Application initialized by system\'],'
198            'Tester,foreigner,0,M.,"Some notice\nin lines.",any password,'
199            '+234-123-12345,123456,98,Exam Room,f,initialized,\r\n'
200            in result)
201        return
Note: See TracBrowser for help on using the repository browser.