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

Last change on this file since 6842 was 6840, checked in by Henrik Bettermann, 14 years ago

Add functional tests for student and student study course importers.

  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
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 6840 2011-10-02 06:33:32Z 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"""
24import os
25import shutil
26import tempfile
27import unittest
28from zope.component import createObject
29from zope.component.hooks import setSite, clearSite
30from zope.interface.verify import verifyClass, verifyObject
31
32from waeup.sirp.app import University
33from waeup.sirp.university.faculty import Faculty
34from waeup.sirp.university.department import Department
35from waeup.sirp.students.batching import (
36    StudentProcessor, StudentStudyCourseProcessor)
37from waeup.sirp.students.student import Student
38from waeup.sirp.students.studycourse import StudentStudyCourse
39from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
40from waeup.sirp.interfaces import IBatchProcessor
41
42STUDENT_SAMPLE_DATA = open(
43    os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'),
44    'rb').read()
45
46STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split(
47    '\n')[0].split(',')
48
49STUDYCOURSE_SAMPLE_DATA = open(
50    os.path.join(os.path.dirname(__file__), 'sample_studycourse_data.csv'),
51    'rb').read()
52
53STUDYCOURSE_HEADER_FIELDS = STUDYCOURSE_SAMPLE_DATA.split(
54    '\n')[0].split(',')
55
56class 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        assert len(self.app['students'].keys()) == 4
136        shutil.rmtree(os.path.dirname(fin_file))
137
138class StudentStudyCourseImporterTest(FunctionalTestCase):
139
140    layer = FunctionalLayer
141
142    def setUp(self):
143        super(StudentStudyCourseImporterTest, self).setUp()
144        app = University()
145        self.dc_root = tempfile.mkdtemp()
146        app['datacenter'].setStoragePath(self.dc_root)
147
148        self.getRootFolder()['app'] = app
149        self.app = self.getRootFolder()['app']
150        setSite(app)
151
152        self.workdir = tempfile.mkdtemp()
153        self.importer = StudentStudyCourseProcessor()
154        self.csv_file = os.path.join(self.workdir, 'sample_studycourse_data.csv')
155        open(self.csv_file, 'wb').write(STUDYCOURSE_SAMPLE_DATA)
156
157        # Import students with subobjects
158        student_file = os.path.join(self.workdir, 'sample_student_data.csv')
159        open(student_file, 'wb').write(STUDENT_SAMPLE_DATA)
160        num, num_warns, fin_file, fail_file = StudentProcessor().doImport(
161            student_file, STUDENT_HEADER_FIELDS)
162        shutil.rmtree(os.path.dirname(fin_file))
163
164        # Populate university
165        self.certificate = createObject('waeup.Certificate')
166        self.certificate.code = 'CERT1'
167        self.certificate.application_category = 'basic'
168        self.certificate.start_level = 100
169        self.certificate.end_level = 500
170        self.app['faculties']['fac1'] = Faculty()
171        self.app['faculties']['fac1']['dep1'] = Department()
172        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
173            self.certificate)
174        return
175
176    def tearDown(self):
177        super(StudentStudyCourseImporterTest, self).tearDown()
178        shutil.rmtree(self.workdir)
179        shutil.rmtree(self.dc_root)
180        clearSite()
181        return
182
183    def test_interface(self):
184        # Make sure we fulfill the interface contracts.
185        assert verifyObject(IBatchProcessor, self.importer) is True
186        assert verifyClass(
187            IBatchProcessor, StudentStudyCourseProcessor) is True
188
189    def test_entryExists(self):
190        assert self.importer.entryExists(
191            dict(reg_number='REG_NONE'), self.app) is False
192        student = self.importer.entryExists(dict(reg_number='1'), self.app)
193        self.assertEqual(student.reg_number, u'1')
194
195    def test_getEntry(self):
196        studycourse = self.importer.getEntry(dict(reg_number='1'), self.app)
197        student = studycourse.__parent__
198        s_id = student.student_id
199        assert studycourse is self.app['students'][s_id]['studycourse']
200
201    def test_import(self):
202        num, num_warns, fin_file, fail_file = self.importer.doImport(
203            self.csv_file, STUDYCOURSE_HEADER_FIELDS,'update')
204        studycourse = self.importer.getEntry(dict(reg_number='1'), self.app)
205        self.assertEqual(studycourse.certificate.code, u'CERT1')
206        shutil.rmtree(os.path.dirname(fin_file))
207
208def test_suite():
209    suite = unittest.TestSuite()
210    for testcase in [
211        StudentImporterTest,StudentStudyCourseImporterTest,
212        ]:
213        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
214                testcase
215                )
216        )
217    return suite
Note: See TracBrowser for help on using the repository browser.