Esempio n. 1
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"
Esempio n. 2
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"
Esempio n. 3
0
def test_title(title, expected_title):
    """Test that title is properly marshaled.

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

    actual_title = marshaler.marshal_title()
    assert actual_title == expected_title, "Wrong title"
Esempio n. 4
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"
Esempio n. 5
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"
Esempio n. 6
0
def test_classes(classes, expected_classes):
    """Test that classes are properly marshaled.

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

    actual_classes = marshaler.marshal_classes()
    assert actual_classes == expected_classes, "Wrong classes"
Esempio n. 7
0
def test_missing_classes():
    """Test that ValueError is raised if an action does not have classes attribute.

    1. Create an action marshaler for an object without classes attribute.
    2. Try to call marshal_classes 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_classes()

    assert error_info.value.args[
        0] == "Failed to get action's classes", "Wrong error"
Esempio n. 8
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"
Esempio n. 9
0
    def create_action_marshaler(self, action):
        """Factory method to create a marshaler for an action.

        :param action: Siren action to marshal.
        :returns: :class:`ActionMarshaler <lila.serialization.json.action.ActionMarshaler>`.
        """
        return ActionMarshaler(action=action, marshaler=self)
Esempio n. 10
0
def test_non_iterable_classes():
    """Test that ValueError is raised if an action provides a non-iterable object as its classes.

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

    assert error_info.value.args[
        0] == "Failed to iterate over action's classes", "Wrong error"
Esempio n. 11
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"
Esempio n. 12
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"
Esempio n. 13
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"