def test_missing_title():
    """Test that ValueError is raised if an embedded representation does not have title attribute.

    1. Create an embedded representation marshaler for an object without title attribute.
    2. Try to call marshal_title 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_title()

    expected_error = "Failed to get title 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_title(title, expected_title):
    """Test that title is properly marshaled.

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

    actual_title = marshaler.marshal_title()
    assert actual_title == expected_title, "Wrong title"