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

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

Remove unneeded default values in interfaces. Adjust tests.

File size: 5.8 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    IApplicantsContainerProvider, 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        provider = [
56            x for x in getUtilitiesFor(IApplicantsContainerProvider)
57            if x[0] == 'waeup.kofa.applicants.ApplicantsContainer'][0]
58        container.provider = provider
59        container.application_category = list(AppCatSource()(container))[0]
60        container.description = u'Some Description\nwith linebreak\n'
61        container.description += u'<<de>>man spriht deutsh'
62        container.startdate = datetime.date(2012, 1, 1)
63        container.enddate = datetime.date(2012, 1, 31)
64        return container
65
66    def test_export(self):
67        # we can export a set of applicants containers (w/o applicants)
68        container = ApplicantsContainer()
69        container = self.setup_container(container)
70        exporter = ApplicantsContainerExporter()
71        exporter.export([container], self.outfile)
72        result = open(self.outfile, 'rb').read()
73        self.assertEqual(
74            result,
75            'code,title,prefix,entry_level,year,provider,application_category,'
76            'description,startdate,enddate,strict_deadline\r\n'
77
78            'dp2012,General Studies 2012/13,app,100,2012,'
79            'waeup.kofa.applicants.ApplicantsContainer,basic,'
80            '"Some Description\nwith linebreak\n<<de>>man spriht deutsh",'
81            '2012-01-01,2012-01-31,1\r\n'
82            )
83        return
84
85class ApplicantsExporterTest(ApplicantImportExportSetup):
86
87    layer = FunctionalLayer
88
89    def setUp(self):
90        super(ApplicantsExporterTest, self).setUp()
91        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
92        self.cat = getUtility(ICatalog, name='applicants_catalog')
93        self.intids = getUtility(IIntIds)
94        return
95
96    def test_ifaces(self):
97        # make sure we fullfill interface contracts
98        obj = ApplicantsExporter()
99        verifyObject(ICSVExporter, obj)
100        verifyClass(ICSVExporter, ApplicantsExporter)
101        return
102
103    def test_get_as_utility(self):
104        # we can get an applicant exporter as utility
105        result = queryUtility(ICSVExporter, name="applicants")
106        self.assertTrue(result is not None)
107        return
108
109    def setup_applicant(self, applicant):
110        # set predictable values for `applicant`
111        applicant.reg_number = u'123456'
112        applicant.applicant_id = u'dp2011_654321'
113        applicant.firstname = u'Anna'
114        applicant.lastname = u'Tester'
115        applicant.middlename = u'M.'
116        applicant.date_of_birth = datetime.date(1981, 2, 4)
117        applicant.sex = 'f'
118        applicant.email = 'anna@sample.com'
119        applicant.phone = u'+234-123-12345'
120        applicant.course1 = self.certificate
121        applicant.course2 = self.certificate
122        applicant.course_admitted = self.certificate
123        applicant.notice = u'Some notice\nin lines.'
124        applicant.screening_score = 98
125        applicant.screening_venue = u'Exam Room'
126        result_entry = ResultEntry(
127            KofaUtils.EXAM_SUBJECTS_DICT.keys()[0],
128            KofaUtils.EXAM_GRADES[0][0]
129            )
130        applicant.school_grades = [
131            result_entry]
132        return applicant
133
134    def test_export(self):
135        # we can really export applicants
136        # set values we can expect in export file
137        applicant = self.setup_applicant(self.applicant)
138        exporter = ApplicantsExporter()
139        exporter.export([applicant], self.outfile)
140        result = open(self.outfile, 'rb').read()
141        self.assertEqual(
142            result,
143            'applicant_id,course1,course2,course_admitted,date_of_birth,'
144            'email,firstname,lastname,lga,middlename,notice,phone,'
145            'reg_number,school_grades,screening_score,screening_venue,'
146            'sex\r\n'
147
148            'dp2011_654321,CERT1,CERT1,CERT1,1981-02-04,anna@sample.com,'
149            'Anna,Tester,foreigner,M.,"Some notice\nin lines.",'
150            '+234-123-12345,123456,'
151            '"[(\'computer_science\', \'A\')]",98,Exam Room,f\r\n'
152            )
153        return
Note: See TracBrowser for help on using the repository browser.