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

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

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 10.6 KB
Line 
1## $Id: test_batching.py 7193 2011-11-25 07:21:29Z 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.students.studycourse import StudentStudyCourse
35from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
36from waeup.sirp.interfaces import IBatchProcessor
37
38STUDENT_SAMPLE_DATA = open(
39    os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'),
40    'rb').read()
41
42STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split(
43    '\n')[0].split(',')
44
45STUDENT_SAMPLE_DATA_UPDATE = open(
46    os.path.join(os.path.dirname(__file__), 'sample_student_data_update.csv'),
47    'rb').read()
48
49STUDENT_HEADER_FIELDS_UPDATE = STUDENT_SAMPLE_DATA_UPDATE.split(
50    '\n')[0].split(',')
51
52STUDENT_SAMPLE_DATA_UPDATE2 = open(
53    os.path.join(os.path.dirname(__file__), 'sample_student_data_update2.csv'),
54    'rb').read()
55
56STUDENT_HEADER_FIELDS_UPDATE2 = STUDENT_SAMPLE_DATA_UPDATE2.split(
57    '\n')[0].split(',')
58
59STUDENT_SAMPLE_DATA = open(
60    os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'),
61    'rb').read()
62
63STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split(
64    '\n')[0].split(',')
65
66STUDYCOURSE_SAMPLE_DATA = open(
67    os.path.join(os.path.dirname(__file__), 'sample_studycourse_data.csv'),
68    'rb').read()
69
70STUDYCOURSE_HEADER_FIELDS = STUDYCOURSE_SAMPLE_DATA.split(
71    '\n')[0].split(',')
72
73class StudentImporterTest(FunctionalTestCase):
74
75    layer = FunctionalLayer
76
77    def setUp(self):
78        super(StudentImporterTest, self).setUp()
79        # Setup a sample site for each test
80        app = University()
81        self.dc_root = tempfile.mkdtemp()
82        app['datacenter'].setStoragePath(self.dc_root)
83
84        # Prepopulate the ZODB...
85        self.getRootFolder()['app'] = app
86        # we add the site immediately after creation to the
87        # ZODB. Catalogs and other local utilities are not setup
88        # before that step.
89        self.app = self.getRootFolder()['app']
90        # Set site here. Some of the following setup code might need
91        # to access grok.getSite() and should get our new app then
92        setSite(app)
93
94        # Add student with subobjects
95        student = Student()
96        student.fullname = u'Anna Tester'
97        student.reg_number = u'123'
98        student.matric_number = u'234'
99        self.app['students'].addStudent(student)
100        self.student = self.app['students'][student.student_id]
101        self.importer = StudentProcessor()
102        self.workdir = tempfile.mkdtemp()
103        self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv')
104        self.csv_file_update = os.path.join(
105            self.workdir, 'sample_student_data_update.csv')
106        self.csv_file_update2 = os.path.join(
107            self.workdir, 'sample_student_data_update2.csv')
108        open(self.csv_file, 'wb').write(STUDENT_SAMPLE_DATA)
109        open(self.csv_file_update, 'wb').write(STUDENT_SAMPLE_DATA_UPDATE)
110        open(self.csv_file_update2, 'wb').write(STUDENT_SAMPLE_DATA_UPDATE2)
111
112    def tearDown(self):
113        super(StudentImporterTest, self).tearDown()
114        shutil.rmtree(self.workdir)
115        shutil.rmtree(self.dc_root)
116        clearSite()
117        return
118
119    def test_interface(self):
120        # Make sure we fulfill the interface contracts.
121        assert verifyObject(IBatchProcessor, self.importer) is True
122        assert verifyClass(
123            IBatchProcessor, StudentProcessor) is True
124
125    def test_parentsExist(self):
126        assert self.importer.parentsExist(None, dict()) is False
127        assert self.importer.parentsExist(None, self.app) is True
128
129    def test_entryExists(self):
130        assert self.importer.entryExists(
131            dict(student_id='ID_NONE'), self.app) is None
132        student = self.importer.getEntry(
133            dict(student_id=self.student.student_id), self.app)
134        self.assertEqual(student.reg_number, u'123')
135
136    def test_getParent(self):
137        parent = self.importer.getParent(None, self.app)
138        assert parent is self.app['students']
139
140    def test_getEntry(self):
141        assert self.importer.getEntry(
142            dict(student_id='ID_NONE'), self.app) is None
143        assert self.importer.getEntry(
144            dict(student_id=self.student.student_id), self.app) is self.student
145
146    def test_addEntry(self):
147        new_student = Student()
148        self.importer.addEntry(
149            new_student, dict(), self.app)
150        assert len(self.app['students'].keys()) == 2
151
152    def test_delEntry(self):
153        self.importer.delEntry(
154            dict(student_id=self.student.student_id), self.app)
155        assert self.student.student_id not in self.app['students'].keys()
156
157    def test_delEntry(self):
158        assert self.student.student_id in self.app['students'].keys()
159        self.importer.delEntry(
160            dict(reg_number=self.student.reg_number), self.app)
161        assert self.student.student_id not in self.app['students'].keys()
162
163    def test_import(self):
164        num, num_warns, fin_file, fail_file = self.importer.doImport(
165            self.csv_file, STUDENT_HEADER_FIELDS)
166        self.assertEqual(num_warns,0)
167        assert len(self.app['students'].keys()) == 4
168        shutil.rmtree(os.path.dirname(fin_file))
169
170    def test_import_update(self):
171        num, num_warns, fin_file, fail_file = self.importer.doImport(
172            self.csv_file, STUDENT_HEADER_FIELDS)
173        shutil.rmtree(os.path.dirname(fin_file))
174        num, num_warns, fin_file, fail_file = self.importer.doImport(
175            self.csv_file_update, STUDENT_HEADER_FIELDS_UPDATE, 'update')
176        self.assertEqual(num_warns,0)
177        shutil.rmtree(os.path.dirname(fin_file))
178
179    def test_import_update2(self):
180        num, num_warns, fin_file, fail_file = self.importer.doImport(
181            self.csv_file, STUDENT_HEADER_FIELDS)
182        shutil.rmtree(os.path.dirname(fin_file))
183        num, num_warns, fin_file, fail_file = self.importer.doImport(
184            self.csv_file_update2, STUDENT_HEADER_FIELDS_UPDATE2, 'update')
185        self.assertEqual(num_warns,0)
186        shutil.rmtree(os.path.dirname(fin_file))
187
188    def test_import_remove(self):
189        num, num_warns, fin_file, fail_file = self.importer.doImport(
190            self.csv_file, STUDENT_HEADER_FIELDS)
191        shutil.rmtree(os.path.dirname(fin_file))
192        num, num_warns, fin_file, fail_file = self.importer.doImport(
193            self.csv_file_update, STUDENT_HEADER_FIELDS_UPDATE, 'remove')
194        self.assertEqual(num_warns,0)
195        shutil.rmtree(os.path.dirname(fin_file))
196
197class StudentStudyCourseImporterTest(FunctionalTestCase):
198
199    layer = FunctionalLayer
200
201    def setUp(self):
202        super(StudentStudyCourseImporterTest, self).setUp()
203        app = University()
204        self.dc_root = tempfile.mkdtemp()
205        app['datacenter'].setStoragePath(self.dc_root)
206
207        self.getRootFolder()['app'] = app
208        self.app = self.getRootFolder()['app']
209        setSite(app)
210
211        self.workdir = tempfile.mkdtemp()
212        self.importer = StudentStudyCourseProcessor()
213        self.csv_file = os.path.join(
214            self.workdir, 'sample_studycourse_data.csv')
215        open(self.csv_file, 'wb').write(STUDYCOURSE_SAMPLE_DATA)
216
217        # Import students with subobjects
218        student_file = os.path.join(self.workdir, 'sample_student_data.csv')
219        open(student_file, 'wb').write(STUDENT_SAMPLE_DATA)
220        num, num_warns, fin_file, fail_file = StudentProcessor().doImport(
221            student_file, STUDENT_HEADER_FIELDS)
222        shutil.rmtree(os.path.dirname(fin_file))
223
224        # Populate university
225        self.certificate = createObject('waeup.Certificate')
226        self.certificate.code = 'CERT1'
227        self.certificate.application_category = 'basic'
228        self.certificate.start_level = 100
229        self.certificate.end_level = 500
230        self.app['faculties']['fac1'] = Faculty()
231        self.app['faculties']['fac1']['dep1'] = Department()
232        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
233            self.certificate)
234        return
235
236    def tearDown(self):
237        super(StudentStudyCourseImporterTest, self).tearDown()
238        shutil.rmtree(self.workdir)
239        shutil.rmtree(self.dc_root)
240        clearSite()
241        return
242
243    def test_interface(self):
244        # Make sure we fulfill the interface contracts.
245        assert verifyObject(IBatchProcessor, self.importer) is True
246        assert verifyClass(
247            IBatchProcessor, StudentStudyCourseProcessor) is True
248
249    def test_entryExists(self):
250        assert self.importer.entryExists(
251            dict(reg_number='REG_NONE'), self.app) is None
252        student = self.importer.entryExists(dict(reg_number='1'), self.app)
253        self.assertEqual(student.reg_number, u'1')
254
255    def test_getEntry(self):
256        studycourse = self.importer.getEntry(dict(reg_number='1'), self.app)
257        student = studycourse.__parent__
258        s_id = student.student_id
259        assert studycourse is self.app['students'][s_id]['studycourse']
260
261    def test_import(self):
262        num, num_warns, fin_file, fail_file = self.importer.doImport(
263            self.csv_file, STUDYCOURSE_HEADER_FIELDS,'update')
264        studycourse = self.importer.getEntry(dict(reg_number='1'), self.app)
265        self.assertEqual(num_warns,0)
266        self.assertEqual(studycourse.certificate.code, u'CERT1')
267        shutil.rmtree(os.path.dirname(fin_file))
268
269def test_suite():
270    suite = unittest.TestSuite()
271    for testcase in [
272        StudentImporterTest,StudentStudyCourseImporterTest,
273        ]:
274        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
275                testcase
276                )
277        )
278    return suite
Note: See TracBrowser for help on using the repository browser.