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

Last change on this file since 6541 was 6441, checked in by uli, 14 years ago

Clean up file in tempdir lingering around after tests.

File size: 4.9 KB
Line 
1##
2## test_batching.py
3## Login : <uli@pu.smp.net>
4## Started on  Tue Aug 24 02:04:44 2010 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2010 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""Unit tests for applicants-related data importers.
23"""
24import grok
25import os
26import shutil
27import tempfile
28import unittest
29from zope.app.testing.functional import FunctionalTestCase
30from zope.component.hooks import setSite, clearSite
31from zope.interface.verify import verifyClass, verifyObject
32
33from waeup.sirp.app import University
34from waeup.sirp.applicants.batching import ApplicantsContainerImporter
35from waeup.sirp.applicants.container import ApplicantsContainer
36from waeup.sirp.applicants.interfaces import IApplicantsContainer
37from waeup.sirp.interfaces import DuplicationError
38from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module
39from waeup.sirp.interfaces import IBatchProcessor
40
41
42# Sample data we can use in tests...
43APPS_CONTAINER_SAMPLE_DATA = open(
44    os.path.join(os.path.dirname(__file__), 'sample_container_data.csv'),
45    'rb').read()
46
47# The header fields of the above CSV snippet
48APPS_CONTAINER_HEADER_FIELDS = APPS_CONTAINER_SAMPLE_DATA.split(
49    '\n')[0].split(',')
50
51class ApplicantsContainerImporterTest(FunctionalTestCase):
52
53    layer = FunctionalLayer
54
55    def setUp(self):
56        super(ApplicantsContainerImporterTest, self).setUp()
57
58        # Setup a sample site for each test
59        app = University()
60        self.dc_root = tempfile.mkdtemp()
61        app['datacenter'].setStoragePath(self.dc_root)
62
63        # Prepopulate the ZODB...
64        self.getRootFolder()['app'] = app
65        self.app = self.getRootFolder()['app']
66        self.container = ApplicantsContainer()
67        self.container.code = u'dp2011'
68        self.app['applicants']['dp2011'] = self.container
69
70        self.importer = ApplicantsContainerImporter()
71        self.workdir = tempfile.mkdtemp()
72        self.csv_file = os.path.join(self.workdir, 'sampledata.csv')
73        open(self.csv_file, 'wb').write(APPS_CONTAINER_SAMPLE_DATA)
74        setSite(self.app)
75        return
76
77    def tearDown(self):
78        super(ApplicantsContainerImporterTest, self).tearDown()
79        shutil.rmtree(self.workdir)
80        shutil.rmtree(self.dc_root)
81        clearSite()
82        return
83
84    def test_interface(self):
85        # Make sure we fulfill the interface contracts.
86        assert verifyObject(IBatchProcessor, self.importer) is True
87        assert verifyClass(
88            IBatchProcessor, ApplicantsContainerImporter) is True
89
90    def test_parentsExist(self):
91        assert self.importer.parentsExist(None, dict()) is False
92        assert self.importer.parentsExist(None, self.app) is True
93
94    def test_entryExists(self):
95        assert self.importer.entryExists(
96            dict(code='REG_NONE'), self.app) is False
97        assert self.importer.entryExists(
98            dict(code='dp2011'), self.app) is True
99
100    def test_getParent(self):
101        parent = self.importer.getParent(None, self.app)
102        assert parent is self.app['applicants']
103
104    def test_getEntry(self):
105        assert self.importer.getEntry(
106            dict(code='REG_NONE'), self.app) is None
107        assert self.importer.getEntry(
108            dict(code='dp2011'), self.app) is self.container
109
110    def test_addEntry(self):
111        self.importer.addEntry(
112            'New application', dict(code='dp2012'), self.app)
113        assert self.app['applicants']['dp2012'] == 'New application'
114
115    def test_delEntry(self):
116        self.importer.delEntry(dict(code='dp2011'), self.app)
117        assert 'dp2011' not in self.app['applicants'].keys()
118
119    def test_import(self):
120        # Do a real import
121        # see local sample_container.csv file for input
122        num, num_warns, fin_file, fail_file = self.importer.doImport(
123            self.csv_file, APPS_CONTAINER_HEADER_FIELDS)
124        avail_containers = [x for x in self.app['applicants'].keys()]
125        self.assertTrue(u'CODE1' in avail_containers)
126        self.assertTrue(u'CODE2' in avail_containers)
127        shutil.rmtree(os.path.dirname(fin_file))
128
129def test_suite():
130    suite = unittest.TestSuite()
131    for testcase in [
132        ApplicantsContainerImporterTest,
133        ]:
134        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
135                testcase
136                )
137        )
138    return suite
Note: See TracBrowser for help on using the repository browser.