Coverage for tests/test_multipart_installation.py: 100%

63 statements  

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

1import pytest 1abcde

2from fastapi import FastAPI, File, Form, UploadFile 1abcde

3from fastapi.dependencies.utils import ( 1abcde

4 multipart_incorrect_install_error, 

5 multipart_not_installed_error, 

6) 

7 

8 

9def test_incorrect_multipart_installed_form(monkeypatch): 1abcde

10 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1abcde

11 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1abcde

12 app = FastAPI() 1abcde

13 

14 @app.post("/") 1abcde

15 async def root(username: str = Form()): 1abcde

16 return username # pragma: nocover 

17 

18 

19def test_incorrect_multipart_installed_file_upload(monkeypatch): 1abcde

20 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1abcde

21 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1abcde

22 app = FastAPI() 1abcde

23 

24 @app.post("/") 1abcde

25 async def root(f: UploadFile = File()): 1abcde

26 return f # pragma: nocover 

27 

28 

29def test_incorrect_multipart_installed_file_bytes(monkeypatch): 1abcde

30 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1abcde

31 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1abcde

32 app = FastAPI() 1abcde

33 

34 @app.post("/") 1abcde

35 async def root(f: bytes = File()): 1abcde

36 return f # pragma: nocover 

37 

38 

39def test_incorrect_multipart_installed_multi_form(monkeypatch): 1abcde

40 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1abcde

41 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1abcde

42 app = FastAPI() 1abcde

43 

44 @app.post("/") 1abcde

45 async def root(username: str = Form(), password: str = Form()): 1abcde

46 return username # pragma: nocover 

47 

48 

49def test_incorrect_multipart_installed_form_file(monkeypatch): 1abcde

50 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1abcde

51 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1abcde

52 app = FastAPI() 1abcde

53 

54 @app.post("/") 1abcde

55 async def root(username: str = Form(), f: UploadFile = File()): 1abcde

56 return username # pragma: nocover 

57 

58 

59def test_no_multipart_installed(monkeypatch): 1abcde

60 monkeypatch.delattr("multipart.__version__", raising=False) 1abcde

61 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1abcde

62 app = FastAPI() 1abcde

63 

64 @app.post("/") 1abcde

65 async def root(username: str = Form()): 1abcde

66 return username # pragma: nocover 

67 

68 

69def test_no_multipart_installed_file(monkeypatch): 1abcde

70 monkeypatch.delattr("multipart.__version__", raising=False) 1abcde

71 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1abcde

72 app = FastAPI() 1abcde

73 

74 @app.post("/") 1abcde

75 async def root(f: UploadFile = File()): 1abcde

76 return f # pragma: nocover 

77 

78 

79def test_no_multipart_installed_file_bytes(monkeypatch): 1abcde

80 monkeypatch.delattr("multipart.__version__", raising=False) 1abcde

81 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1abcde

82 app = FastAPI() 1abcde

83 

84 @app.post("/") 1abcde

85 async def root(f: bytes = File()): 1abcde

86 return f # pragma: nocover 

87 

88 

89def test_no_multipart_installed_multi_form(monkeypatch): 1abcde

90 monkeypatch.delattr("multipart.__version__", raising=False) 1abcde

91 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1abcde

92 app = FastAPI() 1abcde

93 

94 @app.post("/") 1abcde

95 async def root(username: str = Form(), password: str = Form()): 1abcde

96 return username # pragma: nocover 

97 

98 

99def test_no_multipart_installed_form_file(monkeypatch): 1abcde

100 monkeypatch.delattr("multipart.__version__", raising=False) 1abcde

101 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1abcde

102 app = FastAPI() 1abcde

103 

104 @app.post("/") 1abcde

105 async def root(username: str = Form(), f: UploadFile = File()): 1abcde

106 return username # pragma: nocover