Skip to content

Commit 75cf65d

Browse files
test: add comprehensive tests for parallel processing to improve coverage
- Add tests for parallel processing without root element (dict, list, primitive) - Add tests for make_valid_xml_name_cached function with various edge cases - Add tests for parallel processing with sequences, None values, and error cases - Add tests for boolean and datetime values in parallel mode - Coverage improved from 94% to 99% Amp-Thread-ID: https://ampcode.com/threads/T-ab40799c-7282-451b-bdf6-4a74c73a62b7 Co-authored-by: Amp <[email protected]>
1 parent d882c02 commit 75cf65d

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tests/test_parallel.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
get_optimal_workers,
1212
is_free_threaded,
1313
key_is_valid_xml_cached,
14+
make_valid_xml_name_cached,
1415
)
1516

1617
if TYPE_CHECKING:
@@ -240,3 +241,85 @@ def test_parallel_dict_order_preserved(self) -> None:
240241
)
241242

242243
assert result_parallel == result_serial
244+
245+
def test_parallel_no_root(self) -> None:
246+
"""Test parallel processing without root element."""
247+
data = {f"item{i}": i for i in range(20)}
248+
result = dicttoxml.dicttoxml(data, root=False, parallel=True, workers=4)
249+
assert b"<?xml version" not in result
250+
251+
def test_parallel_list_no_root(self) -> None:
252+
"""Test parallel processing of list without root element."""
253+
data = [{"id": i, "name": f"item{i}"} for i in range(20)]
254+
result = dicttoxml.dicttoxml(data, root=False, parallel=True, workers=4)
255+
assert b"<?xml version" not in result
256+
257+
def test_parallel_primitive_no_root(self) -> None:
258+
"""Test parallel processing of primitive value without root element."""
259+
data = 42
260+
result = dicttoxml.dicttoxml(data, root=False, parallel=True, workers=4)
261+
assert b"<?xml version" not in result
262+
assert b"42" in result
263+
264+
def test_make_valid_xml_name_cached_with_digit_string(self) -> None:
265+
"""Test make_valid_xml_name_cached with digit string."""
266+
key, attr = make_valid_xml_name_cached("456", {})
267+
assert key == "n456"
268+
assert attr == {}
269+
270+
def test_make_valid_xml_name_cached_with_space(self) -> None:
271+
"""Test make_valid_xml_name_cached with space in key."""
272+
key, attr = make_valid_xml_name_cached("my key", {})
273+
assert key == "my_key"
274+
assert attr == {}
275+
276+
def test_make_valid_xml_name_cached_with_colon(self) -> None:
277+
"""Test make_valid_xml_name_cached with colon in key."""
278+
key, attr = make_valid_xml_name_cached("ns:element", {})
279+
assert key == "ns:element"
280+
assert attr == {}
281+
282+
def test_make_valid_xml_name_cached_with_invalid_chars(self) -> None:
283+
"""Test make_valid_xml_name_cached with invalid XML characters."""
284+
key, attr = make_valid_xml_name_cached("in<valid>key", {})
285+
assert key == "key"
286+
assert attr["name"] == "in&lt;valid&gt;key"
287+
288+
def test_parallel_with_sequence_value(self) -> None:
289+
"""Test parallel processing with sequence values in dict."""
290+
data = {f"key{i}": [f"val{j}" for j in range(3)] for i in range(15)}
291+
result_parallel = dicttoxml.dicttoxml(data, parallel=True, workers=4)
292+
result_serial = dicttoxml.dicttoxml(data, parallel=False)
293+
assert result_parallel == result_serial
294+
295+
def test_parallel_with_none_values(self) -> None:
296+
"""Test parallel processing with None values."""
297+
data = {f"key{i}": None for i in range(15)}
298+
result_parallel = dicttoxml.dicttoxml(data, parallel=True, workers=4)
299+
result_serial = dicttoxml.dicttoxml(data, parallel=False)
300+
assert result_parallel == result_serial
301+
302+
def test_parallel_unsupported_type_error(self) -> None:
303+
"""Test that unsupported types raise TypeError in parallel mode."""
304+
class CustomType:
305+
pass
306+
307+
data = {f"key{i}": CustomType() for i in range(15)}
308+
with pytest.raises(TypeError, match="Unsupported data type"):
309+
dicttoxml.dicttoxml(data, parallel=True, workers=4)
310+
311+
def test_parallel_with_bool_values(self) -> None:
312+
"""Test parallel processing with boolean values."""
313+
data = {f"key{i}": i % 2 == 0 for i in range(15)}
314+
result_parallel = dicttoxml.dicttoxml(data, parallel=True, workers=4)
315+
result_serial = dicttoxml.dicttoxml(data, parallel=False)
316+
assert result_parallel == result_serial
317+
318+
def test_parallel_with_datetime_values(self) -> None:
319+
"""Test parallel processing with datetime values."""
320+
from datetime import datetime
321+
322+
data = {f"key{i}": datetime(2024, 1, i + 1) for i in range(15)}
323+
result_parallel = dicttoxml.dicttoxml(data, parallel=True, workers=4)
324+
result_serial = dicttoxml.dicttoxml(data, parallel=False)
325+
assert result_parallel == result_serial

0 commit comments

Comments
 (0)