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

    1. Create an action marshaler for an object without method.
    2. Try to call marshal_method 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_method()

    assert error_info.value.args[
        0] == "Failed to get action's method", "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_not_supported_method():
    """Test that ValueError is raised if method of an action is not supported.

    1. Create an action marshaler for an object with unsupported method.
    2. Try to call marshal_method method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    MethodAction = namedtuple("MethodAction", "method")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=MethodAction(method="get"))
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_method()

    assert error_info.value.args[
        0] == "Action's method is not supported", "Wrong error"
Пример #4
0
def test_method(method):
    """Test that method is properly marshaled.

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

    actual_method = marshaler.marshal_method()
    assert actual_method == Method(method).value, "Wrong method"