1 | import datetime |
---|
2 | import os |
---|
3 | import shutil |
---|
4 | import tempfile |
---|
5 | import unittest |
---|
6 | from zope.component import queryUtility, getUtilitiesFor |
---|
7 | from zope.interface.verify import verifyObject, verifyClass |
---|
8 | from waeup.kofa.interfaces import ICSVExporter |
---|
9 | from waeup.kofa.testing import KofaUnitTestLayer |
---|
10 | from waeup.kofa.applicants import ApplicantsContainer |
---|
11 | from waeup.kofa.applicants.export import ApplicantsContainerExporter |
---|
12 | from waeup.kofa.applicants.interfaces import ( |
---|
13 | IApplicantsContainerProvider, AppCatSource) |
---|
14 | |
---|
15 | class ApplicantsContainersExporterTest(unittest.TestCase): |
---|
16 | |
---|
17 | layer = KofaUnitTestLayer |
---|
18 | |
---|
19 | def setUp(self): |
---|
20 | self.workdir = tempfile.mkdtemp() |
---|
21 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
22 | return |
---|
23 | |
---|
24 | def tearDown(self): |
---|
25 | shutil.rmtree(self.workdir) |
---|
26 | return |
---|
27 | |
---|
28 | def test_ifaces(self): |
---|
29 | # make sure we fullfill interface contracts |
---|
30 | obj = ApplicantsContainerExporter() |
---|
31 | verifyObject(ICSVExporter, obj) |
---|
32 | verifyClass(ICSVExporter, ApplicantsContainerExporter) |
---|
33 | return |
---|
34 | |
---|
35 | def test_get_as_utility(self): |
---|
36 | # we can get a faculty exporter as utility |
---|
37 | result = queryUtility(ICSVExporter, name="applicantscontainers") |
---|
38 | self.assertTrue(result is not None) |
---|
39 | return |
---|
40 | |
---|
41 | def setup_container(self, container): |
---|
42 | # set all attributes of a container |
---|
43 | container.code = u'dp2012' |
---|
44 | container.title = u'General Studies 2012/13' |
---|
45 | from waeup.kofa.applicants.interfaces import ( |
---|
46 | ApplicationTypeSource, ApplicantContainerProviderSource,) |
---|
47 | container.prefix = list(ApplicationTypeSource()(container))[0] |
---|
48 | container.year = 2012 |
---|
49 | provider = [ |
---|
50 | x for x in getUtilitiesFor(IApplicantsContainerProvider) |
---|
51 | if x[0] == 'waeup.kofa.applicants.ApplicantsContainer'][0] |
---|
52 | container.provider = provider |
---|
53 | container.application_category = list(AppCatSource()(container))[0] |
---|
54 | container.description = u'Some Description\nwith linebreak\n' |
---|
55 | container.description += u'<<de>>man spriht deutsh' |
---|
56 | container.startdate = datetime.date(2012, 1, 1) |
---|
57 | container.enddate = datetime.date(2012, 1, 31) |
---|
58 | return container |
---|
59 | |
---|
60 | def test_export(self): |
---|
61 | # we can export a set of applicants containers (w/o applicants) |
---|
62 | container = ApplicantsContainer() |
---|
63 | container = self.setup_container(container) |
---|
64 | exporter = ApplicantsContainerExporter() |
---|
65 | exporter.export([container], self.outfile) |
---|
66 | result = open(self.outfile, 'rb').read() |
---|
67 | self.assertEqual( |
---|
68 | result, |
---|
69 | 'code,title,prefix,entry_level,year,provider,application_category,' |
---|
70 | 'description,startdate,enddate,strict_deadline\r\n' |
---|
71 | |
---|
72 | 'dp2012,General Studies 2012/13,app,100,2012,' |
---|
73 | 'waeup.kofa.applicants.ApplicantsContainer,basic,' |
---|
74 | '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",' |
---|
75 | '2012-01-01,2012-01-31,1\r\n' |
---|
76 | ) |
---|
77 | return |
---|