Пример #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)
Пример #2
0
def test_entity_creation_error():
    """Test that ValueError is raised if an error occurs during entity creation.

    1. Create an entity parser.
    2. Replace parse_classes 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 = EntityParser(data={}, parser=JSONParser())
    parser.parse_classes = lambda: 1

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

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