[9263] | 1 | ## $Id: test_batching.py 10448 2013-08-05 06:07:53Z 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) |
---|
[9265] | 38 | from waeup.kofa.accesscodes.batching import ( |
---|
| 39 | AccessCodeBatchProcessor, AccessCodeProcessor) |
---|
[9263] | 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 | |
---|
[9265] | 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 | |
---|
[9266] | 56 | AC_SAMPLE_DATA_UPDATE = open( |
---|
| 57 | os.path.join(os.path.dirname(__file__), 'sample_ac_data_update.csv'), |
---|
| 58 | 'rb').read() |
---|
| 59 | |
---|
| 60 | AC_HEADER_FIELDS_UPDATE = AC_SAMPLE_DATA_UPDATE.split( |
---|
| 61 | '\n')[0].split(',') |
---|
| 62 | |
---|
[9263] | 63 | class ACFullSetup(FunctionalTestCase): |
---|
| 64 | |
---|
| 65 | def setUp(self): |
---|
| 66 | super(ACFullSetup, self).setUp() |
---|
| 67 | |
---|
| 68 | # Setup a sample site for each test |
---|
| 69 | app = University() |
---|
| 70 | self.dc_root = tempfile.mkdtemp() |
---|
| 71 | app['datacenter'].setStoragePath(self.dc_root) |
---|
| 72 | |
---|
| 73 | # Prepopulate the ZODB... |
---|
| 74 | self.getRootFolder()['app'] = app |
---|
| 75 | # we add the site immediately after creation to the |
---|
| 76 | # ZODB. Catalogs and other local utilities are not setup |
---|
| 77 | # before that step. |
---|
| 78 | self.app = self.getRootFolder()['app'] |
---|
| 79 | # Set site here. Some of the following setup code might need |
---|
| 80 | # to access grok.getSite() and should get our new app then |
---|
| 81 | setSite(app) |
---|
| 82 | |
---|
| 83 | # Put the prepopulated site into test ZODB and prepare test |
---|
| 84 | # browser |
---|
| 85 | self.browser = Browser() |
---|
| 86 | self.browser.handleErrors = False |
---|
| 87 | |
---|
| 88 | self.logfile = os.path.join( |
---|
| 89 | self.app['datacenter'].storage, 'logs', 'accesscodes.log') |
---|
[9265] | 90 | self.workdir = tempfile.mkdtemp() |
---|
[9263] | 91 | |
---|
| 92 | def tearDown(self): |
---|
| 93 | super(ACFullSetup, self).tearDown() |
---|
| 94 | clearSite() |
---|
| 95 | shutil.rmtree(self.dc_root) |
---|
[9283] | 96 | shutil.rmtree(self.workdir) |
---|
[9263] | 97 | |
---|
| 98 | class ACBatchProcessorTest(ACFullSetup): |
---|
| 99 | |
---|
| 100 | layer = FunctionalLayer |
---|
| 101 | |
---|
| 102 | def test_import(self): |
---|
| 103 | self.processor = AccessCodeBatchProcessor() |
---|
| 104 | self.csv_file = os.path.join(self.workdir, 'sample_batch_data.csv') |
---|
| 105 | open(self.csv_file, 'wb').write(BATCH_SAMPLE_DATA) |
---|
| 106 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
| 107 | self.csv_file, BATCH_HEADER_FIELDS) |
---|
| 108 | self.assertEqual(num_warns,0) |
---|
[10448] | 109 | self.assertEqual(len(self.app['accesscodes'].keys()), 8) |
---|
[9263] | 110 | self.assertEqual(self.app['accesscodes']['CLR-1'].num,1) |
---|
| 111 | logcontent = open(self.logfile).read() |
---|
| 112 | self.assertTrue( |
---|
[9706] | 113 | 'system - AccessCodeBatch Processor - sample_batch_data - ' |
---|
| 114 | 'CLR-1 - updated: ' |
---|
| 115 | 'num=1, creator=system, used_num=1, ' |
---|
[9266] | 116 | 'entry_num=3, creation_date=2012-04-28 07:28:48.719026+00:00, ' |
---|
[9263] | 117 | 'prefix=CLR, cost=0.0, disabled_num=0' |
---|
| 118 | in logcontent) |
---|
| 119 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
[9265] | 120 | return |
---|
| 121 | |
---|
| 122 | class ACProcessorTest(ACFullSetup): |
---|
| 123 | |
---|
| 124 | layer = FunctionalLayer |
---|
| 125 | |
---|
[9266] | 126 | def test_import_create(self): |
---|
[9265] | 127 | self.processor = AccessCodeBatchProcessor() |
---|
| 128 | self.csv_file = os.path.join(self.workdir, 'sample_batch_data.csv') |
---|
| 129 | open(self.csv_file, 'wb').write(BATCH_SAMPLE_DATA) |
---|
[9283] | 130 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
| 131 | self.csv_file, BATCH_HEADER_FIELDS) |
---|
| 132 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
[9265] | 133 | |
---|
| 134 | self.processor = AccessCodeProcessor() |
---|
| 135 | self.csv_file = os.path.join(self.workdir, 'sample_ac_data.csv') |
---|
| 136 | open(self.csv_file, 'wb').write(AC_SAMPLE_DATA) |
---|
| 137 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
| 138 | self.csv_file, AC_HEADER_FIELDS) |
---|
[9266] | 139 | self.assertEqual(num_warns,3) |
---|
| 140 | fail_file = open(fail_file).read() |
---|
| 141 | self.assertTrue( |
---|
| 142 | 'HOS-1-0007044547,HOS,anything,state: not allowed' in fail_file) |
---|
| 143 | self.assertTrue( |
---|
| 144 | 'HOS-1-555,HOS,anything,workflow: not allowed' in fail_file) |
---|
| 145 | self.assertTrue( |
---|
| 146 | 'HOS-1-666,HOS,anything,transition: not allowed' in fail_file) |
---|
[9265] | 147 | self.assertEqual( |
---|
[9266] | 148 | self.app['accesscodes']['HOS-1']['HOS-1-98769876'].state, 'used') |
---|
| 149 | self.assertEqual( |
---|
[9271] | 150 | self.app['accesscodes']['HOS-1']['HOS-1-76254765'].state, |
---|
| 151 | 'initialized') |
---|
[9266] | 152 | self.assertEqual( |
---|
[9271] | 153 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].batch_serial, |
---|
| 154 | 33) |
---|
[9265] | 155 | self.assertEqual( |
---|
[9271] | 156 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].random_num, |
---|
| 157 | '1625368961') |
---|
[9265] | 158 | self.assertEqual( |
---|
| 159 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].state, 'used') |
---|
[9266] | 160 | self.assertMatches( |
---|
| 161 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].history, |
---|
| 162 | "<YYYY-MM-DD hh:mm:ss> UTC - initialized by system|" |
---|
| 163 | "|<YYYY-MM-DD hh:mm:ss> UTC - state 'used' set by system") |
---|
| 164 | self.assertMatches( |
---|
| 165 | self.app['accesscodes']['HOS-1']['HOS-1-98769876'].history, |
---|
| 166 | "<YYYY-MM-DD hh:mm:ss> UTC - initialized by system|" |
---|
| 167 | "|<YYYY-MM-DD hh:mm:ss> UTC - used by system") |
---|
[9265] | 168 | logcontent = open(self.logfile).read() |
---|
| 169 | self.assertTrue( |
---|
[9706] | 170 | 'INFO - system - AccessCode Processor - sample_ac_data - ' |
---|
| 171 | 'CLR-1-1625368961 - updated: state=used, ' |
---|
| 172 | 'batch_serial=33, random_num=1625368961, ' |
---|
[9266] | 173 | 'cost=100.0, owner=K1000009' |
---|
[9265] | 174 | in logcontent) |
---|
| 175 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
[9266] | 176 | return |
---|
| 177 | |
---|
| 178 | def test_import_update(self): |
---|
| 179 | self.processor = AccessCodeBatchProcessor() |
---|
| 180 | self.csv_file = os.path.join(self.workdir, 'sample_batch_data.csv') |
---|
| 181 | open(self.csv_file, 'wb').write(BATCH_SAMPLE_DATA) |
---|
[9283] | 182 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
| 183 | self.csv_file, BATCH_HEADER_FIELDS) |
---|
| 184 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
[9266] | 185 | |
---|
| 186 | self.processor = AccessCodeProcessor() |
---|
| 187 | self.csv_file = os.path.join(self.workdir, 'sample_ac_data.csv') |
---|
| 188 | open(self.csv_file, 'wb').write(AC_SAMPLE_DATA) |
---|
| 189 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
| 190 | self.csv_file, AC_HEADER_FIELDS) |
---|
[9283] | 191 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
[9266] | 192 | |
---|
[9267] | 193 | self.assertEqual(self.app['accesscodes']['HOS-1'].used_num, 1) |
---|
| 194 | self.assertEqual(self.app['accesscodes']['HOS-1'].disabled_num, 0) |
---|
| 195 | self.assertEqual(self.app['accesscodes']['CLR-1'].used_num, 1) |
---|
| 196 | self.assertEqual(self.app['accesscodes']['CLR-1'].disabled_num, 0) |
---|
| 197 | |
---|
[9266] | 198 | self.csv_file = os.path.join(self.workdir, 'sample_ac_data_update.csv') |
---|
| 199 | open(self.csv_file, 'wb').write(AC_SAMPLE_DATA_UPDATE) |
---|
| 200 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
| 201 | self.csv_file, AC_HEADER_FIELDS_UPDATE, 'update') |
---|
| 202 | self.assertEqual(num_warns,0) |
---|
| 203 | self.assertEqual( |
---|
[9271] | 204 | self.app['accesscodes']['HOS-1']['HOS-1-98769876'].state, |
---|
| 205 | 'disabled') |
---|
[9266] | 206 | self.assertEqual( |
---|
[9271] | 207 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].state, |
---|
| 208 | 'disabled') |
---|
[9266] | 209 | self.assertMatches( |
---|
| 210 | self.app['accesscodes']['CLR-1']['CLR-1-1625368961'].history, |
---|
| 211 | "<YYYY-MM-DD hh:mm:ss> UTC - initialized by system|" |
---|
| 212 | "|<YYYY-MM-DD hh:mm:ss> UTC - state 'used' set by system|" |
---|
| 213 | "|<YYYY-MM-DD hh:mm:ss> UTC - state 'disabled' set by system") |
---|
| 214 | self.assertMatches( |
---|
| 215 | self.app['accesscodes']['HOS-1']['HOS-1-98769876'].history, |
---|
| 216 | "<YYYY-MM-DD hh:mm:ss> UTC - initialized by system|" |
---|
| 217 | "|<YYYY-MM-DD hh:mm:ss> UTC - used by system|" |
---|
| 218 | "|<YYYY-MM-DD hh:mm:ss> UTC - disabled by system") |
---|
[9267] | 219 | # When importing transitions the counters are properly set. |
---|
| 220 | self.assertEqual(self.app['accesscodes']['HOS-1'].used_num, 0) |
---|
| 221 | self.assertEqual(self.app['accesscodes']['HOS-1'].disabled_num, 1) |
---|
| 222 | # When importing states the counters remain unchanged. |
---|
| 223 | self.assertEqual(self.app['accesscodes']['CLR-1'].used_num, 1) |
---|
| 224 | self.assertEqual(self.app['accesscodes']['CLR-1'].disabled_num, 0) |
---|
[9283] | 225 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
[9271] | 226 | return |
---|