source: main/waeup.sirp/branches/henrik-bootstrap/src/waeup/sirp/widgets/tests/test_objectwidget.py @ 10009

Last change on this file since 10009 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: 8.8 KB
Line 
1## $Id: test_objectwidget.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 objectwidget.
19"""
20import doctest
21import unittest
22import sys
23from zope.component import testing
24from zope.interface import Interface, implements
25from zope.interface.verify import verifyClass
26from zope.publisher.browser import TestRequest
27from zope.schema import Object, TextLine
28from zope.schema.interfaces import ITextLine
29from zope.traversing.adapters import DefaultTraversable
30from zope.traversing.interfaces import ITraversable
31from zope.publisher.interfaces.browser import IDefaultBrowserLayer
32from zope.component import provideAdapter
33
34from zope.formlib.interfaces import (
35    IInputWidget, IDisplayWidget,MissingInputError)
36from zope.formlib.widgets import TextWidget, DisplayWidget
37from zope.formlib.tests.test_browserwidget import BrowserWidgetTest
38from zope.formlib.interfaces import IWidgetInputErrorView
39
40from waeup.sirp.widgets.objectwidget import SIRPObjectWidget as ObjectWidget
41from waeup.sirp.widgets.objectwidget import (
42    SIRPObjectDisplayWidget as ObjectDisplayWidget)
43
44class ITestContact(Interface):
45    name = TextLine()
46    email = TextLine()
47   
48class TestContact(object):
49    implements(ITestContact)
50
51class ITestContent(Interface):
52    foo = Object(
53        ITestContact,
54        title = u'Foo Title',
55        description = u'',
56        )
57   
58class ObjectWidgetInputErrorView(object):
59    implements(IWidgetInputErrorView)
60
61    def __init__(self, error, request):
62        self.error = error
63        self.request = request
64
65    def snippet(self):
66        return repr(self.error)
67
68class ObjectWidgetTest(BrowserWidgetTest):
69    """Documents and tests the object widget.
70
71        >>> from zope.interface.verify import verifyClass
72        >>> verifyClass(IInputWidget, ObjectWidget)
73        True
74    """
75
76    _FieldFactory = Object
77    def _WidgetFactory(self, context, request, **kw):
78        kw.update({'factory': TestContact})
79        return ObjectWidget(context, request, **kw)
80
81    def setUpContent(self, desc=u'', title=u'Foo Title'):
82        provideAdapter(TextWidget, (ITextLine, IDefaultBrowserLayer),
83                       IInputWidget)
84       
85        class TestObject(object):
86            implements(ITestContent)
87
88        self.content = TestObject()
89        self.field = ITestContent['foo']
90        self.request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl')
91        self.request.form['field.foo'] = u'Foo Value'
92        self._widget = self._WidgetFactory(self.field, self.request)
93
94    def setUp(self):
95        super(ObjectWidgetTest, self).setUp()
96        self.field = Object(ITestContact, __name__=u'foo')
97        provideAdapter(TextWidget,
98                       (ITextLine, IDefaultBrowserLayer),
99                       IInputWidget)
100
101    def test_applyChanges(self):
102        self.request.form['field.foo.name'] = u'Foo Name'
103        self.request.form['field.foo.email'] = u'foo@foo.test'
104        widget = self._WidgetFactory(self.field, self.request)
105
106        self.assertEqual(widget.applyChanges(self.content), True)
107        self.assertEqual(hasattr(self.content, 'foo'), True)
108        self.assertEqual(isinstance(self.content.foo, TestContact), True)
109        self.assertEqual(self.content.foo.name, u'Foo Name')
110        self.assertEqual(self.content.foo.email, u'foo@foo.test')
111
112    def test_error(self):
113        provideAdapter(
114            ObjectWidgetInputErrorView,
115            (MissingInputError, TestRequest),
116            IWidgetInputErrorView)
117
118        widget = self._WidgetFactory(self.field, self.request)
119        self.assertRaises(MissingInputError, widget.getInputValue)
120        error_html = widget.error()
121        if sys.version_info < (2, 5):
122            self.failUnless("email: <zope.formlib.interfaces.Mis"
123                             in error_html)
124            self.failUnless("name: <zope.formlib.interfaces.Miss"
125                             in error_html)
126        else:
127            self.failUnless(
128                "email: MissingInputError(u'field.foo.email', u'', None)"
129                in error_html)
130            self.failUnless(
131                "name: MissingInputError(u'field.foo.name', u'', None)"
132                in error_html)
133
134    def test_applyChangesNoChange(self):
135        self.content.foo = TestContact()
136        self.content.foo.name = u'Foo Name'
137        self.content.foo.email = u'foo@foo.test'
138
139        self.request.form['field.foo.name'] = u'Foo Name'
140        self.request.form['field.foo.email'] = u'foo@foo.test'
141        widget = self._WidgetFactory(self.field, self.request)
142        widget.setRenderedValue(self.content.foo)
143
144        self.assertEqual(widget.applyChanges(self.content), False)
145        self.assertEqual(hasattr(self.content, 'foo'), True)
146        self.assertEqual(isinstance(self.content.foo, TestContact), True)
147        self.assertEqual(self.content.foo.name, u'Foo Name')
148        self.assertEqual(self.content.foo.email, u'foo@foo.test')
149
150    def test_respect_custom_widgets(self):
151        # We can use our own subwidgets when creating ObjectWidgets
152        class CustomTextWidget(TextWidget):
153            pass
154
155        # We create a custom widget for the `name` field of ITestContact
156        name_widget = CustomTextWidget(ITestContact['name'], self.request)
157
158        # Custom widgets are passed by <fieldname>_widget keyword:
159        widget = self._WidgetFactory(self.field, self.request,
160                                     name_widget=name_widget)
161        widget1, widget2 = widget.subwidgets()
162        self.assertTrue(widget1 is name_widget)
163       
164       
165class ObjectDisplayWidgetTest(BrowserWidgetTest):
166    """Documents and tests the display variant of object widget.
167
168        >>> from zope.interface.verify import verifyClass
169        >>> verifyClass(IDisplayWidget, ObjectDisplayWidget)
170        True
171
172   """
173    _FieldFactory = Object
174    def _WidgetFactory(self, context, request, **kw):
175        kw.update({'factory': TestContact})
176        return ObjectDisplayWidget(context, request, **kw)
177
178    def setUpContent(self, desc=u'', title=u'Foo Title'):
179        provideAdapter(TextWidget, (ITextLine, IDefaultBrowserLayer),
180                       IInputWidget)
181        provideAdapter(DisplayWidget, (ITextLine, IDefaultBrowserLayer),
182                       IDisplayWidget)
183        # The widget must be traversable. We register the default
184        # adapter that can turn nearly any object into an ITraversable.
185        provideAdapter(DefaultTraversable, (None,), ITraversable)
186       
187        class TestObject(object):
188            implements(ITestContent)
189
190        self.content = TestObject()
191        self.content.name = u'Foo Name'
192        self.content.email = u'foo@foo.test'
193        self.field = ITestContent['foo']
194        self.request = TestRequest(HTTP_ACCEPT_LANGUAGE='pl')
195        self.request.form['field.foo'] = u'Foo Value'
196        self._widget = self._WidgetFactory(self.field, self.request)
197
198    def setUp(self):
199        super(ObjectDisplayWidgetTest, self).setUp()
200        self.field = Object(ITestContact, __name__=u'foo')
201        provideAdapter(TextWidget,
202                       (ITextLine, IDefaultBrowserLayer),
203                       IInputWidget)
204        provideAdapter(DisplayWidget,
205                       (ITextLine, IDefaultBrowserLayer),
206                       IDisplayWidget)
207
208       
209    def test_interfaces(self):
210        from zope.interface.verify import DoesNotImplement
211        self.assertTrue(IDisplayWidget.providedBy(self._widget))
212        self.assertFalse(IInputWidget.providedBy(self._widget))
213        self.assertTrue(verifyClass(IDisplayWidget, ObjectDisplayWidget))
214
215    def test_render(self):
216        widget = ObjectDisplayWidget(
217            self.field, self.request, TestContact)
218        widget.setRenderedValue(self.content)
219
220        check_list = [
221            '<td', 'class=', 'row',
222            'Foo Name',
223            '</td>', '<td', 'class=', 'row',
224            'foo@foo.test',
225            '</td>']
226        self.verifyResult(widget(), check_list, inorder=True)   
227
228def test_suite():
229    return unittest.TestSuite((
230        unittest.makeSuite(ObjectWidgetTest),
231        unittest.makeSuite(ObjectDisplayWidgetTest),
232        doctest.DocTestSuite(),
233        ))
234
235if __name__=='__main__':
236    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.