diff --git a/packtools/sps/formats/sps_xml/article.py b/packtools/sps/formats/sps_xml/article.py index 374580f96..0b8ed7742 100644 --- a/packtools/sps/formats/sps_xml/article.py +++ b/packtools/sps/formats/sps_xml/article.py @@ -1,7 +1,50 @@ import xml.etree.ElementTree as ET -def build_article_node(article_data: dict): +def build_article_node(data, nodes): + """ + Builds an XML article node with the specified attributes and child elements. + + Args: + data (dict): A dictionary containing the data to build the
node. The keys should include: + - "dtd-version" (str): The DTD version for the article (required). + - "specific-use" (str): A specific-use attribute value, such as "sps-1.8" (required). + - "article-type" (str): The type of the article, e.g., "research-article" (required). + - "xml:lang" (str): The language of the article, e.g., "pt" (required). + - "children_nodes" (list of xml.etree.ElementTree.Element): A list of child elements to append to the article node (required). + + Returns: + xml.etree.ElementTree.Element: An XML element representing the
node with the specified attributes and children. + + Raises: + KeyError: If any required keys ("dtd-version", "specific-use", "article-type", "xml:lang") are missing from the input dictionary. + ValueError: If "children_nodes" is not provided or is empty. + + Example: + Input: + data = { + "dtd-version": "1.1", + "specific-use": "sps-1.8", + "article-type": "research-article", + "xml:lang": "pt" + } + + nodes = [ + ET.fromstring(''), + ET.fromstring(''), + ET.fromstring(''), + ET.fromstring('') + ] + + Output: +
+ + + + +
+ """ namespaces = { "xlink": "http://www.w3.org/1999/xlink", "mml": "http://www.w3.org/1998/Math/MathML" @@ -16,12 +59,17 @@ def build_article_node(article_data: dict): article_elem = ET.Element("article", { "xmlns:xlink": namespaces["xlink"], "xmlns:mml": namespaces["mml"], - "dtd-version": article_data["dtd-version"], - "specific-use": article_data["specific-use"], - "article-type": article_data["article-type"], - "xml:lang": article_data["xml:lang"] + "dtd-version": data["dtd-version"], + "specific-use": data["specific-use"], + "article-type": data["article-type"], + "xml:lang": data["xml:lang"] }) except KeyError as e: raise KeyError(f"{e} is required") + if nodes: + article_elem.extend(nodes) + else: + raise ValueError("A list of children nodes is required") + return article_elem diff --git a/tests/sps/formats/sps_xml/test_article.py b/tests/sps/formats/sps_xml/test_article.py index f3d724a4b..c6b8d7fec 100644 --- a/tests/sps/formats/sps_xml/test_article.py +++ b/tests/sps/formats/sps_xml/test_article.py @@ -1,46 +1,63 @@ import unittest -from xml.etree.ElementTree import Element +import xml.etree.ElementTree as ET + from packtools.sps.formats.sps_xml.article import build_article_node class TestBuildArticleNode(unittest.TestCase): def test_build_article_node(self): - article_node = build_article_node( - article_data={ + article = build_article_node( + data={ 'dtd-version': '1.1', 'specific-use': 'sps-1.8', 'article-type': 'research-article', 'xml:lang': 'pt' - } + }, + nodes=[ + ET.fromstring(''), + ET.fromstring(''), + ET.fromstring(''), + ET.fromstring('') + ] ) - self.assertIsInstance(article_node, Element) - - self.assertEqual(article_node.tag, 'article') - - expected_attributes = { - 'xmlns:xlink': 'http://www.w3.org/1999/xlink', - 'xmlns:mml': 'http://www.w3.org/1998/Math/MathML', - 'dtd-version': '1.1', - 'specific-use': 'sps-1.8', - 'article-type': 'research-article', - 'xml:lang': 'pt' - } - - self.assertDictEqual(article_node.attrib, expected_attributes) + expected_xml_str = ( + '
' + '' + '' + '' + '' + '
' + ) + generated_xml_str = ET.tostring(article, encoding="unicode", method="xml") + self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip()) - def test_build_article_node_error(self): + def test_build_article_node_error_attribs(self): with self.assertRaises(KeyError) as e: build_article_node( - article_data={ + data={ 'specific-use': 'sps-1.8', 'article-type': 'research-article', 'xml:lang': 'pt' - } + }, + nodes=None ) self.assertEqual(str(e.exception), '"\'dtd-version\' is required"') + def test_build_article_node_error_children(self): + with self.assertRaises(ValueError) as e: + build_article_node( + data={ + 'dtd-version': '1.1', + 'specific-use': 'sps-1.8', + 'article-type': 'research-article', + 'xml:lang': 'pt', + }, + nodes=None + ) + self.assertEqual(str(e.exception), "A list of children nodes is required") if __name__ == '__main__': unittest.main()