def assertExpectWidget(self, instance, new_class_, equal_attributes=None, new_attributes=None): new_instance = floppify_widget(instance) self.assertEqual(new_instance.__class__, new_class_) if equal_attributes: for attribute in equal_attributes: self.assertTrue( hasattr(instance, attribute), 'Cannot check attribute %r, not available on original ' 'widget %r' % (attribute, instance)) self.assertTrue( hasattr(new_instance, attribute), 'Cannot check attribute %r, not available on generated ' 'widget %r' % (attribute, new_instance)) old_attr = getattr(instance, attribute) new_attr = getattr(new_instance, attribute) self.assertEqual(old_attr, new_attr, 'Original widget\'s attribute was not copied: %r != %r' % (old_attr, new_attr)) if new_attributes: for attribute, value in new_attributes.items(): self.assertTrue( hasattr(new_instance, attribute), 'Cannot check new attribute %r, not available on ' 'generated widget %r' % (attribute, new_instance)) new_attr = getattr(new_instance, attribute) self.assertEqual(new_attr, value, 'Generated widget\'s attribute is not as expected: ' '%r != %r' % (new_attr, value))
def assertExpectWidget(self, instance, new_class_, equal_attributes=None, new_attributes=None): new_instance = floppify_widget(instance) self.assertEqual(new_instance.__class__, new_class_) if equal_attributes: for attribute in equal_attributes: self.assertTrue( hasattr(instance, attribute), 'Cannot check attribute %r, not available on original ' 'widget %r' % (attribute, instance)) self.assertTrue( hasattr(new_instance, attribute), 'Cannot check attribute %r, not available on generated ' 'widget %r' % (attribute, new_instance)) old_attr = getattr(instance, attribute) new_attr = getattr(new_instance, attribute) self.assertEqual( old_attr, new_attr, 'Original widget\'s attribute was not copied: %r != %r' % (old_attr, new_attr)) if new_attributes: for attribute, value in new_attributes.items(): self.assertTrue( hasattr(new_instance, attribute), 'Cannot check new attribute %r, not available on ' 'generated widget %r' % (attribute, new_instance)) new_attr = getattr(new_instance, attribute) self.assertEqual( new_attr, value, 'Generated widget\'s attribute is not as expected: ' '%r != %r' % (new_attr, value))
def test_multi_widget(self): self.assertExpectWidget(forms.widgets.MultiWidget([]), floppyforms.widgets.MultiWidget) text_input = forms.widgets.TextInput() widget = forms.widgets.MultiWidget([text_input]) new_widget = floppify_widget(widget) self.assertEqual(widget.widgets, new_widget.widgets) self.assertTrue(new_widget.widgets[0] is text_input)
def test_checkboxinput_widget(self): self.assertExpectWidget(forms.widgets.CheckboxInput(), floppyforms.widgets.CheckboxInput) check_test = lambda v: False widget = forms.widgets.CheckboxInput(check_test=check_test) new_widget = floppify_widget(widget) self.assertEqual(widget.check_test, new_widget.check_test) self.assertTrue(new_widget.check_test is check_test)
def test_created_widget_doesnt_leak_attributes_into_original_widget(self): widget = forms.TextInput() widget.is_required = True widget.attrs = {'placeholder': 'Search ...'} new_widget = floppify_widget(widget) self.assertFalse(widget.__dict__ is new_widget.__dict__) new_widget.is_required = False self.assertEqual(widget.is_required, True) new_widget.attrs['placeholder'] = 'Enter name' self.assertEqual(widget.attrs['placeholder'], 'Search ...')
def test_created_widget_doesnt_leak_attributes_into_original_widget(self): widget = forms.TextInput() widget.is_required = True widget.attrs = {"placeholder": "Search ..."} new_widget = floppify_widget(widget) self.assertFalse(widget.__dict__ is new_widget.__dict__) new_widget.is_required = False self.assertEqual(widget.is_required, True) new_widget.attrs["placeholder"] = "Enter name" self.assertEqual(widget.attrs["placeholder"], "Search ...")
def test_splitdatetime_widget(self): widget = forms.widgets.SplitDateTimeWidget() self.assertExpectWidget(widget, floppyforms.widgets.SplitDateTimeWidget) widget = forms.widgets.SplitDateTimeWidget(date_format="DATE_FORMAT", time_format="TIME_FORMAT") new_widget = floppify_widget(widget) self.assertTrue(isinstance(new_widget.widgets[0], floppyforms.widgets.DateInput)) self.assertTrue(isinstance(new_widget.widgets[1], floppyforms.widgets.TimeInput)) self.assertEqual(new_widget.widgets[0].format, "DATE_FORMAT") self.assertEqual(new_widget.widgets[1].format, "TIME_FORMAT")
def test_splitdatetime_widget(self): widget = forms.widgets.SplitDateTimeWidget() self.assertExpectWidget(widget, floppyforms.widgets.SplitDateTimeWidget) widget = forms.widgets.SplitDateTimeWidget(date_format='DATE_FORMAT', time_format='TIME_FORMAT') new_widget = floppify_widget(widget) self.assertTrue( isinstance(new_widget.widgets[0], floppyforms.widgets.DateInput)) self.assertTrue( isinstance(new_widget.widgets[1], floppyforms.widgets.TimeInput)) self.assertEqual(new_widget.widgets[0].format, 'DATE_FORMAT') self.assertEqual(new_widget.widgets[1].format, 'TIME_FORMAT')
def test_splithiddendatetime_widget(self): widget = forms.widgets.SplitHiddenDateTimeWidget() self.assertExpectWidget( widget, floppyforms.widgets.SplitHiddenDateTimeWidget) widget = forms.widgets.SplitHiddenDateTimeWidget( date_format='DATE_FORMAT', time_format='TIME_FORMAT') new_widget = floppify_widget(widget) self.assertTrue(isinstance( new_widget.widgets[0], floppyforms.widgets.DateInput)) self.assertTrue(isinstance( new_widget.widgets[1], floppyforms.widgets.TimeInput)) self.assertEqual(new_widget.widgets[0].format, 'DATE_FORMAT') self.assertEqual(new_widget.widgets[0].is_hidden, True) self.assertEqual(new_widget.widgets[1].format, 'TIME_FORMAT') self.assertEqual(new_widget.widgets[1].is_hidden, True)