Coverage for tests / test_starlette_urlconvertors.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +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} 1efg

10 

11 

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

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

14 return {"float": param} 1hij

15 

16 

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

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

19 return {"path": param} 1klm

20 

21 

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

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

24 return {"query": param} 1nop

25 

26 

27client = TestClient(app) 1abcd

28 

29 

30def test_route_converters_int(): 1abcd

31 # Test integer conversion 

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

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

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

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

36 

37 

38def test_route_converters_float(): 1abcd

39 # Test float conversion 

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

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

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

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

44 

45 

46def test_route_converters_path(): 1abcd

47 # Test path conversion 

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

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

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

51 

52 

53def test_route_converters_query(): 1abcd

54 # Test query conversion 

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

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

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

58 

59 

60def test_url_path_for_path_convertor(): 1abcd

61 assert ( 1qr

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

63 )