def test_DictCharWidget_id_for_label_uses_first_fields_name(self): names = [factory.make_string()] initials = [] labels = [factory.make_string()] widget = DictCharWidget([widgets.TextInput, widgets.TextInput], names, initials, labels) self.assertEqual(" _%s" % names[0], widget.id_for_label(" "))
def test_DictCharWidget_value_from_datadict_values_from_data(self): # 'value_from_datadict' extracts the values corresponding to the # field as a dictionary. names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] name = factory.make_string() field_1_value = factory.make_string() field_2_value = factory.make_string() # Create a query string with the field2 before the field1 and another # (unknown) value. data = QueryDict("%s_%s=%s&%s_%s=%s&%s=%s" % ( name, names[1], field_2_value, name, names[0], field_1_value, factory.make_string(), factory.make_string(), )) widget = DictCharWidget([widgets.TextInput, widgets.TextInput], names, initials, labels) self.assertEqual( { names[0]: field_1_value, names[1]: field_2_value }, widget.value_from_datadict(data, None, name), )
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self): names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] values = [factory.make_string(), factory.make_string()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, initials, labels, skip_check=True, ) name = factory.make_string() html_widget = fromstring("<root>" + widget.render(name, values) + "</root>") widget_names = XPath("fieldset/input/@name")(html_widget) widget_labels = XPath("fieldset/label/text()")(html_widget) widget_values = XPath("fieldset/input/@value")(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names ] self.assertEqual( [expected_names, labels, values], [widget_names, widget_labels, widget_values], )
def test_DictCharWidget_id_for_label_uses_first_fields_name(self): names = [factory.getRandomString()] labels = [factory.getRandomString()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput], names, labels) self.assertEqual( ' _%s' % names[0], widget.id_for_label(' '))
def test_DictCharWidget_renders_with_initial_when_no_value(self): """Widgets should use initial value if rendered without value.""" names = [factory.make_name()] initials = [factory.make_name()] labels = [factory.make_name()] mock_widget = Mock() mock_widget.configure_mock(**{"render.return_value": ""}) widget = DictCharWidget([mock_widget, widgets.TextInput], names, initials, labels, skip_check=True) widget.render('foo', []) self.assertThat( mock_widget.render, MockCalledOnceWith(ANY, initials[0], ANY))
def test_DictCharWidget_renders_with_empty_string_as_input_data(self): names = [factory.make_string(), factory.make_string()] initials = [] labels = [factory.make_string(), factory.make_string()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, initials, labels, skip_check=True) name = factory.make_string() html_widget = fromstring( '<root>' + widget.render(name, '') + '</root>') widget_names = XPath('fieldset/input/@name')(html_widget) widget_labels = XPath('fieldset/label/text()')(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names] self.assertEqual( [expected_names, labels], [widget_names, widget_labels])
def test_DictCharWidget_renders_with_empty_string_as_input_data(self): names = [factory.getRandomString(), factory.getRandomString()] labels = [factory.getRandomString(), factory.getRandomString()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, labels, skip_check=True) name = factory.getRandomString() self.assertEqual( '<fieldset>' '<label>%s</label>' '<input type="text" name="%s" />' '<label>%s</label>' '<input type="text" name="%s" />' '</fieldset>' % ( labels[0], '%s_%s' % (name, names[0]), labels[1], '%s_%s' % (name, names[1]), ), widget.render(name, ''))
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self): names = [factory.getRandomString(), factory.getRandomString()] labels = [factory.getRandomString(), factory.getRandomString()] values = [factory.getRandomString(), factory.getRandomString()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, labels, skip_check=True) name = factory.getRandomString() self.assertEqual( '<fieldset>' '<label>%s</label>' '<input type="text" name="%s" value="%s" />' '<label>%s</label>' '<input type="text" name="%s" value="%s" />' '</fieldset>' % ( labels[0], '%s_%s' % (name, names[0]), values[0], labels[1], '%s_%s' % (name, names[1]), values[1], ), widget.render(name, values))
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self): names = [factory.getRandomString(), factory.getRandomString()] labels = [factory.getRandomString(), factory.getRandomString()] values = [factory.getRandomString(), factory.getRandomString()] widget = DictCharWidget( [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput], names, labels, skip_check=True) name = factory.getRandomString() html_widget = fromstring('<root>' + widget.render(name, values) + '</root>') widget_names = XPath('fieldset/input/@name')(html_widget) widget_labels = XPath('fieldset/label/text()')(html_widget) widget_values = XPath('fieldset/input/@value')(html_widget) expected_names = [ "%s_%s" % (name, widget_name) for widget_name in names ] self.assertEqual([expected_names, labels, values], [widget_names, widget_labels, widget_values])
def test_DictCharWidget_value_from_datadict_values_from_data(self): # 'value_from_datadict' extracts the values corresponding to the # field as a dictionary. names = [factory.getRandomString(), factory.getRandomString()] labels = [factory.getRandomString(), factory.getRandomString()] name = factory.getRandomString() field_1_value = factory.getRandomString() field_2_value = factory.getRandomString() # Create a query string with the field2 before the field1 and another # (unknown) value. data = QueryDict( '%s_%s=%s&%s_%s=%s&%s=%s' % ( name, names[1], field_2_value, name, names[0], field_1_value, factory.getRandomString(), factory.getRandomString()) ) widget = DictCharWidget( [widgets.TextInput, widgets.TextInput], names, labels) self.assertEqual( {names[0]: field_1_value, names[1]: field_2_value}, widget.value_from_datadict(data, None, name))
def test_empty_DictCharWidget_renders_as_empty_string(self): widget = DictCharWidget( [widgets.CheckboxInput], [], [], skip_check=True) self.assertEqual('', widget.render(factory.getRandomString(), ''))
def test_empty_DictCharWidget_renders_as_empty_string(self): widget = DictCharWidget([widgets.CheckboxInput], [], [], [], skip_check=True) self.assertEqual("", widget.render(factory.make_string(), ""))
def test_empty_DictCharWidget_renders_as_empty_string(self): widget = DictCharWidget([widgets.CheckboxInput], [], [], skip_check=True) self.assertEqual('', widget.render(factory.getRandomString(), ''))
def test_DictCharWidget_id_for_label_uses_first_fields_name(self): names = [factory.getRandomString()] labels = [factory.getRandomString()] widget = DictCharWidget([widgets.TextInput, widgets.TextInput], names, labels) self.assertEqual(' _%s' % names[0], widget.id_for_label(' '))