def test_target_media_type(target_media_type, expected_target_media_type): """Test that target media type is properly marshaled. 1. Create a link marshaler for an object with specific target media type. 2. Marshal link's target media type. 3. Check the marshaled target media type. """ TargetMediaTypeLink = namedtuple("TargetMediaTypeLink", "target_media_type") marshaler = LinkMarshaler(link=TargetMediaTypeLink( target_media_type=target_media_type)) actual_target_media_type = marshaler.marshal_target_media_type() assert actual_target_media_type == expected_target_media_type, "Wrong target media type"
def test_marshal(): """Test that link data is properly marshaled. 1. Create a link. 2. Create a link marshaler for the link. 3. Replace marshaler methods so that they return predefined data. 4. Marshal the link. 5. Check the marshaled data. """ marshaler = LinkMarshaler(Link(relations=["self"], target="/link")) marshaler.marshal_relations = lambda: "marshal_relations" marshaler.marshal_classes = lambda: "marshal_classes" marshaler.marshal_target = lambda: "marshal_target" marshaler.marshal_title = lambda: "marshal_title" marshaler.marshal_target_media_type = lambda: "marshal_target_media_type" actual_data = marshaler.marshal() expected_data = { "rel": "marshal_relations", "class": "marshal_classes", "href": "marshal_target", "title": "marshal_title", "type": "marshal_target_media_type", } assert actual_data == expected_data, "Field is not properly marshaled"
def test_title(title, expected_title): """Test that title is properly marshaled. 1. Create a link marshaler for an object with specific title. 2. Marshal link's title. 3. Check the marshaled title. """ TitleLink = namedtuple("TitleLink", "title") actual_title = LinkMarshaler(link=TitleLink(title=title)).marshal_title() assert actual_title == expected_title, "Wrong title"
def test_relations(relations, expected_relations): """Test that relations are properly marshaled. 1. Create a link marshaler for an object with specific relations. 2. Marshal relations. 3. Check the marshaled relations. """ RelationsLink = namedtuple("RelationsLink", "relations") actual_relations = LinkMarshaler(link=RelationsLink( relations=relations)).marshal_relations() assert actual_relations == expected_relations, "Wrong relations"
def test_target(target, expected_target): """Test that target is properly marshaled. 1. Create a link marshaler for an object with specific target. 2. Marshal link's target. 3. Check the marshaled target. """ TargetLink = namedtuple("TargetLink", "target") actual_target = LinkMarshaler(link=TargetLink( target=target)).marshal_target() assert actual_target == expected_target, "Wrong target"
def test_classes(classes, expected_classes): """Test that classes are properly marshaled. 1. Create a link marshaler for an object with specific classes. 2. Marshal classes. 3. Check the marshaled classes. """ ClassesLink = namedtuple("ClassesLink", "classes") actual_classes = LinkMarshaler(link=ClassesLink( classes=classes)).marshal_classes() assert actual_classes == expected_classes, "Wrong classes"
def test_missing_classes(): """Test that ValueError is raised if a link does not have classes attribute. 1. Create a link marshaler for an object without classes attibute. 2. Try to call marshal_classes method. 3. Check that ValueError is raised. 4. Check the error message. """ with pytest.raises(ValueError) as error_info: LinkMarshaler(link=object()).marshal_classes() assert error_info.value.args[ 0] == "Failed to get link's classes", "Wrong error"
def test_non_iterable_classes(): """Test that ValueError is raised if a link provides a non-iterable object as its classes. 1. Create a link 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. """ ClassesLink = namedtuple("ClassesLink", "classes") with pytest.raises(ValueError) as error_info: LinkMarshaler(link=ClassesLink(classes=None)).marshal_classes() assert error_info.value.args[ 0] == "Failed to iterate over link's classes", "Wrong error"