def test_parse(component_validator): """Test that data of embedded link is properly parsed. 1. Create an embedded link. 2. Create an embedded link parser. 3. Replace parser methods so that they return predefined data. 4. Parse the embedded link. 5. Check the parsed data. """ embedded_link = EmbeddedLink( relations=("parsed relation 1", "parsed relation 2"), classes=("parsed class 1", "parsed class 2"), title="parsed title", target="/parsed/target", target_media_type="application/parsed+media+type", ) parser = EmbeddedLinkParser(data={}) parser.parse_relations = lambda: embedded_link.relations parser.parse_classes = lambda: embedded_link.classes parser.parse_target = lambda: embedded_link.target parser.parse_title = lambda: embedded_link.title parser.parse_target_media_type = lambda: embedded_link.target_media_type actual_embedded_link = parser.parse() component_validator.validate_embedded_link(actual_embedded_link, embedded_link)
def test_relations(relations, expected_relations): """Test that relations are properly parsed. 1. Create an embedded link parser for a dictionary with specific relations. 2. Parse relations. 3. Check the parsed classes. """ actual_relations = EmbeddedLinkParser(data={"rel": relations}).parse_relations() assert actual_relations == expected_relations, "Wrong relations"
def test_missing_target_media_type(): """Test that None is returned if data of embedded link don't have 'type' key. 1. Create an embedded link parser for a dictionary without target media type. 2. Parse target media type. 3. Check that None is returned. """ actual_target_media_type = EmbeddedLinkParser(data={}).parse_target_media_type() assert actual_target_media_type is None, "Wrong target media type"
def test_title(title, expected_title): """Test that title is properly parsed. 1. Create an embedded link parser for dictionary with specific title. 2. Parse a title. 3. Check the parsed title. """ actual_title = EmbeddedLinkParser(data={"title": title}).parse_title() assert actual_title == expected_title, "Wrong title"
def test_target(target, expected_target): """Test that target is properly parsed. 1. Create an embedded link parser for dictionary with specific target. 2. Parse a target. 3. Check the parsed target. """ actual_target = EmbeddedLinkParser(data={"href": target}).parse_target() assert actual_target == expected_target, "Wrong target"
def test_classes(classes, expected_classes): """Test that classes are properly parsed. 1. Create an embedded link parser for a dictionary with specific classes. 2. Parse classes. 3. Check the parsed classes. """ actual_classes = EmbeddedLinkParser(data={"class": classes}).parse_classes() assert actual_classes == expected_classes, "Wrong classes"
def test_missing_classes(): """Test that empty tuple is returned if data of embedded link don't have 'class' key. 1. Create an embedded link parser for a dictionary without classes. 2. Parse classes. 3. Check that empty tuple is returned. """ actual_classes = EmbeddedLinkParser(data={}).parse_classes() assert actual_classes == (), "Wrong classes"
def test_target_media_type(target_media_type): """Test that target media type is properly parsed. 1. Create an embedded link parser for dictionary with specific target media type. 2. Parse a target media type. 3. Check the parsed target media type. """ parser = EmbeddedLinkParser(data={"type": target_media_type}) actual_target_media_type = parser.parse_target_media_type() assert actual_target_media_type == target_media_type, "Wrong target media type"
def test_missing_target(): """Test that ValueError is raised if data of embedded link don't contain 'href' key. 1. Create an embedded link parser for a dictionary without target. 2. Try to call parse_target method. 3. Check that ValueError is raised. 4. Check the error message. """ with pytest.raises(ValueError) as error_info: EmbeddedLinkParser(data={}).parse_target() expected_message = "Data of the embedded link do not have required 'href' key" assert error_info.value.args[0] == expected_message, "Wrong error"
def test_unobtainable_classes(): """Test that ValueError is raised if classes can't be retrieved from data of embedded link. 1. Create an embedded link parser for a non-subscriptable object. 2. Try to call parse_classes method. 3. Check that ValueError is raised. 4. Check the error message. """ with pytest.raises(ValueError) as error_info: EmbeddedLinkParser(data=None).parse_classes() assert error_info.value.args[0] == "Failed to get classes from data of the embedded link", ( "Wrong error" )
def test_missing_relations(): """Test that ValueError is raised if data of embedded link don't contain 'rel' key. 1. Create an embedded link parser for a dictionary without relations. 2. Try to call parse_relations method. 3. Check that ValueError is raised. 4. Check the error message. """ with pytest.raises(ValueError) as error_info: EmbeddedLinkParser(data={}).parse_relations() assert error_info.value.args[0] == "Data of the embedded link do not have required 'rel' key", ( "Wrong error" )
def test_non_iterable_relations(): # pylint: disable=line-too-long """Test that ValueError is raised if data of embedded link has a non-iterable object for relations. 1. Create an embedded link parser for a dictionary with non-iterable relations. 2. Try to call parse_relations method. 3. Check that ValueError is raised. 4. Check the error message. """ # pylint: enable=line-too-long with pytest.raises(ValueError) as error_info: EmbeddedLinkParser(data={"rel": None}).parse_relations() expected_message = "Failed to iterate over relations from data of the embedded link" assert error_info.value.args[0] == expected_message, "Wrong error"
def test_unobtainable_target_media_type(): # pylint: disable=line-too-long """Test that ValueError is raised if target media type can't be retrieved from data of embedded link. 1. Create an embedded link parser for a non-subscriptable object. 2. Try to call parse_target_media_type method. 3. Check that ValueError is raised. 4. Check the error message. """ # pylint: disable=line-too-long with pytest.raises(ValueError) as error_info: EmbeddedLinkParser(data=None).parse_target_media_type() expected_message = "Failed to get target media type from data of the embedded link" assert error_info.value.args[0] == expected_message, "Wrong error"
def test_embedded_link_creation_error(): """Test that ValueError is raised if an error occurs during embedded link creation. 1. Create an embedded link parser. 2. Replace parse_classes method so that it returns invalid object for classes. 3. Try to call parse method. 4. Check that ValueError is raised. 5. Check the error message. """ embedded_link_data = { "rel": ["link relation 1"], "href": "/target", } parser = EmbeddedLinkParser(data=embedded_link_data) parser.parse_classes = lambda: 1 with pytest.raises(ValueError) as error_info: parser.parse() assert error_info.value.args[0] == "Failed to create an embedded link with provided data", ( "Wrong error" )