1 | ## |
---|
2 | ## test_batching.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Tue Aug 24 02:04:44 2010 Uli Fouquet |
---|
5 | ## $Id: test_batching.py 6843 2011-10-02 07:57:57Z henrik $ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """Unit tests for students-related data importers. |
---|
23 | """ |
---|
24 | import os |
---|
25 | import shutil |
---|
26 | import tempfile |
---|
27 | import unittest |
---|
28 | from zope.component import createObject |
---|
29 | from zope.component.hooks import setSite, clearSite |
---|
30 | from zope.interface.verify import verifyClass, verifyObject |
---|
31 | |
---|
32 | from waeup.sirp.app import University |
---|
33 | from waeup.sirp.university.faculty import Faculty |
---|
34 | from waeup.sirp.university.department import Department |
---|
35 | from waeup.sirp.students.batching import ( |
---|
36 | StudentProcessor, StudentStudyCourseProcessor) |
---|
37 | from waeup.sirp.students.student import Student |
---|
38 | from waeup.sirp.students.studycourse import StudentStudyCourse |
---|
39 | from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase |
---|
40 | from waeup.sirp.interfaces import IBatchProcessor |
---|
41 | |
---|
42 | STUDENT_SAMPLE_DATA = open( |
---|
43 | os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'), |
---|
44 | 'rb').read() |
---|
45 | |
---|
46 | STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split( |
---|
47 | '\n')[0].split(',') |
---|
48 | |
---|
49 | STUDYCOURSE_SAMPLE_DATA = open( |
---|
50 | os.path.join(os.path.dirname(__file__), 'sample_studycourse_data.csv'), |
---|
51 | 'rb').read() |
---|
52 | |
---|
53 | STUDYCOURSE_HEADER_FIELDS = STUDYCOURSE_SAMPLE_DATA.split( |
---|
54 | '\n')[0].split(',') |
---|
55 | |
---|
56 | class StudentImporterTest(FunctionalTestCase): |
---|
57 | |
---|
58 | layer = FunctionalLayer |
---|
59 | |
---|
60 | def setUp(self): |
---|
61 | super(StudentImporterTest, self).setUp() |
---|
62 | # Setup a sample site for each test |
---|
63 | app = University() |
---|
64 | self.dc_root = tempfile.mkdtemp() |
---|
65 | app['datacenter'].setStoragePath(self.dc_root) |
---|
66 | |
---|
67 | # Prepopulate the ZODB... |
---|
68 | self.getRootFolder()['app'] = app |
---|
69 | # we add the site immediately after creation to the |
---|
70 | # ZODB. Catalogs and other local utilities are not setup |
---|
71 | # before that step. |
---|
72 | self.app = self.getRootFolder()['app'] |
---|
73 | # Set site here. Some of the following setup code might need |
---|
74 | # to access grok.getSite() and should get our new app then |
---|
75 | setSite(app) |
---|
76 | |
---|
77 | # Add student with subobjects |
---|
78 | student = Student() |
---|
79 | student.fullname = u'Anna Tester' |
---|
80 | student.reg_number = u'123' |
---|
81 | student.matric_number = u'234' |
---|
82 | self.app['students'].addStudent(student) |
---|
83 | self.student = self.app['students'][student.student_id] |
---|
84 | self.importer = StudentProcessor() |
---|
85 | self.workdir = tempfile.mkdtemp() |
---|
86 | self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv') |
---|
87 | open(self.csv_file, 'wb').write(STUDENT_SAMPLE_DATA) |
---|
88 | |
---|
89 | def tearDown(self): |
---|
90 | super(StudentImporterTest, self).tearDown() |
---|
91 | shutil.rmtree(self.workdir) |
---|
92 | shutil.rmtree(self.dc_root) |
---|
93 | clearSite() |
---|
94 | return |
---|
95 | |
---|
96 | def test_interface(self): |
---|
97 | # Make sure we fulfill the interface contracts. |
---|
98 | assert verifyObject(IBatchProcessor, self.importer) is True |
---|
99 | assert verifyClass( |
---|
100 | IBatchProcessor, StudentProcessor) is True |
---|
101 | |
---|
102 | def test_parentsExist(self): |
---|
103 | assert self.importer.parentsExist(None, dict()) is False |
---|
104 | assert self.importer.parentsExist(None, self.app) is True |
---|
105 | |
---|
106 | def test_entryExists(self): |
---|
107 | assert self.importer.entryExists( |
---|
108 | dict(student_id='ID_NONE'), self.app) is False |
---|
109 | assert self.importer.entryExists( |
---|
110 | dict(student_id=self.student.student_id), self.app) is True |
---|
111 | |
---|
112 | def test_getParent(self): |
---|
113 | parent = self.importer.getParent(None, self.app) |
---|
114 | assert parent is self.app['students'] |
---|
115 | |
---|
116 | def test_getEntry(self): |
---|
117 | assert self.importer.getEntry( |
---|
118 | dict(student_id='ID_NONE'), self.app) is None |
---|
119 | assert self.importer.getEntry( |
---|
120 | dict(student_id=self.student.student_id), self.app) is self.student |
---|
121 | |
---|
122 | def test_addEntry(self): |
---|
123 | new_student = Student() |
---|
124 | self.importer.addEntry( |
---|
125 | new_student, dict(), self.app) |
---|
126 | assert len(self.app['students'].keys()) == 2 |
---|
127 | |
---|
128 | def test_delEntry(self): |
---|
129 | self.importer.delEntry(dict(student_id=self.student.student_id), self.app) |
---|
130 | assert self.student.student_id not in self.app['students'].keys() |
---|
131 | |
---|
132 | def test_import(self): |
---|
133 | num, num_warns, fin_file, fail_file = self.importer.doImport( |
---|
134 | self.csv_file, STUDENT_HEADER_FIELDS) |
---|
135 | self.assertEqual(num_warns,0) |
---|
136 | assert len(self.app['students'].keys()) == 4 |
---|
137 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
138 | |
---|
139 | class StudentStudyCourseImporterTest(FunctionalTestCase): |
---|
140 | |
---|
141 | layer = FunctionalLayer |
---|
142 | |
---|
143 | def setUp(self): |
---|
144 | super(StudentStudyCourseImporterTest, self).setUp() |
---|
145 | app = University() |
---|
146 | self.dc_root = tempfile.mkdtemp() |
---|
147 | app['datacenter'].setStoragePath(self.dc_root) |
---|
148 | |
---|
149 | self.getRootFolder()['app'] = app |
---|
150 | self.app = self.getRootFolder()['app'] |
---|
151 | setSite(app) |
---|
152 | |
---|
153 | self.workdir = tempfile.mkdtemp() |
---|
154 | self.importer = StudentStudyCourseProcessor() |
---|
155 | self.csv_file = os.path.join(self.workdir, 'sample_studycourse_data.csv') |
---|
156 | open(self.csv_file, 'wb').write(STUDYCOURSE_SAMPLE_DATA) |
---|
157 | |
---|
158 | # Import students with subobjects |
---|
159 | student_file = os.path.join(self.workdir, 'sample_student_data.csv') |
---|
160 | open(student_file, 'wb').write(STUDENT_SAMPLE_DATA) |
---|
161 | num, num_warns, fin_file, fail_file = StudentProcessor().doImport( |
---|
162 | student_file, STUDENT_HEADER_FIELDS) |
---|
163 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
164 | |
---|
165 | # Populate university |
---|
166 | self.certificate = createObject('waeup.Certificate') |
---|
167 | self.certificate.code = 'CERT1' |
---|
168 | self.certificate.application_category = 'basic' |
---|
169 | self.certificate.start_level = 100 |
---|
170 | self.certificate.end_level = 500 |
---|
171 | self.app['faculties']['fac1'] = Faculty() |
---|
172 | self.app['faculties']['fac1']['dep1'] = Department() |
---|
173 | self.app['faculties']['fac1']['dep1'].certificates.addCertificate( |
---|
174 | self.certificate) |
---|
175 | return |
---|
176 | |
---|
177 | def tearDown(self): |
---|
178 | super(StudentStudyCourseImporterTest, self).tearDown() |
---|
179 | shutil.rmtree(self.workdir) |
---|
180 | shutil.rmtree(self.dc_root) |
---|
181 | clearSite() |
---|
182 | return |
---|
183 | |
---|
184 | def test_interface(self): |
---|
185 | # Make sure we fulfill the interface contracts. |
---|
186 | assert verifyObject(IBatchProcessor, self.importer) is True |
---|
187 | assert verifyClass( |
---|
188 | IBatchProcessor, StudentStudyCourseProcessor) is True |
---|
189 | |
---|
190 | def test_entryExists(self): |
---|
191 | assert self.importer.entryExists( |
---|
192 | dict(reg_number='REG_NONE'), self.app) is False |
---|
193 | student = self.importer.entryExists(dict(reg_number='1'), self.app) |
---|
194 | self.assertEqual(student.reg_number, u'1') |
---|
195 | |
---|
196 | def test_getEntry(self): |
---|
197 | studycourse = self.importer.getEntry(dict(reg_number='1'), self.app) |
---|
198 | student = studycourse.__parent__ |
---|
199 | s_id = student.student_id |
---|
200 | assert studycourse is self.app['students'][s_id]['studycourse'] |
---|
201 | |
---|
202 | def test_import(self): |
---|
203 | num, num_warns, fin_file, fail_file = self.importer.doImport( |
---|
204 | self.csv_file, STUDYCOURSE_HEADER_FIELDS,'update') |
---|
205 | studycourse = self.importer.getEntry(dict(reg_number='1'), self.app) |
---|
206 | self.assertEqual(num_warns,0) |
---|
207 | self.assertEqual(studycourse.certificate.code, u'CERT1') |
---|
208 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
209 | |
---|
210 | def test_suite(): |
---|
211 | suite = unittest.TestSuite() |
---|
212 | for testcase in [ |
---|
213 | StudentImporterTest,StudentStudyCourseImporterTest, |
---|
214 | ]: |
---|
215 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
216 | testcase |
---|
217 | ) |
---|
218 | ) |
---|
219 | return suite |
---|