Coverage for docs_src/app_testing/app_b_an/test_main.py: 100%

27 statements  

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

1from fastapi.testclient import TestClient 1abcde

2 

3from .main import app 1abcde

4 

5client = TestClient(app) 1abcde

6 

7 

8def test_read_item(): 1abcde

9 response = client.get("/items/foo", headers={"X-Token": "coneofsilence"}) 1abcde

10 assert response.status_code == 200 1abcde

11 assert response.json() == { 1abcde

12 "id": "foo", 

13 "title": "Foo", 

14 "description": "There goes my hero", 

15 } 

16 

17 

18def test_read_item_bad_token(): 1abcde

19 response = client.get("/items/foo", headers={"X-Token": "hailhydra"}) 1abcde

20 assert response.status_code == 400 1abcde

21 assert response.json() == {"detail": "Invalid X-Token header"} 1abcde

22 

23 

24def test_read_nonexistent_item(): 1abcde

25 response = client.get("/items/baz", headers={"X-Token": "coneofsilence"}) 1abcde

26 assert response.status_code == 404 1abcde

27 assert response.json() == {"detail": "Item not found"} 1abcde

28 

29 

30def test_create_item(): 1abcde

31 response = client.post( 1abcde

32 "/items/", 

33 headers={"X-Token": "coneofsilence"}, 

34 json={"id": "foobar", "title": "Foo Bar", "description": "The Foo Barters"}, 

35 ) 

36 assert response.status_code == 200 1abcde

37 assert response.json() == { 1abcde

38 "id": "foobar", 

39 "title": "Foo Bar", 

40 "description": "The Foo Barters", 

41 } 

42 

43 

44def test_create_item_bad_token(): 1abcde

45 response = client.post( 1abcde

46 "/items/", 

47 headers={"X-Token": "hailhydra"}, 

48 json={"id": "bazz", "title": "Bazz", "description": "Drop the bazz"}, 

49 ) 

50 assert response.status_code == 400 1abcde

51 assert response.json() == {"detail": "Invalid X-Token header"} 1abcde

52 

53 

54def test_create_existing_item(): 1abcde

55 response = client.post( 1abcde

56 "/items/", 

57 headers={"X-Token": "coneofsilence"}, 

58 json={ 

59 "id": "foo", 

60 "title": "The Foo ID Stealers", 

61 "description": "There goes my stealer", 

62 }, 

63 ) 

64 assert response.status_code == 400 1abcde

65 assert response.json() == {"detail": "Item already exists"} 1abcde