55from fastapi_startkit .inertia .inertia import InertiaResponse , OptionalProp
66from fastapi_startkit .inertia .constant import Header
77
8+
89class TestInertiaResponse (unittest .IsolatedAsyncioTestCase ):
910 def setUp (self ):
1011 self .mock_request = MagicMock (spec = Request )
@@ -13,19 +14,16 @@ def setUp(self):
1314
1415 async def test_inertia_response_to_json_on_inertia_request (self ):
1516 self .mock_request .headers = {Header .INERTIA : "true" }
16-
17+
1718 response = InertiaResponse (
18- component = "User/Index" ,
19- shared_props = {"app" : "Test" },
20- props = {"users" : []},
21- version = "v1"
19+ component = "User/Index" , shared_props = {"app" : "Test" }, props = {"users" : []}, version = "v1"
2220 )
23-
21+
2422 actual_response = await response .to_response (self .mock_request )
25-
23+
2624 self .assertEqual (actual_response .status_code , 200 )
2725 self .assertEqual (actual_response .headers [Header .INERTIA ], "true" )
28-
26+
2927 content = json .loads (actual_response .body )
3028 self .assertEqual (content ["component" ], "User/Index" )
3129 self .assertEqual (content ["props" ], {"app" : "Test" , "users" : []})
@@ -36,18 +34,18 @@ async def test_inertia_response_partial_reload(self):
3634 self .mock_request .headers = {
3735 Header .INERTIA : "true" ,
3836 Header .INERTIA_PARTIAL_COMPONENT : "User/Index" ,
39- "X-Inertia-Partial-Data" : "users"
37+ "X-Inertia-Partial-Data" : "users" ,
4038 }
41-
39+
4240 response = InertiaResponse (
4341 component = "User/Index" ,
4442 shared_props = {"app" : "Test" },
4543 props = {"users" : ["user1" ], "stats" : {"likes" : 10 }},
4644 )
47-
45+
4846 actual_response = await response .to_response (self .mock_request )
4947 data = json .loads (actual_response .body )
50-
48+
5149 # Should only include "users", exclude "app" and "stats"
5250 self .assertIn ("users" , data ["props" ])
5351 self .assertNotIn ("app" , data ["props" ])
@@ -56,8 +54,9 @@ async def test_inertia_response_partial_reload(self):
5654 async def test_inertia_response_optional_props (self ):
5755 # 1. Normal request - optional prop should be excluded
5856 self .mock_request .headers = {Header .INERTIA : "true" }
59-
57+
6058 lazy_called = False
59+
6160 def get_lazy ():
6261 nonlocal lazy_called
6362 lazy_called = True
@@ -68,7 +67,7 @@ def get_lazy():
6867 shared_props = {},
6968 props = {"regular" : "data" , "lazy" : OptionalProp (get_lazy )},
7069 )
71-
70+
7271 actual_response = await response .to_response (self .mock_request )
7372 data = json .loads (actual_response .body )
7473 self .assertEqual (data ["props" ], {"regular" : "data" })
@@ -78,17 +77,17 @@ def get_lazy():
7877 self .mock_request .headers = {
7978 Header .INERTIA : "true" ,
8079 Header .INERTIA_PARTIAL_COMPONENT : "User/Index" ,
81- "X-Inertia-Partial-Data" : "lazy"
80+ "X-Inertia-Partial-Data" : "lazy" ,
8281 }
83-
82+
8483 actual_response = await response .to_response (self .mock_request )
8584 data = json .loads (actual_response .body )
8685 self .assertEqual (data ["props" ], {"lazy" : "lazy data" })
8786 self .assertTrue (lazy_called )
8887
8988 async def test_inertia_response_resolves_callable_props (self ):
9089 self .mock_request .headers = {Header .INERTIA : "true" }
91-
90+
9291 async def get_async_data ():
9392 return "async result"
9493
@@ -97,7 +96,7 @@ async def get_async_data():
9796 shared_props = {"sync" : lambda : "sync result" },
9897 props = {"async" : get_async_data },
9998 )
100-
99+
101100 actual_response = await response .to_response (self .mock_request )
102101 data = json .loads (actual_response .body )
103102 self .assertEqual (data ["props" ]["sync" ], "sync result" )
@@ -106,9 +105,9 @@ async def get_async_data():
106105 async def test_inertia_response_initial_render_raises_if_no_templates (self ):
107106 # Standard request (no X-Inertia header)
108107 self .mock_request .headers = {}
109-
108+
110109 response = InertiaResponse (component = "Test" , shared_props = {}, props = {})
111-
110+
112111 # This should fail because we haven't mocked the application container
113112 with self .assertRaisesRegex (RuntimeError , "Inertia requires 'templates' to be bound" ):
114113 await response .to_response (self .mock_request )
0 commit comments