Coverage for tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial007_pv1.py: 100%

31 statements  

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

1import pytest 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from ...utils import needs_pydanticv1 1abcde

5 

6 

7@pytest.fixture(name="client") 1abcde

8def get_client(): 1abcde

9 from docs_src.path_operation_advanced_configuration.tutorial007_pv1 import app 1abcde

10 

11 client = TestClient(app) 1abcde

12 return client 1abcde

13 

14 

15@needs_pydanticv1 1abcde

16def test_post(client: TestClient): 1abcde

17 yaml_data = """ 1abcde

18 name: Deadpoolio 

19 tags: 

20 - x-force 

21 - x-men 

22 - x-avengers 

23 """ 

24 response = client.post("/items/", content=yaml_data) 1abcde

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

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

27 "name": "Deadpoolio", 

28 "tags": ["x-force", "x-men", "x-avengers"], 

29 } 

30 

31 

32@needs_pydanticv1 1abcde

33def test_post_broken_yaml(client: TestClient): 1abcde

34 yaml_data = """ 1abcde

35 name: Deadpoolio 

36 tags: 

37 x - x-force 

38 x - x-men 

39 x - x-avengers 

40 """ 

41 response = client.post("/items/", content=yaml_data) 1abcde

42 assert response.status_code == 422, response.text 1abcde

43 assert response.json() == {"detail": "Invalid YAML"} 1abcde

44 

45 

46@needs_pydanticv1 1abcde

47def test_post_invalid(client: TestClient): 1abcde

48 yaml_data = """ 1abcde

49 name: Deadpoolio 

50 tags: 

51 - x-force 

52 - x-men 

53 - x-avengers 

54 - sneaky: object 

55 """ 

56 response = client.post("/items/", content=yaml_data) 1abcde

57 assert response.status_code == 422, response.text 1abcde

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

59 "detail": [ 

60 {"loc": ["tags", 3], "msg": "str type expected", "type": "type_error.str"} 

61 ] 

62 } 

63 

64 

65@needs_pydanticv1 1abcde

66def test_openapi_schema(client: TestClient): 1abcde

67 response = client.get("/openapi.json") 1abcde

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

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

70 "openapi": "3.1.0", 

71 "info": {"title": "FastAPI", "version": "0.1.0"}, 

72 "paths": { 

73 "/items/": { 

74 "post": { 

75 "summary": "Create Item", 

76 "operationId": "create_item_items__post", 

77 "requestBody": { 

78 "content": { 

79 "application/x-yaml": { 

80 "schema": { 

81 "title": "Item", 

82 "required": ["name", "tags"], 

83 "type": "object", 

84 "properties": { 

85 "name": {"title": "Name", "type": "string"}, 

86 "tags": { 

87 "title": "Tags", 

88 "type": "array", 

89 "items": {"type": "string"}, 

90 }, 

91 }, 

92 } 

93 } 

94 }, 

95 "required": True, 

96 }, 

97 "responses": { 

98 "200": { 

99 "description": "Successful Response", 

100 "content": {"application/json": {"schema": {}}}, 

101 } 

102 }, 

103 } 

104 } 

105 }, 

106 }