11import inspect
22from typing import Any
3-
4- from unittest import TestCase
5- import tableauserverclient .models as TSC_models # type: ignore # did not set types for this
3+ from test .models ._models import get_unimplemented_models
64import tableauserverclient as TSC
75
86import pytest
97
108
11- # ensure that all models can be instantiated
12- def instantiate_class (name : str , obj : Any ):
9+ def is_concrete (obj : Any ):
10+ return inspect .isclass (obj ) and not inspect .isabstract (obj )
11+
12+ @pytest .mark .parametrize ("class_name, obj" , inspect .getmembers (TSC , is_concrete ))
13+ def test_by_reflection (class_name , obj ):
14+ instance = try_instantiate_class (class_name , obj )
15+ if instance :
16+ class_type = type (instance )
17+ if class_type in get_unimplemented_models ():
18+ print (f"Class '{ class_name } ' has no repr defined, skipping test" )
19+ return
20+ else :
21+ assert type (instance .__repr__ ).__name__ == "method"
22+ print (instance .__repr__ .__name__ )
23+
24+
25+
26+ # Instantiate a class if it doesn't require any parameters
27+ def try_instantiate_class (name : str , obj : Any ) -> Any | None :
1328 # Get the constructor (init) of the class
1429 constructor = getattr (obj , "__init__" , None )
1530 if constructor :
@@ -22,58 +37,12 @@ def instantiate_class(name: str, obj: Any):
2237 print (f"Class '{ name } ' requires the following parameters for instantiation:" )
2338 for param in required_parameters :
2439 print (f"- { param .name } " )
40+ return None
2541 else :
2642 print (f"Class '{ name } ' does not require any parameters for instantiation." )
2743 # Instantiate the class
2844 instance = obj ()
29- print ( f"Instantiated: { name } -> { instance } " )
45+ return instance
3046 else :
3147 print (f"Class '{ name } ' does not have a constructor (__init__ method)." )
32-
33-
34- not_yet_done = [
35- "DQWItem" ,
36- "UnpopulatedPropertyError" ,
37- "FavoriteItem" ,
38- "FileuploadItem" ,
39- "FlowRunItem" ,
40- "IntervalItem" ,
41- "LinkedTaskItem" ,
42- "LinkedTaskStepItem" ,
43- "LinkedTaskFlowRunItem" ,
44- "Permission" ,
45- "SiteAuthConfiguration" ,
46- "Resource" ,
47- "TagItem" ,
48- "ExtractItem" ,
49- ]
50-
51-
52- class TestAllModels (TestCase ):
53-
54- # confirm that all models can be instantiated without params, and have __repr__ implemented
55- # not all do have __repr__ yet: see above list 'not_yet_done'
56- def test_repr_is_implemented (self ):
57- m = TSC_models
58- for type_name in m .__dict__ :
59- if type_name in not_yet_done :
60- continue
61- model = getattr (m , type_name )
62- if inspect .isclass (model ):
63- with self .subTest (type_name ):
64- self .assertTrue (hasattr (model , "__repr__" ))
65- self .assertEqual (type (model .__repr__ ).__name__ , "function" )
66-
67- def is_concrete (obj : Any ):
68- return inspect .isclass (obj ) and not inspect .isabstract (obj )
69-
70-
71- @pytest .mark .parametrize ("class_name, obj" , inspect .getmembers (TSC , is_concrete ))
72- def test_by_reflection (class_name , obj ):
73- instantiate_class (class_name , obj )
74-
75-
76- @pytest .mark .parametrize ("model" , _models .get_defined_models ())
77- def test_repr_is_implemented (model ):
78- print (model .__name__ , type (model .__repr__ ).__name__ )
79- assert type (model .__repr__ ).__name__ == "function"
48+ return None
0 commit comments