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

Last change on this file since 6254 was 5754, checked in by uli, 14 years ago

Fix test: Applicant factory moved to another module.

File size: 4.8 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 the JAMB data importer.
23"""
24
25import grok
26import os
27import shutil
28import tempfile
29import unittest
30from zope.component.hooks import setSite, clearSite
31from zope.interface import verify
32from waeup.sirp.interfaces import IBatchProcessor
33from waeup.sirp.applicants.jambtables import JAMBDataImporter
34
35# Sample data we can use in tests...
36JAMB_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
3791100546DD,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
3891111834CC,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
3991109351AC,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
40'''
41
42# The header fields of the above CSV snippet
43HEADER_FIELDS = JAMB_SAMPLE_DATA.split('\n')[0].split(',')
44
45class FakeSite(dict):
46    def getSiteManager(self):
47        return object()
48
49class JAMBDataImporterTest(unittest.TestCase):
50
51    def setUp(self):
52        self.importer = JAMBDataImporter()
53        self.site1 = dict(
54            jambdata = {
55                'REG_NO_1': 'Application1',
56                }
57            )
58        self.workdir = tempfile.mkdtemp()
59        self.csv_file = os.path.join(self.workdir, 'sampledata.csv')
60        open(self.csv_file, 'wb').write(JAMB_SAMPLE_DATA)
61        return
62
63    def tearDown(self):
64        shutil.rmtree(self.workdir)
65        clearSite()
66        return
67
68    def test_interface(self):
69        """Make sure we fulfill the interface contracts."""
70        assert verify.verifyObject(IBatchProcessor, self.importer) is True
71        assert verify.verifyClass(IBatchProcessor, JAMBDataImporter) is True
72
73    def test_parentsExist(self):
74        assert self.importer.parentsExist(None, dict()) is False
75        assert self.importer.parentsExist(None, self.site1) is True
76
77    def test_entryExists(self):
78        assert self.importer.entryExists(
79            dict(reg_no='REG_NONE'), self.site1) is False
80        assert self.importer.entryExists(
81            dict(reg_no='REG_NO_1'), self.site1) is True
82
83    def test_getParent(self):
84        parent = self.importer.getParent(None, self.site1)
85        assert parent is self.site1['jambdata']
86
87    def test_getEntry(self):
88        assert self.importer.getEntry(
89            dict(reg_no='REG_NONE'), self.site1) is None
90        assert self.importer.getEntry(
91            dict(reg_no='REG_NO_1'), self.site1) == 'Application1'
92
93    def test_addEntry(self):
94        self.importer.addEntry(
95            'New application', dict(reg_no='REG_NO_99'), self.site1)
96        assert self.site1['jambdata']['REG_NO_99'] == 'New application'
97
98    def test_delEntry(self):
99        self.importer.delEntry(dict(reg_no='REG_NO_1'), self.site1)
100        assert 'REG_NO_1' not in self.site1['jambdata'].keys()
101
102    def test_import(self):
103        # Do a real import
104        # The following modules register important components for import
105        # Registers converters...
106        grok.testing.grok('waeup.sirp.utils.converters')
107        # Registers the Applicant factory...
108        grok.testing.grok('waeup.sirp.applicants.applicants')
109
110        # Create a fake site to store datasets in...
111        site = FakeSite()
112        # The site must have a 'jambdata' entry...
113        site['jambdata'] = dict()
114        # Set the fake site as 'current' site object...
115        setSite(site)
116        self.importer.doImport(self.csv_file, HEADER_FIELDS)
117        self.assertTrue(u'91100546DD' in site['jambdata'].keys())
118
119def test_suite():
120    suite = unittest.TestSuite()
121    for testcase in [
122        JAMBDataImporterTest,
123        ]:
124        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
125                testcase
126                )
127        )
128    return suite
Note: See TracBrowser for help on using the repository browser.