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

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

    assert error_info.value.args[
        0] == "Failed to get action's fields", "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_non_iterable_fields():
    """Test that ValueError is raised if an action provides a non-iterable object as its fields.

    1. Create an action marshaler for an object with non-iterable fields.
    2. Try to call marshal_fields.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    FieldsAction = namedtuple("FieldsAction", "fields")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=FieldsAction(fields=None))
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_fields()

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

    1. Create json marshaler that raises exception when marshal_field method is called.
    2. Create an action marshaler with the json marshaler.
    3. Try to call marshal_fields method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    class _FieldErrorMarshaler(JSONMarshaler):
        def marshal_field(self, field):
            raise Exception()

    FieldsAction = namedtuple("FieldsAction", "fields")
    marshaler = ActionMarshaler(
        marshaler=_FieldErrorMarshaler(),
        action=FieldsAction(fields=[Field(name="first")]),
    )

    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_fields()

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

    1. Create an action marshaler.
    2. Replace marshal_field of the marshaler so that it returns fake data.
    3. Marshal fields.
    4. Check the marshaled fields.
    """
    json_marshaler = JSONMarshaler()
    json_marshaler.marshal_field = fields.index

    FieldsAction = namedtuple("FieldsAction", "fields")
    marshaler = ActionMarshaler(marshaler=json_marshaler,
                                action=FieldsAction(fields=fields))

    actual_data = marshaler.marshal_fields()
    assert actual_data == list(range(len(fields))), "Wrong fields"