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