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 | """ |
---|
24 | import grok |
---|
25 | import os |
---|
26 | import shutil |
---|
27 | import tempfile |
---|
28 | import unittest |
---|
29 | from zope.app.testing.functional import FunctionalTestCase |
---|
30 | from zope.component.hooks import setSite, clearSite |
---|
31 | from zope.interface.verify import verifyClass, verifyObject |
---|
32 | |
---|
33 | from waeup.sirp.app import University |
---|
34 | from waeup.sirp.applicants.batching import ApplicantsContainerImporter |
---|
35 | from waeup.sirp.applicants.container import ApplicantsContainer |
---|
36 | from waeup.sirp.applicants.interfaces import IApplicantsContainer |
---|
37 | from waeup.sirp.interfaces import DuplicationError |
---|
38 | from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module |
---|
39 | from waeup.sirp.interfaces import IBatchProcessor |
---|
40 | |
---|
41 | |
---|
42 | # Sample data we can use in tests... |
---|
43 | APPS_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 |
---|
48 | APPS_CONTAINER_HEADER_FIELDS = APPS_CONTAINER_SAMPLE_DATA.split( |
---|
49 | '\n')[0].split(',') |
---|
50 | |
---|
51 | class 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 | self.importer.doImport( |
---|
122 | self.csv_file, APPS_CONTAINER_HEADER_FIELDS) |
---|
123 | #print self.workdir |
---|
124 | #import pdb; pdb.set_trace() |
---|
125 | |
---|
126 | # This currently gives: |
---|
127 | # |
---|
128 | # ac_prefix,code,application_category,provider,prefix,year,--ERRORS-- |
---|
129 | # APP,CODE1,app,base,app,2013,"conversion error: field application_category: <type 'exceptions.LookupError'> LookupError('app',) / conversion error: field provider: <type 'exceptions.TypeError'> TypeError('unhashable type',)" |
---|
130 | # DPP,CODE2,dp,base,app,2013,"conversion error: field application_category: <type 'exceptions.LookupError'> LookupError('dp',) / conversion error: field provider: <type 'exceptions.TypeError'> TypeError('unhashable type',)" |
---|
131 | self.assertTrue(u'CODE1' in self.app['applicants'].keys()) |
---|
132 | |
---|
133 | def test_suite(): |
---|
134 | suite = unittest.TestSuite() |
---|
135 | for testcase in [ |
---|
136 | ApplicantsContainerImporterTest, |
---|
137 | ]: |
---|
138 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
139 | testcase |
---|
140 | ) |
---|
141 | ) |
---|
142 | return suite |
---|