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

Last change on this file since 8048 was 8047, checked in by uli, 13 years ago

Fix and extend applicant exporter tests.

File size: 6.9 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        result_entry = ResultEntry(
122            KofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
123            KofaUtils.EXAM_GRADES[0][0]
124            )
125        applicant.school_grades = [
126            result_entry]
127        return applicant
128
129    def test_export_emtpy(self):
130        # we can export nearly empty applicants
131        self.applicant.applicant_id = u'dp2011_654321'
132        exporter = ApplicantsExporter()
133        exporter.export([self.applicant], self.outfile)
134        result = open(self.outfile, 'rb').read()
135        self.assertEqual(
136            result,
137            'applicant_id,course1,course2,course_admitted,date_of_birth,'
138            'email,firstname,lastname,lga,middlename,notice,phone,'
139            'reg_number,screening_score,screening_venue,sex\r\n'
140
141            'dp2011_654321,,,,,,Anna,Tester,foreigner,,,,,,,\r\n'
142            )
143        return
144
145    def test_export(self):
146        # we can really export applicants
147        # set values we can expect in export file
148        applicant = self.setup_applicant(self.applicant)
149        exporter = ApplicantsExporter()
150        exporter.export([applicant], self.outfile)
151        result = open(self.outfile, 'rb').read()
152        self.assertEqual(
153            result,
154            'applicant_id,course1,course2,course_admitted,date_of_birth,'
155            'email,firstname,lastname,lga,middlename,notice,phone,'
156            'reg_number,screening_score,screening_venue,'
157            'sex\r\n'
158
159            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04,anna@sample.com,'
160            'Anna,Tester,foreigner,M.,"Some notice\nin lines.",'
161            '+234-123-12345,123456,98,Exam Room,f\r\n'
162            )
163        return
164
165    def test_export_all(self):
166        # we can export all applicants in a portal
167        # set values we can expect in export file
168        self.applicant = self.setup_applicant(self.applicant)
169        exporter = ApplicantsExporter()
170        exporter.export_all(self.app, self.outfile)
171        result = open(self.outfile, 'rb').read()
172        self.assertEqual(
173            result,
174            'applicant_id,course1,course2,course_admitted,date_of_birth,'
175            'email,firstname,lastname,lga,middlename,notice,phone,'
176            'reg_number,screening_score,screening_venue,'
177            'sex\r\n'
178
179            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04,anna@sample.com,'
180            'Anna,Tester,foreigner,M.,"Some notice\nin lines.",'
181            '+234-123-12345,123456,98,Exam Room,f\r\n'
182            )
183        return
Note: See TracBrowser for help on using the repository browser.