Skip to content

Commit fa53ca5

Browse files
committed
Add (failing) tests for conditions that are triggered by a multiple choice field
1 parent f7dd549 commit fa53ca5

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

demo/tests/fixtures/test_conditions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ class DropdownConditionsTestForm(FormidableForm):
5252
accesses={'padawan': constants.EDITABLE})
5353

5454

55+
class MultipleChoicesConditionsTestForm(FormidableForm):
56+
main_choices = fields.MultipleChoiceField(
57+
choices=(
58+
('a', 'A'),
59+
('b', 'B'),
60+
('no_condition', 'No_condition')
61+
),
62+
accesses={'padawan': constants.EDITABLE}
63+
)
64+
a = fields.CharField(accesses={'padawan': constants.EDITABLE})
65+
b = fields.CharField(accesses={'padawan': constants.EDITABLE})
66+
c = fields.CharField(accesses={'padawan': constants.EDITABLE})
67+
68+
5569
class SimpleConditionTestCaseTestForm(FormidableForm):
5670
checkbox = fields.BooleanField(
5771
label='My checkbox',

demo/tests/test_conditions.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,92 @@ def test_ab_only_checked_without_name(self):
465465
self.assertTrue('b' in form.cleaned_data)
466466

467467

468+
class MultipleChoiceConditionsTestCase(TestCase):
469+
470+
def get_form_class(self, formidable, role):
471+
return get_dynamic_form_class(formidable, role)
472+
473+
def setUp(self):
474+
super().setUp()
475+
conditions_schema = [
476+
{
477+
'name': 'Show a if "a" is selected',
478+
'action': 'display_iff',
479+
'fields_ids': ['a', ],
480+
'tests': [
481+
{
482+
'field_id': 'main_choices',
483+
'operator': 'eq',
484+
'values': ['a'],
485+
}
486+
]
487+
},
488+
{
489+
'name': 'Show b if value "b" selected',
490+
'action': 'display_iff',
491+
'fields_ids': ['b'],
492+
'tests': [
493+
{
494+
'field_id': 'main_choices',
495+
'operator': 'eq',
496+
'values': ['b'],
497+
}
498+
]
499+
}
500+
]
501+
502+
form_class = test_conditions_fixtures.MultipleChoicesConditionsTestForm
503+
self.formidable = form_class.to_formidable(
504+
label='Multiple Choice Test Form')
505+
self.formidable.conditions = conditions_schema
506+
507+
def test_none_selected(self):
508+
form_class = self.get_form_class(self.formidable, 'padawan')
509+
data = {}
510+
511+
form = form_class(data)
512+
self.assertTrue(form.is_valid())
513+
self.assertTrue('a' not in form.cleaned_data)
514+
self.assertTrue('b' not in form.cleaned_data)
515+
self.assertTrue('c' in form.cleaned_data)
516+
517+
def test_a_only_selected(self):
518+
form_class = self.get_form_class(self.formidable, 'padawan')
519+
data = {
520+
'main_choices': ['a'],
521+
}
522+
523+
form = form_class(data)
524+
self.assertTrue(form.is_valid())
525+
self.assertTrue('a' in form.cleaned_data)
526+
self.assertTrue('b' not in form.cleaned_data)
527+
self.assertTrue('c' in form.cleaned_data)
528+
529+
def test_b_only_selected(self):
530+
form_class = self.get_form_class(self.formidable, 'padawan')
531+
data = {
532+
'main_choices': ['b']
533+
}
534+
535+
form = form_class(data)
536+
self.assertTrue(form.is_valid())
537+
self.assertTrue('a' not in form.cleaned_data)
538+
self.assertTrue('b' in form.cleaned_data)
539+
self.assertTrue('c' in form.cleaned_data)
540+
541+
def test_a_and_b_selected(self):
542+
form_class = self.get_form_class(self.formidable, 'padawan')
543+
data = {
544+
'main_choices': ['a', 'b']
545+
}
546+
547+
form = form_class(data)
548+
self.assertTrue(form.is_valid())
549+
self.assertTrue('a' in form.cleaned_data)
550+
self.assertTrue('b' in form.cleaned_data)
551+
self.assertTrue('c' in form.cleaned_data)
552+
553+
468554
class ConditionSerializerTestCase(TestCase):
469555

470556
payload = {

0 commit comments

Comments
 (0)