55from dashipy .lib .callback import Input , Callback
66
77
8- def my_callback (ctx , a : int , / , b : str = "" , c : bool = False ) -> str :
9- return f"{ a } -{ b } -{ c } "
8+ def my_callback (
9+ ctx ,
10+ a : int ,
11+ / ,
12+ b : str | int = "" ,
13+ c : bool | None = False ,
14+ d : list [str ] = (),
15+ ) -> str :
16+ return f"{ a } -{ b } -{ c } -{ d } "
1017
1118
12- class CallTest (unittest .TestCase ):
19+ class CallbackTest (unittest .TestCase ):
1320 def test_make_function_args (self ):
1421 callback = Callback (my_callback , [Input ("a" ), Input ("b" ), Input ("c" )], [])
1522 ctx = object ()
1623 args , kwargs = callback .make_function_args (ctx , [13 , "Wow" , True ])
1724 self .assertEqual ((ctx , 13 ), args )
1825 self .assertEqual ({"b" : "Wow" , "c" : True }, kwargs )
1926
27+ def test_to_dict (self ):
28+ callback = Callback (
29+ my_callback , [Input ("a" ), Input ("b" ), Input ("c" ), Input ("d" )], []
30+ )
31+ d = callback .to_dict ()
32+ # print(json.dumps(d, indent=2))
33+ self .assertEqual (
34+ {
35+ "function" : {
36+ "name" : "my_callback" ,
37+ "parameters" : [
38+ {"name" : "a" , "type" : "integer" },
39+ {"name" : "b" , "type" : ["string" , "integer" ], "default" : "" },
40+ {"name" : "c" , "type" : ["boolean" , "null" ], "default" : False },
41+ {"name" : "d" , "type" : "string[]" , "default" : ()},
42+ ],
43+ "returnType" : "string" ,
44+ },
45+ "inputs" : [
46+ {"id" : "a" , "property" : "value" , "kind" : "Component" },
47+ {"id" : "b" , "property" : "value" , "kind" : "Component" },
48+ {"id" : "c" , "property" : "value" , "kind" : "Component" },
49+ {"id" : "d" , "property" : "value" , "kind" : "Component" },
50+ ],
51+ },
52+ d ,
53+ )
54+
2055
2156# noinspection PyMethodMayBeStatic
2257class FromDecoratorTest (unittest .TestCase ):
2358
24- def test_inputs_given_but_not_in_order (self ):
25- callback = Callback .from_decorator (
26- "test" , (Input ("b" ), Input ("c" ), Input ("a" )), my_callback
27- )
28- self .assertIsInstance (callback , Callback )
29- self .assertIs (my_callback , callback .function )
30- self .assertEqual (3 , len (callback .inputs ))
31- self .assertEqual (0 , len (callback .outputs ))
32-
3359 def test_too_few_inputs (self ):
3460 with pytest .raises (
3561 TypeError ,
36- match = "too few inputs in decorator 'test' for function 'my_callback': expected 3, but got 0" ,
62+ match = "too few inputs in decorator 'test' for function"
63+ " 'my_callback': expected 4, but got 0" ,
3764 ):
3865 Callback .from_decorator ("test" , (), my_callback )
3966
4067 def test_too_many_inputs (self ):
4168 with pytest .raises (
4269 TypeError ,
43- match = "too many inputs in decorator 'test' for function 'my_callback': expected 3, but got 4" ,
70+ match = "too many inputs in decorator 'test' for function"
71+ " 'my_callback': expected 4, but got 5" ,
4472 ):
4573 Callback .from_decorator (
46- "test" , tuple (Input (c ) for c in "abcd " ), my_callback
74+ "test" , tuple (Input (c ) for c in "abcde " ), my_callback
4775 )
4876
4977 def test_decorator_target (self ):
@@ -57,12 +85,14 @@ def test_decorator_target(self):
5785 def test_decorator_args (self ):
5886 with pytest .raises (
5987 TypeError ,
60- match = "arguments for decorator 'test' must be of type Input, but got 'int'" ,
88+ match = "arguments for decorator 'test' must be of"
89+ " type Input, but got 'int'" ,
6190 ):
6291 Callback .from_decorator ("test" , (13 ,), my_callback )
6392
6493 with pytest .raises (
6594 TypeError ,
66- match = "arguments for decorator 'test' must be of type Input or Output, but got 'int'" ,
95+ match = "arguments for decorator 'test' must be of"
96+ " type Input or Output, but got 'int'" ,
6797 ):
6898 Callback .from_decorator ("test" , (13 ,), my_callback , outputs_allowed = True )
0 commit comments