@@ -26,34 +26,34 @@ class TestGetUniqueIdDuplicateGeneration:
2626
2727 def test_get_unique_id_generates_id_when_duplicates_occur (self ) -> None :
2828 """Test that get_unique_id handles the while loop by regenerating IDs on duplicates.
29-
29+
3030 Line 52 (this_id = make_id(element)) is executed when a duplicate is found.
3131 Since make_id uses SystemRandom, we can't guarantee duplicates, but we can
3232 ensure the function returns a valid ID in the correct format.
3333 """
3434 result = get_unique_id ("test_element" )
35-
35+
3636 # Verify it returns a string in the expected format
3737 assert isinstance (result , str )
3838 assert result .startswith ("test_element_" )
3939 assert len (result ) > len ("test_element_" )
40-
40+
4141 # Verify the numeric part is valid
4242 numeric_part = result .replace ("test_element_" , "" )
4343 assert numeric_part .isdigit ()
4444 assert 100000 <= int (numeric_part ) <= 999999
4545
4646 def test_get_unique_id_duplicate_id_regeneration (self ) -> None :
4747 """Test line 52: trigger duplicate ID regeneration.
48-
48+
4949 Line 52 (this_id = make_id(element)) only executes if this_id is in the ids list.
5050 We can't easily trigger this with the current implementation since ids starts empty,
5151 but we test the function's robustness when make_id returns duplicates.
5252 """
5353 # Call get_unique_id multiple times - while theoretically one could duplicate
5454 # due to SystemRandom, the function handles this correctly
5555 ids_generated = [get_unique_id ("test" ) for _ in range (10 )]
56-
56+
5757 # All generated IDs should be properly formatted
5858 for id_val in ids_generated :
5959 assert isinstance (id_val , str )
@@ -68,19 +68,19 @@ class TestGetXmlTypeWithNumbers:
6868
6969 def test_get_xml_type_with_decimal (self ) -> None :
7070 """Test get_xml_type with Decimal (numbers.Number subclass).
71-
71+
7272 Line 90 (return "number") is executed for instances of numbers.Number
7373 that don't match other type checks (str, int, float, bool).
7474 """
7575 from decimal import Decimal
76-
76+
7777 result = get_xml_type (Decimal ("123.45" ))
7878 assert result == "number"
7979
8080 def test_get_xml_type_with_fraction (self ) -> None :
8181 """Test get_xml_type with Fraction (numbers.Number subclass)."""
8282 from fractions import Fraction
83-
83+
8484 result = get_xml_type (Fraction (3 , 4 ))
8585 assert result == "number"
8686
@@ -95,30 +95,30 @@ class TestGetXpath31TagNameWithBytesAndBytearray:
9595
9696 def test_get_xpath31_tag_name_with_bytes (self ) -> None :
9797 """Test get_xpath31_tag_name with bytes.
98-
98+
9999 Line 219 (return "string") is executed for bytes instances.
100100 """
101101 result = get_xpath31_tag_name (b"hello" )
102102 assert result == "string"
103103
104104 def test_get_xpath31_tag_name_with_bytearray (self ) -> None :
105105 """Test get_xpath31_tag_name with bytearray.
106-
106+
107107 Line 219 (return "string") is executed for bytearray instances.
108108 """
109109 result = get_xpath31_tag_name (bytearray (b"hello" ))
110110 assert result == "string"
111111
112112 def test_get_xpath31_tag_name_with_unsupported_type (self ) -> None :
113113 """Test get_xpath31_tag_name with unsupported type falls back to string.
114-
114+
115115 Line 222 (return "string") is the fallback for types that don't match
116116 any of the specific type checks.
117117 """
118118 # Create an object that isn't any of the handled types
119119 class CustomObject :
120120 pass
121-
121+
122122 result = get_xpath31_tag_name (CustomObject ())
123123 assert result == "string"
124124
@@ -138,16 +138,16 @@ class TestConvertToXpath31Fallback:
138138
139139 def test_convert_to_xpath31_with_unsupported_type (self ) -> None :
140140 """Test convert_to_xpath31 with unsupported type falls back to string conversion.
141-
141+
142142 Line 261 is the fallback case that converts unsupported types to strings.
143143 """
144144 # Create an object that will trigger the fallback
145145 class CustomObject :
146146 def __str__ (self ) -> str :
147147 return "custom_object_string"
148-
148+
149149 result = convert_to_xpath31 (CustomObject ())
150-
150+
151151 # Should wrap it in a string tag
152152 assert "<string>" in result
153153 assert "custom_object_string" in result
@@ -158,27 +158,27 @@ def test_convert_to_xpath31_with_custom_object_and_parent_key(self) -> None:
158158 class CustomObject :
159159 def __str__ (self ) -> str :
160160 return "test_value"
161-
161+
162162 result = convert_to_xpath31 (CustomObject (), parent_key = "my_key" )
163-
163+
164164 # Should include key attribute
165165 assert 'key="my_key"' in result
166166 assert "<string" in result
167167
168168 def test_convert_to_xpath31_fallback_line_261 (self ) -> None :
169169 """Test line 261: fallback case when get_xpath31_tag_name returns unexpected value.
170-
170+
171171 This test mocks get_xpath31_tag_name to return an unexpected value,
172172 forcing the fallback on line 261 to execute.
173173 """
174174 test_obj = {"data" : "test" }
175-
175+
176176 with patch ('json2xml.dicttoxml.get_xpath31_tag_name' ) as mock_tag :
177177 # Return an unexpected tag name that won't match any if statements
178178 mock_tag .return_value = "unexpected_type"
179-
179+
180180 result = convert_to_xpath31 (test_obj )
181-
181+
182182 # Should fall through to line 261 and return string representation
183183 assert "<string>" in result
184184 assert result .startswith ("<string>" )
@@ -187,12 +187,12 @@ def test_convert_to_xpath31_fallback_line_261(self) -> None:
187187 def test_convert_to_xpath31_fallback_with_key_attr (self ) -> None :
188188 """Test line 261 fallback with key attribute."""
189189 test_obj = object ()
190-
190+
191191 with patch ('json2xml.dicttoxml.get_xpath31_tag_name' ) as mock_tag :
192192 mock_tag .return_value = "unknown"
193-
193+
194194 result = convert_to_xpath31 (test_obj , parent_key = "test_key" )
195-
195+
196196 # Should include key attribute in fallback output
197197 assert 'key="test_key"' in result
198198 assert "<string" in result
@@ -205,7 +205,7 @@ def test_xpath31_with_bytes_value(self) -> None:
205205 """Test xpath31 format conversion with bytes in dict."""
206206 obj = {"data" : b"binary_data" }
207207 result = dicttoxml (obj , xpath_format = True )
208-
208+
209209 # Should not raise and should produce valid XML
210210 assert b"<?xml" in result
211211 assert b"map" in result
@@ -214,10 +214,10 @@ def test_xpath31_with_bytes_value(self) -> None:
214214 def test_xpath31_with_decimal_value (self ) -> None :
215215 """Test xpath31 format conversion with Decimal number."""
216216 from decimal import Decimal
217-
217+
218218 obj = {"amount" : Decimal ("99.99" )}
219219 result = dicttoxml (obj , xpath_format = True )
220-
220+
221221 # Should convert Decimal to number tag via fallback handling
222222 assert b"<?xml" in result
223223 assert b"map" in result
@@ -227,10 +227,10 @@ def test_xpath31_with_nested_custom_object(self) -> None:
227227 class Point :
228228 def __str__ (self ) -> str :
229229 return "0,0"
230-
230+
231231 obj = {"location" : Point ()}
232232 result = dicttoxml (obj , xpath_format = True )
233-
233+
234234 assert b"<?xml" in result
235235 assert b"map" in result
236236 assert b"string" in result
@@ -244,10 +244,10 @@ def test_get_xml_type_with_custom_number_class(self) -> None:
244244 class CustomNumber (numbers .Number ):
245245 def __init__ (self , value : float ) -> None :
246246 self .value = value
247-
247+
248248 def __str__ (self ) -> str :
249249 return str (self .value )
250-
250+
251251 result = get_xml_type (CustomNumber (42 ))
252252 assert result == "number"
253253
@@ -256,7 +256,7 @@ def test_get_xml_type_prioritizes_bool_over_number(self) -> None:
256256 # This tests the ordering logic that prevents True/False from being typed as int
257257 result_true = get_xml_type (True )
258258 result_false = get_xml_type (False )
259-
259+
260260 assert result_true == "bool"
261261 assert result_false == "bool"
262262 # If the order was wrong, they'd be "int"
@@ -271,33 +271,33 @@ def test_xpath31_tag_name_all_types(self) -> None:
271271 """Test all type branches in get_xpath31_tag_name."""
272272 # None
273273 assert get_xpath31_tag_name (None ) == "null"
274-
274+
275275 # bool (must come before int check)
276276 assert get_xpath31_tag_name (True ) == "boolean"
277277 assert get_xpath31_tag_name (False ) == "boolean"
278-
278+
279279 # dict
280280 assert get_xpath31_tag_name ({}) == "map"
281281 assert get_xpath31_tag_name ({"key" : "value" }) == "map"
282-
282+
283283 # numbers
284284 assert get_xpath31_tag_name (42 ) == "number"
285285 assert get_xpath31_tag_name (3.14 ) == "number"
286-
286+
287287 # str
288288 assert get_xpath31_tag_name ("hello" ) == "string"
289289 assert get_xpath31_tag_name ("" ) == "string"
290-
290+
291291 # bytes and bytearray
292292 assert get_xpath31_tag_name (b"hello" ) == "string"
293293 assert get_xpath31_tag_name (bytearray (b"hello" )) == "string"
294-
294+
295295 # Sequence (list, tuple)
296296 assert get_xpath31_tag_name ([]) == "array"
297297 assert get_xpath31_tag_name ([1 , 2 , 3 ]) == "array"
298298 assert get_xpath31_tag_name (()) == "array"
299299 assert get_xpath31_tag_name ((1 , 2 , 3 )) == "array"
300-
300+
301301 # Fallback
302302 class CustomType :
303303 pass
@@ -310,13 +310,13 @@ class TestGetUniqueIdDuplicateIteration:
310310 def test_get_unique_id_returns_valid_format (self ) -> None :
311311 """Test that get_unique_id returns properly formatted ID."""
312312 from json2xml .dicttoxml import get_unique_id
313-
313+
314314 # Call multiple times to ensure consistency
315315 ids = [get_unique_id ("element" ) for _ in range (5 )]
316-
316+
317317 # All should be unique
318318 assert len (set (ids )) == 5
319-
319+
320320 # All should follow the format
321321 for id_val in ids :
322322 assert isinstance (id_val , str )
@@ -332,7 +332,7 @@ def test_convert_to_xpath31_null(self) -> None:
332332 """Test null conversion."""
333333 result = convert_to_xpath31 (None )
334334 assert result == "<null/>"
335-
335+
336336 # With key
337337 result = convert_to_xpath31 (None , parent_key = "empty" )
338338 assert 'key="empty"' in result
@@ -342,10 +342,10 @@ def test_convert_to_xpath31_boolean(self) -> None:
342342 """Test boolean conversion."""
343343 result_true = convert_to_xpath31 (True )
344344 assert "<boolean>true</boolean>" == result_true
345-
345+
346346 result_false = convert_to_xpath31 (False )
347347 assert "<boolean>false</boolean>" == result_false
348-
348+
349349 # With key
350350 result = convert_to_xpath31 (True , parent_key = "is_active" )
351351 assert 'key="is_active"' in result
@@ -356,15 +356,15 @@ def test_convert_to_xpath31_number(self) -> None:
356356 """Test number conversion."""
357357 result = convert_to_xpath31 (42 )
358358 assert "<number>42</number>" == result
359-
359+
360360 result = convert_to_xpath31 (3.14 )
361361 assert "<number>3.14</number>" == result
362362
363363 def test_convert_to_xpath31_string (self ) -> None :
364364 """Test string conversion."""
365365 result = convert_to_xpath31 ("hello" )
366366 assert "<string>hello</string>" == result
367-
367+
368368 # With special characters that need escaping
369369 result = convert_to_xpath31 ("hello & <world>" )
370370 assert "<string>" in result
@@ -399,7 +399,7 @@ def test_convert_to_xpath31_custom_object_fallback(self) -> None:
399399 class Point :
400400 def __str__ (self ) -> str :
401401 return "(1,2)"
402-
402+
403403 result = convert_to_xpath31 (Point ())
404404 assert "<string>" in result
405405 assert "(1,2)" in result
0 commit comments