-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adiciona 'build_back' * Adiciona testes * Adiciona 'dcostring' e muda 'nodes' para lista * Adapta testes
- Loading branch information
1 parent
ac48bd9
commit 306b3b8
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |