Ignore:
Timestamp:
6 Aug 2010, 08:50:23 (14 years ago)
Author:
uli
Message:

Update tests. Test coverage now at 96%.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.widgets.core/trunk/src/waeup/widgets/core/tests/test_dictwidget.py

    r5371 r5376  
    156156        sw = CustomWidgetFactory(DictWidget)
    157157        widget = sw(self.field, request)
    158         assert widget.subwidget is None
     158        assert widget.key_subwidget is None
     159        assert widget.value_subwidget is None
    159160        assert widget.context.value_type is value_type
    160161
     
    162163        class PollOption(object) : pass
    163164        ow = CustomWidgetFactory(ObjectWidget, PollOption)
    164         sw = CustomWidgetFactory(DictWidget, subwidget=ow)
     165        sw = CustomWidgetFactory(DictWidget, key_subwidget=ow)
    165166        widget = sw(self.field, request)
    166         assert widget.subwidget is ow
     167        assert widget.key_subwidget is ow
    167168        assert widget.context.value_type is value_type
    168169        assert widget.context.key_type is key_type
     
    181182        ow = CustomWidgetFactory(ObjectWidget, PollOption)
    182183        widget = DictWidget(
    183             self.field, request, subwidget=ow)
    184         assert widget.subwidget is ow
     184            self.field, request, key_subwidget=ow, value_subwidget=ow)
     185        assert widget.key_subwidget is ow
     186        assert widget.value_subwidget is ow
    185187        assert widget() is not None
    186188
     189    def test_renders_subwidgets(self):
     190        self.field = Dict(__name__=u'foo',
     191                          key_type=TextLine(__name__=u'bar'),
     192                          value_type=TextLine(__name__=u'baz'),
     193                          )
     194        request = TestRequest(form={'field.foo.add': u'Add bar',
     195                                    'field.foo.count': u'0'})
     196
     197        # a text widget the outputs an additional silly header
     198        class CustomTextWidget(TextWidget):
     199            def __call__(self):
     200                result = super(CustomTextWidget, self).__call__()
     201                return 'CUSTOM WIDGET\n' + result
     202            pass
     203        ctw = CustomWidgetFactory(CustomTextWidget)
     204        widget = DictWidget(
     205            self.field, request, key_subwidget=ctw, value_subwidget=ctw)
     206        assert widget.key_subwidget is ctw
     207        assert widget.value_subwidget is ctw
     208
     209        check_list = (
     210            'checkbox', 'field.foo.remove_0',
     211            'CUSTOM WIDGET',
     212            'input', 'field.foo.key.0.bar',
     213            'CUSTOM WIDGET',
     214            'input', 'field.foo.value.0.baz',
     215            'submit', 'submit', 'field.foo.add',
     216            'field.foo.count" value="1"',
     217        )
     218        self.verifyResult(widget(), check_list, inorder=True)
     219       
    187220    def test_dict(self):
    188221        self.field = Dict(
     
    439472        assert widget.applyChanges(foo_obj) is True
    440473
     474    def test_missing_value(self):
     475        self.field.missing_value = u'No Input'
     476        request = TestRequest()
     477        widget = DictWidget(
     478            self.field, request)
     479        # Passing a different string than the missing value will mean trouble
     480        widget.setRenderedValue('Some string unequal to "No Input"')
     481        self.assertRaises(
     482            ValueError, widget._getRenderedValue,)
     483        # Setting the content to the specified string, however, works:
     484        widget.setRenderedValue(u'No Input')
     485        assert isinstance(widget._getRenderedValue(), type({}))
     486        assert widget._getRenderedValue() == {}
     487
     488    def test_default_value(self):
     489        request = TestRequest()
     490        self.field.default = {u'somekey': u'somevalue'}
     491        widget = DictWidget(
     492            self.field, request)
     493        assert widget._getRenderedValue() == {u'somekey': u'somevalue'}
    441494
    442495class SequenceDisplayWidgetTest(
     
    490543        self.verifyResult(self.widget(), check_list, inorder=True)
    491544
    492     def test_honors_subwidget(self):
     545    def test_honors_key_subwidget(self):
    493546        self.widget = self._WidgetFactory(
    494547            self.field, self.request,
    495             subwidget=UppercaseDisplayWidget)
     548            key_subwidget=UppercaseDisplayWidget)
    496549        self.content.foo = {u'first value': u'second value'}
    497550        check_list = ['<ol', 'class=', 'testwidget',
    498                       '<li', 'FIRST VALUE: SECOND VALUE', '</li',
     551                      '<li', 'FIRST VALUE: second value', '</li',
     552                      '</ol']
     553        self.verifyResult(self.widget(), check_list, inorder=True)
     554
     555    def test_honors_value_subwidget(self):
     556        self.widget = self._WidgetFactory(
     557            self.field, self.request,
     558            value_subwidget=UppercaseDisplayWidget)
     559        self.content.foo = {u'first value': u'second value'}
     560        check_list = ['<ol', 'class=', 'testwidget',
     561                      '<li', 'first value: SECOND VALUE', '</li',
    499562                      '</ol']
    500563        self.verifyResult(self.widget(), check_list, inorder=True)
Note: See TracChangeset for help on using the changeset viewer.