source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_applicants.py @ 6288

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

Improve some applicants related tests: for setup register
IFileRetrieval, because otherwise stuff concerning
w.s.applicants.interfaces cannot be tested properly.

File size: 5.7 KB
Line 
1##
2## test_applicants.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 logging
25import unittest
26from StringIO import StringIO
27from hurry.file.file import IdFileRetrieval
28from hurry.file.interfaces import IFileRetrieval
29from zope.app.testing import placelesssetup
30from zope.component import (
31    provideAdapter, adapts, getGlobalSiteManager, provideUtility)
32from zope.component.interfaces import IFactory
33from zope.interface import verify, implements
34from zope.location.interfaces import ILocation
35from zope.publisher.base import TestRequest
36from zope.publisher.interfaces import NotFound
37from waeup.sirp.interfaces import IWAeUPSIRPPluggable
38from waeup.sirp.image.image import WAeUPImageFile
39from waeup.sirp.image.interfaces import IWAeUPImageFile
40from waeup.sirp.applicants import (
41    ResultEntry, Applicant, ApplicantFactory, ApplicantTraverser,
42    )
43from waeup.sirp.applicants.interfaces import (
44    IResultEntry, IApplicant,
45    )
46
47class FakeImageLocation(object):
48    implements(ILocation)
49    adapts(IWAeUPImageFile)
50    def __init__(self, context):
51        pass
52
53class ResultEntryTest(unittest.TestCase):
54
55    def setUp(self):
56        self.result_entry = ResultEntry()
57        return
58
59    def tearDown(self):
60        pass
61
62    def test_interfaces(self):
63        verify.verifyClass(IResultEntry, ResultEntry)
64        verify.verifyObject(IResultEntry, self.result_entry)
65
66    def test_resultentry(self):
67        entry = ResultEntry('Some subject', 3.7)
68        assert entry.subject == 'Some subject'
69        assert entry.score == 3.7
70
71class ApplicantTest(unittest.TestCase):
72
73    def setUp(self):
74        placelesssetup.setUp()
75        provideUtility(IdFileRetrieval(), IFileRetrieval)
76        self.applicant = Applicant()
77
78    def tearDown(self):
79        placelesssetup.tearDown()
80        pass
81
82    def test_interfaces(self):
83        verify.verifyClass(IApplicant, Applicant)
84        verify.verifyObject(IApplicant, self.applicant)
85
86class ApplicantFactoryTest(unittest.TestCase):
87
88    def setUp(self):
89        self.factory = ApplicantFactory()
90
91    def tearDown(self):
92        pass
93
94    def test_interfaces(self):
95        verify.verifyClass(IFactory, ApplicantFactory)
96        verify.verifyObject(IFactory, self.factory)
97
98    def test_factory(self):
99        obj = self.factory()
100        assert isinstance(obj, Applicant)
101
102    def test_getInterfaces(self):
103        implemented_by = self.factory.getInterfaces()
104        assert implemented_by.isOrExtends(IApplicant)
105
106class ApplicantTraverserTest(unittest.TestCase):
107
108    def setUp(self):
109        self.applicant = Applicant()
110        self.request = TestRequest('')
111        self.gsm = getGlobalSiteManager()
112        self.gsm.registerAdapter(FakeImageLocation)
113        self.fileretrieval = IdFileRetrieval()
114        provideUtility(self.fileretrieval, IFileRetrieval)
115        return
116
117    def tearDown(self):
118        self.gsm.unregisterAdapter(FakeImageLocation)
119        self.gsm.unregisterUtility(self.fileretrieval, IFileRetrieval)
120        return
121
122    def test_traverse_wo_passport(self):
123        # Ask for some attribute not provided
124        traverser = ApplicantTraverser(
125            self.applicant, self.request
126            )
127        self.assertRaises(
128            NotFound,
129            traverser.publishTraverse, self.request, 'passport'
130            )
131        return
132
133    def test_traverse_wo_image_passport_jpg(self):
134        # Ask for applicant pic if we didn't provided one
135        # We get a placeholder.
136        traverser = ApplicantTraverser(
137            self.applicant, self.request
138            )
139        result = traverser.publishTraverse(self.request, 'passport.jpg')
140        self.assertTrue(isinstance(result, FakeImageLocation))
141        return
142
143    def test_traverse_w_image_passport_jpg(self):
144        # Ask for applicant pic that's named 'passport.jpg'
145        traverser = ApplicantTraverser(
146            self.applicant, self.request
147            )
148        self.applicant.passport = WAeUPImageFile('nofile.jpg', '')
149        self.applicant.passport.filename = 'mypic.jpg'
150        result = traverser.publishTraverse(self.request, 'passport.jpg')
151        self.assertTrue(isinstance(result, FakeImageLocation))
152        return
153
154    def test_traverse_w_image_some_jpg(self):
155        # Ask for applicant pic by correct name
156        traverser = ApplicantTraverser(
157            self.applicant, self.request
158            )
159        self.applicant.passport = WAeUPImageFile('nofile.jpg', '')
160        self.applicant.passport.filename = 'mypic.jpg'
161        result = traverser.publishTraverse(self.request, 'mypic.jpg')
162        self.assertTrue(isinstance(result, FakeImageLocation))
163        return
164
165def test_suite():
166    suite = unittest.TestSuite()
167    for testcase in [
168        ResultEntryTest, ApplicantTest, ApplicantFactoryTest,
169        ApplicantTraverserTest,
170        ]:
171        suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
172                testcase
173                )
174        )
175    return suite
Note: See TracBrowser for help on using the repository browser.