Пример #1
0
def test_missing_media_type():
    """Test that ValueError is raised if an action does not have media type attribute.

    1. Create an action marshaler for an object without media type attibute.
    2. Try to call marshal_media_type method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(), action=object())
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_media_type()

    assert error_info.value.args[
        0] == "Failed to get action's media type", "Wrong error"
Пример #2
0
def test_marshal():
    """Test that action data is properly marshaled.

    1. Create an action.
    2. Create an action marshaler for the action.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the action.
    5. Check the marshaled data.
    """
    marshaler = ActionMarshaler(
        marshaler=JSONMarshaler(),
        action=Action(name="action", target="/target"),
    )
    marshaler.marshal_name = lambda: "marshal_name"
    marshaler.marshal_classes = lambda: "marshal_classes"
    marshaler.marshal_method = lambda: "marshal_method"
    marshaler.marshal_target = lambda: "marshal_target"
    marshaler.marshal_title = lambda: "marshal_title"
    marshaler.marshal_media_type = lambda: "marshal_media_type"
    marshaler.marshal_fields = lambda: "marshal_fields"

    actual_data = marshaler.marshal()
    expected_data = {
        "name": "marshal_name",
        "class": "marshal_classes",
        "method": "marshal_method",
        "href": "marshal_target",
        "title": "marshal_title",
        "type": "marshal_media_type",
        "fields": "marshal_fields",
    }
    assert actual_data == expected_data, "Action is not properly marshaled"
Пример #3
0
def test_media_type(media_type, expected_media_type):
    """Test that media type is properly marshaled.

    1. Create an action marshaler for an object with specific media type.
    2. Marshal action's media type.
    3. Check the marshaled media type.
    """
    MediaTypeAction = namedtuple("MediaTypeAction", "media_type")
    marshaler = ActionMarshaler(
        marshaler=JSONMarshaler(),
        action=MediaTypeAction(media_type=media_type),
    )

    actual_media_type = marshaler.marshal_media_type()
    assert actual_media_type == expected_media_type, "Wrong media type"