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

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

More copyright adjustments.

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