Beispiel #1
0
def test_parse(component_validator):
    """Test that action data is properly parsed.

    1. Create an action.
    2. Create an action parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the action.
    5. Check the parsed data.
    """
    action = Action(
        name="parsed name",
        classes=("parsed class 1", "parsed class 2"),
        method=Method.PUT,
        target="/parsed/target",
        title="parsed title",
        media_type="application/parsed+type",
        fields=(Field(name="first"), Field(name="second")),
    )

    parser = ActionParser(data={}, parser=JSONParser())
    parser.parse_name = lambda: action.name
    parser.parse_classes = lambda: action.classes
    parser.parse_method = lambda: action.method
    parser.parse_target = lambda: action.target
    parser.parse_title = lambda: action.title
    parser.parse_media_type = lambda: action.media_type
    parser.parse_fields = lambda: action.fields

    actual_action = parser.parse()
    component_validator.validate_action(actual_action, action)
Beispiel #2
0
def test_action_creation_error():
    """Test that ValueError is raised if an error occurs during action creation.

    1. Create an action parser.
    2. Replace parse_method method so that it returns invalid method.
    3. Try to call parse method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    action_data = {
        "name": "name",
        "classes": [],
        "href": "/target",
    }
    parser = ActionParser(data=action_data, parser=JSONParser())
    parser.parse_method = lambda: "invalid method"

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

    assert error_info.value.args[
        0] == "Failed to create an action with provided data", ("Wrong error")