예제 #1
0
def test_parse(component_validator):
    """Test that data of embedded link is properly parsed.

    1. Create an embedded link.
    2. Create an embedded link parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the embedded link.
    5. Check the parsed data.
    """
    embedded_link = EmbeddedLink(
        relations=("parsed relation 1", "parsed relation 2"),
        classes=("parsed class 1", "parsed class 2"),
        title="parsed title",
        target="/parsed/target",
        target_media_type="application/parsed+media+type",
        )

    parser = EmbeddedLinkParser(data={})
    parser.parse_relations = lambda: embedded_link.relations
    parser.parse_classes = lambda: embedded_link.classes
    parser.parse_target = lambda: embedded_link.target
    parser.parse_title = lambda: embedded_link.title
    parser.parse_target_media_type = lambda: embedded_link.target_media_type

    actual_embedded_link = parser.parse()
    component_validator.validate_embedded_link(actual_embedded_link, embedded_link)
예제 #2
0
def test_embedded_link_creation_error():
    """Test that ValueError is raised if an error occurs during embedded link creation.

    1. Create an embedded link parser.
    2. Replace parse_classes method so that it returns invalid object for classes.
    3. Try to call parse method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    embedded_link_data = {
        "rel": ["link relation 1"],
        "href": "/target",
        }
    parser = EmbeddedLinkParser(data=embedded_link_data)
    parser.parse_classes = lambda: 1

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

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