Skip to content

Commit

Permalink
Feat: build back element (#761)
Browse files Browse the repository at this point in the history
* Adiciona 'build_back'

* Adiciona testes

* Adiciona 'dcostring' e muda 'nodes' para lista

* Adapta testes
  • Loading branch information
Rossi-Luciano authored Nov 21, 2024
1 parent ac48bd9 commit 306b3b8
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packtools/sps/formats/sps_xml/back.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import xml.etree.ElementTree as ET

def build_back(nodes):
"""
Constructs an XML 'back' element and populates it with child elements.
This function takes a list of lists, where each sublist contains XML elements,
and appends all these elements as children of a newly created 'back' element.
Args:
nodes (list of list of xml.etree.ElementTree.Element):
A list of lists containing 'Element' objects to be added as children of the 'back' element.
Returns:
xml.etree.ElementTree.Element:
An XML 'back' element with the provided child elements appended.
Raises:
ValueError: If the input 'nodes' list is empty.
Example:
nodes = [
[<Element>, <Element>],
[<Element>]
]
"""
if not nodes:
raise ValueError("A list of child elements is required.")

back_elem = ET.Element("back")
for element_list in nodes:
back_elem.extend(element_list)

return back_elem
81 changes: 81 additions & 0 deletions tests/sps/formats/sps_xml/test_back.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import unittest
import xml.etree.ElementTree as ET
from packtools.sps.formats.sps_xml.back import build_back


class TestBuildBack(unittest.TestCase):
def test_build_back(self):
self.maxDiff = None
node = [
[
ET.fromstring(
'<ack>'
'<title>Acknowledgments</title>'
'<p>Federal University of Rio de Janeiro (UFRJ), School of Medicine,...</p>'
'<p>This study was funded by the Hospital Municipal Conde Modesto Leal...</p>'
'</ack>'
)
],
[
ET.fromstring(
'<ref-list>'
'<title>References</title>'
'<ref id="B1">'
'<label>1</label>'
'<mixed-citation>Goldberg DS, McGee SJ. Pain as a global public health priority. BMC Public Health. 2011;11:770.</mixed-citation>'
'<element-citation publication-type="journal">'
'<person-group person-group-type="author">'
'<name>'
'<surname>Goldberg</surname>'
'<given-names>DS</given-names>'
'</name>'
'</person-group>'
'<article-title>Pain as a global public health priority</article-title>'
'<source>BMC Public Health</source>'
'<volume>11</volume>'
'<year>2011</year>'
'<fpage>770</fpage>'
'</element-citation>'
'</ref>'
'</ref-list>'
)
]
]
expected_xml_str = (
'<back>'
'<ack>'
'<title>Acknowledgments</title>'
'<p>Federal University of Rio de Janeiro (UFRJ), School of Medicine,...</p>'
'<p>This study was funded by the Hospital Municipal Conde Modesto Leal...</p>'
'</ack>'
'<ref-list>'
'<title>References</title>'
'<ref id="B1">'
'<label>1</label>'
'<mixed-citation>Goldberg DS, McGee SJ. Pain as a global public health priority. BMC Public Health. 2011;11:770.</mixed-citation>'
'<element-citation publication-type="journal">'
'<person-group person-group-type="author">'
'<name>'
'<surname>Goldberg</surname>'
'<given-names>DS</given-names>'
'</name>'
'</person-group>'
'<article-title>Pain as a global public health priority</article-title>'
'<source>BMC Public Health</source>'
'<volume>11</volume>'
'<year>2011</year>'
'<fpage>770</fpage>'
'</element-citation>'
'</ref>'
'</ref-list>'
'</back>'
)
back_elem = build_back(node)
generated_xml_str = ET.tostring(back_elem, encoding="unicode", method="xml")
self.assertEqual(generated_xml_str.strip(), expected_xml_str.strip())

def test_build_back_None(self):
node = []
with self.assertRaises(ValueError) as e:
build_back(node)
self.assertEqual(str(e.exception), "A list of child elements is required.")

0 comments on commit 306b3b8

Please sign in to comment.