Beispiel #1
0
def test_missing_links():
    """Test that ValueError is raised if an entity does not have links attribute.

    1. Create an entity marshaler for an object without links attribute.
    2. Try to call marshal_links method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=object())
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_links()

    assert error_info.value.args[
        0] == "Failed to get entity's links", "Wrong error"
Beispiel #2
0
def test_marshal():
    """Test that entity data is properly marshaled.

    1. Create an entity.
    2. Create an entity marshaler for the entity.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the entity.
    5. Check the marshaled data.
    """
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=Entity())
    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 = {
        "class": "marshal_classes",
        "properties": "marshal_properties",
        "entities": "marshal_entities",
        "links": "marshal_links",
        "actions": "marshal_actions",
        "title": "marshal_title",
    }
    assert actual_data == expected_data, "Entity is not properly marshaled"
Beispiel #3
0
def test_non_iterable_links():
    """Test that ValueError is raised if an entity provides a non-iterable object as its links.

    1. Create an entity marshaler for an object with non-iterable links.
    2. Try to call marshal_links method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    LinksEntity = namedtuple("LinksEntity", "links")
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(),
                                entity=LinksEntity(links=None))
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_links()

    assert error_info.value.args[
        0] == "Failed to iterate over entity's links", "Wrong error"
Beispiel #4
0
def test_non_marshalable_links():
    """Test that ValueError is raised if one of links of the entity is not marshallable.

    1. Create json marshaler that raises exception when marshal_link method is called.
    2. Create an entity marshaler.
    3. Try to call marshal_links method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    class _LinkErrorMarshaler(JSONMarshaler):
        def marshal_link(self, link):
            raise Exception()

    LinksEntity = namedtuple("LinksEntity", "links")

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

    assert error_info.value.args[
        0] == "Failed to marshal entity's links", "Wrong error"
Beispiel #5
0
def test_links(links):
    """Test that links are properly marshaled.

    1. Create an entity 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

    LinksEntity = namedtuple("LinksEntity", "links")
    marshaler = EntityMarshaler(marshaler=json_marshaler,
                                entity=LinksEntity(links=links))

    actual_data = marshaler.marshal_links()
    assert actual_data == list(range(len(links))), "Wrong links"