1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
|
import random
import django
import pytest
from crispy_forms.bootstrap import (
Accordion,
AccordionGroup,
Alert,
AppendedText,
FieldWithButtons,
InlineCheckboxes,
InlineField,
InlineRadios,
Modal,
PrependedAppendedText,
PrependedText,
StrictButton,
Tab,
TabHolder,
)
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Field, Layout, MultiWidgetField
from crispy_forms.utils import render_crispy_form
from django import forms
from django.template import Context, Template
from django.test import override_settings
from django.utils.translation import activate, deactivate
from django.utils.translation import gettext as _
from crispy_bootstrap5.bootstrap5 import BS5Accordion, FloatingField, Switch
from .forms import (
CheckboxesSampleForm,
CustomCheckboxSelectMultiple,
CustomRadioSelect,
GroupedChoiceForm,
InputsForm,
SampleForm,
SampleFormCustomWidgets,
)
from .utils import parse_expected, parse_form
CONVERTERS = {
"textinput": "inputtext textinput textInput",
"fileinput": "fileinput fileUpload",
"passwordinput": "textinput textInput",
}
def test_field_with_custom_template():
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
Field("email", template="custom_field_template.html")
)
html = render_crispy_form(test_form)
assert "<h1>Special custom field</h1>" in html
def test_multiwidget_field():
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy form %}
"""
)
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
MultiWidgetField(
"datetime_field",
attrs=(
{"rel": "test_dateinput"},
{"rel": "test_timeinput", "style": "width: 30px;", "type": "hidden"},
),
)
)
c = Context({"form": test_form})
html = template.render(c)
assert html.count('class="dateinput') == 1
assert html.count('rel="test_dateinput"') == 1
assert html.count('rel="test_timeinput"') == 2
assert html.count('style="width: 30px;"') == 2
assert html.count('type="hidden"') == 2
def test_field_type_hidden():
template = Template(
"""
{% load crispy_forms_tags %}
{% crispy test_form %}
"""
)
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
Field("email", type="hidden", data_test=12),
Field("datetime_field"),
)
c = Context({"test_form": test_form})
html = template.render(c)
# Check form parameters
assert html.count('data-test="12"') == 1
assert html.count('name="email"') == 1
assert html.count('class="dateinput') == 1
assert html.count('class="timeinput') == 1
def test_field_wrapper_class(settings):
form = SampleForm()
form.helper = FormHelper()
form.helper.layout = Layout(Field("email", wrapper_class="testing"))
html = render_crispy_form(form)
if settings.CRISPY_TEMPLATE_PACK == "bootstrap":
assert html.count('class="control-group testing"') == 1
elif settings.CRISPY_TEMPLATE_PACK in ("bootstrap3", "bootstrap4"):
assert html.count('class="form-group testing"') == 1
def test_html_with_carriage_returns(settings):
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
HTML(
"""
if (a==b){
// some comment
a+1;
foo();
}
"""
)
)
html = render_crispy_form(test_form)
if settings.CRISPY_TEMPLATE_PACK == "uni_form":
assert html.count("\n") == 23
elif settings.CRISPY_TEMPLATE_PACK == "bootstrap":
assert html.count("\n") == 25
else:
assert html.count("\n") == 27
def test_i18n():
activate("es")
form = SampleForm()
form.helper = FormHelper()
form.helper.layout = Layout(HTML(_("Enter a valid value.")))
html = render_crispy_form(form)
assert "Introduzca un valor vĂ¡lido" in html
deactivate()
def test_remove_labels():
form = SampleForm()
# remove boolean field as label is still printed in boostrap
del form.fields["is_company"]
for fields in form:
fields.label = False
html = render_crispy_form(form)
assert "<label" not in html
@pytest.mark.parametrize(
"input,expected",
[
("text_input", "text_input.html"),
("text_area", "text_area.html"),
("checkboxes", "checkboxes.html"),
("radio", "radio.html"),
("single_checkbox", "single_checkbox.html"),
],
)
def test_inputs(input, expected):
form = InputsForm()
form.helper = FormHelper()
form.helper.layout = Layout(input)
assert parse_form(form) == parse_expected(expected)
class TestBootstrapLayoutObjects:
def test_custom_django_widget(self):
# Make sure an inherited RadioSelect gets rendered as it
form = SampleFormCustomWidgets()
assert isinstance(form.fields["inline_radios"].widget, CustomRadioSelect)
form.helper = FormHelper()
form.helper.layout = Layout("inline_radios")
html = render_crispy_form(form)
print(html)
assert 'class="form-check-input"' in html
# Make sure an inherited CheckboxSelectMultiple gets rendered as it
assert isinstance(
form.fields["checkboxes"].widget, CustomCheckboxSelectMultiple
)
form.helper.layout = Layout("checkboxes")
html = render_crispy_form(form)
assert 'class="form-check-input"' in html
@override_settings(CRISPY_CLASS_CONVERTERS=CONVERTERS)
def test_prepended_appended_text(self):
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
PrependedAppendedText(
"email", "@<>&", "gmail.com", css_class="form-control-lg"
),
AppendedText("password1", "#"),
PrependedText("password2", "$"),
)
if django.VERSION < (5, 0):
expected = "test_prepended_appended_text_lt50.html"
else:
expected = "test_prepended_appended_text.html"
assert parse_form(test_form) == parse_expected(expected)
def test_inline_radios(self):
form = CheckboxesSampleForm()
form.helper = FormHelper()
form.helper.layout = Layout(InlineRadios("inline_radios"))
assert parse_form(form) == parse_expected("inline_radios.html")
def test_inline_checkboxes(self):
form = CheckboxesSampleForm()
form.helper = FormHelper()
form.helper.layout = InlineRadios("checkboxes")
assert parse_form(form) == parse_expected("inline_checkboxes.html")
def test_inline_radios_failing(self):
form = CheckboxesSampleForm({})
form.helper = FormHelper()
form.helper.layout = Layout(InlineRadios("inline_radios"))
if django.VERSION < (5, 0):
expected = "inline_radios_failing_lt50.html"
else:
expected = "inline_radios_failing.html"
assert parse_form(form) == parse_expected(expected)
def test_inline_checkboxes_failing(self):
form = CheckboxesSampleForm({})
form.helper = FormHelper()
form.helper.layout = InlineRadios("checkboxes")
if django.VERSION < (5, 0):
expected = "inline_checkboxes_failing_lt50.html"
else:
expected = "inline_checkboxes_failing.html"
assert parse_form(form) == parse_expected(expected)
@override_settings(CRISPY_CLASS_CONVERTERS=CONVERTERS)
def test_accordion_and_accordiongroup(self):
random.seed(0)
form = SampleForm()
form.helper = FormHelper()
form.helper.form_tag = False
form.helper.layout = Layout(
Accordion(
AccordionGroup("one", "first_name"),
AccordionGroup("two", "password1", "password2"),
)
)
assert parse_form(form) == parse_expected("accordion.html")
def test_accordion_active_false_not_rendered(self):
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
Accordion(
AccordionGroup("one", "first_name"),
# there is no ``active`` kwarg here.
)
)
# The first time, there should be one of them there.
html = render_crispy_form(test_form)
accordion_class = "collapse show"
assert (
html.count('<div id="one" class="accordion-collapse %s"' % accordion_class)
== 1
)
test_form.helper.layout = Layout(
Accordion(
AccordionGroup("one", "first_name", active=False),
) # now ``active`` manually set as False
)
# This time, it shouldn't be there at all.
html = render_crispy_form(test_form)
assert (
html.count('<div id="one" class="accordion-collapse %s"' % accordion_class)
== 0
)
@override_settings(CRISPY_CLASS_CONVERTERS=CONVERTERS)
def test_bs5accordion(self):
random.seed(0)
form = SampleForm()
form.helper = FormHelper()
form.helper.form_tag = False
form.helper.layout = Layout(
BS5Accordion(
AccordionGroup("one", "first_name"),
AccordionGroup("two", "password1", "password2"),
)
)
assert parse_form(form) == parse_expected("accordion.html")
def test_bs5accordion_active_false_not_rendered(self):
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
BS5Accordion(
AccordionGroup("one", "first_name"),
# there is no ``active`` kwarg here.
)
)
# The first time, there should be one of them there.
html = render_crispy_form(test_form)
accordion_class = "collapse show"
assert (
html.count('<div id="one" class="accordion-collapse %s"' % accordion_class)
== 1
)
test_form.helper.layout = Layout(
BS5Accordion(
AccordionGroup("one", "first_name", active=False),
) # now ``active`` manually set as False
)
# This time, it shouldn't be there at all.
html = render_crispy_form(test_form)
assert (
html.count('<div id="one" class="accordion-collapse %s"' % accordion_class)
== 0
)
@override_settings(CRISPY_CLASS_CONVERTERS=CONVERTERS)
def test_bs5accordion_flush(self):
random.seed(0)
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.form_tag = False
test_form.helper.layout = Layout(
BS5Accordion(
AccordionGroup("one", "first_name"),
AccordionGroup("two", "password1", "password2"),
flush=True,
)
)
assert parse_form(test_form) == parse_expected("accordion_flush.html")
@override_settings(CRISPY_CLASS_CONVERTERS=CONVERTERS)
def test_bs5accordion_always_open(self):
random.seed(0)
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.form_tag = False
test_form.helper.layout = Layout(
BS5Accordion(
AccordionGroup("one", "first_name"),
AccordionGroup("two", "password1", "password2"),
always_open=True,
)
)
assert parse_form(test_form) == parse_expected("accordion_always_open.html")
def test_alert(self):
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.form_tag = False
test_form.helper.layout = Layout(
Alert(content="Testing...", css_class="alert-primary"),
Alert(content="Testing...", css_class="alert-primary", dismiss=False),
)
assert parse_form(test_form) == parse_expected("alert.html")
def test_tab_and_tab_holder(self):
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.layout = Layout(
TabHolder(
Tab(
"one",
"first_name",
css_id="custom-name",
css_class="first-tab-class active",
),
Tab("two", "password1", "password2"),
)
)
html = render_crispy_form(test_form)
assert (
html.count(
'<ul class="nav nav-tabs"> <li class="nav-item">'
'<a class="nav-link active" href="#custom-name" data-bs-toggle="tab">'
"One</a></li>"
)
== 1
)
assert html.count("tab-pane") == 2
assert html.count('class="tab-pane first-tab-class active"') == 1
assert html.count('<div id="custom-name"') == 1
assert html.count('<div id="two"') == 1
assert html.count('name="first_name"') == 1
assert html.count('name="password1"') == 1
assert html.count('name="password2"') == 1
def test_tab_helper_reuse(self):
# this is a proper form, according to the docs.
# note that the helper is a class property here,
# shared between all instances
class SampleForm(forms.Form):
val1 = forms.CharField(required=False)
val2 = forms.CharField(required=True)
helper = FormHelper()
helper.layout = Layout(
TabHolder(
Tab("one", "val1"),
Tab("two", "val2"),
)
)
# first render of form => everything is fine
test_form = SampleForm()
html = render_crispy_form(test_form)
# second render of form => first tab should be active,
# but not duplicate class
test_form = SampleForm()
html = render_crispy_form(test_form)
assert html.count('class="nav-item active active"') == 0
# render a new form, now with errors
test_form = SampleForm(data={"val1": "foo"})
html = render_crispy_form(test_form)
tab_class = "tab-pane"
# if settings.CRISPY_TEMPLATE_PACK == 'bootstrap4':
# tab_class = 'nav-link'
# else:
# tab_class = 'tab-pane'
# tab 1 should not be active
assert html.count('<div id="one" \n class="{} active'.format(tab_class)) == 0
# tab 2 should be active
assert html.count('<div id="two" \n class="{} active'.format(tab_class)) == 1
def test_radio_attrs(self):
form = CheckboxesSampleForm()
form.fields["inline_radios"].widget.attrs = {"class": "first"}
form.fields["checkboxes"].widget.attrs = {"class": "second"}
html = render_crispy_form(form)
assert 'class="first"' in html
assert 'class="second"' in html
def test_field_with_buttons(self):
form = SampleForm()
form.helper = FormHelper()
form.helper.layout = Layout(
FieldWithButtons(
Field("password1", css_class="span4"),
StrictButton("Go!", css_id="go-button"),
StrictButton("No!", css_class="extra"),
StrictButton("Test", type="submit", name="whatever", value="something"),
css_class="extra",
autocomplete="off",
)
)
html = render_crispy_form(form)
form_group_class = "mb-3"
assert html.count('class="%s extra"' % form_group_class) == 1
assert html.count('autocomplete="off"') == 1
assert html.count('class="span4') == 1
assert html.count('id="go-button"') == 1
assert html.count("Go!") == 1
assert html.count("No!") == 1
assert html.count('class="btn"') == 2
assert html.count('class="btn extra"') == 1
assert html.count('type="submit"') == 1
assert html.count('name="whatever"') == 1
assert html.count('value="something"') == 1
def test_hidden_fields(self):
form = SampleForm()
# All fields hidden
for field in form.fields:
form.fields[field].widget = forms.HiddenInput()
form.helper = FormHelper()
form.helper.layout = Layout(
AppendedText("password1", "foo"),
PrependedText("password2", "bar"),
PrependedAppendedText("email", "bar"),
InlineCheckboxes("first_name"),
InlineRadios("last_name"),
)
html = render_crispy_form(form)
assert html.count("<input") == 5
assert html.count('type="hidden"') == 5
assert html.count("<label") == 0
def test_multiplecheckboxes(self):
test_form = CheckboxesSampleForm()
html = render_crispy_form(test_form)
assert html.count("checked") == 6
test_form.helper = FormHelper(test_form)
test_form.helper[1].wrap(InlineCheckboxes, inline=True)
html = render_crispy_form(test_form)
# TODO Fix this test
# assert html.count('form-check-input"') == 3
def test_multiple_checkboxes_unique_ids(self):
test_form = CheckboxesSampleForm()
html = render_crispy_form(test_form)
expected_ids = [
"checkboxes_0",
"checkboxes_1",
"checkboxes_2",
"alphacheckboxes_0",
"alphacheckboxes_1",
"alphacheckboxes_2",
"numeric_multiple_checkboxes_0",
"numeric_multiple_checkboxes_1",
"numeric_multiple_checkboxes_2",
]
for id_suffix in expected_ids:
expected_str = f'id="id_{id_suffix}"'
assert html.count(expected_str) == 1
def test_inline_field(self):
form = SampleForm()
form.helper = FormHelper()
form.helper.layout = Layout(
InlineField("first_name", wrapper_class="col-4"),
InlineField("is_company", wrapper_class="col-4"),
)
form.helper.form_class = "row row-cols-lg-auto align-items-center"
assert parse_form(form) == parse_expected("test_inline_field.html")
def test_float_field(self):
form = SampleForm()
form.helper = FormHelper()
form.helper.layout = Layout(
FloatingField("first_name"),
)
assert parse_form(form) == parse_expected("test_floating_field.html")
form = InputsForm({})
form.helper = FormHelper()
form.helper.layout = Layout(
FloatingField("text_area"),
FloatingField("select_input"),
)
if django.VERSION < (5, 0):
expected = "test_floating_field_failing_lt50.html"
else:
expected = "test_floating_field_failing.html"
assert parse_form(form) == parse_expected(expected)
def test_grouped_checkboxes_radios(self):
form = GroupedChoiceForm()
form.helper = FormHelper()
form.helper.layout = Layout("checkbox_select_multiple")
assert parse_form(form) == parse_expected("test_grouped_checkboxes.html")
form.helper.layout = Layout("radio")
assert parse_form(form) == parse_expected("test_grouped_radios.html")
form = GroupedChoiceForm({})
form.helper = FormHelper()
form.helper.layout = Layout("checkbox_select_multiple")
if django.VERSION < (5, 0):
expected = "test_grouped_checkboxes_failing_lt50.html"
else:
expected = "test_grouped_checkboxes_failing.html"
assert parse_form(form) == parse_expected(expected)
form.helper.layout = Layout("radio")
if django.VERSION < (5, 0):
expected = "test_grouped_radios_failing_lt50.html"
else:
expected = "test_grouped_radios_failing.html"
assert parse_form(form) == parse_expected(expected)
def test_switch(self):
form = SampleForm()
form["is_company"].help_text = "is_company help text"
form.helper = FormHelper()
form.helper.layout = Layout(Switch("is_company"))
assert parse_form(form) == parse_expected("test_switch.html")
def test_switch_horizontal(self):
form = SampleForm()
form["is_company"].help_text = "is_company help text"
form.helper = FormHelper()
form.helper.label_class = "col-lg-2"
form.helper.field_class = "col-lg-8"
form.helper.form_class = "form-horizontal"
form.helper.layout = Layout(Switch("is_company"), "first_name")
assert parse_form(form) == parse_expected("test_switch_horizontal.html")
def test_modal(self):
test_form = SampleForm()
test_form.helper = FormHelper()
test_form.helper.form_tag = False
test_form.helper.layout = Layout(
Modal(
'field1',
css_id="modal-id-ex",
css_class="modal-class-ex",
title="This is my modal",
)
)
assert parse_form(test_form) == parse_expected("modal.html")
def test_inline_checkboxes(self):
form = CheckboxesSampleForm()
form.helper = FormHelper()
form.helper.layout = InlineRadios("checkboxes")
assert parse_form(form) == parse_expected("inline_checkboxes.html")
|