def test_button_with_icon(self): res = render_template_with_form( "{% bootstrap_button 'test' icon='info-sign' %}") self.assertEqual( res.strip(), '<button class="btn btn-default"><span class="glyphicon glyphicon-info-sign"></span> test</button>', ) res = render_template_with_form( "{% bootstrap_button 'test' icon='info-sign' button_class='btn-primary' %}" ) self.assertEqual( res.strip(), '<button class="btn btn-primary"><span class="glyphicon glyphicon-info-sign"></span> test</button>', ) res = render_template_with_form( "{% bootstrap_button 'test' icon='info-sign' button_type='submit' %}" ) self.assertHTMLEqual( res, "<button" ' class="btn btn-default"' ' type="submit">' "<span" ' class="glyphicon glyphicon-info-sign"></span>' " test</button>", )
def test_settings_filter(self): res = render_template_with_form( '{{ "required_css_class"|bootstrap_setting }}') self.assertEqual(res.strip(), "bootstrap3-req") res = render_template_with_form( '{% if "javascript_in_head"|bootstrap_setting %}head{% else %}body{% endif %}' ) self.assertEqual(res.strip(), "head")
def test_button(self): res = render_template_with_form("{% bootstrap_button 'button' size='lg' %}") self.assertEqual(res.strip(), '<button class="btn btn-default btn-lg">button</button>') res = render_template_with_form("{% bootstrap_button 'button' size='lg' href='#' %}") self.assertIn( res.strip(), '<a class="btn btn-default btn-lg" href="#">button</a><a href="#" class="btn btn-lg">button</a>', )
def test_overwrite_placeholder(self): res = render_template_with_form('{% bootstrap_field form.sender placeholder="foo" %}') self.assertIn('placeholder="foo', res) # If set_placeholder is set, also consider label override for placeholder res = render_template_with_form('{% bootstrap_field form.sender label="foo" %}') self.assertNotIn("Sender", res) self.assertIn('placeholder="foo', res) self.assertIn("foo</label>", res)
def test_error_class(self): form = TestForm({"sender": "sender"}) res = render_template_with_form("{% bootstrap_form form %}", {"form": form}) self.assertIn("bootstrap3-err", res) res = render_template_with_form('{% bootstrap_form form error_css_class="successful-test" %}', {"form": form}) self.assertIn("successful-test", res) res = render_template_with_form('{% bootstrap_form form error_css_class="" %}', {"form": form}) self.assertNotIn("bootstrap3-err", res)
def test_icon(self): res = render_template_with_form('{% bootstrap_icon "star" %}') self.assertEqual(res.strip(), '<span class="glyphicon glyphicon-star"></span>') res = render_template_with_form('{% bootstrap_icon "star" title="alpha centauri" %}') self.assertIn( res.strip(), [ '<span class="glyphicon glyphicon-star" title="alpha centauri"></span>', '<span title="alpha centauri" class="glyphicon glyphicon-star"></span>', ], )
def test_layout_horizontal(self): form = TestForm() res = render_template_with_form('{% bootstrap_form form layout="horizontal" %}', {"form": form}) self.assertIn("col-md-3", res) self.assertIn("col-md-9", res) res = render_template_with_form( '{% bootstrap_form form layout="horizontal" ' + 'horizontal_label_class="hlabel" ' + 'horizontal_field_class="hfield" %}', {"form": form}, ) self.assertIn("hlabel", res) self.assertIn("hfield", res)
def test_input_group(self): res = render_template_with_form( '{% bootstrap_field form.subject addon_before="$" addon_after=".00" %}' ) self.assertIn('class="input-group"', res) self.assertIn('class="input-group-addon">$', res) self.assertIn('class="input-group-addon">.00', res)
def test_show_help(self): res = render_form_field("subject") self.assertIn("my_help_text", res) self.assertNotIn("<i>my_help_text</i>", res) res = render_template_with_form( "{% bootstrap_field form.subject show_help=0 %}") self.assertNotIn("my_help_text", res)
def test_for_formset(self): TestFormSet = formset_factory(TestForm, extra=1) test_formset = TestFormSet() res = render_template_with_form( "{% bootstrap_formset formset show_label=False %}", {"formset": test_formset}) self.assertIn("sr-only", res)
def test_buttons_tag(self): form = TestForm() res = render_template_with_form( '{% buttons layout="horizontal" %}{% endbuttons %}', {"form": form}) self.assertIn("col-md-3", res) self.assertIn("col-md-9", res)
def test_bootstrap_css_tag(self): res = render_template_with_form("{% bootstrap_css %}").strip() css_url = get_bootstrap_setting("css_url") expected_html = ( '<link href="{url}" crossorigin="{crossorigin}" integrity="{integrity}" rel="stylesheet">' .format(**css_url) + '<link href="//example.com/theme.css" rel="stylesheet">') self.assertHTMLEqual(expected_html, res)
def test_bootstrap_javascript_tag(self): res = render_template_with_form("{% bootstrap_javascript %}") javascript_url = get_bootstrap_setting("javascript_url") self.assertHTMLEqual( res, '<script src="{url}" crossorigin="{crossorigin}" integrity="{integrity}"></script>' .format(**javascript_url), )
def test_input_group_addon_button(self): res = render_template_with_form( "{% bootstrap_field form.subject " 'addon_before="$" addon_before_class="input-group-btn" ' 'addon_after=".00" addon_after_class="input-group-btn" %}') self.assertIn('class="input-group"', res) self.assertIn('class="input-group-btn">$', res) self.assertIn('class="input-group-btn">.00', res)
def test_alert_with_safe_html(self): res = render_template_with_form('{% bootstrap_alert "Foo<br>Bar"|safe %}') self.assertEqual( res.strip(), '<div class="alert alert-info alert-dismissable">' + '<button type="button" class="close" data-dismiss="alert" ' + 'aria-hidden="true">' + "×</button>Foo<br>Bar</div>", )
def test_alert(self): res = render_template_with_form( '{% bootstrap_alert "content" alert_type="danger" %}') self.assertEqual( res.strip(), '<div class="alert alert-danger alert-dismissable">' + '<button type="button" class="close" data-dismiss="alert" ' + 'aria-hidden="true">' + "×</button>content</div>", )
def test_error_types(self): form = SmallTestForm({"sender": "sender"}) pattern = re.compile(r"\s") res = render_template_with_form( '{% bootstrap_form form error_types="all" %}', {"form": form}) expected = """ <div class="alert alert-danger alert-dismissable alert-link"> <button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button> Enter a valid email address.<br> This field is required.<br> This error was added to show the non field errors styling. </div> """ self.assertIn(re.sub(pattern, "", expected), re.sub(pattern, "", res)) res = render_template_with_form( '{% bootstrap_form form error_types="non_field_errors" %}', {"form": form}) expected = """ <div class="alert alert-danger alert-dismissable alert-link"> <button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button> This error was added to show the non field errors styling. </div> """ self.assertIn(re.sub(pattern, "", expected), re.sub(pattern, "", res)) res2 = render_template_with_form("{% bootstrap_form form %}", {"form": form}) self.assertEqual(res, res2) res = render_template_with_form( '{% bootstrap_form form error_types="field_errors" %}', {"form": form}) expected = """ <div class="alert alert-danger alert-dismissable alert-link"> <button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button> Enter a valid email address.<br> This field is required. </div> """ self.assertIn(re.sub(pattern, "", expected), re.sub(pattern, "", res))
def test_required_field(self): required_css_class = "bootstrap3-req" required_field = render_form_field("subject") self.assertIn(required_css_class, required_field) not_required_field = render_form_field("message") self.assertNotIn(required_css_class, not_required_field) # Required settings in field form_field = "form.subject" rendered = render_template_with_form( "{% bootstrap_field " + form_field + ' required_css_class="test-required" %}') self.assertIn("test-required", rendered)
def assertFieldHasAddons(self, field): """Assert that a given field has an after and before addon.""" addon_before = "bf" addon_after = "af" res = render_template_with_form( '{{% bootstrap_field form.{0} addon_before="{1}" addon_after="{2}" %}}' .format(field, addon_before, addon_after)) self.assertIn('class="input-group"', res) self.assertIn('class="input-group-addon">{0}'.format(addon_before), res) self.assertIn('class="input-group-addon">{0}'.format(addon_after), res)
def test_exclude(self): form = TestForm() res = render_template_with_form( '{% bootstrap_form form exclude="cc_myself" %}', {"form": form}) self.assertNotIn("cc_myself", res)
def test_show_label(self): form = TestForm() res = render_template_with_form( "{% bootstrap_form form show_label=False %}", {"form": form}) self.assertIn("sr-only", res)
def test_messages(self): class FakeMessage: """Follows the `django.contrib.messages.storage.base.Message` API.""" level = None message = None extra_tags = None def __init__(self, level, message, extra_tags=None): self.level = level self.extra_tags = extra_tags self.message = message def __str__(self): return self.message pattern = re.compile(r"\s+") messages = [FakeMessage(DEFAULT_MESSAGE_LEVELS.WARNING, "hello")] res = render_template_with_form("{% bootstrap_messages messages %}", {"messages": messages}) expected = """ <div class="alert alert-warning alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> hello </div> """ self.assertEqual(re.sub(pattern, "", res), re.sub(pattern, "", expected)) messages = [FakeMessage(DEFAULT_MESSAGE_LEVELS.ERROR, "hello")] res = render_template_with_form("{% bootstrap_messages messages %}", {"messages": messages}) expected = """ <div class="alert alert-danger alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> hello </div> """ self.assertEqual(re.sub(pattern, "", res), re.sub(pattern, "", expected)) messages = [FakeMessage(None, "hello")] res = render_template_with_form("{% bootstrap_messages messages %}", {"messages": messages}) expected = """ <div class="alert alert-danger alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> hello </div> """ self.assertEqual(re.sub(pattern, "", res), re.sub(pattern, "", expected)) messages = [ FakeMessage(DEFAULT_MESSAGE_LEVELS.ERROR, "hello http://example.com") ] res = render_template_with_form("{% bootstrap_messages messages %}", {"messages": messages}) expected = """ <div class="alert alert-danger alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> hello http://example.com </div> """ self.assertEqual( re.sub(pattern, "", res).replace('rel="nofollow"', ""), re.sub(pattern, "", expected).replace('rel="nofollow"', ""), ) messages = [FakeMessage(DEFAULT_MESSAGE_LEVELS.ERROR, "hello\nthere")] res = render_template_with_form("{% bootstrap_messages messages %}", {"messages": messages}) expected = """ <div class="alert alert-danger alert-dismissable"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> hello there </div> """ self.assertEqual(re.sub(pattern, "", res), re.sub(pattern, "", expected))
def test_overwrite_label(self): res = render_template_with_form( '{% bootstrap_field form.sender label="foo" %}') self.assertNotIn("Sender", res) self.assertIn("foo", res)
def test_placeholder(self): res = render_template_with_form("{% bootstrap_field form.sender %}") self.assertIn('placeholder="Sender', res)
def test_label(self): res = render_template_with_form( '{% bootstrap_label "foobar" label_for="subject" %}') self.assertEqual('<label for="subject">foobar</label>', res)
def test_javascript_with_jquery(self): res = render_template_with_form("{% bootstrap_javascript jquery=1 %}") self.assertIn("bootstrap", res) self.assertIn("jquery", res)
def test_text_template(self): res = render_template_with_form("some text") self.assertEqual(res.strip(), "some text")
def _test_size(param, klass): res = render_template_with_form( '{% bootstrap_field form.subject size="' + param + '" %}') self.assertIn(klass, res)
def test_empty_template(self): res = render_template_with_form("") self.assertEqual(res.strip(), "")
def _test_size_medium(param): res = render_template_with_form( '{% bootstrap_field form.subject size="' + param + '" %}') self.assertNotIn("input-lg", res) self.assertNotIn("input-sm", res) self.assertNotIn("input-md", res)