source: main/waeup.ikoba/branches/uli-fake-gw-provider/src/waeup/ikoba/widgets/tests/test_objectwidget.py @ 12642

Last change on this file since 12642 was 11949, checked in by Henrik Bettermann, 10 years ago

Change of name.

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