Пример #1
0
def test_missing_actions():
    """Test that ValueError is raised if an entity does not have actions attribute.

    1. Create an entity marshaler for an object without actions attribute.
    2. Try to call marshal_actions 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_actions()

    assert error_info.value.args[
        0] == "Failed to get entity's actions", "Wrong error"
Пример #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"
Пример #3
0
def test_non_iterable_actions():
    """Test that ValueError is raised if an entity provides a non-iterable object as its actions.

    1. Create an entity 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.
    """
    ActionsEntity = namedtuple("ActionsEntity", "actions")
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(),
                                entity=ActionsEntity(actions=None))
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_actions()

    assert error_info.value.args[
        0] == "Failed to iterate over entity's actions", "Wrong error"
Пример #4
0
def test_non_marshalable_actions():
    """Test that ValueError is raised if one of actions of the entity is not marshallable.

    1. Create an entity 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.
    """
    class _ActionErrorMarshaler(JSONMarshaler):
        def marshal_action(self, action):
            raise Exception()

    ActionsEntity = namedtuple("ActionsEntity", "actions")

    actions = [Action(name="action", target="/action")]
    marshaler = EntityMarshaler(
        marshaler=_ActionErrorMarshaler(),
        entity=ActionsEntity(actions=actions),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_actions()

    assert error_info.value.args[
        0] == "Failed to marshal entity's actions", "Wrong error"
Пример #5
0
def test_actions(actions):
    """Test that actions are properly marshaled.

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

    ActionsEntity = namedtuple("ActionsEntity", "actions")
    marshaler = EntityMarshaler(marshaler=json_marshaler,
                                entity=ActionsEntity(actions=actions))

    actual_data = marshaler.marshal_actions()
    assert actual_data == list(range(len(actions))), "Wrong actions"