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

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

Add test for import_datetime.

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