1+ import dataclasses
2+ from typing import Any , Type
3+
4+ import pytest
5+
6+ from cadence .api .v1 .common_pb2 import Payload
7+ from cadence .data_converter import DefaultDataConverter
8+ from msgspec import json
9+
10+ @dataclasses .dataclass
11+ class TestDataClass :
12+ foo : str = "foo"
13+ bar : int = - 1
14+ baz : 'TestDataClass' = None
15+
16+ @pytest .mark .parametrize (
17+ "json,types,expected" ,
18+ [
19+ pytest .param (
20+ '"Hello world"' , [str ], ["Hello world" ], id = "happy path"
21+ ),
22+ pytest .param (
23+ '"Hello" "world"' , [str , str ], ["Hello" , "world" ], id = "space delimited"
24+ ),
25+ pytest .param (
26+ '["Hello", "world"]' , [str , str ], ["Hello" , "world" ], id = "json array"
27+ ),
28+ pytest .param (
29+ "[1]" , [int , int ], [1 , 0 ], id = "ints"
30+ ),
31+ pytest .param (
32+ "[1.5]" , [float , float ], [1.5 , 0.0 ], id = "floats"
33+ ),
34+ pytest .param (
35+ "[true]" , [bool , bool ], [True , False ], id = "bools"
36+ ),
37+ pytest .param (
38+ '[{"foo": "hello world", "bar": 42, "baz": {"bar": 43}}]' , [TestDataClass , TestDataClass ], [TestDataClass ("hello world" , 42 , TestDataClass (bar = 43 )), None ], id = "data classes"
39+ ),
40+ pytest .param (
41+ '[{"foo": "hello world"}]' , [dict , dict ], [{"foo" : "hello world" }, None ], id = "dicts"
42+ ),
43+ pytest .param (
44+ '[{"foo": 52}]' , [dict [str , int ], dict ], [{"foo" : 52 }, None ], id = "generic dicts"
45+ ),
46+ 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"
51+ ),
52+ pytest .param (
53+ '["hello", "world"]' , [list [str ]], [["hello" , "world" ]], id = "list"
54+ ),
55+ pytest .param (
56+ '{"foo": "bar"} {"bar": 100} ["hello"] "world"' , [TestDataClass , TestDataClass , list [str ], str ],
57+ [TestDataClass (foo = "bar" ), TestDataClass (bar = 100 ), ["hello" ], "world" ], id = "space delimited mix"
58+ ),
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+ ),
63+ ]
64+ )
65+ @pytest .mark .asyncio
66+ async def test_data_converter_from_data (json : str , types : list [Type ], expected : list [Any ]):
67+ converter = DefaultDataConverter ()
68+ actual = await converter .from_data (Payload (data = json .encode ()), types )
69+ assert expected == actual
70+
71+ @pytest .mark .parametrize (
72+ "values,expected" ,
73+ [
74+ pytest .param (
75+ ["hello world" ], '"hello world"' , id = "happy path"
76+ ),
77+ pytest .param (
78+ ["hello" , "world" ], '["hello", "world"]' , id = "multiple values"
79+ ),
80+ pytest .param (
81+ [TestDataClass ()], '{"foo": "foo", "bar": -1, "baz": null}' , id = "data classes"
82+ ),
83+ ]
84+ )
85+ @pytest .mark .asyncio
86+ async def test_data_converter_to_data (values : list [Any ], expected : str ):
87+ converter = DefaultDataConverter ()
88+ actual = await converter .to_data (values )
89+ # Parse both rather than trying to compare strings
90+ actual_parsed = json .decode (actual .data )
91+ expected_parsed = json .decode (expected )
92+ assert expected_parsed == actual_parsed
0 commit comments