def test_build_choices_without_placeholder(dropdown_answer_schema):
    handler = DropdownHandler(dropdown_answer_schema)

    expected_choices = [("", "Select an answer")
                        ] + [(option["label"], option["value"])
                             for option in dropdown_answer_schema["options"]]

    assert handler.build_choices(
        dropdown_answer_schema["options"]) == expected_choices
예제 #2
0
def test_build_choices_with_placeholder(answer_options, choices,
                                        value_source_resolver, rule_evaluator):
    dropdown_answer_schema = get_dropdown_answer_schema(answer_options)
    dropdown_answer_schema["placeholder"] = "Select an option"
    handler = DropdownHandler(dropdown_answer_schema, value_source_resolver,
                              rule_evaluator, error_messages)

    expected_choices = [("", "Select an option")] + choices

    assert handler.choices == expected_choices
예제 #3
0
def test_build_choices_no_dynamic_or_static_choices(value_source_resolver,
                                                    rule_evaluator):
    handler = DropdownHandler(
        dynamic_radio_options_no_static_options(),
        value_source_resolver,
        rule_evaluator,
        error_messages,
    )
    with pytest.raises(InvalidSchemaConfigurationException) as context:
        assert handler.choices
    assert "No dynamic or static choices" == str(context.value)
def test_get_field(dropdown_answer_schema):
    handler = DropdownHandler(dropdown_answer_schema)

    expected_choices = [("", "Select an answer")
                        ] + [(option["label"], option["value"])
                             for option in dropdown_answer_schema["options"]]

    class TestForm(Form):
        test_field = handler.get_field()

    form = TestForm()

    assert isinstance(form.test_field, SelectField)
    assert form.test_field.label.text == dropdown_answer_schema["label"]
    assert form.test_field.description == ""
    assert form.test_field.default == ""
    assert form.test_field.choices == expected_choices
예제 #5
0
def test_get_field(answer_options, choices, value_source_resolver,
                   rule_evaluator):
    dropdown_answer_schema = get_dropdown_answer_schema(answer_options)
    handler = DropdownHandler(dropdown_answer_schema, value_source_resolver,
                              rule_evaluator, error_messages)

    expected_choices = [("", "Select an answer")] + choices

    class TestForm(Form):
        test_field = handler.get_field()

    form = TestForm()

    assert isinstance(form.test_field, SelectField)
    assert form.test_field.label.text == dropdown_answer_schema["label"]
    assert form.test_field.description == ""
    assert form.test_field.default == ""
    assert form.test_field.choices == expected_choices
예제 #6
0
def test_get_field():
    dropdown_json = {
        "type":
        "Dropdown",
        "id":
        "dropdown-with-label-answer",
        "mandatory":
        False,
        "label":
        "Please choose an option",
        "description":
        "This is an optional dropdown",
        "options": [
            {
                "label": "Liverpool",
                "value": "Liverpool"
            },
            {
                "label": "Chelsea",
                "value": "Chelsea"
            },
            {
                "label": "Rugby is better!",
                "value": "Rugby is better!"
            },
        ],
    }

    handler = DropdownHandler(dropdown_json)

    expected_choices = [("", "Select an answer")
                        ] + [(option["label"], option["value"])
                             for option in dropdown_json["options"]]

    class TestForm(Form):
        test_field = handler.get_field()

    form = TestForm()

    assert isinstance(form.test_field, SelectField)
    assert form.test_field.label.text == dropdown_json["label"]
    assert form.test_field.description == ""
    assert form.test_field.default == ""
    assert form.test_field.choices == expected_choices