From 306b3b8e69a7f9d6cd7ffd9996e8e51d71d81fc6 Mon Sep 17 00:00:00 2001 From: Luciano <41302084+Rossi-Luciano@users.noreply.github.com> Date: Thu, 21 Nov 2024 08:59:35 -0300 Subject: [PATCH] Feat: build back element (#761) * Adiciona 'build_back' * Adiciona testes * Adiciona 'dcostring' e muda 'nodes' para lista * Adapta testes --- packtools/sps/formats/sps_xml/back.py | 34 +++++++++++ tests/sps/formats/sps_xml/test_back.py | 81 ++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 packtools/sps/formats/sps_xml/back.py create mode 100644 tests/sps/formats/sps_xml/test_back.py diff --git a/packtools/sps/formats/sps_xml/back.py b/packtools/sps/formats/sps_xml/back.py new file mode 100644 index 000000000..b4eb79869 --- /dev/null +++ b/packtools/sps/formats/sps_xml/back.py @@ -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 = [ + [, ], + [] + ] + """ + 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 diff --git a/tests/sps/formats/sps_xml/test_back.py b/tests/sps/formats/sps_xml/test_back.py new file mode 100644 index 000000000..b9d313ee1 --- /dev/null +++ b/tests/sps/formats/sps_xml/test_back.py @@ -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( + '' + 'Acknowledgments' + '

Federal University of Rio de Janeiro (UFRJ), School of Medicine,...

' + '

This study was funded by the Hospital Municipal Conde Modesto Leal...

' + '
' + ) + ], + [ + ET.fromstring( + '' + 'References' + '' + '' + 'Goldberg DS, McGee SJ. Pain as a global public health priority. BMC Public Health. 2011;11:770.' + '' + '' + '' + 'Goldberg' + 'DS' + '' + '' + 'Pain as a global public health priority' + 'BMC Public Health' + '11' + '2011' + '770' + '' + '' + '' + ) + ] + ] + expected_xml_str = ( + '' + '' + 'Acknowledgments' + '

Federal University of Rio de Janeiro (UFRJ), School of Medicine,...

' + '

This study was funded by the Hospital Municipal Conde Modesto Leal...

' + '
' + '' + 'References' + '' + '' + 'Goldberg DS, McGee SJ. Pain as a global public health priority. BMC Public Health. 2011;11:770.' + '' + '' + '' + 'Goldberg' + 'DS' + '' + '' + 'Pain as a global public health priority' + 'BMC Public Health' + '11' + '2011' + '770' + '' + '' + '' + '
' + ) + 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.")