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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
import collections
import textwrap
import fsspec
import pytest
from safe_rcm import xml
def dedent(text):
return textwrap.dedent(text.removeprefix("\n").rstrip())
schemas = [
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</xsd:schema>
"""
),
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="schema2.xsd"/>
</xsd:schema>
"""
),
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="schema1.xsd"/>
<xsd:include schemaLocation="schema2.xsd"/>
</xsd:schema>
"""
),
]
Container = collections.namedtuple("SchemaSetup", ["mapper", "path", "expected"])
SchemaProperties = collections.namedtuple(
"SchemaProperties", ["root_elements", "simple_types", "complex_types"]
)
@pytest.fixture(params=enumerate(schemas))
def schema_setup(request):
schema_index, schema = request.param
mapper = fsspec.get_mapper("memory")
mapper["schemas/root.xsd"] = schema.encode()
mapper["schemas/schema1.xsd"] = dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="schema3.xsd"/>
<xsd:element name="manifest" type="manifest"/>
</xsd:schema>
"""
).encode()
mapper["schemas/schema2.xsd"] = dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="schema4.xsd"/>
<xsd:element name="count" type="count"/>
</xsd:schema>
"""
).encode()
mapper["schemas/schema3.xsd"] = dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="schema3.xsd"/>
<xsd:complexType name="manifest">
<xsd:sequence>
<xsd:element name="quantity_a" type="count"/>
<xsd:element name="quantity_b" type="count"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
"""
).encode()
mapper["schemas/schema4.xsd"] = dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="count">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
"""
).encode()
return schema_index, mapper
@pytest.fixture
def schema_paths_setup(schema_setup):
schema_index, mapper = schema_setup
expected = [
["schemas/root.xsd"],
["schemas/root.xsd", "schemas/schema2.xsd", "schemas/schema4.xsd"],
[
"schemas/root.xsd",
"schemas/schema1.xsd",
"schemas/schema2.xsd",
"schemas/schema3.xsd",
"schemas/schema4.xsd",
],
]
return Container(mapper, "schemas/root.xsd", expected[schema_index])
@pytest.fixture
def schema_content_setup(schema_setup):
schema_index, mapper = schema_setup
count_type = {"name": "count", "type": "simple", "base_type": "integer"}
manifest_type = {"name": "manifest", "type": "complex"}
manifest_element = {"name": "manifest", "type": manifest_type}
count_element = {"name": "count", "type": count_type}
expected = [
SchemaProperties([], [], []),
SchemaProperties([count_element], [count_type], []),
SchemaProperties(
[manifest_element, count_element], [count_type], [manifest_type]
),
]
return Container(mapper, "schemas/root.xsd", expected[schema_index])
@pytest.fixture(params=["data.xml", "data/file.xml"])
def data_file_setup(request):
path = request.param
mapper = fsspec.get_mapper("memory")
mapper["schemas/root.xsd"] = dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="schema1.xsd"/>
<xsd:include schemaLocation="schema2.xsd"/>
<xsd:complexType name="elements">
<xsd:sequence>
<xsd:element name="summary" type="manifest"/>
<xsd:element name="count" type="count"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="elements" type="elements"/>
</xsd:schema>
"""
).encode()
mapper["schemas/schema1.xsd"] = dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="schema2.xsd"/>
<xsd:complexType name="manifest">
<xsd:sequence>
<xsd:element name="quantity_a" type="count"/>
<xsd:element name="quantity_b" type="count"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
"""
).encode()
mapper["schemas/schema2.xsd"] = dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="count">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
"""
).encode()
schema_path = "schemas/root.xsd" if "/" not in path else "../schemas/root.xsd"
mapper[path] = dedent(
f"""
<?xml version="1.0" encoding="UTF-8"?>
<elements xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="schema {schema_path}">
<summary>
<quantity_a>1</quantity_a>
<quantity_b>2</quantity_b>
</summary>
<count>3</count>
</elements>
"""
).encode()
expected = {
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"@xsi:schemaLocation": f"schema {schema_path}",
"summary": {"quantity_a": 1, "quantity_b": 2},
"count": 3,
}
return Container(mapper, path, expected)
def convert_type(t):
def strip_namespace(name):
return name.split("}", maxsplit=1)[1]
if hasattr(t, "content"):
# complex type
return {"name": t.name, "type": "complex"}
elif hasattr(t, "base_type"):
# simple type, only restriction
return {
"name": t.name,
"base_type": strip_namespace(t.base_type.name),
"type": "simple",
}
def convert_element(el):
return {"name": el.name, "type": convert_type(el.type)}
def extract_schema_properties(schema):
return SchemaProperties(
[convert_element(v) for v in schema.root_elements],
[convert_type(v) for v in schema.simple_types],
[convert_type(v) for v in schema.complex_types],
)
def test_remove_includes():
expected = schemas[0]
actual = xml.remove_includes(schemas[1])
assert actual == expected
@pytest.mark.parametrize(
["schema", "expected"],
(
(schemas[0], []),
(schemas[1], ["schema2.xsd"]),
(schemas[2], ["schema1.xsd", "schema2.xsd"]),
),
)
def test_extract_includes(schema, expected):
actual = xml.extract_includes(schema)
assert actual == expected
@pytest.mark.parametrize(
["root", "path", "expected"],
(
("", "file.xml", "file.xml"),
("/root", "file.xml", "/root/file.xml"),
("/root", "/other_root/file.xml", "/other_root/file.xml"),
),
)
def test_normalize(root, path, expected):
actual = xml.normalize(root, path)
assert actual == expected
def test_schema_paths(schema_paths_setup):
actual = xml.schema_paths(schema_paths_setup.mapper, schema_paths_setup.path)
expected = schema_paths_setup.expected
assert actual == expected
def test_open_schemas(schema_content_setup):
container = schema_content_setup
actual = xml.open_schema(container.mapper, container.path)
expected = container.expected
assert extract_schema_properties(actual) == expected
def test_read_xml(data_file_setup):
container = data_file_setup
actual = xml.read_xml(container.mapper, container.path)
assert actual == container.expected
|