def test_save_workflow( db, snapshot, workflow, workflow_start_tasks, workflow_allow_forms, schema_executor ): query = """ mutation SaveWorkflow($input: SaveWorkflowInput!) { saveWorkflow(input: $input) { workflow { allowAllForms allowForms { totalCount edges { node { slug } } } slug name meta } clientMutationId } } """ inp = { "input": extract_serializer_input_fields( serializers.SaveWorkflowSerializer, workflow ) } workflow.delete() # test creation result = schema_executor(query, variable_values=inp) assert not result.errors snapshot.assert_match(result.data)
def test_save_form_question(db, snapshot, question, question_option, schema_executor): query = """ mutation SaveFormQuestion($input: SaveFormQuestionInput!) { saveFormQuestion(input: $input) { question { id slug label meta __typename ... on FormQuestion { subForm { slug } } } clientMutationId } } """ inp = { "input": extract_serializer_input_fields(serializers.SaveFormQuestionSerializer, question) } question.delete() # test creation result = schema_executor(query, variable_values=inp) assert not result.errors snapshot.assert_match(result.data)
def test_save_static_question(db, snapshot, question, schema_executor): query = """ mutation SaveStaticQuestion($input: SaveStaticQuestionInput!) { saveStaticQuestion(input: $input) { question { id slug label meta __typename ... on StaticQuestion { staticContent } } clientMutationId } } """ inp = { "input": extract_serializer_input_fields( serializers.SaveStaticQuestionSerializer, question) } result = schema_executor(query, variable_values=inp) assert not bool(result.errors) snapshot.assert_match(result.data)
def test_save_question(db, snapshot, question, mutation, schema_executor, success): mutation_func = mutation[0].lower() + mutation[1:] query = f""" mutation {mutation}($input: {mutation}Input!) {{ {mutation_func}(input: $input) {{ question {{ id slug label meta __typename }} clientMutationId }} }} """ inp = { "input": extract_serializer_input_fields(serializers.SaveQuestionSerializer, question) } result = schema_executor(query, variable_values=inp) assert not bool(result.errors) == success if success: snapshot.assert_match(result.data)
def test_save_textarea_question(db, question, answer, schema_executor): query = """ mutation SaveTextareaQuestion($input: SaveTextareaQuestionInput!) { saveTextareaQuestion(input: $input) { question { id slug label meta __typename ... on TextareaQuestion { maxLength defaultAnswer { value } } } clientMutationId } } """ question.default_answer = answer question.save() inp = { "input": extract_serializer_input_fields( serializers.SaveTextareaQuestionSerializer, question) } result = schema_executor(query, variable_values=inp) assert not result.errors assert result.data["saveTextareaQuestion"]["question"]["maxLength"] == 10 assert (result.data["saveTextareaQuestion"]["question"]["defaultAnswer"] ["value"] == "foo")
def test_save_choice_question( db, snapshot, question, question_option, answer_factory, schema_executor, with_default, ): query = """ mutation SaveChoiceQuestion($input: SaveChoiceQuestionInput!) { saveChoiceQuestion(input: $input) { question { id slug label meta __typename ... on ChoiceQuestion { options { edges { node { slug label } } } hintText defaultAnswer { value } } } clientMutationId } } """ if with_default: question.default_answer = answer_factory( value=question_option.option.slug, question=question) question.hint_text = "test" question.save() inp = { "input": extract_serializer_input_fields( serializers.SaveChoiceQuestionSerializer, question) } if not with_default: question.delete() result = schema_executor(query, variable_values=inp) assert not result.errors snapshot.assert_match(result.data)
def test_save_text_question(db, question, schema_executor, answer, success): query = """ mutation SaveTextQuestion($input: SaveTextQuestionInput!) { saveTextQuestion(input: $input) { question { id slug label meta __typename ... on TextQuestion { maxLength hintText formatValidators { edges { node { slug name regex errorMsg } } } defaultAnswer { value } } } clientMutationId } } """ question.default_answer = answer question.hint_text = "test" question.save() inp = { "input": extract_serializer_input_fields(serializers.SaveTextQuestionSerializer, question) } result = schema_executor(query, variable_values=inp) assert not bool(result.errors) == success if success: assert result.data["saveTextQuestion"]["question"]["maxLength"] == 10 if question.format_validators: assert ( result.data["saveTextQuestion"]["question"]["formatValidators"] ["edges"][0]["node"]["slug"] == "email") assert (result.data["saveTextQuestion"]["question"]["defaultAnswer"] ["value"] == "foo") assert result.data["saveTextQuestion"]["question"][ "hintText"] == "test"
def test_save_default_answer_python_api( db, snapshot, question, answer, question_option, document_factory, answer_factory, answer_document_factory, question_factory, success, delete_answer, admin_user, ): inp = extract_serializer_input_fields( serializers.SaveDefaultAnswerSerializer, answer) if question.type == Question.TYPE_TABLE: documents = document_factory.create_batch(2, form=question.row_form) # create a subtree sub_question = question_factory(type=Question.TYPE_TEXT) document_answer = answer_factory(question=sub_question) documents[0].answers.add(document_answer) answer_document_factory(answer=answer, document=documents[0]) inp["value"] = [str(document.pk) for document in documents] if question.type == Question.TYPE_DATE: inp["value"] = answer.date answer.value = None answer.save() if success: inp["value"] = parse_date(inp["value"]) question.default_answer = answer question.save() if delete_answer: # delete answer to force create test instead of update Answer.objects.filter(pk=answer.pk).delete() if success: answer = api.save_default_answer(question, user=admin_user, value=inp["value"]) snapshot.assert_match(answer) else: with pytest.raises(Exception): api.save_default_answer(question, user=admin_user, value=inp["value"])
def test_save_multiple_choice_question(db, snapshot, question, question_option_factory, answer_factory, schema_executor): question_option_factory.create_batch(2, question=question) option_ids = question.options.order_by("-slug").values_list("slug", flat=True) question.default_answer = answer_factory(value=list(option_ids), question=question) question.hint_text = "test" question.save() query = """ mutation SaveMultipleChoiceQuestion($input: SaveMultipleChoiceQuestionInput!) { saveMultipleChoiceQuestion(input: $input) { question { id slug label meta __typename ... on MultipleChoiceQuestion { options { edges { node { slug label } } } hintText defaultAnswer { value } } } clientMutationId } } """ inp = { "input": extract_serializer_input_fields( serializers.SaveMultipleChoiceQuestionSerializer, question) } inp["input"]["options"] = option_ids result = schema_executor(query, variable_values=inp) assert not result.errors snapshot.assert_match(result.data)
def test_save_dynamic_multiple_choice_question( db, snapshot, question, delete, question_option_factory, schema_executor, data_source_settings, ): query = """ mutation SaveDynamicMultipleChoiceQuestion($input: SaveDynamicMultipleChoiceQuestionInput!) { saveDynamicMultipleChoiceQuestion(input: $input) { question { id slug label meta __typename ... on DynamicMultipleChoiceQuestion { hintText options { edges { node { slug label } } } } } clientMutationId } } """ question.hint_text = "test" question.save() inp = { "input": extract_serializer_input_fields( serializers.SaveDynamicMultipleChoiceQuestionSerializer, question) } if delete: question.delete() # test creation result = schema_executor(query, variable_values=inp) assert not result.errors snapshot.assert_match(result.data)
def test_save_calculated_dependency_default_answer(db, snapshot, question, answer, question_factory, admin_user): question_factory( type=Question.TYPE_CALCULATED_FLOAT, calc_expression=f"'{question.slug}'|answer * 10", ) inp = extract_serializer_input_fields( serializers.SaveDefaultAnswerSerializer, answer) question.default_answer = answer question.save() answer = api.save_default_answer(question, user=admin_user, value=inp["value"]) snapshot.assert_match(answer)
def test_save_integer_question(db, snapshot, question, answer, success, schema_executor): query = """ mutation SaveIntegerQuestion($input: SaveIntegerQuestionInput!) { saveIntegerQuestion(input: $input) { question { id slug label meta __typename ... on IntegerQuestion { minValue maxValue hintText defaultAnswer { value } } } clientMutationId } } """ question.default_answer = answer question.hint_text = "test" question.save() inp = { "input": extract_serializer_input_fields( serializers.SaveIntegerQuestionSerializer, question) } result = schema_executor(query, variable_values=inp) assert not bool(result.errors) == success if success: snapshot.assert_match(result.data) assert (result.data["saveIntegerQuestion"]["question"]["defaultAnswer"] ["value"] == 23) assert result.data["saveIntegerQuestion"]["question"][ "hintText"] == "test"
def test_save_default_answer_graphql( db, snapshot, question, answer, mutation, question_option, document_factory, answer_factory, answer_document_factory, question_factory, success, schema_executor, delete_answer, admin_user, ): mutation_func = mutation[0].lower() + mutation[1:] query = f""" mutation {mutation}($input: {mutation}Input!) {{ {mutation_func}(input: $input) {{ answer {{ __typename ... on StringAnswer {{ stringValue: value }} ... on IntegerAnswer {{ integerValue: value }} ... on ListAnswer {{ listValue: value }} ... on FloatAnswer {{ floatValue: value }} ... on ListAnswer {{ listValue: value }} ... on DateAnswer {{ dateValue: value }} ... on TableAnswer {{ table_value: value {{ form {{ slug }} }} }} }} clientMutationId }} }} """ inp = { "input": extract_serializer_input_fields( serializers.SaveDefaultAnswerSerializer, answer) } if question.type == Question.TYPE_TABLE: documents = document_factory.create_batch(2, form=question.row_form) # create a subtree sub_question = question_factory(type=Question.TYPE_TEXT) document_answer = answer_factory(question=sub_question) documents[0].answers.add(document_answer) answer_document_factory(answer=answer, document=documents[0]) inp["input"]["value"] = [str(document.pk) for document in documents] if question.type == Question.TYPE_DATE: inp["input"]["value"] = answer.date answer.value = None answer.save() question.default_answer = answer question.save() if delete_answer: # delete answer to force create test instead of update Answer.objects.filter(pk=answer.pk).delete() result = schema_executor(query, variable_values=inp) assert not bool(result.errors) == success if success: snapshot.assert_match(result.data)