1616 ("yes" , True ),
1717 ("on" , True ),
1818 ("false" , False ),
19+ ("False" , False ),
1920 ("0" , False ),
2021 ("no" , False ),
2122 ("off" , False ),
22- ("" , False ),
23- ("maybe" , False ), # unexpected value
2423 ],
2524)
26- def test_cast_bool (val , expected ):
25+ def test_cast_bool_valid (val , expected ):
2726 assert _cast (val , bool ) == expected
2827
2928
29+ @pytest .mark .parametrize ("val" , ["" , "maybe" , "truth" , "none" ])
30+ def test_cast_bool_invalid (val ):
31+ with pytest .raises (ValueError ):
32+ _cast (val , bool )
33+
34+
3035def test_cast_list_normal ():
3136 assert _cast ("a,b , c" , list ) == ["a" , "b" , "c" ]
3237
3338
3439def test_cast_list_empty ():
35- assert _cast ("" , list ) == ["" ]
40+ assert _cast ("" , list ) == []
3641
3742
3843def test_cast_path ():
@@ -45,14 +50,20 @@ def test_cast_decimal_valid():
4550
4651
4752def test_cast_decimal_invalid ():
48- with pytest .raises (Exception ):
53+ with pytest .raises (ValueError ):
4954 _cast ("abc" , Decimal )
5055
5156
52- def test_cast_datetime_valid ():
57+ def test_cast_datetime_valid_iso ():
5358 assert _cast ("2024-01-01T10:00:00" , datetime ) == datetime (2024 , 1 , 1 , 10 , 0 , 0 )
5459
5560
61+ def test_cast_datetime_valid_custom_format ():
62+ val = "01-02-2025 15:30"
63+ fmt = "%d-%m-%Y %H:%M"
64+ assert _cast (val , datetime , datetime_format = fmt ) == datetime (2025 , 2 , 1 , 15 , 30 )
65+
66+
5667def test_cast_datetime_invalid ():
5768 with pytest .raises (ValueError ):
5869 _cast ("not-a-date" , datetime )
@@ -62,7 +73,11 @@ def test_cast_dict_valid():
6273 assert _cast ('{"debug": true, "port": 8000}' , dict ) == {"debug" : True , "port" : 8000 }
6374
6475
65- def test_cast_dict_invalid ():
76+ def test_cast_list_json_valid ():
77+ assert _cast ('["a", "b", "c"]' , list ) == ["a" , "b" , "c" ]
78+
79+
80+ def test_cast_dict_invalid_json ():
6681 with pytest .raises (ValueError ):
6782 _cast ("{not: valid}" , dict )
6883
@@ -89,11 +104,11 @@ def test_cast_float_valid():
89104
90105
91106class Dummy :
92- pass
107+ def __init__ (self , v ):
108+ raise TypeError ("Cannot instantiate Dummy with value" )
93109
94110
95111def test_cast_unknown_type ():
96112 val = "123"
97- dummy_type = Dummy
98- with pytest .raises (TypeError ):
99- _cast (val , dummy_type )
113+ with pytest .raises (ValueError ):
114+ _cast (val , Dummy )
0 commit comments