Skip to content

Commit f7dd549

Browse files
committed
Add (failing) tests for integration when validating a form with conditions linked to a multiple choice field
1 parent 4c656ff commit f7dd549

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed
Lines changed: 173 additions & 0 deletions
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/test_integration.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,81 @@ def test_can_create_form_with_dropdown_conditional_fields_via_api(self):
634634
format='json'
635635
)
636636
self.assertEqual(res.status_code, 201)
637+
638+
639+
class TestConditionalRulesWithMultiChoice(FormidableAPITestCase):
640+
641+
def test_can_validate_form_with_multiple_choice_fields(self):
642+
url = 'formidable:form_validation'
643+
644+
class MultipleChoiceForm(FormidableForm):
645+
main_choices = fields.MultipleChoiceField(
646+
choices=(
647+
('first', 'First'),
648+
('second', 'Second'),
649+
('third', 'Third'),
650+
),
651+
accesses={'padawan': constants.EDITABLE}
652+
)
653+
first_field = fields.CharField(
654+
accesses={'padawan': constants.EDITABLE})
655+
second_field = fields.CharField(
656+
accesses={'padawan': constants.EDITABLE})
657+
third_field = fields.CharField(
658+
accesses={'padawan': constants.EDITABLE})
659+
another_field = fields.CharField(
660+
accesses={'padawan': constants.EDITABLE})
661+
662+
form = MultipleChoiceForm.to_formidable(label='Drop Down Test Form')
663+
form.conditions = [
664+
{
665+
'name': 'Show first and second if value "first" selected',
666+
'action': 'display_iff',
667+
'fields_ids': ['first_field', 'second_field', ],
668+
'tests': [
669+
{
670+
'field_id': 'main_choices',
671+
'operator': 'eq',
672+
'values': ['first'],
673+
}
674+
]
675+
},
676+
{
677+
'name': 'Show third if value "second" selected',
678+
'action': 'display_iff',
679+
'fields_ids': ['third_field'],
680+
'tests': [
681+
{
682+
'field_id': 'main_choices',
683+
'operator': 'eq',
684+
'values': ['second'],
685+
}
686+
]
687+
}
688+
]
689+
form.save()
690+
session = self.client.session
691+
session['role'] = 'padawan'
692+
session.save()
693+
res = self.client.post(
694+
reverse(url, args=[form.pk]),
695+
{"main_dropdown": "first", "third_field": "test"},
696+
format='json'
697+
)
698+
self.assertEqual(res.status_code, 204)
699+
700+
def test_can_create_form_with_multiple_choices_via_api(self):
701+
conditions_schema = json.load(open(
702+
os.path.join(
703+
TESTS_DIR, 'fixtures', 'multiple-choices-conditions.json'
704+
)
705+
))
706+
session = self.client.session
707+
session['role'] = 'padawan'
708+
session.save()
709+
res = self.client.post(
710+
reverse('formidable:form_create'),
711+
conditions_schema,
712+
format='json'
713+
)
714+
self.assertEqual(res.status_code, 201)

0 commit comments

Comments
 (0)