@@ -216,40 +216,54 @@ def test_any_config_timedelta_float_negative(mode):
216216    assert  s .to_json ({one_half_s : 'foo' }) ==  b'{"-1.5":"foo"}' 
217217
218218
219- def  test_any_config_timedelta_millisecond ():
220-     s  =  SchemaSerializer (core_schema .any_schema (), config = {'ser_json_timedelta' : 'milliseconds_float' })
221-     h2  =  timedelta (hours = 2 )
222-     assert  s .to_python (h2 ) ==  h2 
223-     assert  s .to_python (h2 , mode = 'json' ) ==  7200000.0 
224-     assert  s .to_json (h2 ) ==  b'7200000.0' 
225- 
226-     assert  s .to_python ({h2 : 'foo' }) ==  {h2 : 'foo' }
227-     assert  s .to_python ({h2 : 'foo' }, mode = 'json' ) ==  {'7200000' : 'foo' }
228-     assert  s .to_json ({h2 : 'foo' }) ==  b'{"7200000":"foo"}' 
229- 
230- 
231- def  test_any_config_timedelta_millisecond_fraction ():
232-     s  =  SchemaSerializer (core_schema .any_schema (), config = {'ser_json_timedelta' : 'milliseconds_float' })
233-     h2  =  timedelta (seconds = 1.5 )
234-     assert  s .to_python (h2 ) ==  h2 
235-     assert  s .to_python (h2 , mode = 'json' ) ==  1500.0 
236-     assert  s .to_json (h2 ) ==  b'1500.0' 
237- 
238-     assert  s .to_python ({h2 : 'foo' }) ==  {h2 : 'foo' }
239-     assert  s .to_python ({h2 : 'foo' }, mode = 'json' ) ==  {'1500' : 'foo' }
240-     assert  s .to_json ({h2 : 'foo' }) ==  b'{"1500":"foo"}' 
241- 
242- 
243- def  test_any_config_timedelta_millisecond_negative ():
219+ @pytest .mark .parametrize ( 
220+     'td,expected_to_python,expected_to_json,expected_to_python_dict,expected_to_json_dict' , 
221+     [ 
222+         (timedelta (hours = 2 ), 7200000.0 , b'7200000.0' , {'7200000' : 'foo' }, b'{"7200000":"foo"}' ), 
223+         (timedelta (hours = - 2 ), - 7200000.0 , b'-7200000.0' , {'-7200000' : 'foo' }, b'{"-7200000":"foo"}' ), 
224+         (timedelta (seconds = 1.5 ), 1500.0 , b'1500.0' , {'1500' : 'foo' }, b'{"1500":"foo"}' ), 
225+         (timedelta (seconds = - 1.5 ), - 1500.0 , b'-1500.0' , {'-1500' : 'foo' }, b'{"-1500":"foo"}' ), 
226+         (timedelta (microseconds = 1 ), 0.001 , b'0.001' , {'0.001' : 'foo' }, b'{"0.001":"foo"}' ), 
227+         (timedelta (microseconds = - 1 ), - 0.001 , b'-0.001' , {'-0.001' : 'foo' }, b'{"-0.001":"foo"}' ), 
228+         (timedelta (days = 1 ), 86400000.0 , b'86400000.0' , {'86400000' : 'foo' }, b'{"86400000":"foo"}' ), 
229+         (timedelta (days = - 1 ), - 86400000.0 , b'-86400000.0' , {'-86400000' : 'foo' }, b'{"-86400000":"foo"}' ), 
230+         (timedelta (days = 1 , seconds = 1 ), 86401000.0 , b'86401000.0' , {'86401000' : 'foo' }, b'{"86401000":"foo"}' ), 
231+         (timedelta (days = - 1 , seconds = - 1 ), - 86401000.0 , b'-86401000.0' , {'-86401000' : 'foo' }, b'{"-86401000":"foo"}' ), 
232+         (timedelta (days = 1 , seconds = - 1 ), 86399000.0 , b'86399000.0' , {'86399000' : 'foo' }, b'{"86399000":"foo"}' ), 
233+         ( 
234+             timedelta (days = 1 , seconds = 1 , microseconds = 1 ), 
235+             86401000.001 , 
236+             b'86401000.001' , 
237+             {'86401000.001' : 'foo' }, 
238+             b'{"86401000.001":"foo"}' , 
239+         ), 
240+         ( 
241+             timedelta (days = - 1 , seconds = - 1 , microseconds = - 1 ), 
242+             - 86401000.001 , 
243+             b'-86401000.001' , 
244+             {'-86401000.001' : 'foo' }, 
245+             b'{"-86401000.001":"foo"}' , 
246+         ), 
247+         ( 
248+             timedelta (days = - 1 , seconds = - 1 , microseconds = - 1 ), 
249+             - 86401000.001 , 
250+             b'-86401000.001' , 
251+             {'-86401000.001' : 'foo' }, 
252+             b'{"-86401000.001":"foo"}' , 
253+         ), 
254+     ], 
255+ ) 
256+ def  test_any_config_timedelta_millisecond (
257+     td : timedelta , expected_to_python , expected_to_json , expected_to_python_dict , expected_to_json_dict 
258+ ):
244259    s  =  SchemaSerializer (core_schema .any_schema (), config = {'ser_json_timedelta' : 'milliseconds_float' })
245-     h2  =  timedelta (seconds = - 1.5 )
246-     assert  s .to_python (h2 ) ==  h2 
247-     assert  s .to_python (h2 , mode = 'json' ) ==  - 1500.0 
248-     assert  s .to_json (h2 ) ==  b'-1500.0' 
260+     assert  s .to_python (td ) ==  td 
261+     assert  s .to_python (td , mode = 'json' ) ==  expected_to_python 
262+     assert  s .to_json (td ) ==  expected_to_json 
249263
250-     assert  s .to_python ({h2 : 'foo' }) ==  {h2 : 'foo' }
251-     assert  s .to_python ({h2 : 'foo' }, mode = 'json' ) ==  { '-1500' :  'foo' } 
252-     assert  s .to_json ({h2 : 'foo' }) ==  b'{"-1500":"foo"}' 
264+     assert  s .to_python ({td : 'foo' }) ==  {td : 'foo' }
265+     assert  s .to_python ({td : 'foo' }, mode = 'json' ) ==  expected_to_python_dict 
266+     assert  s .to_json ({td : 'foo' }) ==  expected_to_json_dict 
253267
254268
255269def  test_recursion (any_serializer ):
0 commit comments