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

Last change on this file since 10009 was 5311, checked in by uli, 14 years ago

Add missing tests for WAeUPObjectWidget and add test case for
WAeUPDisplayWidget. objectwidget is now at 100% test coverage.

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