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

Last change on this file since 5649 was 5465, checked in by uli, 14 years ago

Bring coverage for these tests to 100%.

File size: 7.2 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_clear_broken_path(self):
95        table = JAMBDataTable()
96        table.importFromCSV(self.datafile)
97        path = table._datafile_path
98        table._datafile_path = 'not-existent-path'
99        table.clear()
100       
101    def test_import_datetime(self):
102        table = JAMBDataTable()
103        self.assertTrue(table.import_datetime is None)
104        table.importFromCSV(self.datafile)
105        now = datetime.now()
106        diff = now - table.import_datetime
107        self.assertTrue(diff.seconds < 5)
108        table.clear()
109        self.assertTrue(table.import_datetime is None)
110
111    def test_importer_username(self):
112        table = JAMBDataTable()
113        self.assertTrue(table.importer_username is None)
114        table.importFromCSV(self.datafile, 'manfred')
115        self.assertEqual(table.importer_username, u'manfred')
116        self.assertTrue(isinstance(table.importer_username, unicode))
117        table.importFromCSV(self.datafile, 'fred')
118        self.assertEqual(table.importer_username, u'fred')
119        table.importFromCSV(self.datafile)
120        self.assertTrue(table.importer_username is None)
121        table.clear()
122        self.assertTrue(table.importer_username is None)
123
124    def test_keys(self):
125        table = JAMBDataTable()
126        self.assertFalse('91100546DD' in table.keys())
127        table.importFromCSV(self.datafile)
128        self.assertTrue('91100546DD' in table.keys())
129        self.assertFalse('91100546DE' in table.keys())
130
131    def test_items(self):
132        table = JAMBDataTable()
133        self.assertEqual([], list(table.items()))
134        table.importFromCSV(self.datafile)
135        self.assertNotEqual([], list(table.items()))
136
137    def test_interface_compliance(self):
138        self.assertTrue(verifyClass(IJAMBDataTable, JAMBDataTable))
139        table = JAMBDataTable()
140        self.assertTrue(verifyObject(IJAMBDataTable, table))
141        table.importFromCSV(self.datafile)
142        self.assertTrue(verifyObject(IJAMBDataTable, table))
143
144class JAMBTableFunctionalTestCase(JAMBTableTestCaseBase):
145    """A test case where we check JAMB tables inside a site.
146    """
147
148    def setUp(self):
149        super(JAMBTableFunctionalTestCase, self).setUp()
150       
151        # We need at least these to register factories for a University.
152        grok.testing.grok('waeup.sirp.student')
153        grok.testing.grok('waeup.sirp.hostel')
154        grok.testing.grok('waeup.sirp.university')
155
156        self.site = University()
157        sm = LocalSiteManager(self.site)
158        self.site.setSiteManager(sm)
159        self.datacenter_dir = tempfile.mkdtemp()
160        self.datacenter = self.site['datacenter']
161        self.datacenter.setStoragePath(self.datacenter_dir)
162        # If the old datacenter already had a jambtables dir, remove it...
163        if os.path.isdir(
164            os.path.join(self.datacenter_dir, 'jambdata')
165            ):
166            shutil.rmtree(os.path.join(self.datacenter_dir, 'jambdata'))
167        return
168
169    def tearDown(self):
170        shutil.rmtree(self.tempdir)
171        shutil.rmtree(self.datacenter_dir)
172        # Unregister site...
173        setSite()
174        return
175
176    def test_createJAMBTableInSite(self):
177        table = JAMBDataTable()
178        self.assertEqual(table._data_len, 0)
179        self.assertEqual(table._temporary, False)
180        self.assertEqual(table._datafile_path, None)
181        table.importFromCSV(self.datafile)
182        return
183
184    def test_createJAMBTableOutsideSite(self):
185        clearSite(self.site)
186        table = JAMBDataTable()
187        self.assertEqual(table._data_len, 0)
188        self.assertEqual(table._temporary, False)
189        self.assertEqual(table._datafile_path, None)
190        table.importFromCSV(self.datafile)
191        return
192
193       
194def test_suite():
195    suite = unittest.TestSuite()
196    for testcase in [
197        JAMBTableTestCase,
198        JAMBTableFunctionalTestCase,
199        ]:
200        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
201                testcase
202                )
203        )
204    return suite
Note: See TracBrowser for help on using the repository browser.