Skip to content

Commit 299baa0

Browse files
committed
fix: get the dicttoxml test improved
1 parent ea0b743 commit 299baa0

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

.coverage

0 Bytes
Binary file not shown.

tests/test_dict2xml.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,3 +1135,59 @@ def test_make_attrstring_function_directly(self) -> None:
11351135
empty_attrs: dict[str, Any] = {}
11361136
result = make_attrstring(empty_attrs)
11371137
assert result == ""
1138+
1139+
def test_get_unique_id_collision_coverage(self) -> None:
1140+
"""Test get_unique_id to cover line 50 - the collision case."""
1141+
import json2xml.dicttoxml as module
1142+
1143+
# Clear the global _used_ids set to start fresh
1144+
original_used_ids = module._used_ids.copy()
1145+
module._used_ids.clear()
1146+
1147+
# Mock make_id to return the same ID twice, then a different one
1148+
original_make_id = module.make_id
1149+
call_count = 0
1150+
1151+
def mock_make_id(element: str, start: int = 100000, end: int = 999999) -> str:
1152+
nonlocal call_count
1153+
call_count += 1
1154+
if call_count == 1:
1155+
return "test_123456" # First call - will be added to _used_ids
1156+
elif call_count == 2:
1157+
return "test_123456" # Second call - will collide, triggering line 50
1158+
else:
1159+
return "test_789012" # Third call - unique
1160+
1161+
module.make_id = mock_make_id
1162+
1163+
try:
1164+
# First call - adds "test_123456" to _used_ids
1165+
result1 = dicttoxml.get_unique_id("test")
1166+
assert result1 == "test_123456"
1167+
1168+
# Reset call count for second test
1169+
call_count = 0
1170+
1171+
# Second call - should trigger collision and regenerate
1172+
result2 = dicttoxml.get_unique_id("test")
1173+
assert result2 == "test_789012"
1174+
assert call_count == 3 # Should have called make_id 3 times
1175+
finally:
1176+
module.make_id = original_make_id
1177+
module._used_ids.clear()
1178+
module._used_ids.update(original_used_ids)
1179+
1180+
def test_get_xml_type_numbers_number_coverage(self) -> None:
1181+
"""Test get_xml_type to cover line 90 - numbers.Number that's not int/float."""
1182+
import decimal
1183+
import fractions
1184+
1185+
# Test with Decimal (numbers.Number but not int/float)
1186+
decimal_val = decimal.Decimal('3.14159')
1187+
result = dicttoxml.get_xml_type(decimal_val)
1188+
assert result == "number"
1189+
1190+
# Test with Fraction (numbers.Number but not int/float)
1191+
fraction_val = fractions.Fraction(22, 7)
1192+
result = dicttoxml.get_xml_type(fraction_val)
1193+
assert result == "number"

0 commit comments

Comments
 (0)