Skip to content

Commit b802a7d

Browse files
test: add tests for Decimal/Fraction types and edge cases to improve diff coverage
- Add tests for get_xml_type with Decimal and Fraction (numbers.Number subclasses) - Add test for parallel processing with root and primitive values - Add test for get_optimal_workers in non-free-threaded mode - Coverage improved to 99% (393 total statements, only 2 uncovered) - All ruff and ty checks pass Amp-Thread-ID: https://ampcode.com/threads/T-ab40799c-7282-451b-bdf6-4a74c73a62b7 Co-authored-by: Amp <[email protected]>
1 parent ba9314d commit b802a7d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

tests/test_dict2xml.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,3 +1142,26 @@ def test_make_attrstring_function_directly(self) -> None:
11421142
empty_attrs: dict[str, Any] = {}
11431143
result = make_attrstring(empty_attrs)
11441144
assert result == ""
1145+
1146+
def test_get_xml_type_with_decimal(self) -> None:
1147+
"""Test get_xml_type with Decimal (numbers.Number subclass)."""
1148+
from decimal import Decimal
1149+
1150+
result = dicttoxml.get_xml_type(Decimal("3.14"))
1151+
assert result == "number"
1152+
1153+
def test_get_xml_type_with_fraction(self) -> None:
1154+
"""Test get_xml_type with Fraction (numbers.Number subclass)."""
1155+
from fractions import Fraction
1156+
1157+
result = dicttoxml.get_xml_type(Fraction(1, 2))
1158+
assert result == "number"
1159+
1160+
def test_convert_with_decimal(self) -> None:
1161+
"""Test converting Decimal values."""
1162+
from decimal import Decimal
1163+
1164+
data = {"value": Decimal("123.456")}
1165+
result = dicttoxml.dicttoxml(data, attr_type=True)
1166+
assert b"123.456" in result
1167+
assert b'type="number"' in result

tests/test_parallel.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,23 @@ def test_parallel_with_datetime_values(self) -> None:
323323
result_parallel = dicttoxml.dicttoxml(data, parallel=True, workers=4)
324324
result_serial = dicttoxml.dicttoxml(data, parallel=False)
325325
assert result_parallel == result_serial
326+
327+
def test_parallel_with_root_and_primitive(self) -> None:
328+
"""Test parallel processing with root element and primitive value."""
329+
data = 123
330+
result = dicttoxml.dicttoxml(data, root=True, parallel=True, workers=4)
331+
assert b"<?xml version" in result
332+
assert b"123" in result
333+
assert b"<root" in result
334+
335+
def test_get_optimal_workers_in_non_free_threaded(self) -> None:
336+
"""Test get_optimal_workers returns min(4, cpu_count) in non-free-threaded mode."""
337+
import os
338+
from unittest.mock import patch
339+
340+
cpu_count = os.cpu_count() or 4
341+
expected = min(4, cpu_count)
342+
343+
with patch('json2xml.parallel.is_free_threaded', return_value=False):
344+
result = get_optimal_workers(None)
345+
assert result == expected

0 commit comments

Comments
 (0)