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"
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"
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"
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"