示例#1
0
def test_non_parsable_sub_entities():
    """Test that ValueError is raised if one of sub-entities can't be parsed.

    1. Create json parser that raises exception when either parse_embedded_link or
       parse_embedded_representation is called.
    2. Create an embedded representation parser with the parser.
    3. Try to call parse_entities method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    class _SubEntityErrorParser(JSONParser):
        def parse_embedded_link(self, data):
            raise Exception()

        def parse_embedded_representation(self, data):
            raise Exception()

    parser = EmbeddedRepresentationParser(
        data={"entities": [{}, {
            "href": "/target"
        }]},
        parser=_SubEntityErrorParser(),
    )

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

    expected_message = "Failed to parse sub-entities of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
示例#2
0
def test_sub_entities(sub_entities_data):
    """Test that sub-entities are properly parsed.

    1. Create json parser with overridden parse_embedded_link and parse_embedded_representation.
    2. Create an embedded representation parser with the parser.
    3. Parse fields.
    4. Check the parsed fields.
    """
    class _SubEntitiesParser(JSONParser):
        def parse_embedded_link(self, data):
            if "href" not in data:
                pytest.fail(
                    "Try to parse embedded link instead of embedded representation"
                )
            return sub_entities_data.index(data)

        def parse_embedded_representation(self, data):
            if "href" in data:
                pytest.fail(
                    "Try to parse embedded representation instead of embedded link"
                )
            return sub_entities_data.index(data)

    parser = EmbeddedRepresentationParser(
        data={"entities": sub_entities_data},
        parser=_SubEntitiesParser(),
    )
    actual_sub_entities = parser.parse_entities()
    assert actual_sub_entities == tuple(range(
        len(sub_entities_data))), "Wrong sub-entities"
示例#3
0
def test_missing_sub_entities():
    # pylint: disable=line-too-long
    """Test that empty tuple is returned if data of embedded representation don't have 'entities' key.

    1. Create an embedded representation parser for a dictionary without sub-entities.
    2. Parse sub-entities.
    3. Check that empty tuple is returned.
    """
    # pylint: enable=line-too-long
    parser = EmbeddedRepresentationParser(data={}, parser=JSONParser())
    actual_sub_entities = parser.parse_entities()
    assert actual_sub_entities == (), "Wrong sub-entities"
示例#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)