コード例 #1
0
def test_non_marshalable_sub_entities(sub_entity):
    # pylint: disable=line-too-long
    """Test that ValueError is raised if one of sub-entities of the embedded representation is not marshallable.

    1. Create json marshaler that raises exception when either marshal_embedded_link or
       marshal_embedded_representation is called.
    2. Create an embedded representation marshaler.
    3. Try to call marshal_entities method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """

    # pylint: enable=line-too-long
    class _SubEntityErrorMarshaler(JSONMarshaler):
        def marshal_embedded_link(self, embedded_link):
            raise Exception()

        def marshal_embedded_representation(self, embedded_representation):
            raise Exception()

    SubEntitiesRepresentation = namedtuple("SubEntitiesRepresentation",
                                           "entities")
    marshaler = RepresentationMarshaler(
        marshaler=_SubEntityErrorMarshaler(),
        embedded_representation=SubEntitiesRepresentation(
            entities=[sub_entity]),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_entities()

    expected_message = "Failed to marshal sub-entities of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
コード例 #2
0
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"
コード例 #3
0
def test_sub_entities(sub_entities):
    """Test that sub-entities are properly marshaled.

    1. Create json marshaler with overridden marshal_embedded_link and
       marshal_embedded_representation.
    2. Create an embedded representation marshaler.
    3. Marshal sub-entities.
    4. Check the marshaled sub-entities.
    """
    class _SubEntitiesMarshaler(JSONMarshaler):
        def marshal_embedded_link(self, embedded_link):
            if not hasattr(embedded_link, "target"):
                pytest.fail(
                    "Try to mershal embedded link instead of embedded representation"
                )
            return sub_entities.index(embedded_link)

        def marshal_embedded_representation(self, embedded_representation):
            if hasattr(embedded_representation, "target"):
                pytest.fail(
                    "Try to marshal embedded representation instead of embedded link"
                )
            return sub_entities.index(embedded_representation)

    SubEntitiesRepresentation = namedtuple("SubEntitiesRepresentation",
                                           "entities")
    marshaler = RepresentationMarshaler(
        marshaler=_SubEntitiesMarshaler(),
        embedded_representation=SubEntitiesRepresentation(
            entities=sub_entities),
    )

    actual_data = marshaler.marshal_entities()
    assert actual_data == list(range(len(sub_entities))), "Wrong entities"
コード例 #4
0
def test_missing_sub_entities():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if an embedded representation does not have entities attribute.

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

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

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

    expected_message = "Failed to iterate over sub-entities of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"