|
1 | 1 | import pytest
|
| 2 | +from graphql_relay import to_global_id |
2 | 3 |
|
3 | 4 | from ...caluma_core.tests import extract_serializer_input_fields
|
4 | 5 | from .. import models, serializers
|
@@ -50,3 +51,191 @@ def test_copy_option(db, option, schema_executor):
|
50 | 51 | assert new_option.label == "Test Option"
|
51 | 52 | assert new_option.meta == option.meta
|
52 | 53 | assert new_option.source == option
|
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture(params=[models.Question.TYPE_CHOICE, True]) |
| 57 | +def option_jexl_setup( |
| 58 | + document, |
| 59 | + question_option_factory, |
| 60 | + question_factory, |
| 61 | + answer_document_factory, |
| 62 | + form_question_factory, |
| 63 | + request, |
| 64 | +): |
| 65 | + def setup(): |
| 66 | + question_type, is_hidden = request.param |
| 67 | + text_question = question_factory(type=models.Question.TYPE_TEXT) |
| 68 | + is_hidden_jexl = "false" |
| 69 | + if is_hidden: |
| 70 | + is_hidden_jexl = f"'{text_question.slug}'|answer == 'foo'" |
| 71 | + question_option = question_option_factory( |
| 72 | + option__is_hidden=is_hidden_jexl, |
| 73 | + option__slug="bar", |
| 74 | + question__type=question_type, |
| 75 | + ) |
| 76 | + question_option_selected = question_option_factory( |
| 77 | + question=question_option.question |
| 78 | + ) |
| 79 | + answer_document_factory( |
| 80 | + document=document, |
| 81 | + answer__document=document, |
| 82 | + answer__question=text_question, |
| 83 | + answer__value="foo", |
| 84 | + ) |
| 85 | + answer_document_factory( |
| 86 | + document=document, |
| 87 | + answer__document=document, |
| 88 | + answer__question=question_option.question, |
| 89 | + answer__value=question_option_selected.option.pk, |
| 90 | + ) |
| 91 | + form_question_factory(form=document.form, question=question_option.question) |
| 92 | + form_question_factory(form=document.form, question=text_question) |
| 93 | + return document, is_hidden, question_option |
| 94 | + |
| 95 | + return setup |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize( |
| 99 | + "option_jexl_setup,option_for_multiple_questions", |
| 100 | + [ |
| 101 | + ( |
| 102 | + [models.Question.TYPE_CHOICE, True], |
| 103 | + False, |
| 104 | + ), |
| 105 | + ( |
| 106 | + [models.Question.TYPE_CHOICE, False], |
| 107 | + False, |
| 108 | + ), |
| 109 | + ( |
| 110 | + [models.Question.TYPE_MULTIPLE_CHOICE, True], |
| 111 | + False, |
| 112 | + ), |
| 113 | + ( |
| 114 | + [models.Question.TYPE_MULTIPLE_CHOICE, False], |
| 115 | + False, |
| 116 | + ), |
| 117 | + ( |
| 118 | + [models.Question.TYPE_CHOICE, True], |
| 119 | + True, |
| 120 | + ), |
| 121 | + ], |
| 122 | + indirect=["option_jexl_setup"], |
| 123 | +) |
| 124 | +def test_option_is_hidden( |
| 125 | + db, |
| 126 | + option_jexl_setup, |
| 127 | + option_for_multiple_questions, |
| 128 | + question_option_factory, |
| 129 | + question_factory, |
| 130 | + schema_executor, |
| 131 | +): |
| 132 | + document, is_hidden, question_option = option_jexl_setup() |
| 133 | + if option_for_multiple_questions: |
| 134 | + question_option_factory( |
| 135 | + option=question_option.option, question=question_factory() |
| 136 | + ) |
| 137 | + |
| 138 | + query = """ |
| 139 | + query Document($id: ID!, $question_id: ID!) { |
| 140 | + allDocuments(filter: [{id: $id}]) { |
| 141 | + edges { |
| 142 | + node { |
| 143 | + answers(filter: [{question: $question_id}]) { |
| 144 | + edges { |
| 145 | + node { |
| 146 | + ... on StringAnswer { |
| 147 | + question { |
| 148 | + ... on ChoiceQuestion { |
| 149 | + options(filter: [{visibleInDocument: $id}]) { |
| 150 | + edges { |
| 151 | + node { |
| 152 | + slug |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + ... on ListAnswer { |
| 160 | + question { |
| 161 | + ... on MultipleChoiceQuestion { |
| 162 | + options(filter: [{visibleInDocument: $id}]) { |
| 163 | + edges { |
| 164 | + node { |
| 165 | + slug |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + """ |
| 180 | + |
| 181 | + variables = { |
| 182 | + "id": to_global_id("Document", document), |
| 183 | + "question_id": to_global_id("Question", question_option.question.pk), |
| 184 | + } |
| 185 | + |
| 186 | + result = schema_executor(query, variable_values=variables) |
| 187 | + assert bool(result.errors) is option_for_multiple_questions |
| 188 | + if option_for_multiple_questions: |
| 189 | + assert len(result.errors) == 1 |
| 190 | + assert result.errors[0].message == ( |
| 191 | + "[ErrorDetail(string='The `visibleInDocument`-filter can only be used, if " |
| 192 | + "the filtered Options all belong to one unique question', code='invalid')]" |
| 193 | + ) |
| 194 | + return |
| 195 | + |
| 196 | + options = result.data["allDocuments"]["edges"][0]["node"]["answers"]["edges"][0][ |
| 197 | + "node" |
| 198 | + ]["question"]["options"]["edges"] |
| 199 | + expected = [{"node": {"slug": "bar"}}, {"node": {"slug": "thing-piece"}}] |
| 200 | + if is_hidden: |
| 201 | + expected = [{"node": {"slug": "thing-piece"}}] |
| 202 | + assert options == expected |
| 203 | + |
| 204 | + |
| 205 | +@pytest.mark.parametrize( |
| 206 | + "option_jexl_setup", |
| 207 | + [ |
| 208 | + [models.Question.TYPE_CHOICE, True], |
| 209 | + [models.Question.TYPE_CHOICE, False], |
| 210 | + ], |
| 211 | + indirect=["option_jexl_setup"], |
| 212 | +) |
| 213 | +def test_option_is_hidden_save( |
| 214 | + db, |
| 215 | + option_jexl_setup, |
| 216 | + schema_executor, |
| 217 | +): |
| 218 | + document, is_hidden, choice_question_option = option_jexl_setup() |
| 219 | + |
| 220 | + query = """ |
| 221 | + mutation saveDocumentStringAnswer($input: SaveDocumentStringAnswerInput!) { |
| 222 | + saveDocumentStringAnswer(input: $input) { |
| 223 | + answer { |
| 224 | + __typename |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + """ |
| 229 | + |
| 230 | + variables = { |
| 231 | + "input": { |
| 232 | + "document": to_global_id("Document", document.pk), |
| 233 | + "question": to_global_id( |
| 234 | + "ChoiceQuestion", choice_question_option.question.pk |
| 235 | + ), |
| 236 | + "value": choice_question_option.option.pk, |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + result = schema_executor(query, variable_values=variables) |
| 241 | + assert bool(result.errors) is is_hidden |
0 commit comments