source: main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/jambtables/tests/test_jambtables.py @ 11031

Last change on this file since 11031 was 5253, checked in by uli, 14 years ago

Update tests to reflect interface changes.

File size: 7.0 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 os
27import shutil
28import tempfile
29import unittest
30
31import grok
32
33from datetime import datetime
34
35from zope.interface.verify import verifyClass, verifyObject
36from zope.site import LocalSiteManager
37from zope.site.hooks import setSite, clearSite
38
39from waeup.sirp.app import University
40from waeup.sirp.datacenter import DataCenter
41from waeup.sirp.jambtables import JAMBDataTable
42from waeup.sirp.jambtables.interfaces import IJAMBDataTable
43
44
45class JAMBTableTestCaseBase(unittest.TestCase):
46
47    def setUp(self):
48        self.tempdir = tempfile.mkdtemp()
49        self.datafile = os.path.join(
50            self.tempdir, 'jambsample.csv'
51            )
52        open(self.datafile, 'wb').write(
53            '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' +
54            '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' +
55            '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' +
56            '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'
57            )
58        setSite()
59        return
60
61    def tearDown(self):
62        shutil.rmtree(self.tempdir)
63        return
64
65class JAMBTableTestCase(JAMBTableTestCaseBase):
66    def test_createJAMBTable(self):
67        table = JAMBDataTable()
68        self.assertEqual(table._data_len, 0)
69        self.assertEqual(table._temporary, False)
70        self.assertEqual(table._datafile_path, None)
71        return
72
73    def test_iterator(self):
74        table = JAMBDataTable()
75        self.assertEqual(len(list(table)), 0)
76        table.importFromCSV(self.datafile)
77        self.assertEqual(len(list(table)), 3)
78
79    def test_import(self):
80        table = JAMBDataTable()
81        self.assertEqual(table.import_datetime, None)
82        table.importFromCSV(self.datafile)
83        self.assertNotEqual(table.import_datetime, None)
84
85    def test_clear(self):
86        table = JAMBDataTable()
87        table.importFromCSV(self.datafile)
88        path = table._datafile_path
89        table.clear()
90        exists = os.path.exists(path)
91        self.assertEqual(exists, False)
92        self.assertEqual(0, len(list(table)))
93
94    def test_import_datetime(self):
95        table = JAMBDataTable()
96        self.assertTrue(table.import_datetime is None)
97        table.importFromCSV(self.datafile)
98        now = datetime.now()
99        diff = now - table.import_datetime
100        self.assertTrue(diff.seconds < 5)
101        table.clear()
102        self.assertTrue(table.import_datetime is None)
103
104    def test_importer_username(self):
105        table = JAMBDataTable()
106        self.assertTrue(table.importer_username is None)
107        table.importFromCSV(self.datafile, 'manfred')
108        self.assertEqual(table.importer_username, u'manfred')
109        self.assertTrue(isinstance(table.importer_username, unicode))
110        table.importFromCSV(self.datafile, 'fred')
111        self.assertEqual(table.importer_username, u'fred')
112        table.importFromCSV(self.datafile)
113        self.assertTrue(table.importer_username is None)
114        table.clear()
115        self.assertTrue(table.importer_username is None)
116
117    def test_keys(self):
118        table = JAMBDataTable()
119        self.assertFalse('91100546DD' in table.keys())
120        table.importFromCSV(self.datafile)
121        self.assertTrue('91100546DD' in table.keys())
122        self.assertFalse('91100546DE' in table.keys())
123
124    def test_items(self):
125        table = JAMBDataTable()
126        self.assertEqual([], list(table.items()))
127        table.importFromCSV(self.datafile)
128        self.assertNotEqual([], list(table.items()))
129
130    def test_interface_compliance(self):
131        self.assertTrue(verifyClass(IJAMBDataTable, JAMBDataTable))
132        table = JAMBDataTable()
133        self.assertTrue(verifyObject(IJAMBDataTable, table))
134        table.importFromCSV(self.datafile)
135        self.assertTrue(verifyObject(IJAMBDataTable, table))
136
137class JAMBTableFunctionalTestCase(JAMBTableTestCaseBase):
138    """A test case where we check JAMB tables inside a site.
139    """
140
141    def setUp(self):
142        super(JAMBTableFunctionalTestCase, self).setUp()
143       
144        # We need at least these to register factories for a University.
145        grok.testing.grok('waeup.sirp.student')
146        grok.testing.grok('waeup.sirp.hostel')
147        grok.testing.grok('waeup.sirp.university')
148
149        self.site = University()
150        sm = LocalSiteManager(self.site)
151        self.site.setSiteManager(sm)
152        self.datacenter_dir = tempfile.mkdtemp()
153        self.datacenter = self.site['datacenter']
154        self.datacenter.setStoragePath(self.datacenter_dir)
155        # If the old datacenter already had a jambtables dir, remove it...
156        if os.path.isdir(
157            os.path.join(self.datacenter_dir, 'jambdata')
158            ):
159            shutil.rmtree(os.path.join(self.datacenter_dir, 'jambdata'))
160        return
161
162    def tearDown(self):
163        shutil.rmtree(self.tempdir)
164        shutil.rmtree(self.datacenter_dir)
165        # Unregister site...
166        setSite()
167        return
168
169    def test_createJAMBTableInSite(self):
170        table = JAMBDataTable()
171        self.assertEqual(table._data_len, 0)
172        self.assertEqual(table._temporary, False)
173        self.assertEqual(table._datafile_path, None)
174        table.importFromCSV(self.datafile)
175        return
176
177    def test_createJAMBTableOutsideSite(self):
178        clearSite(self.site)
179        table = JAMBDataTable()
180        self.assertEqual(table._data_len, 0)
181        self.assertEqual(table._temporary, False)
182        self.assertEqual(table._datafile_path, None)
183        table.importFromCSV(self.datafile)
184        return
185
186       
187def test_suite():
188    suite = unittest.TestSuite()
189    for testcase in [
190        JAMBTableTestCase,
191        JAMBTableFunctionalTestCase,
192        ]:
193        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
194                testcase
195                )
196        )
197    return suite
Note: See TracBrowser for help on using the repository browser.