|
1 |
| -import xml.etree.ElementTree as ET |
| 1 | +# docker-compose -f local.yml run --rm django pytest config_generation/tests/test_db_to_xml.py |
| 2 | +from xml.etree.ElementTree import ElementTree, ParseError, fromstring |
| 3 | + |
| 4 | +import pytest |
2 | 5 |
|
3 | 6 | from ..db_to_xml import XmlEditor
|
4 | 7 |
|
@@ -28,39 +31,112 @@ def elements_equal(e1, e2):
|
28 | 31 | return False
|
29 | 32 | return all(elements_equal(c1, c2) for c1, c2 in zip(e1, e2))
|
30 | 33 |
|
31 |
| - tree1 = ET.fromstring(xml1) |
32 |
| - tree2 = ET.fromstring(xml2) |
33 |
| - return elements_equal(tree1, tree2) |
| 34 | + tree1 = ElementTree(fromstring(xml1)) |
| 35 | + tree2 = ElementTree(fromstring(xml2)) |
34 | 36 |
|
| 37 | + return elements_equal(tree1.getroot(), tree2.getroot()) |
35 | 38 |
|
36 |
| -def test_update_or_add_element_value(): |
37 |
| - xml_string = """<root> |
38 |
| - <child> |
39 |
| - <grandchild>old_value</grandchild> |
40 |
| - </child> |
41 |
| - </root>""" |
42 | 39 |
|
| 40 | +# Tests for valid and invalid XML initializations |
| 41 | +def test_valid_xml_initialization(): |
| 42 | + xml_string = "<root><child>Test</child></root>" |
43 | 43 | editor = XmlEditor(xml_string)
|
| 44 | + assert editor.get_tag_value("child") == ["Test"] |
44 | 45 |
|
45 |
| - # To update an existing element's value |
46 |
| - updated_xml = editor.update_or_add_element_value("child/grandchild", "new_value") |
47 |
| - expected_output = """<root> |
48 |
| - <child> |
49 |
| - <grandchild>new_value</grandchild> |
50 |
| - </child> |
51 |
| - </root> |
52 |
| - """ |
53 |
| - assert xmls_equal(updated_xml, expected_output) |
54 |
| - |
55 |
| - # To create a new element and set its value |
56 |
| - new_xml = editor.update_or_add_element_value("newchild", "some_value") |
57 |
| - expected_output = """<root> |
58 |
| - <child> |
59 |
| - <grandchild>new_value</grandchild> |
60 |
| - </child> |
61 |
| - <newchild> |
62 |
| - some_value |
63 |
| - </newchild> |
64 |
| - </root> |
65 |
| - """ |
66 |
| - assert xmls_equal(new_xml, expected_output) |
| 46 | + |
| 47 | +def test_invalid_xml_initialization(): |
| 48 | + with pytest.raises(ParseError): |
| 49 | + XmlEditor("<root><child></root>") |
| 50 | + |
| 51 | + |
| 52 | +# Test retrieval of single and multiple tag values |
| 53 | +def test_get_single_tag_value(): |
| 54 | + xml_string = "<root><child>Test</child></root>" |
| 55 | + editor = XmlEditor(xml_string) |
| 56 | + assert editor.get_tag_value("child", strict=True) == "Test" |
| 57 | + |
| 58 | + |
| 59 | +def test_get_nonexistent_tag_value(): |
| 60 | + xml_string = "<root><child>Test</child></root>" |
| 61 | + editor = XmlEditor(xml_string) |
| 62 | + assert editor.get_tag_value("nonexistent", strict=False) == [] |
| 63 | + |
| 64 | + |
| 65 | +def test_get_tag_value_strict_multiple_elements(): |
| 66 | + xml_string = "<root><child>One</child><child>Two</child></root>" |
| 67 | + editor = XmlEditor(xml_string) |
| 68 | + with pytest.raises(ValueError): |
| 69 | + editor.get_tag_value("child", strict=True) |
| 70 | + |
| 71 | + |
| 72 | +# Test updating and adding XML elements |
| 73 | +def test_update_existing_element(): |
| 74 | + xml_string = "<root><child>Old</child></root>" |
| 75 | + editor = XmlEditor(xml_string) |
| 76 | + editor.update_or_add_element_value("child", "New") |
| 77 | + updated_xml = editor.update_config_xml() |
| 78 | + assert "New" in updated_xml and "Old" not in updated_xml |
| 79 | + |
| 80 | + |
| 81 | +def test_add_new_element(): |
| 82 | + xml_string = "<root></root>" |
| 83 | + editor = XmlEditor(xml_string) |
| 84 | + editor.update_or_add_element_value("newchild", "Value") |
| 85 | + updated_xml = editor.update_config_xml() |
| 86 | + assert "Value" in updated_xml and "<newchild>Value</newchild>" in updated_xml |
| 87 | + |
| 88 | + |
| 89 | +def test_add_third_level_hierarchy(): |
| 90 | + xml_string = "<root></root>" |
| 91 | + editor = XmlEditor(xml_string) |
| 92 | + editor.update_or_add_element_value("parent/child/grandchild", "DeeplyNested") |
| 93 | + updated_xml = editor.update_config_xml() |
| 94 | + root = fromstring(updated_xml) |
| 95 | + grandchild = root.find(".//grandchild") |
| 96 | + assert grandchild is not None, "Grandchild element not found" |
| 97 | + assert grandchild.text == "DeeplyNested", "Grandchild does not contain the correct text" |
| 98 | + |
| 99 | + # Check complete path |
| 100 | + parent = root.find(".//parent/child/grandchild") |
| 101 | + assert parent is not None, "Complete path to grandchild not found" |
| 102 | + assert parent.text == "DeeplyNested", "Complete path to grandchild does not contain correct text" |
| 103 | + |
| 104 | + |
| 105 | +# Test transformations and generic mapping |
| 106 | +def test_convert_indexer_to_scraper_transformation(): |
| 107 | + xml_string = """<root><Plugin>Indexer</Plugin></root>""" |
| 108 | + editor = XmlEditor(xml_string) |
| 109 | + editor.convert_indexer_to_scraper() |
| 110 | + updated_xml = editor.update_config_xml() |
| 111 | + assert "<Plugin>SMD_Plugins/Sinequa.Plugin.ListCandidateUrls</Plugin>" in updated_xml |
| 112 | + assert "<Plugin>Indexer</Plugin>" not in updated_xml |
| 113 | + |
| 114 | + |
| 115 | +def test_generic_mapping_addition(): |
| 116 | + xml_string = "<root></root>" |
| 117 | + editor = XmlEditor(xml_string) |
| 118 | + editor._generic_mapping(name="id", value="doc.url1", selection="url1") |
| 119 | + updated_xml = editor.update_config_xml() |
| 120 | + assert "<Mapping>" in updated_xml |
| 121 | + assert "<Name>id</Name>" in updated_xml |
| 122 | + assert "<Value>doc.url1</Value>" in updated_xml |
| 123 | + |
| 124 | + |
| 125 | +# Test XML serialization with headers |
| 126 | +def test_xml_serialization_with_header(): |
| 127 | + xml_string = "<root><child>Value</child></root>" |
| 128 | + editor = XmlEditor(xml_string) |
| 129 | + xml_output = editor.update_config_xml() |
| 130 | + assert '<?xml version="1.0" encoding="utf-8"?>' in xml_output |
| 131 | + assert "<root>" in xml_output and "<child>Value</child>" in xml_output |
| 132 | + |
| 133 | + |
| 134 | +# Test handling multiple changes accumulation |
| 135 | +def test_multiple_changes_accumulation(): |
| 136 | + xml_string = "<root><child>Initial</child></root>" |
| 137 | + editor = XmlEditor(xml_string) |
| 138 | + editor.update_or_add_element_value("child", "Modified") |
| 139 | + editor.update_or_add_element_value("newchild", "Added") |
| 140 | + updated_xml = editor.update_config_xml() |
| 141 | + assert "Modified" in updated_xml and "Added" in updated_xml |
| 142 | + assert "Initial" not in updated_xml |
0 commit comments