1 | import datetime |
---|
2 | import os |
---|
3 | import shutil |
---|
4 | import tempfile |
---|
5 | import unittest |
---|
6 | from zope.catalog.interfaces import ICatalog |
---|
7 | from zope.component import queryUtility, getUtility, getUtilitiesFor |
---|
8 | from zope.interface.verify import verifyObject, verifyClass |
---|
9 | from zope.intid.interfaces import IIntIds |
---|
10 | from waeup.kofa.applicants import ApplicantsContainer |
---|
11 | from waeup.kofa.applicants.export import ( |
---|
12 | ApplicantsContainerExporter, ApplicantsExporter) |
---|
13 | from waeup.kofa.applicants.interfaces import ( |
---|
14 | AppCatSource, ApplicationTypeSource) |
---|
15 | from waeup.kofa.applicants.tests.test_batching import ( |
---|
16 | ApplicantImportExportSetup) |
---|
17 | from waeup.kofa.interfaces import ICSVExporter |
---|
18 | from waeup.kofa.schoolgrades import ResultEntry |
---|
19 | from waeup.kofa.testing import KofaUnitTestLayer, FunctionalLayer |
---|
20 | from waeup.kofa.utils.utils import KofaUtils |
---|
21 | |
---|
22 | class 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 | |
---|
80 | class 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(self): |
---|
130 | # we can really export applicants |
---|
131 | # set values we can expect in export file |
---|
132 | applicant = self.setup_applicant(self.applicant) |
---|
133 | exporter = ApplicantsExporter() |
---|
134 | exporter.export([applicant], self.outfile) |
---|
135 | result = open(self.outfile, 'rb').read() |
---|
136 | self.assertEqual( |
---|
137 | result, |
---|
138 | 'applicant_id,course1,course2,course_admitted,date_of_birth,' |
---|
139 | 'email,firstname,lastname,lga,middlename,notice,phone,' |
---|
140 | 'reg_number,school_grades,screening_score,screening_venue,' |
---|
141 | 'sex\r\n' |
---|
142 | |
---|
143 | 'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04,anna@sample.com,' |
---|
144 | 'Anna,Tester,foreigner,M.,"Some notice\nin lines.",' |
---|
145 | '+234-123-12345,123456,' |
---|
146 | '"[(\'computer_science\', \'A\')]",98,Exam Room,f\r\n' |
---|
147 | ) |
---|
148 | return |
---|