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

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

    # pylint: enable=line-too-long
    class _LinkErrorMarshaler(JSONMarshaler):
        def marshal_link(self, link):
            raise Exception()

    LinksRepresentation = namedtuple("LinksRepresentation", "links")

    links = [Link(relations=["first"], target="/first")]
    marshaler = RepresentationMarshaler(
        marshaler=_LinkErrorMarshaler(),
        embedded_representation=LinksRepresentation(links=links),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_links()

    expected_message = "Failed to marshal links of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
コード例 #2
0
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"
コード例 #3
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"
コード例 #4
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"
コード例 #5
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"
コード例 #6
0
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"
コード例 #7
0
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"
コード例 #8
0
def test_classes(classes, expected_classes):
    """Test that classes are properly marshaled.

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

    actual_classes = marshaler.marshal_classes()
    assert actual_classes == expected_classes, "Wrong classes"
コード例 #9
0
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"
コード例 #10
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"
コード例 #11
0
def test_properties(properties):
    """Test that properties are properly marshaled.

    1. Create an embedded representation marshaler for an object with specific properties.
    2. Marshal properties.
    3. Check the marshaled properties.
    """
    PropertiesRepresentation = namedtuple("PropertiesRepresentation",
                                          "properties")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=PropertiesRepresentation(
            properties=properties),
    )
    actual_properties = json.dumps(marshaler.marshal_properties(),
                                   sort_keys=True)
    expected_properties = json.dumps(properties, sort_keys=True)
    assert actual_properties == expected_properties, "Wrong properties"
コード例 #12
0
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"
コード例 #13
0
def test_links(links):
    """Test that links are properly marshaled.

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

    LinksRepresentation = namedtuple("LinksRepresentation", "links")
    marshaler = RepresentationMarshaler(
        marshaler=json_marshaler,
        embedded_representation=LinksRepresentation(links=links),
    )

    actual_data = marshaler.marshal_links()
    assert actual_data == list(range(len(links))), "Wrong links"
コード例 #14
0
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"
コード例 #15
0
ファイル: marshaler.py プロジェクト: KillAChicken/lila
    def create_embedded_representation_marshaler(self,
                                                 embedded_representation):
        """Factory method to create a marshaler for an embedded representation.

        :param embedded_representation: Siren embedded representation to marshal.
        :returns: :class:`EmbeddedRepresentationMarshaler
            <lila.serialization.json.entity.EmbeddedRepresentationMarshaler>`.
        """
        return EmbeddedRepresentationMarshaler(
            embedded_representation=embedded_representation,
            marshaler=self,
        )
コード例 #16
0
def test_invalid_properties():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if properties of an embedded representaion are not valid json object.

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

    expected_message = "Failed to marshal properties of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"