source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_batching.py @ 7218

Last change on this file since 7218 was 7193, checked in by Henrik Bettermann, 13 years ago

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1## $Id: test_batching.py 7193 2011-11-25 07:21:29Z 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"""Unit tests for applicants-related data importers.
19"""
20import os
21import shutil
22import tempfile
23import unittest
24from zope.component.hooks import setSite, clearSite
25from zope.interface.verify import verifyClass, verifyObject
26
27from waeup.sirp.app import University
28from waeup.sirp.applicants.batching import ApplicantsContainerImporter
29from waeup.sirp.applicants.container import ApplicantsContainer
30from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
31from waeup.sirp.interfaces import IBatchProcessor
32
33
34# Sample data we can use in tests...
35APPS_CONTAINER_SAMPLE_DATA = open(
36    os.path.join(os.path.dirname(__file__), 'sample_container_data.csv'),
37    'rb').read()
38
39# The header fields of the above CSV snippet
40APPS_CONTAINER_HEADER_FIELDS = APPS_CONTAINER_SAMPLE_DATA.split(
41    '\n')[0].split(',')
42
43class ApplicantsContainerImporterTest(FunctionalTestCase):
44
45    layer = FunctionalLayer
46
47    def setUp(self):
48        super(ApplicantsContainerImporterTest, self).setUp()
49
50        # Setup a sample site for each test
51        app = University()
52        self.dc_root = tempfile.mkdtemp()
53        app['datacenter'].setStoragePath(self.dc_root)
54
55        # Prepopulate the ZODB...
56        self.getRootFolder()['app'] = app
57        self.app = self.getRootFolder()['app']
58        self.container = ApplicantsContainer()
59        self.container.code = u'dp2011'
60        self.app['applicants']['dp2011'] = self.container
61
62        self.importer = ApplicantsContainerImporter()
63        self.workdir = tempfile.mkdtemp()
64        self.csv_file = os.path.join(self.workdir, 'sampledata.csv')
65        open(self.csv_file, 'wb').write(APPS_CONTAINER_SAMPLE_DATA)
66        setSite(self.app)
67        return
68
69    def tearDown(self):
70        super(ApplicantsContainerImporterTest, self).tearDown()
71        shutil.rmtree(self.workdir)
72        shutil.rmtree(self.dc_root)
73        clearSite()
74        return
75
76    def test_interface(self):
77        # Make sure we fulfill the interface contracts.
78        assert verifyObject(IBatchProcessor, self.importer) is True
79        assert verifyClass(
80            IBatchProcessor, ApplicantsContainerImporter) is True
81
82    def test_parentsExist(self):
83        assert self.importer.parentsExist(None, dict()) is False
84        assert self.importer.parentsExist(None, self.app) is True
85
86    def test_entryExists(self):
87        assert self.importer.entryExists(
88            dict(code='REG_NONE'), self.app) is False
89        assert self.importer.entryExists(
90            dict(code='dp2011'), self.app) is True
91
92    def test_getParent(self):
93        parent = self.importer.getParent(None, self.app)
94        assert parent is self.app['applicants']
95
96    def test_getEntry(self):
97        assert self.importer.getEntry(
98            dict(code='REG_NONE'), self.app) is None
99        assert self.importer.getEntry(
100            dict(code='dp2011'), self.app) is self.container
101
102    def test_addEntry(self):
103        self.importer.addEntry(
104            'New application', dict(code='dp2012'), self.app)
105        assert self.app['applicants']['dp2012'] == 'New application'
106
107    def test_delEntry(self):
108        self.importer.delEntry(dict(code='dp2011'), self.app)
109        assert 'dp2011' not in self.app['applicants'].keys()
110
111    def test_import(self):
112        # Do a real import
113        # see local sample_container.csv file for input
114        num, num_warns, fin_file, fail_file = self.importer.doImport(
115            self.csv_file, APPS_CONTAINER_HEADER_FIELDS)
116        avail_containers = [x for x in self.app['applicants'].keys()]
117        self.assertTrue(u'CODE1' in avail_containers)
118        self.assertTrue(u'CODE2' in avail_containers)
119        shutil.rmtree(os.path.dirname(fin_file))
120
121def test_suite():
122    suite = unittest.TestSuite()
123    for testcase in [
124        ApplicantsContainerImporterTest,
125        ]:
126        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
127                testcase
128                )
129        )
130    return suite
Note: See TracBrowser for help on using the repository browser.