1+ from unittest import TestCase
2+ from osbot_fast_api .admin_ui .api .routes .Routes__Admin__Docs import Routes__Admin__Docs
3+ from osbot_fast_api .admin_ui .api .testing .Admin_UI__Test_Objs import setup__admin_ui_test_objs , cleanup_admin_ui_test_objs
4+ from osbot_fast_api .api .routes .Fast_API__Routes import Fast_API__Routes
5+ from osbot_utils .type_safe .Type_Safe import Type_Safe
6+ from osbot_utils .utils .Objects import base_types
7+ from osbot_utils .utils .Misc import list_set
8+
9+
10+ class test_Routes__Admin__Docs (TestCase ):
11+ """Test Routes__Admin__Docs class directly"""
12+
13+ @classmethod
14+ def setUpClass (cls ):
15+ """Setup routes instance"""
16+ cls .test_objs = setup__admin_ui_test_objs (with_parent = True )
17+ cls .routes_docs = Routes__Admin__Docs ()
18+ cls .routes_docs .parent_app = cls .test_objs .parent_fast_api
19+
20+ @classmethod
21+ def tearDownClass (cls ):
22+ """Cleanup"""
23+ cleanup_admin_ui_test_objs (cls .test_objs )
24+
25+ def test_01_setUpClass (self ):
26+ """Verify class setup and inheritance"""
27+ with self .routes_docs as _ :
28+ assert type (_ ) is Routes__Admin__Docs
29+ assert Fast_API__Routes in base_types (_ )
30+ assert Type_Safe in base_types (_ )
31+ assert _ .tag == 'admin-docs'
32+ assert _ .parent_app is not None
33+
34+ def test_02_api__docs_endpoints (self ):
35+ """Test docs endpoints listing"""
36+ result = self .routes_docs .api__docs_endpoints ()
37+
38+ assert isinstance (result , list )
39+ assert len (result ) == 6 # Should have 6 doc endpoints
40+
41+ # Check endpoint structure
42+ for endpoint in result :
43+ assert 'name' in endpoint
44+ assert 'description' in endpoint
45+ assert 'url' in endpoint
46+ assert 'type' in endpoint
47+ assert 'icon' in endpoint
48+
49+ # Check specific endpoints
50+ endpoint_types = [e ['type' ] for e in result ]
51+ assert 'swagger' in endpoint_types
52+ assert 'redoc' in endpoint_types
53+ assert 'openapi' in endpoint_types
54+ assert 'client' in endpoint_types
55+ assert 'routes' in endpoint_types
56+ assert 'admin' in endpoint_types
57+
58+ # Verify Swagger endpoint
59+ swagger = next (e for e in result if e ['type' ] == 'swagger' )
60+ assert swagger ['name' ] == 'Swagger UI'
61+ assert swagger ['url' ] == '/docs'
62+ assert swagger ['icon' ] == 'swagger'
63+
64+ # Verify ReDoc endpoint
65+ redoc = next (e for e in result if e ['type' ] == 'redoc' )
66+ assert redoc ['url' ] == '/redoc'
67+
68+ def test_03_api__client_examples (self ):
69+ """Test client code examples"""
70+ result = self .routes_docs .api__client_examples ()
71+
72+ assert isinstance (result , dict )
73+ assert len (result ) == 3 # curl, python, javascript
74+
75+ # Check curl example
76+ assert 'curl' in result
77+ curl = result ['curl' ]
78+ assert curl ['name' ] == 'cURL'
79+ assert 'curl' in curl ['example' ]
80+ assert '/config/status' in curl ['example' ]
81+ assert '/admin/api/cookie/set' in curl ['example' ]
82+
83+ # Check Python example
84+ assert 'python' in result
85+ python = result ['python' ]
86+ assert python ['name' ] == 'Python'
87+ assert 'import requests' in python ['example' ]
88+ assert 'response.json()' in python ['example' ]
89+
90+ # Check JavaScript example
91+ assert 'javascript' in result
92+ js = result ['javascript' ]
93+ assert js ['name' ] == 'JavaScript'
94+ assert 'fetch' in js ['example' ]
95+ assert 'then' in js ['example' ]
96+
97+ def test_04_api__api_info (self ):
98+ """Test API metadata retrieval"""
99+ result = self .routes_docs .api__api_info ()
100+
101+ assert isinstance (result , dict )
102+
103+ # Check expected fields
104+ expected_fields = [
105+ 'openapi_version' ,
106+ 'api_title' ,
107+ 'api_version' ,
108+ 'api_description' ,
109+ 'servers' ,
110+ 'total_paths' ,
111+ 'total_schemas' ,
112+ 'tags'
113+ ]
114+ assert list_set (result .keys ()) == sorted (expected_fields )
115+
116+ # Verify values
117+ assert result ['api_title' ] == 'Test Parent API'
118+ assert result ['api_version' ] == 'v1.0.99'
119+ assert result ['api_description' ] == 'Parent API for Admin UI testing'
120+ assert result ['total_paths' ] > 0
121+ assert isinstance (result ['tags' ], list )
122+
123+ # def test_05_api__api_info_no_parent(self): # this is not a realistic scenario any more
124+ # """Test API info without parent app"""
125+ # routes = Routes__Admin__Docs()
126+ # routes.parent_app = None
127+ #
128+ # result = routes.api__api_info()
129+ #
130+ # assert result == {"error": "Parent app not configured"}
131+
132+ def test_06_extract_tags (self ):
133+ """Test tag extraction from OpenAPI spec"""
134+ # Create a mock OpenAPI spec
135+ mock_spec = {
136+ 'paths' : {
137+ '/users' : {
138+ 'get' : {'tags' : ['users' ], 'summary' : 'Get users' },
139+ 'post' : {'tags' : ['users' ], 'summary' : 'Create user' }
140+ },
141+ '/items' : {
142+ 'get' : {'tags' : ['items' ], 'summary' : 'Get items' }
143+ },
144+ '/admin/info' : {
145+ 'get' : {'tags' : ['admin' , 'info' ], 'summary' : 'Admin info' }
146+ }
147+ },
148+ 'tags' : [
149+ {'name' : 'users' , 'description' : 'User operations' },
150+ {'name' : 'items' , 'description' : 'Item operations' }
151+ ]
152+ }
153+
154+ routes = self .routes_docs
155+ tags = routes ._extract_tags (mock_spec )
156+
157+ assert isinstance (tags , list )
158+ assert len (tags ) >= 2
159+
160+ # Check tag structure
161+ for tag in tags :
162+ assert 'name' in tag
163+ assert 'count' in tag
164+ assert 'paths' in tag
165+
166+ # Check users tag
167+ users_tag = next ((t for t in tags if t ['name' ] == 'users' ), None )
168+ if users_tag :
169+ assert users_tag ['count' ] == 2
170+ assert len (users_tag ['paths' ]) == 2
171+ assert users_tag .get ('description' ) == 'User operations'
0 commit comments