Coverage for docs_src/app_testing/app_b_an_py39/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 1abcd

2 

3from .main import app 1abcd

4 

5client = TestClient(app) 1abcd

6 

7 

8def test_read_item(): 1abcd

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

10 assert response.status_code == 200 1abcd

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

12 "id": "foo", 

13 "title": "Foo", 

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

15 } 

16 

17 

18def test_read_item_bad_token(): 1abcd

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

20 assert response.status_code == 400 1abcd

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

22 

23 

24def test_read_nonexistent_item(): 1abcd

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

26 assert response.status_code == 404 1abcd

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

28 

29 

30def test_create_item(): 1abcd

31 response = client.post( 1abcd

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 1abcd

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

38 "id": "foobar", 

39 "title": "Foo Bar", 

40 "description": "The Foo Barters", 

41 } 

42 

43 

44def test_create_item_bad_token(): 1abcd

45 response = client.post( 1abcd

46 "/items/", 

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

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

49 ) 

50 assert response.status_code == 400 1abcd

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

52 

53 

54def test_create_existing_item(): 1abcd

55 response = client.post( 1abcd

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 1abcd

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