コード例 #1
0
def test_missing_name():
    """Test that ValueError is raised if an action does not have name attribute.

    1. Create an action marshaler for an object without name attibute.
    2. Try to call marshal_name 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_name()

    assert error_info.value.args[
        0] == "Failed to get action's name", "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_name(name, expected_name):
    """Test that name is properly marshaled.

    1. Create an action marshaler for an object with specific name.
    2. Marshal action's name.
    3. Check the marshaled name.
    """
    NameAction = namedtuple("NameAction", "name")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=NameAction(name=name))
    actual_name = marshaler.marshal_name()
    assert actual_name == expected_name, "Wrong name"