Exemplo n.º 1
0
def test_parse(component_validator):
    """Test that entity data is properly parsed.

    1. Create an entity.
    2. Create an entity parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the entity.
    5. Check the entity data.
    """
    entity = Entity(
        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 = EntityParser(data={}, parser=JSONParser())
    parser.parse_classes = lambda: entity.classes
    parser.parse_properties = lambda: entity.properties
    parser.parse_entities = lambda: entity.entities
    parser.parse_links = lambda: entity.links
    parser.parse_actions = lambda: entity.actions
    parser.parse_title = lambda: entity.title

    actual_entity = parser.parse()
    component_validator.validate_entity(actual_entity, entity)
Exemplo n.º 2
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 entity parser with the parser.
    4. Parse fields.
    5. Check the parsed fields.
    """
    json_parser = JSONParser()
    json_parser.parse_action = actions_data.index

    parser = EntityParser(data={"actions": actions_data}, parser=json_parser)
    actual_actions = parser.parse_actions()
    assert actual_actions == tuple(range(len(actions_data))), "Wrong actions"