source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/jambtables/tests/test_jambtables.py @ 6002

Last change on this file since 6002 was 5802, checked in by uli, 14 years ago

Use clearSite when tearing down tests.

File size: 7.1 KB
Line 
1##
2## test_jambtables.py
3## Login : <uli@pu.smp.net>
4## Started on  Thu Jun 24 08:08:12 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"""
23Tests for JAMB tables.
24
25"""
26import logging
27import os
28import shutil
29import tempfile
30import unittest
31
32import grok
33
34from cStringIO import StringIO
35from datetime import datetime
36
37from zope.interface.verify import verifyClass, verifyObject
38from zope.site import LocalSiteManager
39from zope.site.hooks import setSite, clearSite
40from zope.site.testing import createSiteManager
41
42from waeup.sirp.app import University
43from waeup.sirp.datacenter import DataCenter
44from waeup.sirp.testing import WAeUPSIRPUnitTestLayer
45from waeup.sirp.applicants.jambtables import (
46    JAMBDataPlugin, JAMBDataRoot, get_jambdata,
47    )
48from waeup.sirp.applicants.jambtables.interfaces import (
49    IJAMBDataRoot,
50    )
51from waeup.sirp.applicants import Applicant
52from waeup.sirp.applicants.container import ApplicantsContainer
53
54
55class FakeSite(dict):
56    pass
57
58class JAMBTableTestCaseBase(unittest.TestCase):
59
60    def setUp(self):
61        self.tempdir = tempfile.mkdtemp()
62        self.datafile = os.path.join(
63            self.tempdir, 'jambsample.csv'
64            )
65        open(self.datafile, 'wb').write(
66            '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\n' +
67            '91100546DD,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\n' +
68            '91111834CC,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\n' +
69            '91109351AC,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\n'
70            )
71        setSite()
72        return
73
74    def tearDown(self):
75        shutil.rmtree(self.tempdir)
76        return
77
78class JAMBDataRootTestCase(unittest.TestCase):
79
80    def test_interfaces(self):
81        # Make sure the correct interfaces are implemented.
82        self.assertTrue(
83            verifyClass(IJAMBDataRoot, JAMBDataRoot)
84            )
85        self.assertTrue(
86            verifyObject(IJAMBDataRoot, JAMBDataRoot())
87            )
88        return
89
90class JAMBDataPluginTestCase(unittest.TestCase):
91    def create_logger(self):
92        # create a logger suitable for local tests.
93        test_logger = logging.getLogger('waeup.sirp.jambdata.testlogger')
94        log = StringIO()
95        handler = logging.StreamHandler(log)
96        handler.setLevel(logging.DEBUG)
97        test_logger.addHandler(handler)
98        test_logger.setLevel(logging.DEBUG)
99        self.logger = test_logger
100        self.log = log
101        self.handler = handler
102        return self.logger
103
104    def remove_logger(self):
105        del self.handler
106        del self.logger
107        del self.log
108        pass
109
110    def get_log(self):
111        self.log.seek(0)
112        return self.log.read()
113
114    def setUp(self):
115        self.create_logger()
116        return
117
118    def tearDown(self):
119        self.remove_logger()
120        return
121
122    # Real tests start here...
123    def test_pluginsetup(self):
124        # Make sure we can add ApplicantsRoot to sites.
125        site = FakeSite()
126        plugin = JAMBDataPlugin()
127        plugin.setup(site, 'blah', self.logger)
128        self.assertTrue('jambdata' in site.keys())
129        log = self.get_log()
130        self.assertTrue('JAMBDataPlugin: Installed JAMB data root.' in log)
131        return
132
133    def test_update_new(self):
134        # Run update on a site without applicants root.
135        site = FakeSite()
136        plugin = JAMBDataPlugin()
137        plugin.update(site, 'blah', self.logger)
138        self.assertTrue('jambdata' in site.keys())
139        log = self.get_log()
140        self.assertTrue('Updating site at <Unnamed Site>' in log)
141        self.assertTrue('Installed JAMB data root.' in log)
142        return
143
144    def test_update_outdated(self):
145        # Run update on a site with outdated applicants root.
146        site = FakeSite()
147        root = object() # # This is not a proper applicants root
148        site['jambdata'] = root
149        plugin = JAMBDataPlugin()
150        plugin.update(site, 'blah', self.logger)
151        self.assertTrue(site['jambdata'] is not root)
152        self.assertTrue(isinstance(site['jambdata'], JAMBDataRoot))
153        log = self.get_log()
154        self.assertTrue('Outdated JAMB data folder detected' in log)
155        self.assertTrue('Updating site at <Unnamed Site>' in log)
156        self.assertTrue('Installed JAMB data root.' in log)
157        return
158
159    def test_update_uptodate(self):
160        # Run update on a site with proper applicants root.
161        site = FakeSite()
162        root = JAMBDataRoot()
163        site['jambdata'] = root
164        plugin = JAMBDataPlugin()
165        plugin.update(site, 'blah', self.logger)
166        self.assertTrue(site['jambdata'] is root)
167        log = self.get_log()
168        self.assertTrue('Updating site at <Unnamed Site>' in log)
169        self.assertTrue('Nothing to do' in log)
170        return
171
172    def test_update_log(self):
173        # Check that sitename is used in log messages on updates.
174        site = FakeSite()
175        site.__name__ = 'my_site'
176        plugin = JAMBDataPlugin()
177        plugin.update(site, 'blah', self.logger)
178        log = self.get_log()
179        self.assertTrue('Updating site at my_site.' in log)
180        return
181
182class GetJAMBDataTestCase(unittest.TestCase):
183
184    layer = WAeUPSIRPUnitTestLayer
185   
186    def setUp(self):
187        self.app = University()
188        # Fill up some JAMB data...
189        self.appl1 = Applicant()
190        self.appl2 = Applicant()
191        self.app['jambdata']['91100546DD'] = self.appl1
192        self.app['jambdata']['91100546DE'] = self.appl2
193        createSiteManager(self.app, setsite=True)
194        return
195
196    def tearDown(self):
197        clearSite()
198        return
199
200    def test_contained(self):
201        self.assertEqual(get_jambdata('91100546DD'), self.appl1)
202        return
203
204    def test_not_contained(self):
205        self.assertEqual(get_jambdata('not-existing'), None)
206        return
207
208def test_suite():
209    suite = unittest.TestSuite()
210    for testcase in [
211        JAMBDataRootTestCase, JAMBDataPluginTestCase, GetJAMBDataTestCase,
212        ]:
213        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
214                testcase
215                )
216        )
217    return suite
Note: See TracBrowser for help on using the repository browser.