Coverage for docs_src/app_testing/app_b_py310/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 1abc

2 

3from .main import app 1abc

4 

5client = TestClient(app) 1abc

6 

7 

8def test_read_item(): 1abc

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

10 assert response.status_code == 200 1abc

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

12 "id": "foo", 

13 "title": "Foo", 

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

15 } 

16 

17 

18def test_read_item_bad_token(): 1abc

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

20 assert response.status_code == 400 1abc

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

22 

23 

24def test_read_nonexistent_item(): 1abc

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

26 assert response.status_code == 404 1abc

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

28 

29 

30def test_create_item(): 1abc

31 response = client.post( 1abc

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

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

38 "id": "foobar", 

39 "title": "Foo Bar", 

40 "description": "The Foo Barters", 

41 } 

42 

43 

44def test_create_item_bad_token(): 1abc

45 response = client.post( 1abc

46 "/items/", 

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

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

49 ) 

50 assert response.status_code == 400 1abc

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

52 

53 

54def test_create_existing_item(): 1abc

55 response = client.post( 1abc

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 == 409 1abc

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