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

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

Remove unused imports.

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