[7866] | 1 | ## $Id: tests.py 7866 2012-03-13 07:16:03Z 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 | import os |
---|
| 19 | import shutil |
---|
| 20 | import tempfile |
---|
| 21 | from zope.interface.verify import verifyClass, verifyObject |
---|
| 22 | from zope.component.hooks import setSite, clearSite |
---|
| 23 | from zope.component import createObject |
---|
| 24 | from waeup.kofa.app import University |
---|
| 25 | from waeup.kofa.university.faculty import Faculty |
---|
| 26 | from waeup.kofa.university.department import Department |
---|
| 27 | from waeup.kofa.testing import FunctionalTestCase |
---|
| 28 | from waeup.custom.testing import FunctionalLayer |
---|
| 29 | from waeup.kofa.interfaces import IBatchProcessor |
---|
| 30 | from waeup.kofa.applicants.applicant import Applicant |
---|
| 31 | from waeup.custom.applicants.container import PGApplicantsContainer |
---|
| 32 | from waeup.custom.applicants.applicant import PGApplicant |
---|
| 33 | from waeup.custom.applicants.batching import PGApplicantImporter |
---|
| 34 | from waeup.custom.applicants.interfaces import IPGApplicant, IPGApplicantEdit |
---|
| 35 | |
---|
| 36 | APPLICANT_SAMPLE_DATA = open( |
---|
| 37 | os.path.join(os.path.dirname(__file__), 'sample_applicant_data.csv'), |
---|
| 38 | 'rb').read() |
---|
| 39 | APPLICANT_HEADER_FIELDS = APPLICANT_SAMPLE_DATA.split( |
---|
| 40 | '\n')[0].split(',') |
---|
| 41 | |
---|
| 42 | class PGApplicantImporterTest(FunctionalTestCase): |
---|
| 43 | """Perform some batching tests. |
---|
| 44 | """ |
---|
| 45 | layer = FunctionalLayer |
---|
| 46 | |
---|
| 47 | def setUp(self): |
---|
| 48 | super(PGApplicantImporterTest, self).setUp() |
---|
| 49 | # Setup a sample site for each test |
---|
| 50 | app = University() |
---|
| 51 | self.dc_root = tempfile.mkdtemp() |
---|
| 52 | app['datacenter'].setStoragePath(self.dc_root) |
---|
| 53 | |
---|
| 54 | # Prepopulate the ZODB... |
---|
| 55 | self.getRootFolder()['app'] = app |
---|
| 56 | # we add the site immediately after creation to the |
---|
| 57 | # ZODB. Catalogs and other local utilities are not setup |
---|
| 58 | # before that step. |
---|
| 59 | self.app = self.getRootFolder()['app'] |
---|
| 60 | # Set site here. Some of the following setup code might need |
---|
| 61 | # to access grok.getSite() and should get our new app then |
---|
| 62 | setSite(app) |
---|
| 63 | |
---|
| 64 | # Add an applicants container |
---|
| 65 | self.container = PGApplicantsContainer() |
---|
| 66 | self.container.code = u'pg2011' |
---|
| 67 | self.app['applicants']['pg2011'] = self.container |
---|
| 68 | |
---|
| 69 | # Populate university |
---|
| 70 | self.certificate = createObject('waeup.Certificate') |
---|
| 71 | self.certificate.code = 'CERT1' |
---|
| 72 | self.certificate.application_category = 'basic' |
---|
| 73 | self.certificate.start_level = 100 |
---|
| 74 | self.certificate.end_level = 500 |
---|
| 75 | self.app['faculties']['fac1'] = Faculty() |
---|
| 76 | self.app['faculties']['fac1']['dep1'] = Department() |
---|
| 77 | self.app['faculties']['fac1']['dep1'].certificates.addCertificate( |
---|
| 78 | self.certificate) |
---|
| 79 | |
---|
| 80 | # Add applicant with subobjects |
---|
| 81 | applicant = PGApplicant() |
---|
| 82 | applicant.firstname = u'Anna' |
---|
| 83 | applicant.lastname = u'Tester' |
---|
| 84 | self.app['applicants']['pg2011'].addApplicant(applicant) |
---|
| 85 | self.application_number = applicant.application_number |
---|
| 86 | self.applicant = self.app['applicants']['pg2011'][ |
---|
| 87 | self.application_number] |
---|
| 88 | self.importer = PGApplicantImporter() |
---|
| 89 | self.workdir = tempfile.mkdtemp() |
---|
| 90 | self.csv_file = os.path.join(self.workdir, 'sample_applicant_data.csv') |
---|
| 91 | open(self.csv_file, 'wb').write(APPLICANT_SAMPLE_DATA) |
---|
| 92 | |
---|
| 93 | def tearDown(self): |
---|
| 94 | super(PGApplicantImporterTest, self).tearDown() |
---|
| 95 | shutil.rmtree(self.workdir) |
---|
| 96 | shutil.rmtree(self.dc_root) |
---|
| 97 | clearSite() |
---|
| 98 | return |
---|
| 99 | |
---|
| 100 | def test_interfaces(self): |
---|
| 101 | # Make sure we fulfill the interface contracts. |
---|
| 102 | assert verifyObject(IBatchProcessor, self.importer) is True |
---|
| 103 | assert verifyClass( |
---|
| 104 | IBatchProcessor, PGApplicantImporter) is True |
---|
| 105 | assert verifyObject(IPGApplicant, self.applicant) is True |
---|
| 106 | assert verifyClass( |
---|
| 107 | IPGApplicant, PGApplicant) is True |
---|
| 108 | assert verifyObject(IPGApplicantEdit, self.applicant) is True |
---|
| 109 | assert verifyClass( |
---|
| 110 | IPGApplicantEdit, PGApplicant) is True |
---|
| 111 | |
---|
| 112 | def test_wrong_child(self): |
---|
| 113 | applicant = Applicant() |
---|
| 114 | applicant.firstname = u'Klaus' |
---|
| 115 | applicant.lastname = u'Tester' |
---|
| 116 | self.assertRaises( |
---|
| 117 | TypeError, self.app['applicants']['pg2011'].addApplicant, applicant) |
---|
| 118 | |
---|
| 119 | def test_import(self): |
---|
| 120 | num, num_warns, fin_file, fail_file = self.importer.doImport( |
---|
| 121 | self.csv_file, APPLICANT_HEADER_FIELDS) |
---|
| 122 | self.assertEqual(num_warns,0) |
---|
| 123 | keys = self.app['applicants']['pg2011'].keys() |
---|
| 124 | assert len(keys) == 4 |
---|
| 125 | container = self.app['applicants']['pg2011'] |
---|
| 126 | assert container.__implemented__.__name__ == ( |
---|
| 127 | 'waeup.custom.applicants.container.PGApplicantsContainer') |
---|
| 128 | applicant = container[keys[0]] |
---|
| 129 | assert applicant.__implemented__.__name__ == ( |
---|
| 130 | 'waeup.custom.applicants.applicant.PGApplicant') |
---|
| 131 | shutil.rmtree(os.path.dirname(fin_file)) |
---|