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

Last change on this file since 5889 was 5764, checked in by uli, 14 years ago

Add tests for ApplicantTraverser?.

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