Projet

Général

Profil

0001-add-CheckboxesWidget.get_options-and-tests-23403.patch

avec tests - Anonyme, 25 avril 2018 17:49

Télécharger (3,17 ko)

Voir les différences:

Subject: [PATCH] add CheckboxesWidget.get_options and tests (#23403)

 tests/test_widgets.py | 13 ++++++++++++-
 wcs/qommon/form.py    | 25 +++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
tests/test_widgets.py
2 2
import sys
3 3
import shutil
4 4
import copy
5
import collections
5 6

  
6 7
from quixote import cleanup
7 8
from quixote.http_request import parse_query
......
407 408

  
408 409
def test_checkboxes_widget():
409 410
    widget = CheckboxesWidget('test',
410
            options=[('apple', 'Apple', 'apple'), ('pear', 'Pear', 'pear'), ('peach', 'Peach', 'peach')])
411
            options=[('apple', 'Apple', 'apple', {'datetime': datetime.datetime.now()}), ('pear', 'Pear', 'pear'), ('peach', 'Peach', 'peach')],
412
            options_with_attributes=[('apple', 'Apple', 'apple', {'datetime': datetime.datetime.now()}), ('pear', 'Pear', 'pear', {'datetime': datetime.datetime.now()}), ('peach', 'Peach', 'peach', {'datetime': datetime.datetime.now()})])
411 413
    mock_form_submission(req, widget, {'test$elementpeach': ['yes'], 'test$elementpear': ['yes']})
412 414
    assert widget.parse() == ['pear', 'peach']
415
    assert isinstance(widget.get_options(), collections.Iterator)
416
    iter_options = list(widget.get_options())
417
    assert len(iter_options) > 0
418
    for option in iter_options:
419
        assert option.get('description', False)
420
        assert option.get('key', False)
421
        assert option.get('attrs', False)
422
        assert option.get('options', False)
423
        assert isinstance(option['options']['datetime'], datetime.datetime)
413 424

  
414 425

  
415 426
def test_composite_widget():
wcs/qommon/form.py
1109 1109
    def has_error(self, request=None):
1110 1110
        return Widget.has_error(self, request=request)
1111 1111

  
1112
    def get_options(self):
1113
        options = []
1114
        tags = []
1115

  
1116
        if self.options_with_attributes:
1117
            options = self.options_with_attributes[:]
1118

  
1119
        for option in options:
1120
            object, description, key = option[:3]
1121
            html_attrs = {}
1122
            html_attrs['value'] = key
1123
            if self.options_with_attributes and option[-1].get('disabled'):
1124
                html_attrs['disabled'] = 'disabled'
1125
            if self.options_with_attributes and option[-1].get('checked'):
1126
                html_attrs['checked'] = 'true'
1127
            if description is None:
1128
                description = ''
1129

  
1130
            yield {
1131
                'description': description,
1132
                'key': key,
1133
                'attrs': html_attrs,
1134
                'options': option[-1] if self.options_with_attributes else None
1135
            }
1136

  
1112 1137

  
1113 1138
class ValidatedStringWidget(StringWidget):
1114 1139
    '''StringWidget which checks the value entered is correct according to a regex'''
1115
-