Пример #1
0
def test_parse(component_validator):
    """Test that field data is properly parsed.

    1. Create a field.
    2. Create a field parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the field.
    5. Check the parsed data.
    """
    field = Field(
        name="parsed name",
        classes=("parsed class 1", "parsed class 2"),
        input_type=InputType.HIDDEN,
        value="parsed value",
        title="parsed title",
        )

    parser = FieldParser(data={})
    parser.parse_name = lambda: field.name
    parser.parse_classes = lambda: field.classes
    parser.parse_input_type = lambda: field.input_type
    parser.parse_value = lambda: field.value
    parser.parse_title = lambda: field.title

    actual_field = parser.parse()
    component_validator.validate_field(actual_field, field)
Пример #2
0
def test_field_creation_error():
    """Test that ValueError is raised if an error occurs during field creation.

    1. Create a field parser.
    2. Replace parse_input_type method so that it returns invalid input type.
    3. Try to call parse method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    field_data = {
        "name": "name",
        "classes": [],
        }
    parser = FieldParser(data=field_data)
    parser.parse_input_type = lambda: "invalid input type"

    with pytest.raises(ValueError) as error_info:
        parser.parse()

    assert error_info.value.args[0] == "Failed to create a field with provided data", "Wrong error"