Coverage for tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial007.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_pydanticv2 1abcde

5 

6 

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

8def get_client(): 1abcde

9 from docs_src.path_operation_advanced_configuration.tutorial007 import app 1abcde

10 

11 client = TestClient(app) 1abcde

12 return client 1abcde

13 

14 

15@needs_pydanticv2 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_pydanticv2 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_pydanticv2 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 # insert_assert(response.json()) 

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

60 "detail": [ 

61 { 

62 "type": "string_type", 

63 "loc": ["tags", 3], 

64 "msg": "Input should be a valid string", 

65 "input": {"sneaky": "object"}, 

66 } 

67 ] 

68 } 

69 

70 

71@needs_pydanticv2 1abcde

72def test_openapi_schema(client: TestClient): 1abcde

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

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

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

76 "openapi": "3.1.0", 

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

78 "paths": { 

79 "/items/": { 

80 "post": { 

81 "summary": "Create Item", 

82 "operationId": "create_item_items__post", 

83 "requestBody": { 

84 "content": { 

85 "application/x-yaml": { 

86 "schema": { 

87 "title": "Item", 

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

89 "type": "object", 

90 "properties": { 

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

92 "tags": { 

93 "title": "Tags", 

94 "type": "array", 

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

96 }, 

97 }, 

98 } 

99 } 

100 }, 

101 "required": True, 

102 }, 

103 "responses": { 

104 "200": { 

105 "description": "Successful Response", 

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

107 } 

108 }, 

109 } 

110 } 

111 }, 

112 }