@@ -23,31 +23,28 @@ class _TestDataClass:
2323 '"Hello" "world"' , [str , str ], ["Hello" , "world" ], id = "space delimited"
2424 ),
2525 pytest .param (
26- '["Hello ", "world"]' , [ str , str ], ["Hello" , "world" ], id = "json array "
26+ "1 " , [ int , int ], [1 , 0 ], id = "ints "
2727 ),
2828 pytest .param (
29- "[1] " , [int , int ], [1 , 0 ], id = "ints "
29+ "1.5 " , [float , float ], [1.5 , 0.0 ], id = "floats "
3030 ),
3131 pytest .param (
32- "[1.5] " , [float , float ], [1.5 , 0.0 ], id = "floats "
32+ "true " , [bool , bool ], [True , False ], id = "bools "
3333 ),
3434 pytest .param (
35- "[true]" , [ bool , bool ], [True , False ], id = "bools "
35+ '{"foo": "hello world", "bar": 42, "baz": {"bar": 43}}' , [ _TestDataClass , _TestDataClass ], [_TestDataClass ( "hello world" , 42 , _TestDataClass ( bar = 43 )), None ], id = "data classes "
3636 ),
3737 pytest .param (
38- '[ {"foo": "hello world", "bar": 42, "baz": {"bar": 43}}] ' , [_TestDataClass , _TestDataClass ], [_TestDataClass ( " hello world", 42 , _TestDataClass ( bar = 43 )), None ], id = "data classes "
38+ '{"foo": "hello world"} ' , [dict , dict ], [{ "foo" : " hello world"}, None ], id = "dicts "
3939 ),
4040 pytest .param (
41- '[ {"foo": "hello world"}] ' , [dict , dict ], [{"foo" : "hello world" }, None ], id = "dicts"
41+ '{"foo": 52} ' , [dict [ str , int ], dict ], [{"foo" : 52 }, None ], id = "generic dicts"
4242 ),
4343 pytest .param (
44- '[{"foo": 52} ]' , [dict [str , int ], dict ] , [{ "foo" : 52 } , None ], id = "generic dicts "
44+ '["hello" ]' , [list [str ], list [ str ]] , [[ "hello" ] , None ], id = "lists "
4545 ),
4646 pytest .param (
47- '[["hello"]]' , [list [str ], list [str ]], [["hello" ], None ], id = "lists"
48- ),
49- pytest .param (
50- '[["hello"]]' , [set [str ], set [str ]], [{"hello" }, None ], id = "sets"
47+ '["hello"]' , [set [str ], set [str ]], [{"hello" }, None ], id = "sets"
5148 ),
5249 pytest .param (
5350 '["hello", "world"]' , [list [str ]], [["hello" , "world" ]], id = "list"
@@ -56,26 +53,21 @@ class _TestDataClass:
5653 '{"foo": "bar"} {"bar": 100} ["hello"] "world"' , [_TestDataClass , _TestDataClass , list [str ], str ],
5754 [_TestDataClass (foo = "bar" ), _TestDataClass (bar = 100 ), ["hello" ], "world" ], id = "space delimited mix"
5855 ),
59- pytest .param (
60- '[{"foo": "bar"},{"bar": 100},["hello"],"world"]' , [_TestDataClass , _TestDataClass , list [str ], str ],
61- [_TestDataClass (foo = "bar" ), _TestDataClass (bar = 100 ), ["hello" ], "world" ], id = "json array mix"
62- ),
6356 pytest .param (
6457 "" , [], [], id = "no input expected"
6558 ),
6659 pytest .param (
6760 "" , [str ], [None ], id = "no input unexpected"
6861 ),
6962 pytest .param (
70- '[ "hello world", {"foo":"bar"}, 7] ' , [None , None , None ], ["hello world" , {"foo" :"bar" }, 7 ], id = "no type hints"
63+ '"hello world" {"foo":"bar"} 7 ' , [None , None , None ], ["hello world" , {"foo" :"bar" }, 7 ], id = "no type hints"
7164 ),
7265 pytest .param (
7366 '"hello" "world" "goodbye"' , [str , str ], ["hello" , "world" ],
7467 id = "extra content"
7568 ),
7669 ]
7770)
78- @pytest .mark .asyncio
7971async def test_data_converter_from_data (json : str , types : list [Type ], expected : list [Any ]) -> None :
8072 converter = DefaultDataConverter ()
8173 actual = await converter .from_data (Payload (data = json .encode ()), types )
@@ -88,18 +80,30 @@ async def test_data_converter_from_data(json: str, types: list[Type], expected:
8880 ["hello world" ], '"hello world"' , id = "happy path"
8981 ),
9082 pytest .param (
91- ["hello" , "world" ], '["hello", "world"]' , id = "multiple values"
83+ ["hello" , "world" ], '"hello" "world"' , id = "multiple values"
84+ ),
85+ pytest .param (
86+ [[["hello" ]], ["world" ]], '[["hello"]] ["world"]' , id = "lists"
87+ ),
88+ pytest .param (
89+ [1 , 2 , 10 ], '1 2 10' , id = "numeric values"
90+ ),
91+ pytest .param (
92+ [True , False ], 'true false' , id = "bool values"
93+ ),
94+ pytest .param (
95+ [{'foo' : 'foo' , 'bar' : 20 }], '{"bar":20,"foo":"foo"}' , id = "dict values"
96+ ),
97+ pytest .param (
98+ [{'foo' , 'bar' }], '["bar","foo"]' , id = "set values"
9299 ),
93100 pytest .param (
94- [_TestDataClass ()], '{"foo": "foo", "bar": -1, "baz": null}' , id = "data classes"
101+ [_TestDataClass ()], '{"foo":"foo","bar":-1,"baz":null}' , id = "data classes"
95102 ),
96103 ]
97104)
98- @pytest .mark .asyncio
99105async def test_data_converter_to_data (values : list [Any ], expected : str ) -> None :
100106 converter = DefaultDataConverter ()
107+ converter ._encoder = json .Encoder (order = 'deterministic' )
101108 actual = await converter .to_data (values )
102- # Parse both rather than trying to compare strings
103- actual_parsed = json .decode (actual .data )
104- expected_parsed = json .decode (expected )
105- assert expected_parsed == actual_parsed
109+ assert actual .data .decode () == expected
0 commit comments