def test_render_attrs_raises_for_some_common_pitfall_types(): with pytest.raises( TypeError, match= "Only the class and style attributes can be dicts, you sent {'a': 1} for key foo" ): render_attrs_test(dict(foo=dict(a=1))) with pytest.raises( TypeError, match="Attributes can't be of type list, you sent \\[] for key foo" ): render_attrs_test(dict(foo=[])) with pytest.raises( TypeError, match= "Attributes can't be of type tuple, you sent \\(\\) for key foo"): render_attrs_test(dict(foo=tuple())) with pytest.raises( TypeError, match= "Attributes can't be callable, you sent foo=lambda foo: foo for key foo" ): # fmt: off render_attrs(dict(foo=lambda foo: foo))
def test_apply_checkbox_style(): from iommi import Form from iommi import Field class MyForm(Form): class Meta: iommi_style = 'bootstrap' foo = Field.boolean() form = MyForm() form = form.bind(request=None) assert get_style_name_for(form.fields.foo) == 'bootstrap' assert get_style_data_for_object(style_name=get_style_name_for( form.fields.foo), obj=form.fields.foo)['attrs'] == { 'class': { 'form-group': True, 'form-check': True } } assert render_attrs( form.fields.foo.attrs) == ' class="form-check form-group"' assert render_attrs( form.fields.foo.input.attrs ) == ' class="form-check-input" id="id_foo" name="foo" type="checkbox"' assert render_attrs(form.fields.foo.label.attrs ) == ' class="form-check-label" for="id_foo"'
def fragment__render(fragment, context): if not fragment.include: return '' rendered_children = fragment.render_text_or_children(context=context) if fragment.template: return render_template(fragment.get_request(), fragment.template, dict(**context, **fragment.iommi_evaluate_parameters(), rendered_children=rendered_children)) is_void_element = fragment.tag in _void_elements if fragment.tag: if rendered_children: assert not is_void_element, f'{fragment.tag} is a void element, but it has children: {rendered_children}' return format_html( '<{tag}{attrs}>{children}</{tag}>', tag=fragment.tag, attrs=render_attrs(fragment.attrs), children=rendered_children, ) else: return format_html( '<{tag}{attrs}>' if is_void_element else '<{tag}{attrs}></{tag}>', tag=fragment.tag, attrs=render_attrs(fragment.attrs), ) else: return format_html( '{}', rendered_children, )
def test_render_attrs_non_standard_types(): assert render_attrs({ 'apa': True, 'bepa': '', 'cepa': None, 'class': 'bar baz' }) == ' apa bepa="" class="bar baz"'
def test_empty_class_and_style_and_another(): actual = render_attrs(attrs={ 'class': {}, 'style': {}, 'z': 'bar', }, ) expected = ' z="bar"' assert actual == expected
def test_style_bulk_form(): from iommi import Column, Table from tests.models import Foo with register_style( 'my_style', Style( base, Table__bulk__attrs__class__foo=True, ), ): class MyTable(Table): class Meta: iommi_style = 'my_style' model = Foo bar = Column(bulk__include=True) table = MyTable() table = table.bind(request=None) assert 'foo' in render_attrs(table.bulk.attrs)
def test_render_attrs_none(): assert render_attrs(None) == ''