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

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

Export also container_code. This eases re-importing data.

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