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

Last change on this file since 7074 was 7063, checked in by uli, 13 years ago

Merge changes from branch ulif-extimgstore back into trunk.
Beside external image storage also waeupdocs should work again.

File size: 7.0 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$
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 unittest
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 test_get_regno_or_ac(self):
51        # we can get reg_no or access_code of an applicants if it is set
52        appl1 = Applicant()
53        appl2 = Applicant()
54        appl2.reg_no = u'foo'
55        appl3 = Applicant()
56        appl3.access_code = u'bar'
57        appl4 = Applicant()
58        appl4.reg_no = u'foo'
59        appl4.access_code = u'bar'
60        self.assertTrue(
61            get_regno_or_ac(appl1) is None)
62        self.assertEqual(
63            get_regno_or_ac(appl2), u'foo')
64        self.assertEqual(
65            get_regno_or_ac(appl3), u'bar')
66        self.assertEqual(
67            get_regno_or_ac(appl4), u'foo')
68        return
69
70    def test_image_store_handler_util_accessible(self):
71        # we can get an IFileStoreHandler utility for applicants
72        handler = queryUtility(IFileStoreHandler, name='img-applicant')
73        self.assertTrue(
74            isinstance(handler, ApplicantImageStoreHandler))
75        return
76
77    def test_image_store_handler(self):
78        store = DefaultStorage()
79        handler = queryUtility(IFileStoreHandler, name='img-applicant')
80        result1 = handler.pathFromFileID(
81            store, '/fake-root', '__img-applicant__sample.jpg')
82        result2 = handler.pathFromFileID(
83            store, '/fake-root', '__img-applicant__dir1/sample.jpg')
84        result3 = handler.pathFromFileID(
85            store, '/fake-root', '__img-applicant__dir1/dir2/sample.jpg')
86        self.assertEqual(
87            result1, '/fake-root/applicants/sample.jpg')
88        self.assertEqual(
89            result2, '/fake-root/applicants/dir1/sample.jpg')
90        self.assertEqual(
91            result3, '/fake-root/applicants/dir1/dir2/sample.jpg')
92        return
93
94class ApplicantImageNameChooserTests(FunctionalTestCase):
95
96    layer = FunctionalLayer
97
98    def test_iface(self):
99        # make sure we implement promised interfaces
100        obj = ApplicantImageNameChooser(None) # needs a context normally
101        verify.verifyClass(IFileStoreNameChooser, ApplicantImageNameChooser)
102        verify.verifyObject(IFileStoreNameChooser, obj)
103        return
104
105    def test_name_chooser_available(self):
106        # we can get a name chooser for applicant objects as adapter
107        appl = Applicant()
108        chooser = IFileStoreNameChooser(appl)
109        self.assertTrue(chooser is not None)
110        return
111
112    def test_name_chooser_applicant_wo_container(self):
113        # we can get an image filename for applicants not in a container
114        appl = Applicant()
115        appl.reg_no = u'MY_REG_NO'
116        chooser = IFileStoreNameChooser(appl)
117        result = chooser.chooseName()
118        # the file would be stored in a ``_default`` directory.
119        self.assertEqual(
120            result, '__img-applicant___default/MY_REG_NO.jpg')
121        return
122
123    def test_name_chooser_applicant_w_container(self):
124        appl = Applicant()
125        appl.reg_no = u'MY_REG_NO'
126        fake_container = grok.Container()
127        fake_container.__name__ = 'folder'
128        fake_container['appl'] = appl
129        appl.__parent__ = fake_container
130        chooser = IFileStoreNameChooser(appl)
131        result = chooser.chooseName()
132        self.assertEqual(
133            result, '__img-applicant__folder/MY_REG_NO.jpg')
134        return
135
136    def test_name_chooser_check_name(self):
137        # we can check file ids for applicants
138        appl = Applicant()
139        appl.reg_no = u'MY_REG_NO'
140        fake_container = grok.Container()
141        fake_container.__name__ = 'folder'
142        fake_container['appl'] = appl
143        appl.__parent__ = fake_container
144        chooser = IFileStoreNameChooser(appl)
145        result1 = chooser.checkName('foo')
146        result2 = chooser.checkName('__img-applicant__folder/MY_REG_NO.jpg')
147        self.assertEqual(result1, False)
148        self.assertEqual(result2, True)
149        return
150
151class ResultEntryTest(unittest.TestCase):
152
153    def setUp(self):
154        self.result_entry = ResultEntry()
155        return
156
157    def tearDown(self):
158        pass
159
160    def test_interfaces(self):
161        verify.verifyClass(IResultEntry, ResultEntry)
162        verify.verifyObject(IResultEntry, self.result_entry)
163
164    def test_resultentry(self):
165        entry = ResultEntry('Some subject', 3.7)
166        assert entry.subject == 'Some subject'
167        assert entry.score == 3.7
168
169class ApplicantTest(FunctionalTestCase):
170
171    layer = FunctionalLayer
172
173    def setUp(self):
174        super(ApplicantTest, self).setUp()
175        self.applicant = Applicant()
176        return
177
178    def tearDown(self):
179        super(ApplicantTest, self).tearDown()
180        return
181
182    def test_interfaces(self):
183        verify.verifyClass(IApplicant, Applicant)
184        verify.verifyObject(IApplicant, self.applicant)
185        return
186
187class ApplicantFactoryTest(FunctionalTestCase):
188
189    layer = FunctionalLayer
190
191    def setUp(self):
192        super(ApplicantFactoryTest, self).setUp()
193        self.factory = ApplicantFactory()
194        return
195
196    def tearDown(self):
197        super(ApplicantFactoryTest, self).tearDown()
198        return
199
200    def test_interfaces(self):
201        verify.verifyClass(IFactory, ApplicantFactory)
202        verify.verifyObject(IFactory, self.factory)
203
204    def test_factory(self):
205        obj = self.factory()
206        assert isinstance(obj, Applicant)
207
208    def test_getInterfaces(self):
209        implemented_by = self.factory.getInterfaces()
210        assert implemented_by.isOrExtends(IApplicant)
Note: See TracBrowser for help on using the repository browser.