-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix dsfr_input_class_attr triggering eaunexpected validation in DsfrB…
…aseForm.__init__ (#190)
- Loading branch information
1 parent
830c563
commit 7c18a3d
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import NoReturn | ||
|
||
from django import forms | ||
from django.test import SimpleTestCase | ||
|
||
|
||
from dsfr.forms import DsfrBaseForm | ||
|
||
|
||
class DsfrBaseFormTestCase(SimpleTestCase): | ||
class TestForm(DsfrBaseForm): | ||
book_format = forms.ChoiceField( | ||
label="Format", | ||
choices=( | ||
("PAPER", "Papier"), | ||
("NUM", "Numérique"), | ||
), | ||
) | ||
book_format2 = forms.ChoiceField( | ||
label="Format", | ||
choices=( | ||
("PAPER", "Papier"), | ||
("NUM", "Numérique"), | ||
), | ||
widget=forms.RadioSelect, | ||
) | ||
user_id = forms.CharField(widget=forms.HiddenInput) | ||
|
||
def test_init_sets_attributes(self): | ||
form = self.TestForm(data={}) | ||
self.assertEqual(form.fields["book_format"].widget.attrs["class"], "fr-select") | ||
self.assertEqual( | ||
form.fields["book_format"].widget.group_class, "fr-select-group" | ||
) | ||
self.assertEqual(form.fields["book_format2"].widget.attrs["dsfr"], "dsfr") | ||
self.assertEqual( | ||
form.fields["book_format2"].widget.group_class, "fr-radio-group" | ||
) | ||
self.assertEqual(form.fields["user_id"].widget.attrs, {}) | ||
|
||
def test_init_dont_trigger_form_validation(self): | ||
test_ctx = self | ||
|
||
class AssertForm(self.TestForm): | ||
@property | ||
def errors(self) -> NoReturn: | ||
test_ctx.fail("No validation expected") | ||
|
||
# __init__ should not trigger validation | ||
AssertForm(data={}) | ||
|
||
with self.assertRaises(self.failureException): | ||
# is_valid should trigger validation | ||
AssertForm(data={}).is_valid() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters