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

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

Replace the term 'WAeUP' by SIRP which is a WAeUP product.

  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1## $Id: test_applicant.py 7321 2011-12-10 06:15:17Z 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 ISIRPImageFile
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(ISIRPImageFile)
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        appl.applicant_id = u'dummy_123456'
126        chooser = IFileStoreNameChooser(appl)
127        result = chooser.chooseName()
128        # the file would be stored in a ``_default`` directory.
129        self.assertEqual(
130            result, '__img-applicant___default/dummy_123456.jpg')
131        return
132
133    def test_name_chooser_applicant_w_container(self):
134        fake_container = grok.Container()
135        fake_container.__name__ = 'folder'
136        fake_container.code = 'folder'
137        appl = Applicant()
138        appl.applicant_id = u'folder_123456'
139        appl.__parent__ = fake_container
140        chooser = IFileStoreNameChooser(appl)
141        result = chooser.chooseName()
142        self.assertEqual(
143            result, '__img-applicant__folder/%s.jpg' % appl.applicant_id)
144        return
145
146    def test_name_chooser_check_name(self):
147        # we can check file ids for applicants
148        fake_container = grok.Container()
149        fake_container.__name__ = 'folder'
150        fake_container.code = 'folder'
151        appl = Applicant()
152        appl.applicant_id = u'folder_123456'
153        appl.__parent__ = fake_container
154        chooser = IFileStoreNameChooser(appl)
155        result1 = chooser.checkName('foo')
156        result2 = chooser.checkName(
157            '__img-applicant__folder/%s.jpg' % appl.applicant_id)
158        self.assertEqual(result1, False)
159        self.assertEqual(result2, True)
160        return
161
162class ApplicantTest(FunctionalTestCase):
163
164    layer = FunctionalLayer
165
166    def setUp(self):
167        super(ApplicantTest, self).setUp()
168        self.applicant = Applicant()
169        return
170
171    def tearDown(self):
172        super(ApplicantTest, self).tearDown()
173        return
174
175    def test_interfaces(self):
176        verify.verifyClass(IApplicant, Applicant)
177        verify.verifyObject(IApplicant, self.applicant)
178        return
179
180class ApplicantFactoryTest(FunctionalTestCase):
181
182    layer = FunctionalLayer
183
184    def setUp(self):
185        super(ApplicantFactoryTest, self).setUp()
186        self.factory = ApplicantFactory()
187        return
188
189    def tearDown(self):
190        super(ApplicantFactoryTest, self).tearDown()
191        return
192
193    def test_interfaces(self):
194        verify.verifyClass(IFactory, ApplicantFactory)
195        verify.verifyObject(IFactory, self.factory)
196
197    def test_factory(self):
198        obj = self.factory()
199        assert isinstance(obj, Applicant)
200
201    def test_getInterfaces(self):
202        implemented_by = self.factory.getInterfaces()
203        assert implemented_by.isOrExtends(IApplicant)
Note: See TracBrowser for help on using the repository browser.