コード例 #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_title(title, expected_title):
    """Test that title is properly parsed.

    1. Create an embedded representation parser for dictionary with specific title.
    2. Parse a title.
    3. Check the parsed title.
    """
    parser = EmbeddedRepresentationParser(data={"title": title},
                                          parser=JSONParser())
    actual_title = parser.parse_title()
    assert actual_title == expected_title, "Wrong title"
コード例 #4
0
def test_relations(relations, expected_relations):
    """Test that relations are properly parsed.

    1. Create an embedded representation parser for a dictionary with specific relations.
    2. Parse relations.
    3. Check the parsed classes.
    """
    parser = EmbeddedRepresentationParser(data={"rel": relations},
                                          parser=JSONParser())
    actual_relations = parser.parse_relations()
    assert actual_relations == expected_relations, "Wrong relations"
コード例 #5
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"
コード例 #6
0
def test_classes(classes, expected_classes):
    """Test that classes are properly parsed.

    1. Create an embedded representation parser for a dictionary with specific classes.
    2. Parse classes.
    3. Check the parsed classes.
    """
    parser = EmbeddedRepresentationParser(data={"class": classes},
                                          parser=JSONParser())
    actual_classes = parser.parse_classes()
    assert actual_classes == expected_classes, "Wrong classes"
コード例 #7
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"
コード例 #8
0
def test_actions(actions_data):
    """Test that actions are properly parsed.

    1. Create json parser.
    2. Replace parse_action method of the parser so that it returns fake data.
    3. Create an embedded representation parser with the parser.
    4. Parse fields.
    5. Check the parsed fields.
    """
    json_parser = JSONParser()
    json_parser.parse_action = actions_data.index

    parser = EmbeddedRepresentationParser(data={"actions": actions_data},
                                          parser=json_parser)
    actual_actions = parser.parse_actions()
    assert actual_actions == tuple(range(len(actions_data))), "Wrong actions"
コード例 #9
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"
コード例 #10
0
ファイル: parser.py プロジェクト: KillAChicken/lila
    def create_embedded_representation_parser(self, data):
        """Factory method to create a parser for an embedded representation.

        :param data: data of embedded representation to parse.
        :returns: :class:`EmbeddedRepresentationParser
            <lila.serialization.json.entity.EmbeddedRepresentationParser>`.
        """
        return EmbeddedRepresentationParser(data=data, parser=self)
コード例 #11
0
def test_embedded_representation_creation_error():
    """Test that ValueError is raised if an error occurs during embedded representation creation.

    1. Create an embedded representation parser.
    2. Replace parse_relations method so that it returns invalid classes.
    3. Try to call parse method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    parser = EmbeddedRepresentationParser(data={}, parser=JSONParser())
    parser.parse_relations = lambda: 1

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

    expected_message = "Failed to create an embedded representation with provided data"
    assert error_info.value.args[0] == expected_message, "Wrong error"
コード例 #12
0
def test_missing_title():
    """Test that None is returned if data of embedded representation don't have 'title' key.

    1. Create an embedded representation parser for a dictionary without title.
    2. Parse title.
    3. Check that None is returned.
    """
    actual_title = EmbeddedRepresentationParser(
        data={}, parser=JSONParser()).parse_title()
    assert actual_title is None, "Wrong title"
コード例 #13
0
def test_missing_links():
    """Test that empty tuple is returned if data of embedded representation don't have 'links' key.

    1. Create an embedded representation parser for a dictionary without links.
    2. Parse links.
    3. Check that empty tuple is returned.
    """
    actual_links = EmbeddedRepresentationParser(
        data={}, parser=JSONParser()).parse_links()
    assert actual_links == (), "Wrong links"
コード例 #14
0
def test_non_parsable_actions():
    """Test that ValueError is raised if one of actions can't be parsed.

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

    parser = EmbeddedRepresentationParser(data={"actions": [{}]},
                                          parser=_ActionErrorParser())
    with pytest.raises(ValueError) as error_info:
        parser.parse_actions()

    assert error_info.value.args[
        0] == "Failed to parse actions of the embedded representation", (
            "Wrong error")
コード例 #15
0
def test_missing_actions():
    # pylint: disable=line-too-long
    """Test that empty tuple is returned if data of embedded representation don't have 'actions' key.

    1. Create an embedded representation parser for a dictionary without actions.
    2. Parse actions.
    3. Check that empty tuple is returned.
    """
    # pylint: enable=line-too-long
    actual_actions = EmbeddedRepresentationParser(
        data={}, parser=JSONParser()).parse_actions()
    assert actual_actions == (), "Wrong actions"
コード例 #16
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)
コード例 #17
0
def test_missing_relations():
    """Test that ValueError is raised if data of embedded representation don't contain 'rel' key.

    1. Create an embedded representation parser for a dictionary without relations.
    2. Try to call parse_relations method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        EmbeddedRepresentationParser(data={},
                                     parser=JSONParser()).parse_relations()

    expected_message = "Data of the embedded representation do not have required 'rel' key"
    assert error_info.value.args[0] == expected_message, "Wrong error"
コード例 #18
0
def test_unobtainable_classes():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if classes can't be retrieved from data of embedded representation.

    1. Create an embedded representation parser for a non-subscriptable object.
    2. Try to call parse_classes method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    # pylint: enable=line-too-long
    with pytest.raises(ValueError) as error_info:
        EmbeddedRepresentationParser(data=None,
                                     parser=JSONParser()).parse_classes()

    expected_message = "Failed to get classes from data of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
コード例 #19
0
def test_non_iterable_relations():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if data of embedded representation has a non-iterable object for relations.

    1. Create an embedded representation parser for a dictionary with non-iterable relations.
    2. Try to call parse_relations method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    # pylint: enable=line-too-long
    with pytest.raises(ValueError) as error_info:
        EmbeddedRepresentationParser(data={
            "rel": None
        }, parser=JSONParser()).parse_relations()

    expected_message = "Failed to iterate over relations from data of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"