def test_non_marshalable_actions():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if one of actions of the embedded representation is not marshallable.

    1. Create an embedded representation marshaler for an object with an action that
       can't be marshaled.
    2. Try to call marshal_actions method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """

    # pylint: enable=line-too-long
    class _ActionErrorMarshaler(JSONMarshaler):
        def marshal_action(self, action):
            raise Exception()

    ActionsRepresentation = namedtuple("ActionsRepresentation", "actions")

    actions = [Action(name="action", target="/action")]
    marshaler = RepresentationMarshaler(
        marshaler=_ActionErrorMarshaler(),
        embedded_representation=ActionsRepresentation(actions=actions),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_actions()

    expected_message = "Failed to marshal actions of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
def test_missing_actions():
    """Test that ValueError is raised if an embedded representation does not have actions attribute.

    1. Create an embedded representation marshaler for an object without actions attribute.
    2. Try to call marshal_actions method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    marshaler = RepresentationMarshaler(marshaler=JSONMarshaler(),
                                        embedded_representation=object())
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_actions()

    expected_error = "Failed to get actions of the embedded representation"
    assert error_info.value.args[0] == expected_error, "Wrong error"
def test_marshal():
    """Test that data of an embedded representation is properly marshaled.

    1. Create an embedded representation.
    2. Create an embedded representation marshaler for the representation.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the representation.
    5. Check the marshaled data.
    """
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=EmbeddedRepresentation(relations=["self"]),
    )
    marshaler.marshal_relations = lambda: "marshal_relations"
    marshaler.marshal_classes = lambda: "marshal_classes"
    marshaler.marshal_properties = lambda: "marshal_properties"
    marshaler.marshal_entities = lambda: "marshal_entities"
    marshaler.marshal_links = lambda: "marshal_links"
    marshaler.marshal_actions = lambda: "marshal_actions"
    marshaler.marshal_title = lambda: "marshal_title"

    actual_data = marshaler.marshal()
    expected_data = {
        "rel": "marshal_relations",
        "class": "marshal_classes",
        "properties": "marshal_properties",
        "entities": "marshal_entities",
        "links": "marshal_links",
        "actions": "marshal_actions",
        "title": "marshal_title",
    }
    assert actual_data == expected_data, "Embedded represenation is not properly marshaled"
def test_non_iterable_actions():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if an embedded representation has a non-iterable object as its actions.

    1. Create an embedded representation marshaler for an object with non-iterable actions.
    2. Try to call marshal_actions method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    # pylint: enable=line-too-long
    ActionsRepresentation = namedtuple("ActionsRepresentation", "actions")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=ActionsRepresentation(actions=None),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_actions()

    expected_message = "Failed to iterate over actions of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
def test_actions(actions):
    """Test that actions are properly marshaled.

    1. Create an embedded representation marshaler.
    2. Replace marshal_action of the marshaler so that it returns fake data.
    3. Marshal actions.
    4. Check the marshaled actions.
    """
    json_marshaler = JSONMarshaler()
    json_marshaler.marshal_action = actions.index

    ActionsRepresentation = namedtuple("ActionsRepresentation", "actions")
    marshaler = RepresentationMarshaler(
        marshaler=json_marshaler,
        embedded_representation=ActionsRepresentation(actions=actions),
    )

    actual_data = marshaler.marshal_actions()
    assert actual_data == list(range(len(actions))), "Wrong actions"