-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jexl): add support for nested forms (#398)
* feat(jexl): add support for nested forms * Add tests * Refactor jexl api: prefix path in answer transform
- Loading branch information
Showing
5 changed files
with
173 additions
and
32 deletions.
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
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
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,86 @@ | ||
import pytest | ||
from rest_framework.exceptions import ValidationError | ||
|
||
from ...form.models import Document, Question | ||
from ..validators import DocumentValidator | ||
|
||
|
||
@pytest.mark.parametrize("is_required", ["true", "false"]) | ||
def test_validate_simple_required_field( | ||
db, is_required, form_question, document_factory | ||
): | ||
form_question.question.is_required = is_required | ||
form_question.question.save() | ||
|
||
document = document_factory(form=form_question.form) | ||
error_msg = f"Questions {form_question.question.slug} are required but not provided" | ||
if is_required == "true": | ||
with pytest.raises(ValidationError, match=error_msg): | ||
DocumentValidator().validate(document) | ||
else: | ||
DocumentValidator().validate(document) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"required_jexl,should_throw", | ||
[ | ||
("true", True), | ||
("false", False), | ||
("'parent.sub_2.sub_2_question_1'|answer == 'foo'", True), | ||
("'parent.sub_2.sub_2_question_1'|answer == 'bar'", False), | ||
], | ||
) | ||
def test_validate_nested_form( | ||
db, | ||
required_jexl, | ||
should_throw, | ||
form, | ||
form_question_factory, | ||
document_factory, | ||
question_factory, | ||
answer_factory, | ||
answer_document_factory, | ||
): | ||
sub_form_question_1 = form_question_factory( | ||
form__slug="sub_1", | ||
question__type=Question.TYPE_TEXT, | ||
question__is_required=required_jexl, | ||
question__slug="sub_1_question_1", | ||
) | ||
sub_form_question_2 = form_question_factory( | ||
form__slug="sub_2", | ||
question__type=Question.TYPE_TEXT, | ||
question__is_required="true", | ||
question__slug="sub_2_question_1", | ||
) | ||
|
||
main_form_question_1 = form_question_factory( | ||
question__type=Question.TYPE_FORM, | ||
question__sub_form=sub_form_question_1.form, | ||
question__slug="sub_1", | ||
question__is_required="false", | ||
) | ||
form_question_factory( | ||
form=main_form_question_1.form, | ||
question__type=Question.TYPE_FORM, | ||
question__sub_form=sub_form_question_2.form, | ||
question__slug="sub_2", | ||
) | ||
|
||
main_document = document_factory(form=main_form_question_1.form) | ||
|
||
Document.objects.create_and_link_child_documents( | ||
main_form_question_1.form, main_document | ||
) | ||
|
||
sub_2_document = Document.objects.filter(form__slug="sub_2").first() | ||
answer_factory( | ||
document=sub_2_document, question=sub_form_question_2.question, value="foo" | ||
) | ||
|
||
if should_throw: | ||
error_msg = f"Questions {sub_form_question_1.question.slug} are required but not provided" | ||
with pytest.raises(ValidationError, match=error_msg): | ||
DocumentValidator().validate(main_document) | ||
else: | ||
DocumentValidator().validate(main_document) |
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
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