Ejemplo 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)
Ejemplo n.º 2
0
    def parse(self):
        """Parse an action from the data.

        :returns: :class:`Action <lila.core.action.Action>`.
        :raises: :class:ValueError.
        """
        logger = logging.getLogger(__name__)
        logger.debug("Try to parse an action")

        action_name = self.parse_name()
        action_classes = self.parse_classes()
        action_method = self.parse_method()
        action_target = self.parse_target()
        action_title = self.parse_title()
        action_media_type = self.parse_media_type()
        action_fields = self.parse_fields()

        try:
            action = Action(
                name=action_name,
                classes=action_classes,
                method=action_method,
                target=action_target,
                title=action_title,
                media_type=action_media_type,
                fields=action_fields,
            )
        except Exception as error:
            logger.error("Failed to create an action with provided data")
            raise ValueError(
                "Failed to create an action with provided data") from error
        else:
            logger.info("Successfully parsed an action")

        return action
Ejemplo n.º 3
0
def test_marshal():
    """Test that action data is properly marshaled.

    1. Create an action.
    2. Create an action marshaler for the action.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the action.
    5. Check the marshaled data.
    """
    marshaler = ActionMarshaler(
        marshaler=JSONMarshaler(),
        action=Action(name="action", target="/target"),
    )
    marshaler.marshal_name = lambda: "marshal_name"
    marshaler.marshal_classes = lambda: "marshal_classes"
    marshaler.marshal_method = lambda: "marshal_method"
    marshaler.marshal_target = lambda: "marshal_target"
    marshaler.marshal_title = lambda: "marshal_title"
    marshaler.marshal_media_type = lambda: "marshal_media_type"
    marshaler.marshal_fields = lambda: "marshal_fields"

    actual_data = marshaler.marshal()
    expected_data = {
        "name": "marshal_name",
        "class": "marshal_classes",
        "method": "marshal_method",
        "href": "marshal_target",
        "title": "marshal_title",
        "type": "marshal_media_type",
        "fields": "marshal_fields",
    }
    assert actual_data == expected_data, "Action is not properly marshaled"
Ejemplo n.º 4
0
def test_parse(component_validator):
    """Test that action data is properly parsed.

    1. Create an action.
    2. Create an action parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the action.
    5. Check the parsed data.
    """
    action = Action(
        name="parsed name",
        classes=("parsed class 1", "parsed class 2"),
        method=Method.PUT,
        target="/parsed/target",
        title="parsed title",
        media_type="application/parsed+type",
        fields=(Field(name="first"), Field(name="second")),
    )

    parser = ActionParser(data={}, parser=JSONParser())
    parser.parse_name = lambda: action.name
    parser.parse_classes = lambda: action.classes
    parser.parse_method = lambda: action.method
    parser.parse_target = lambda: action.target
    parser.parse_title = lambda: action.title
    parser.parse_media_type = lambda: action.media_type
    parser.parse_fields = lambda: action.fields

    actual_action = parser.parse()
    component_validator.validate_action(actual_action, action)
Ejemplo n.º 5
0
def test_default_title():
    """Check default title of an action.

    1. Create an action without specifying a title.
    2. Check the title of the action.
    """
    action = Action(name="test default title", target="/test/default/title")
    assert action.title is None, "Wrong title"
Ejemplo n.º 6
0
def test_default_fields():
    """Check default set of fields of an action.

    1. Create an action without specifying fields.
    2. Get fields of the action.
    3. Check the fields.
    """
    action = Action(name="test default fields", target="/test-default-fields")
    assert action.fields == (), "Wrong fields"
Ejemplo n.º 7
0
def test_default_method():
    """Check default method of an action.

    1. Create an action without specifying a method.
    2. Get method of the action.
    3. Check the method.
    """
    action = Action(name="test default method", target="/test-default-method/")
    assert action.method == Method.GET, "Wrong method"
Ejemplo n.º 8
0
def test_invalid_classes():
    """Check that ValueError is raised if invalid classes are passed.

    1. Try to create an action with non-iterable classes.
    2. Check that ValueError is raised.
    """
    with pytest.raises(ValueError):
        Action(name="test invalid classes",
               target="/test-invalid-classes",
               classes=1)
Ejemplo n.º 9
0
def test_fields(fields):
    """Check fields of an action.

    1. Create an action with different number of fields.
    2. Get fields of the action.
    3. Check the fields.
    """
    action = Action(name="test action fields",
                    target="/test-action-fields",
                    fields=fields)
    assert action.fields == tuple(fields), "Wrong fields"
Ejemplo n.º 10
0
def test_title(title, expected_title):
    """Check title of an action.

    1. Create an action with different titles.
    2. Get title of the action.
    3. Check the title.
    """
    action = Action(name="test action title",
                    target="/test/action/title",
                    title=title)
    assert action.title == expected_title, "Wrong title"
Ejemplo n.º 11
0
def test_method(method):
    """Check method of an action.

    1. Create an action with different methods.
    2. Get method of the action.
    3. Check the method.
    """
    action = Action(name="test action method",
                    target="/test-action-method/",
                    method=method)
    assert action.method == Method(method), "Wrong method"
Ejemplo n.º 12
0
def test_marshal_action():
    """Check that NotImplementedError is raised if marshal_action is called.

    1. Create an instance of Marshaler class.
    2. Try to marshal an action.
    3. Check that NotImplementedError is raised.
    4. Check the error message.
    """
    with pytest.raises(NotImplementedError) as error_info:
        Marshaler().marshal_action(action=Action(name="action", target="/action"))

    assert error_info.value.args[0] == "Marshaler does not support siren actions"
Ejemplo n.º 13
0
def test_incompatible_fields(fields):
    """Check that ValueError is raised if at least one of the fields is of incompatible type.

    1. Try to create an action with incompatible field type.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        Action(name="test incompatible fields",
               target="/test-incompatible-fields",
               fields=fields)

    assert error_info.value.args[
        0] == "Some of the fields are of incompatible type"
Ejemplo n.º 14
0
def test_invalid_method(method):
    """Check that ValueError is raised if an invalid method is passed.

    1. Try to create an action with unsupported method.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        Action(name="test invalid method",
               target="/test-invalid-method",
               method=method)

    expected_method = "Method '{0}' is not supported".format(method)
    assert error_info.value.args[0] == expected_method, "Wrong error message"
Ejemplo n.º 15
0
def test_duplicated_field_names():
    """Check that ValueError is raised if some of the fields have the same name.

    1. Create 2 fields with the same name.
    1. Try to create an action with the created fields.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    fields = [
        Field(name="duplicate", title="First field with non-unique name"),
        Field(name="unique", title="Field with unique name"),
        Field(name="duplicate", title="Second field with non-unique name"),
    ]

    with pytest.raises(ValueError) as error_info:
        Action(name="test duplicate fields",
               target="/test/duplicate/fields",
               fields=fields)

    assert error_info.value.args[
        0] == "Some of the fields have the same name", "Wrong error"