Exemple #1
0
def test_missing_target():
    """Test that ValueError is raised if an action does not have target attribute.

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

    assert error_info.value.args[
        0] == "Failed to get action's target", "Wrong error"
Exemple #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"
Exemple #3
0
def test_target(target, expected_target):
    """Test that target is properly marshaled.

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

    actual_target = marshaler.marshal_target()
    assert actual_target == expected_target, "Wrong target"