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_missing_relations():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if an embedded representation does not have relations attribute.

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

    assert error_info.value.args[
        0] == "Failed to get relations of the embedded representation", (
            "Wrong error")
def test_non_iterable_relations():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if an embedded representation has a non-iterable object as its relations.

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

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

    1. Create an embedded representation marshaler for an object with specific relations.
    2. Marshal relations.
    3. Check the marshaled relations.
    """
    RelationsRepresentation = namedtuple("RelationsRepresentation",
                                         "relations")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=RelationsRepresentation(relations=relations),
    )

    actual_relations = marshaler.marshal_relations()
    assert actual_relations == expected_relations, "Wrong relations"