source: main/waeup.kofa/trunk/src/waeup/kofa/accesscodes/tests/test_batching.py @ 9263

Last change on this file since 9263 was 9263, checked in by Henrik Bettermann, 12 years ago

Add batch processor for ac batches.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
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"""
19Tests for hostels and their UI components.
20"""
21import os
22import shutil
23import tempfile
24import grok
25import pytz
26from datetime import datetime, timedelta
27from zope.event import notify
28from zope.interface.verify import verifyClass, verifyObject
29from zope.component.hooks import setSite, clearSite
30from zope.testbrowser.testing import Browser
31from zope.security.interfaces import Unauthorized
32from zope.catalog.interfaces import ICatalog
33from zope.component import queryUtility
34from waeup.kofa.app import University
35from waeup.kofa.interfaces import IObjectHistory, IKofaPluggable, IKofaUtils
36from waeup.kofa.testing import (
37    FunctionalLayer, FunctionalTestCase, setUp, tearDown, getRootFolder)
38from waeup.kofa.accesscodes.batching import AccessCodeBatchProcessor
39
40
41BATCH_SAMPLE_DATA = open(
42    os.path.join(os.path.dirname(__file__), 'sample_batch_data.csv'),
43    'rb').read()
44
45BATCH_HEADER_FIELDS = BATCH_SAMPLE_DATA.split(
46    '\n')[0].split(',')
47
48class 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
81class 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
Note: See TracBrowser for help on using the repository browser.