Coverage for tests/test_empty_router.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1abcde

2from fastapi import APIRouter, FastAPI 1abcde

3from fastapi.exceptions import FastAPIError 1abcde

4from fastapi.testclient import TestClient 1abcde

5 

6app = FastAPI() 1abcde

7 

8router = APIRouter() 1abcde

9 

10 

11@router.get("") 1abcde

12def get_empty(): 1abcde

13 return ["OK"] 1abcde

14 

15 

16app.include_router(router, prefix="/prefix") 1abcde

17 

18 

19client = TestClient(app) 1abcde

20 

21 

22def test_use_empty(): 1abcde

23 with client: 1abcde

24 response = client.get("/prefix") 1abcde

25 assert response.status_code == 200, response.text 1abcde

26 assert response.json() == ["OK"] 1abcde

27 

28 response = client.get("/prefix/") 1abcde

29 assert response.status_code == 200, response.text 1abcde

30 assert response.json() == ["OK"] 1abcde

31 

32 

33def test_include_empty(): 1abcde

34 # if both include and router.path are empty - it should raise exception 

35 with pytest.raises(FastAPIError): 1abcde

36 app.include_router(router) 1abcde