-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml2python_test.py
More file actions
159 lines (153 loc) · 3.99 KB
/
xml2python_test.py
File metadata and controls
159 lines (153 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Copyright (c) 2025, binary butterfly GmbH
Use of this source code is governed by an MIT-style license that can be found in the LICENSE.txt.
"""
from typing import List, Optional, Tuple
import pytest
from lxml import etree
from tests.test_data import (
conditional_remote_type_tags_2a,
conditional_remote_type_tags_3a,
ensure_array_keys_1c,
ignore_attributes_4,
remote_type_tags_1a,
remote_type_tags_1b,
remote_type_tags_2a,
xml_dict_example_1,
xml_dict_example_1a,
xml_dict_example_1b,
xml_dict_example_1c,
xml_dict_example_2,
xml_dict_example_2a,
xml_dict_example_3,
xml_dict_example_3a,
xml_dict_example_4,
xml_dict_example_5,
xml_etree_example_1,
xml_etree_example_2,
xml_etree_example_3,
xml_etree_example_4,
xml_etree_example_5,
)
from xml2python import Xml2Python
@pytest.mark.parametrize(
'input_tag, expected_output, ensure_array_keys, remote_type_tags, conditional_remote_type_tags, ignore_attributes',
[
(
# simple case
xml_etree_example_1,
xml_dict_example_1,
None,
None,
None,
None,
),
(
# skipping some tags
xml_etree_example_1,
xml_dict_example_1a,
None,
remote_type_tags_1a,
None,
None,
),
(
# skipping even more tags
xml_etree_example_1,
xml_dict_example_1b,
None,
remote_type_tags_1b,
None,
None,
),
(
# setting some array keys
xml_etree_example_1,
xml_dict_example_1c,
ensure_array_keys_1c,
None,
None,
None,
),
(
# other simple example
xml_etree_example_2,
xml_dict_example_2,
None,
None,
None,
None,
),
(
# using remote type tags to make it even simpler
xml_etree_example_2,
xml_dict_example_2a,
None,
remote_type_tags_2a,
None,
None,
),
(
# the same but with conditional remote type tags
xml_etree_example_2,
xml_dict_example_2a,
None,
None,
conditional_remote_type_tags_2a,
None,
),
(
# another simple example with a redundant tag
xml_etree_example_3,
xml_dict_example_3,
None,
None,
None,
None,
),
(
# removing the redundant tag
xml_etree_example_3,
xml_dict_example_3a,
None,
None,
conditional_remote_type_tags_3a,
None,
),
(
# solution with ignore_attributes
xml_etree_example_4,
xml_dict_example_4,
None,
None,
None,
ignore_attributes_4,
),
(
# an attribute and a tag each named 'class',
# and also a text in combination with children and attributes
xml_etree_example_5,
xml_dict_example_5,
None,
None,
None,
None,
),
],
)
def test_xml_to_dict(
input_tag: etree.Element,
expected_output: dict,
ensure_array_keys: Optional[List[Tuple[str, str]]],
remote_type_tags: Optional[List[str]],
conditional_remote_type_tags: Optional[List[Tuple[str, str]]],
ignore_attributes: Optional[List[str]],
):
result_dict: dict = Xml2Python.xml_to_dict(
tag=input_tag,
ensure_array_keys=ensure_array_keys,
remote_type_tags=remote_type_tags,
conditional_remote_type_tags=conditional_remote_type_tags,
ignore_attributes=ignore_attributes,
)
assert result_dict == expected_output