Esempio n. 1
0
def test_non_parsable_properties():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if data of embedded representation has invalid JSON object as properties.

    1. Create an embedded representation parser for a dictionary with invalid JSON object in 'properties'.
    2. Try to call parse_properties method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    # pylint: enable=line-too-long
    parser = EmbeddedRepresentationParser(data={"properties": object()},
                                          parser=JSONParser())
    with pytest.raises(ValueError) as error_info:
        parser.parse_properties()

    expected_message = "Failed to parse properties of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
Esempio n. 2
0
def test_properties(properties, expected_properties):
    """Test that properties are properly parsed.

    1. Create an embedded representation parser for a dictionary with specific properties.
    2. Parse properties.
    3. Check the parsed properties.
    """
    parser = EmbeddedRepresentationParser(data={"properties": properties},
                                          parser=JSONParser())
    actual_properties = parser.parse_properties()
    assert actual_properties == expected_properties, "Wrong properties"
Esempio n. 3
0
def test_missing_properties():
    # pylint: disable=line-too-long
    """Test that empty dictionary is returned if data of embedded representation don't have 'properties' key.

    1. Create an embedded representation parser for a dictionary without properties.
    2. Parse properties.
    3. Check that empty dictionary is returned.
    """
    # pylint: enable=line-too-long
    parser = EmbeddedRepresentationParser(data={}, parser=JSONParser())
    actual_properties = parser.parse_properties()
    assert actual_properties == {}, "Wrong properties"
Esempio n. 4
0
def test_parse(component_validator):
    """Test that data of embedded representation is properly parsed.

    1. Create an embedded representation.
    2. Create an embedded representation parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the embedded representation.
    5. Check the data of the embedded representation.
    """
    representation = EmbeddedRepresentation(
        relations=["parsed relation"],
        classes=("parsed class 1", "parsed class 2"),
        properties={
            "property 1": 1,
            "property 2": [1, 2]
        },
        entities=(
            EmbeddedLink(target="/embedded/link/target",
                         relations=["relation"]),
            EmbeddedRepresentation(relations=["relation"]),
        ),
        links=[Link(target="/link/target", relations=["relation"])],
        actions=[Action(target="/action/target", name="action")],
        title="parsed title",
    )

    parser = EmbeddedRepresentationParser(data={}, parser=JSONParser())
    parser.parse_relations = lambda: representation.relations
    parser.parse_classes = lambda: representation.classes
    parser.parse_properties = lambda: representation.properties
    parser.parse_entities = lambda: representation.entities
    parser.parse_links = lambda: representation.links
    parser.parse_actions = lambda: representation.actions
    parser.parse_title = lambda: representation.title

    actual_representation = parser.parse()
    component_validator.validate_embedded_representation(
        actual_representation, representation)