Skip to content

Commit

Permalink
Feat: complements build article element (#763)
Browse files Browse the repository at this point in the history
* Adiciona 'docstring' e elementos filhos

* Complementa testes

* Adiciona parâmetro 'nodes'

* Atualiza testes
  • Loading branch information
Rossi-Luciano authored Nov 22, 2024
1 parent 306b3b8 commit fd48eb2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 26 deletions.
58 changes: 53 additions & 5 deletions packtools/sps/formats/sps_xml/article.py
Original file line number Diff line number Diff line change
@@ -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 <article> 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 <article> 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('<front />'),
ET.fromstring('<body />'),
ET.fromstring('<back />'),
ET.fromstring('<sub-article />')
]
Output:
<article 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">
<front />
<body />
<back />
<sub-article />
</article>
"""
namespaces = {
"xlink": "http://www.w3.org/1999/xlink",
"mml": "http://www.w3.org/1998/Math/MathML"
Expand All @@ -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
59 changes: 38 additions & 21 deletions tests/sps/formats/sps_xml/test_article.py
Original file line number Diff line number Diff line change
@@ -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('<front />'),
ET.fromstring('<body />'),
ET.fromstring('<back />'),
ET.fromstring('<sub-article />')
]
)

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 = (
'<article 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">'
'<front />'
'<body />'
'<back />'
'<sub-article />'
'</article>'
)
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()

0 comments on commit fd48eb2

Please sign in to comment.