source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_batching.py @ 6251

Last change on this file since 6251 was 6251, checked in by uli, 13 years ago

First steps to make the copied batch-stuff work with regular applicants. Right now we only get more errors.

File size: 5.2 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$
6##
7## Copyright (C) 2010 Uli Fouquet
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 applicants-related data importers.
23"""
24import grok
25import os
26import shutil
27import tempfile
28import unittest
29from zope.app.testing.functional import FunctionalTestCase
30from zope.component.hooks import setSite, clearSite
31from zope.interface.verify import verifyClass, verifyObject
32
33from waeup.sirp.app import University
34from waeup.sirp.applicants.batching import ApplicantsContainerImporter
35from waeup.sirp.applicants.interfaces import IApplicantsContainer
36from waeup.sirp.interfaces import DuplicationError
37from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module
38from waeup.sirp.interfaces import IBatchProcessor
39
40
41# Sample data we can use in tests...
42JAMB_SAMPLE_DATA = '''reg_no,fst_sit_fname,lastname,firstname,middlenames,sex,date_of_birth,jamb_state,jamb_lga,course1,screening_date,screening_venue,entry_session,screening_type,ignore_this_col
4391100546DD,ISUOSUO MOSES ODOMERO,ISUOSUO,MOSES,ODOMERO,M,25/5/1982,DEL,ISO-S,BSCPOL,2009/09/25 09:00:00 GMT+1,REPRINT SLIP AS FROM WED 23/09/2009,9,pde,blah
4491111834CC,DURUIHEOMA AUGUSTINA ADANNA,DURUIHEOMA,AUGUSTINA,ADANNA,F,15/4/1986,IMO,MBAIT,BSCPOL,2009/09/25 09:00:00 GMT+1,REPRINT SLIP AS FROM WED 23/09/2009,9,pde,blah
4591109351AC,ARISERE EBIKEBUNA COMFORT,ARISERE,EBIKEBUNA,COMFORT,F,6/1/1984,EDO,OV-SW,BSCPOL,2009/09/25 09:00:00 GMT+1,REPRINT SLIP AS FROM WED 23/09/2009,9,pde,blah
46'''
47
48# The header fields of the above CSV snippet
49HEADER_FIELDS = JAMB_SAMPLE_DATA.split('\n')[0].split(',')
50
51class FakeSite(dict):
52    def getSiteManager(self):
53        return object()
54
55class ApplicantsContainerImporterTest(unittest.TestCase):
56
57    def setUp(self):
58        self.importer = ApplicantsContainerImporter()
59        self.site1 = dict(
60            jambdata = {
61                'REG_NO_1': 'Application1',
62                }
63            )
64        self.workdir = tempfile.mkdtemp()
65        self.csv_file = os.path.join(self.workdir, 'sampledata.csv')
66        open(self.csv_file, 'wb').write(JAMB_SAMPLE_DATA)
67        return
68
69    def tearDown(self):
70        shutil.rmtree(self.workdir)
71        clearSite()
72        return
73
74    def test_interface(self):
75        """Make sure we fulfill the interface contracts."""
76        assert verifyObject(IBatchProcessor, self.importer) is True
77        assert verifyClass(
78            IBatchProcessor, ApplicantsContainerImporter) is True
79
80    def test_parentsExist(self):
81        assert self.importer.parentsExist(None, dict()) is False
82        assert self.importer.parentsExist(None, self.site1) is True
83
84    def test_entryExists(self):
85        assert self.importer.entryExists(
86            dict(reg_no='REG_NONE'), self.site1) is False
87        assert self.importer.entryExists(
88            dict(reg_no='REG_NO_1'), self.site1) is True
89
90    def test_getParent(self):
91        parent = self.importer.getParent(None, self.site1)
92        assert parent is self.site1['jambdata']
93
94    def test_getEntry(self):
95        assert self.importer.getEntry(
96            dict(reg_no='REG_NONE'), self.site1) is None
97        assert self.importer.getEntry(
98            dict(reg_no='REG_NO_1'), self.site1) == 'Application1'
99
100    def test_addEntry(self):
101        self.importer.addEntry(
102            'New application', dict(reg_no='REG_NO_99'), self.site1)
103        assert self.site1['jambdata']['REG_NO_99'] == 'New application'
104
105    def test_delEntry(self):
106        self.importer.delEntry(dict(reg_no='REG_NO_1'), self.site1)
107        assert 'REG_NO_1' not in self.site1['jambdata'].keys()
108
109    def test_import(self):
110        # Do a real import
111        # The following modules register important components for import
112        # Registers converters...
113        grok.testing.grok('waeup.sirp.utils.converters')
114        # Registers the Applicant factory...
115        grok.testing.grok('waeup.sirp.applicants.applicants')
116
117        # Create a fake site to store datasets in...
118        site = FakeSite()
119        # The site must have a 'jambdata' entry...
120        site['jambdata'] = dict()
121        # Set the fake site as 'current' site object...
122        setSite(site)
123        self.importer.doImport(self.csv_file, HEADER_FIELDS)
124        self.assertTrue(u'91100546DD' in site['jambdata'].keys())
125
126def test_suite():
127    suite = unittest.TestSuite()
128    for testcase in [
129        ApplicantsContainerImporterTest,
130        ]:
131        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
132                testcase
133                )
134        )
135    return suite
Note: See TracBrowser for help on using the repository browser.