source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_applicant.py @ 7137

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

Set value Id for property svn:keywords in all Python files.

  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1##
2## test_applicant.py
3## Login : <uli@pu.smp.net>
4## Started on  Tue Aug 24 04:36:11 2010 Uli Fouquet
5## $Id: test_applicant.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"""Tests for applicants and related.
23"""
24import grok
25import os
26import shutil
27import tempfile
28import unittest
29from StringIO import StringIO
30from zope.component import adapts, queryUtility
31from zope.component.interfaces import IFactory
32from zope.interface import verify, implements
33from zope.location.interfaces import ILocation
34from waeup.sirp.image.interfaces import IWAeUPImageFile
35from waeup.sirp.imagestorage import DefaultStorage
36from waeup.sirp.interfaces import IFileStoreHandler, IFileStoreNameChooser
37from waeup.sirp.applicants import (
38    ResultEntry, Applicant, ApplicantFactory, get_regno_or_ac,
39    ApplicantImageStoreHandler, ApplicantImageNameChooser,
40    )
41from waeup.sirp.applicants.interfaces import IResultEntry, IApplicant
42from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer
43
44class FakeImageLocation(object):
45    implements(ILocation)
46    adapts(IWAeUPImageFile)
47    def __init__(self, context):
48        pass
49
50class HelperTests(FunctionalTestCase):
51
52    layer = FunctionalLayer
53
54    def setUp(self):
55        super(HelperTests, self).setUp()
56        self.workdir = tempfile.mkdtemp()
57        return
58
59    def tearDown(self):
60        super(HelperTests, self).tearDown()
61        shutil.rmtree(self.workdir)
62        return
63
64    def test_get_regno_or_ac(self):
65        # we can get reg_no or access_code of an applicants if it is set
66        appl1 = Applicant()
67        appl2 = Applicant()
68        appl2.reg_no = u'foo'
69        appl3 = Applicant()
70        appl3.access_code = u'bar'
71        appl4 = Applicant()
72        appl4.reg_no = u'foo'
73        appl4.access_code = u'bar'
74        self.assertTrue(
75            get_regno_or_ac(appl1) is None)
76        self.assertEqual(
77            get_regno_or_ac(appl2), u'foo')
78        self.assertEqual(
79            get_regno_or_ac(appl3), u'bar')
80        self.assertEqual(
81            get_regno_or_ac(appl4), u'foo')
82        return
83
84    def test_image_store_handler_util_accessible(self):
85        # we can get an IFileStoreHandler utility for applicants
86        handler = queryUtility(IFileStoreHandler, name='img-applicant')
87        self.assertTrue(
88            isinstance(handler, ApplicantImageStoreHandler))
89        return
90
91    def test_image_store_handler(self):
92        store = DefaultStorage()
93        handler = queryUtility(IFileStoreHandler, name='img-applicant')
94        result1 = handler.pathFromFileID(
95            store, '/fake-root', '__img-applicant__sample.jpg')
96        result2 = handler.pathFromFileID(
97            store, '/fake-root', '__img-applicant__dir1/sample.jpg')
98        result3 = handler.pathFromFileID(
99            store, '/fake-root', '__img-applicant__dir1/dir2/sample.jpg')
100        self.assertEqual(
101            result1, '/fake-root/applicants/sample')
102        self.assertEqual(
103            result2, '/fake-root/applicants/dir1/sample')
104        self.assertEqual(
105            result3, '/fake-root/applicants/dir1/dir2/sample')
106        return
107
108    def test_image_store_handler_create(self):
109        # we can create files in image store with the applicant image
110        # store handler
111        store = DefaultStorage(self.workdir)
112        handler = queryUtility(IFileStoreHandler, name='img-applicant')
113        file_obj, path, waeup_file = handler.createFile(
114            store, store.root, 'sample.jpg', '__img_applicant__sample',
115            StringIO('I am a JPG file'))
116        self.assertEqual(path[-21:], 'applicants/sample.jpg')
117        return
118
119    def test_image_store_handler_invalid_filename_ext(self):
120        # we only accept '.jpg' and '.png' as filename extensions.
121        store = DefaultStorage()
122        handler = queryUtility(IFileStoreHandler, name='img-applicant')
123        self.assertRaises(
124            ValueError,
125            handler.createFile,
126            store, store.root, 'sample.txt', '__img_applicant__sample',
127            StringIO('I am a txt file'))
128        return
129
130class ApplicantImageNameChooserTests(FunctionalTestCase):
131
132    layer = FunctionalLayer
133
134    def test_iface(self):
135        # make sure we implement promised interfaces
136        obj = ApplicantImageNameChooser(None) # needs a context normally
137        verify.verifyClass(IFileStoreNameChooser, ApplicantImageNameChooser)
138        verify.verifyObject(IFileStoreNameChooser, obj)
139        return
140
141    def test_name_chooser_available(self):
142        # we can get a name chooser for applicant objects as adapter
143        appl = Applicant()
144        chooser = IFileStoreNameChooser(appl)
145        self.assertTrue(chooser is not None)
146        return
147
148    def test_name_chooser_applicant_wo_container(self):
149        # we can get an image filename for applicants not in a container
150        appl = Applicant()
151        appl.reg_no = u'MY_REG_NO'
152        chooser = IFileStoreNameChooser(appl)
153        result = chooser.chooseName()
154        # the file would be stored in a ``_default`` directory.
155        self.assertEqual(
156            result, '__img-applicant___default/MY_REG_NO.jpg')
157        return
158
159    def test_name_chooser_applicant_w_container(self):
160        appl = Applicant()
161        appl.reg_no = u'MY_REG_NO'
162        fake_container = grok.Container()
163        fake_container.__name__ = 'folder'
164        fake_container['appl'] = appl
165        appl.__parent__ = fake_container
166        chooser = IFileStoreNameChooser(appl)
167        result = chooser.chooseName()
168        self.assertEqual(
169            result, '__img-applicant__folder/MY_REG_NO.jpg')
170        return
171
172    def test_name_chooser_check_name(self):
173        # we can check file ids for applicants
174        appl = Applicant()
175        appl.reg_no = u'MY_REG_NO'
176        fake_container = grok.Container()
177        fake_container.__name__ = 'folder'
178        fake_container['appl'] = appl
179        appl.__parent__ = fake_container
180        chooser = IFileStoreNameChooser(appl)
181        result1 = chooser.checkName('foo')
182        result2 = chooser.checkName('__img-applicant__folder/MY_REG_NO.jpg')
183        self.assertEqual(result1, False)
184        self.assertEqual(result2, True)
185        return
186
187class ResultEntryTest(unittest.TestCase):
188
189    def setUp(self):
190        self.result_entry = ResultEntry()
191        return
192
193    def tearDown(self):
194        pass
195
196    def test_interfaces(self):
197        verify.verifyClass(IResultEntry, ResultEntry)
198        verify.verifyObject(IResultEntry, self.result_entry)
199
200    def test_resultentry(self):
201        entry = ResultEntry('Some subject', 3.7)
202        assert entry.subject == 'Some subject'
203        assert entry.score == 3.7
204
205class ApplicantTest(FunctionalTestCase):
206
207    layer = FunctionalLayer
208
209    def setUp(self):
210        super(ApplicantTest, self).setUp()
211        self.applicant = Applicant()
212        return
213
214    def tearDown(self):
215        super(ApplicantTest, self).tearDown()
216        return
217
218    def test_interfaces(self):
219        verify.verifyClass(IApplicant, Applicant)
220        verify.verifyObject(IApplicant, self.applicant)
221        return
222
223class ApplicantFactoryTest(FunctionalTestCase):
224
225    layer = FunctionalLayer
226
227    def setUp(self):
228        super(ApplicantFactoryTest, self).setUp()
229        self.factory = ApplicantFactory()
230        return
231
232    def tearDown(self):
233        super(ApplicantFactoryTest, self).tearDown()
234        return
235
236    def test_interfaces(self):
237        verify.verifyClass(IFactory, ApplicantFactory)
238        verify.verifyObject(IFactory, self.factory)
239
240    def test_factory(self):
241        obj = self.factory()
242        assert isinstance(obj, Applicant)
243
244    def test_getInterfaces(self):
245        implemented_by = self.factory.getInterfaces()
246        assert implemented_by.isOrExtends(IApplicant)
Note: See TracBrowser for help on using the repository browser.