source: main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_batching.py @ 7259

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

pyflakes

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