1 | ## $Id: test_batching.py 9265 2012-10-01 21:18:48Z 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 ( |
---|
39 | AccessCodeBatchProcessor, AccessCodeProcessor) |
---|
40 | |
---|
41 | |
---|
42 | BATCH_SAMPLE_DATA = open( |
---|
43 | os.path.join(os.path.dirname(__file__), 'sample_batch_data.csv'), |
---|
44 | 'rb').read() |
---|
45 | |
---|
46 | BATCH_HEADER_FIELDS = BATCH_SAMPLE_DATA.split( |
---|
47 | '\n')[0].split(',') |
---|
48 | |
---|
49 | AC_SAMPLE_DATA = open( |
---|
50 | os.path.join(os.path.dirname(__file__), 'sample_ac_data.csv'), |
---|
51 | 'rb').read() |
---|
52 | |
---|
53 | AC_HEADER_FIELDS = AC_SAMPLE_DATA.split( |
---|
54 | '\n')[0].split(',') |
---|
55 | |
---|
56 | class ACFullSetup(FunctionalTestCase): |
---|
57 | |
---|
58 | def setUp(self): |
---|
59 | super(ACFullSetup, self).setUp() |
---|
60 | |
---|
61 | # Setup a sample site for each test |
---|
62 | app = University() |
---|
63 | self.dc_root = tempfile.mkdtemp() |
---|
64 | app['datacenter'].setStoragePath(self.dc_root) |
---|
65 | |
---|
66 | # Prepopulate the ZODB... |
---|
67 | self.getRootFolder()['app'] = app |
---|
68 | # we add the site immediately after creation to the |
---|
69 | # ZODB. Catalogs and other local utilities are not setup |
---|
70 | # before that step. |
---|
71 | self.app = self.getRootFolder()['app'] |
---|
72 | # Set site here. Some of the following setup code might need |
---|
73 | # to access grok.getSite() and should get our new app then |
---|
74 | setSite(app) |
---|
75 | |
---|
76 | # Put the prepopulated site into test ZODB and prepare test |
---|
77 | # browser |
---|
78 | self.browser = Browser() |
---|
79 | self.browser.handleErrors = False |
---|
80 | |
---|
81 | self.logfile = os.path.join( |
---|
82 | self.app['datacenter'].storage, 'logs', 'accesscodes.log') |
---|
83 | self.workdir = tempfile.mkdtemp() |
---|
84 | |
---|
85 | def tearDown(self): |
---|
86 | super(ACFullSetup, self).tearDown() |
---|
87 | clearSite() |
---|
88 | shutil.rmtree(self.dc_root) |
---|
89 | |
---|
90 | class ACBatchProcessorTest(ACFullSetup): |
---|
91 | |
---|
92 | layer = FunctionalLayer |
---|
93 | |
---|
94 | def test_import(self): |
---|
95 | self.processor = AccessCodeBatchProcessor() |
---|
96 | self.csv_file = os.path.join(self.workdir, 'sample_batch_data.csv') |
---|
97 | open(self.csv_file, 'wb').write(BATCH_SAMPLE_DATA) |
---|
98 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
99 | self.csv_file, BATCH_HEADER_FIELDS) |
---|
100 | self.assertEqual(num_warns,0) |
---|
101 | self.assertEqual(len(self.app['accesscodes'].keys()), 7) |
---|
102 | self.assertEqual(self.app['accesscodes']['CLR-1'].num,1) |
---|
103 | logcontent = open(self.logfile).read() |
---|
104 | self.assertTrue( |
---|
105 | 'system - AccessCodeBatch Processor - CLR-1 - ' |
---|
106 | 'Batch updated: num=1, creator=system, entry_num=3, ' |
---|
107 | 'creation_date=2012-04-28 07:28:48.719026+00:00, ' |
---|
108 | 'prefix=CLR, cost=0.0, disabled_num=0' |
---|
109 | in logcontent) |
---|
110 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
111 | return |
---|
112 | |
---|
113 | class ACProcessorTest(ACFullSetup): |
---|
114 | |
---|
115 | layer = FunctionalLayer |
---|
116 | |
---|
117 | def test_import(self): |
---|
118 | self.processor = AccessCodeBatchProcessor() |
---|
119 | self.csv_file = os.path.join(self.workdir, 'sample_batch_data.csv') |
---|
120 | open(self.csv_file, 'wb').write(BATCH_SAMPLE_DATA) |
---|
121 | self.processor.doImport(self.csv_file, BATCH_HEADER_FIELDS) |
---|
122 | |
---|
123 | self.processor = AccessCodeProcessor() |
---|
124 | self.csv_file = os.path.join(self.workdir, 'sample_ac_data.csv') |
---|
125 | open(self.csv_file, 'wb').write(AC_SAMPLE_DATA) |
---|
126 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
127 | self.csv_file, AC_HEADER_FIELDS) |
---|
128 | self.assertEqual(num_warns,0) |
---|
129 | self.assertEqual( |
---|
130 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].batch_serial, 33) |
---|
131 | self.assertEqual( |
---|
132 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].random_num, '1625368961') |
---|
133 | self.assertEqual( |
---|
134 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].state, 'used') |
---|
135 | logcontent = open(self.logfile).read() |
---|
136 | self.assertTrue( |
---|
137 | 'INFO - system - AccessCode Processor - CLR-1-1625368961 - ' |
---|
138 | 'AC updated: state=used, history=2012-07-11 14:51:56 UTC - ' |
---|
139 | 'initialized by Manager||2012-07-11 14:55:42 UTC - ' |
---|
140 | 'invalidated by Manager, batch_serial=33, ' |
---|
141 | 'random_num=1625368961, cost=100.0, owner=K1000009' |
---|
142 | in logcontent) |
---|
143 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
144 | return |
---|