1+ from unittest import TestCase
2+ from fastapi import FastAPI
3+ from starlette .testclient import TestClient
4+ from osbot_fast_api .client .Fast_API__Route__Extractor import Fast_API__Route__Extractor
5+ from osbot_utils .type_safe .Type_Safe import Type_Safe
6+ from osbot_utils .type_safe .primitives .domains .identifiers .Safe_Id import Safe_Id
7+ from osbot_fast_api .api .routes .Type_Safe__Route__Registration import Type_Safe__Route__Registration
8+
9+
10+ class Schema__Integration_User (Type_Safe ): # Test schema for integration
11+ user_id : Safe_Id
12+ name : str
13+ age : int = 0
14+
15+
16+ class test_Fast_API__Type_Safe__routes_support (TestCase ):
17+
18+ @classmethod
19+ def setUpClass (cls ): # ONE-TIME setup for integration tests
20+ cls .app = FastAPI ()
21+ cls .registration = Type_Safe__Route__Registration ()
22+ cls .client = None # Will be created after routes registered
23+
24+ def test__full_cycle__original_types_preserved (self ): # Test complete cycle: register -> extract -> serialize -> deserialize
25+ # Step 1: Register route with Type_Safe parameter
26+ def create_user (user : Schema__Integration_User ) -> Schema__Integration_User :
27+ return user
28+
29+ self .registration .register_route (self .app .router , create_user , ['POST' ])
30+
31+ # Step 2: Make actual request through TestClient
32+ if self .client is None :
33+ self .client = TestClient (self .app )
34+
35+ response = self .client .post ('/create-user' , json = { 'user_id' : 'USER-123' ,
36+ 'name' : 'Test User' ,
37+ 'age' : 25 })
38+
39+ # Step 3: Verify response works correctly
40+ assert response .status_code == 200
41+ result = response .json ()
42+ assert result ['user_id' ] == 'USER-123' # Type_Safe handled conversion
43+ assert result ['name' ] == 'Test User'
44+ assert result ['age' ] == 25
45+
46+ # Step 4: Extract routes and verify types
47+ extractor = Fast_API__Route__Extractor (app = self .app , include_default = False )
48+ collection = extractor .extract_routes ()
49+
50+ # Find our route
51+ our_route = None # todo: we should have a helper method to provide this
52+ for route in collection .routes : # need to find a particular route
53+ if route .method_name == 'create_user' :
54+ our_route = route
55+ break
56+
57+ assert our_route is not None
58+
59+ # Step 5: CRITICAL - Verify original types preserved throughout
60+ assert len (our_route .body_params ) == 1
61+ assert our_route .body_params [0 ].param_type is Schema__Integration_User # Original class
62+ assert our_route .return_type is Schema__Integration_User # Original class
63+
64+ # Step 6: Serialize and deserialize
65+ json_data = collection .json ()
66+ restored = collection .__class__ .from_json (json_data )
67+
68+ # Step 7: Verify round-trip preserved everything
69+ assert collection .obj () == restored .obj ()
70+
71+ # todo: add more edge cases tests
0 commit comments