1 | ## $Id: test_batching.py 9263 2012-10-01 06:55:04Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | Tests for hostels and their UI components. |
---|
20 | """ |
---|
21 | import os |
---|
22 | import shutil |
---|
23 | import tempfile |
---|
24 | import grok |
---|
25 | import pytz |
---|
26 | from datetime import datetime, timedelta |
---|
27 | from zope.event import notify |
---|
28 | from zope.interface.verify import verifyClass, verifyObject |
---|
29 | from zope.component.hooks import setSite, clearSite |
---|
30 | from zope.testbrowser.testing import Browser |
---|
31 | from zope.security.interfaces import Unauthorized |
---|
32 | from zope.catalog.interfaces import ICatalog |
---|
33 | from zope.component import queryUtility |
---|
34 | from waeup.kofa.app import University |
---|
35 | from waeup.kofa.interfaces import IObjectHistory, IKofaPluggable, IKofaUtils |
---|
36 | from waeup.kofa.testing import ( |
---|
37 | FunctionalLayer, FunctionalTestCase, setUp, tearDown, getRootFolder) |
---|
38 | from waeup.kofa.accesscodes.batching import AccessCodeBatchProcessor |
---|
39 | |
---|
40 | |
---|
41 | BATCH_SAMPLE_DATA = open( |
---|
42 | os.path.join(os.path.dirname(__file__), 'sample_batch_data.csv'), |
---|
43 | 'rb').read() |
---|
44 | |
---|
45 | BATCH_HEADER_FIELDS = BATCH_SAMPLE_DATA.split( |
---|
46 | '\n')[0].split(',') |
---|
47 | |
---|
48 | class ACFullSetup(FunctionalTestCase): |
---|
49 | |
---|
50 | def setUp(self): |
---|
51 | super(ACFullSetup, self).setUp() |
---|
52 | |
---|
53 | # Setup a sample site for each test |
---|
54 | app = University() |
---|
55 | self.dc_root = tempfile.mkdtemp() |
---|
56 | app['datacenter'].setStoragePath(self.dc_root) |
---|
57 | |
---|
58 | # Prepopulate the ZODB... |
---|
59 | self.getRootFolder()['app'] = app |
---|
60 | # we add the site immediately after creation to the |
---|
61 | # ZODB. Catalogs and other local utilities are not setup |
---|
62 | # before that step. |
---|
63 | self.app = self.getRootFolder()['app'] |
---|
64 | # Set site here. Some of the following setup code might need |
---|
65 | # to access grok.getSite() and should get our new app then |
---|
66 | setSite(app) |
---|
67 | |
---|
68 | # Put the prepopulated site into test ZODB and prepare test |
---|
69 | # browser |
---|
70 | self.browser = Browser() |
---|
71 | self.browser.handleErrors = False |
---|
72 | |
---|
73 | self.logfile = os.path.join( |
---|
74 | self.app['datacenter'].storage, 'logs', 'accesscodes.log') |
---|
75 | |
---|
76 | def tearDown(self): |
---|
77 | super(ACFullSetup, self).tearDown() |
---|
78 | clearSite() |
---|
79 | shutil.rmtree(self.dc_root) |
---|
80 | |
---|
81 | class ACBatchProcessorTest(ACFullSetup): |
---|
82 | |
---|
83 | layer = FunctionalLayer |
---|
84 | |
---|
85 | def test_import(self): |
---|
86 | self.processor = AccessCodeBatchProcessor() |
---|
87 | self.workdir = tempfile.mkdtemp() |
---|
88 | self.csv_file = os.path.join(self.workdir, 'sample_batch_data.csv') |
---|
89 | open(self.csv_file, 'wb').write(BATCH_SAMPLE_DATA) |
---|
90 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
91 | self.csv_file, BATCH_HEADER_FIELDS) |
---|
92 | self.assertEqual(num_warns,0) |
---|
93 | self.assertEqual(len(self.app['accesscodes'].keys()), 7) |
---|
94 | self.assertEqual(self.app['accesscodes']['CLR-1'].num,1) |
---|
95 | logcontent = open(self.logfile).read() |
---|
96 | self.assertTrue( |
---|
97 | 'system - AccessCodeBatch Processor - CLR-1 - ' |
---|
98 | 'Batch updated: num=1, creator=system, entry_num=3, ' |
---|
99 | 'creation_date=2012-04-28 07:28:48.719026+00:00, ' |
---|
100 | 'prefix=CLR, cost=0.0, disabled_num=0' |
---|
101 | in logcontent) |
---|
102 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
103 | return |
---|