Skip to content

Commit f42a42d

Browse files
authored
Merge pull request #381 from peopledoc/fix-conditional-rules-multiple-answers
Fix conditional rules multiple answers
2 parents 3fc14df + b8cfe2a commit f42a42d

File tree

6 files changed

+377
-24
lines changed

6 files changed

+377
-24
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ master (unreleased)
66
===================
77

88
- DOC: removed ``setup.py`` Python2-related classifiers.
9+
- Fix the support for conditions when their trigger field is a multiple choice field.
910

1011
Release 4.0.0 (2020-01-08)
1112
==========================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
{
2+
"label": "Multiple Choices w/ condition Test Form",
3+
"description": "Multiple Choices w/ condition Test Form",
4+
"fields": [{
5+
"slug": "main_choices",
6+
"label": "main_choices",
7+
"type_id": "checkboxes",
8+
"placeholder": "",
9+
"description": "Main Choices",
10+
"accesses": [{
11+
"access_id": "padawan",
12+
"level": "EDITABLE"
13+
},
14+
{
15+
"access_id": "robot",
16+
"level": "EDITABLE"
17+
},
18+
{
19+
"access_id": "jedi",
20+
"level": "EDITABLE"
21+
},
22+
{
23+
"access_id": "jedi-master",
24+
"level": "EDITABLE"
25+
},
26+
{
27+
"access_id": "human",
28+
"level": "EDITABLE"
29+
}
30+
],
31+
"validations": [],
32+
"defaults": [],
33+
"items": [{
34+
"value": "first",
35+
"label": "First",
36+
"description": null
37+
},
38+
{
39+
"value": "second",
40+
"label": "Second",
41+
"description": null
42+
},
43+
{
44+
"value": "third",
45+
"label": "Third",
46+
"description": null
47+
}
48+
],
49+
"multiple": true
50+
},
51+
{
52+
"slug": "first_field",
53+
"label": "first_field",
54+
"type_id": "text",
55+
"placeholder": "first_field",
56+
"description": "first_field",
57+
"accesses": [{
58+
"access_id": "padawan",
59+
"level": "EDITABLE"
60+
},
61+
{
62+
"access_id": "robot",
63+
"level": "EDITABLE"
64+
},
65+
{
66+
"access_id": "jedi",
67+
"level": "EDITABLE"
68+
},
69+
{
70+
"access_id": "jedi-master",
71+
"level": "EDITABLE"
72+
},
73+
{
74+
"access_id": "human",
75+
"level": "EDITABLE"
76+
}
77+
],
78+
"validations": [],
79+
"defaults": []
80+
},
81+
{
82+
"slug": "second_field",
83+
"label": "second_field",
84+
"type_id": "text",
85+
"placeholder": "second_field",
86+
"description": "second_field",
87+
"accesses": [{
88+
"access_id": "padawan",
89+
"level": "EDITABLE"
90+
},
91+
{
92+
"access_id": "robot",
93+
"level": "EDITABLE"
94+
},
95+
{
96+
"access_id": "jedi",
97+
"level": "EDITABLE"
98+
},
99+
{
100+
"access_id": "jedi-master",
101+
"level": "EDITABLE"
102+
},
103+
{
104+
"access_id": "human",
105+
"level": "EDITABLE"
106+
}
107+
],
108+
"validations": [],
109+
"defaults": []
110+
},
111+
{
112+
"slug": "third_field",
113+
"label": "third_field",
114+
"type_id": "text",
115+
"placeholder": null,
116+
"description": "",
117+
"accesses": [{
118+
"access_id": "padawan",
119+
"level": "EDITABLE"
120+
},
121+
{
122+
"access_id": "robot",
123+
"level": "EDITABLE"
124+
},
125+
{
126+
"access_id": "jedi",
127+
"level": "EDITABLE"
128+
},
129+
{
130+
"access_id": "jedi-master",
131+
"level": "EDITABLE"
132+
},
133+
{
134+
"access_id": "human",
135+
"level": "EDITABLE"
136+
}
137+
],
138+
"validations": [],
139+
"defaults": []
140+
}
141+
],
142+
"id": 1,
143+
"conditions": [{
144+
"name": "Show first and second if value 'first' selected",
145+
"fields_ids": [
146+
"first_field",
147+
"second_field"
148+
],
149+
"action": "display_iff",
150+
"tests": [{
151+
"field_id": "main_choices",
152+
"operator": "eq",
153+
"values": [
154+
"first"
155+
]
156+
}]
157+
},
158+
{
159+
"name": "Show third if value 'second' selected",
160+
"fields_ids": [
161+
"third_field"
162+
],
163+
"action": "display_iff",
164+
"tests": [{
165+
"field_id": "main_choices",
166+
"operator": "eq",
167+
"values": [
168+
"second"
169+
]
170+
}]
171+
}
172+
]
173+
}

demo/tests/fixtures/test_conditions.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from formidable import constants
55

66

7-
class DropdownConditionsTestCaseTestForm(FormidableForm):
7+
class CheckboxConditionsTestForm(FormidableForm):
88
checkbox_a = fields.BooleanField(
99
label='My checkbox',
1010
accesses={'padawan': constants.EDITABLE,
@@ -35,7 +35,7 @@ class DropdownConditionsTestCaseTestForm(FormidableForm):
3535
)
3636

3737

38-
class DropdownConditionsTestCaseDropDownForm(FormidableForm):
38+
class DropdownConditionsTestForm(FormidableForm):
3939
main_dropdown = fields.ChoiceField(
4040
choices=(
4141
('ab', 'AB'),
@@ -52,7 +52,21 @@ class DropdownConditionsTestCaseDropDownForm(FormidableForm):
5252
accesses={'padawan': constants.EDITABLE})
5353

5454

55-
class ConditionTestCaseTestForm(FormidableForm):
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+
69+
class SimpleConditionTestCaseTestForm(FormidableForm):
5670
checkbox = fields.BooleanField(
5771
label='My checkbox',
5872
accesses={'padawan': constants.EDITABLE,

demo/tests/test_conditions.py

+89-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def setUp(self):
4747
]
4848
}
4949
]
50-
form_class = test_conditions_fixtures.ConditionTestCaseTestForm
50+
form_class = test_conditions_fixtures.SimpleConditionTestCaseTestForm
5151
self.formidable = form_class.to_formidable(label='title')
5252
self.formidable.conditions = conditions_schema
5353

@@ -310,7 +310,7 @@ def setUp(self):
310310
}
311311
]
312312

313-
form_class = test_conditions_fixtures.DropdownConditionsTestCaseDropDownForm # noqa
313+
form_class = test_conditions_fixtures.DropdownConditionsTestForm
314314
self.formidable = form_class.to_formidable(
315315
label='Drop Down Test Form')
316316
self.formidable.conditions = conditions_schema
@@ -396,7 +396,7 @@ def setUp(self):
396396
}
397397

398398
]
399-
test_form = test_conditions_fixtures.DropdownConditionsTestCaseTestForm
399+
test_form = test_conditions_fixtures.CheckboxConditionsTestForm
400400
self.formidable = test_form.to_formidable(label='title')
401401
self.formidable.conditions = conditions_schema
402402

@@ -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)