Coverage for tests / test_starlette_urlconvertors.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from fastapi import FastAPI, Path, Query 1abcd

2from fastapi.testclient import TestClient 1abcd

3 

4app = FastAPI() 1abcd

5 

6 

7@app.get("/int/{param:int}") 1abcd

8def int_convertor(param: int = Path()): 1abcd

9 return {"int": param} 1efgh

10 

11 

12@app.get("/float/{param:float}") 1abcd

13def float_convertor(param: float = Path()): 1abcd

14 return {"float": param} 1ijkl

15 

16 

17@app.get("/path/{param:path}") 1abcd

18def path_convertor(param: str = Path()): 1abcd

19 return {"path": param} 1mnop

20 

21 

22@app.get("/query/") 1abcd

23def query_convertor(param: str = Query()): 1abcd

24 return {"query": param} 1qrst

25 

26 

27client = TestClient(app) 1abcd

28 

29 

30def test_route_converters_int(): 1abcd

31 # Test integer conversion 

32 response = client.get("/int/5") 1efgh

33 assert response.status_code == 200, response.text 1efgh

34 assert response.json() == {"int": 5} 1efgh

35 assert app.url_path_for("int_convertor", param=5) == "/int/5" # type: ignore 1efgh

36 

37 

38def test_route_converters_float(): 1abcd

39 # Test float conversion 

40 response = client.get("/float/25.5") 1ijkl

41 assert response.status_code == 200, response.text 1ijkl

42 assert response.json() == {"float": 25.5} 1ijkl

43 assert app.url_path_for("float_convertor", param=25.5) == "/float/25.5" # type: ignore 1ijkl

44 

45 

46def test_route_converters_path(): 1abcd

47 # Test path conversion 

48 response = client.get("/path/some/example") 1mnop

49 assert response.status_code == 200, response.text 1mnop

50 assert response.json() == {"path": "some/example"} 1mnop

51 

52 

53def test_route_converters_query(): 1abcd

54 # Test query conversion 

55 response = client.get("/query", params={"param": "Qué tal!"}) 1qrst

56 assert response.status_code == 200, response.text 1qrst

57 assert response.json() == {"query": "Qué tal!"} 1qrst

58 

59 

60def test_url_path_for_path_convertor(): 1abcd

61 assert ( 1uvw

62 app.url_path_for("path_convertor", param="some/example") == "/path/some/example" 

63 )