Coverage for tests/test_multipart_installation.py: 100%

101 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-01-13 13:38 +0000

1import warnings 1abcde

2 

3import pytest 1abcde

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

5from fastapi.dependencies.utils import ( 1abcde

6 multipart_incorrect_install_error, 

7 multipart_not_installed_error, 

8) 

9 

10 

11def test_incorrect_multipart_installed_form(monkeypatch): 1abcde

12 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1fghij

13 with warnings.catch_warnings(record=True): 1fghij

14 warnings.simplefilter("always") 1fghij

15 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1fghij

16 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1fghij

17 app = FastAPI() 1fghij

18 

19 @app.post("/") 1fghij

20 async def root(username: str = Form()): 1fghij

21 return username # pragma: nocover 

22 

23 

24def test_incorrect_multipart_installed_file_upload(monkeypatch): 1abcde

25 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1klmno

26 with warnings.catch_warnings(record=True): 1klmno

27 warnings.simplefilter("always") 1klmno

28 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1klmno

29 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1klmno

30 app = FastAPI() 1klmno

31 

32 @app.post("/") 1klmno

33 async def root(f: UploadFile = File()): 1klmno

34 return f # pragma: nocover 

35 

36 

37def test_incorrect_multipart_installed_file_bytes(monkeypatch): 1abcde

38 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1pqrst

39 with warnings.catch_warnings(record=True): 1pqrst

40 warnings.simplefilter("always") 1pqrst

41 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1pqrst

42 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1pqrst

43 app = FastAPI() 1pqrst

44 

45 @app.post("/") 1pqrst

46 async def root(f: bytes = File()): 1pqrst

47 return f # pragma: nocover 

48 

49 

50def test_incorrect_multipart_installed_multi_form(monkeypatch): 1abcde

51 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1uvwxy

52 with warnings.catch_warnings(record=True): 1uvwxy

53 warnings.simplefilter("always") 1uvwxy

54 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1uvwxy

55 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1uvwxy

56 app = FastAPI() 1uvwxy

57 

58 @app.post("/") 1uvwxy

59 async def root(username: str = Form(), password: str = Form()): 1uvwxy

60 return username # pragma: nocover 

61 

62 

63def test_incorrect_multipart_installed_form_file(monkeypatch): 1abcde

64 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1zABCD

65 with warnings.catch_warnings(record=True): 1zABCD

66 warnings.simplefilter("always") 1zABCD

67 monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False) 1zABCD

68 with pytest.raises(RuntimeError, match=multipart_incorrect_install_error): 1zABCD

69 app = FastAPI() 1zABCD

70 

71 @app.post("/") 1zABCD

72 async def root(username: str = Form(), f: UploadFile = File()): 1zABCD

73 return username # pragma: nocover 

74 

75 

76def test_no_multipart_installed(monkeypatch): 1abcde

77 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1EFGHI

78 with warnings.catch_warnings(record=True): 1EFGHI

79 warnings.simplefilter("always") 1EFGHI

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

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

82 app = FastAPI() 1EFGHI

83 

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

85 async def root(username: str = Form()): 1EFGHI

86 return username # pragma: nocover 

87 

88 

89def test_no_multipart_installed_file(monkeypatch): 1abcde

90 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1JKLMN

91 with warnings.catch_warnings(record=True): 1JKLMN

92 warnings.simplefilter("always") 1JKLMN

93 monkeypatch.delattr("multipart.__version__", raising=False) 1JKLMN

94 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1JKLMN

95 app = FastAPI() 1JKLMN

96 

97 @app.post("/") 1JKLMN

98 async def root(f: UploadFile = File()): 1JKLMN

99 return f # pragma: nocover 

100 

101 

102def test_no_multipart_installed_file_bytes(monkeypatch): 1abcde

103 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1OPQRS

104 with warnings.catch_warnings(record=True): 1OPQRS

105 warnings.simplefilter("always") 1OPQRS

106 monkeypatch.delattr("multipart.__version__", raising=False) 1OPQRS

107 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1OPQRS

108 app = FastAPI() 1OPQRS

109 

110 @app.post("/") 1OPQRS

111 async def root(f: bytes = File()): 1OPQRS

112 return f # pragma: nocover 

113 

114 

115def test_no_multipart_installed_multi_form(monkeypatch): 1abcde

116 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1TUVWX

117 with warnings.catch_warnings(record=True): 1TUVWX

118 warnings.simplefilter("always") 1TUVWX

119 monkeypatch.delattr("multipart.__version__", raising=False) 1TUVWX

120 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1TUVWX

121 app = FastAPI() 1TUVWX

122 

123 @app.post("/") 1TUVWX

124 async def root(username: str = Form(), password: str = Form()): 1TUVWX

125 return username # pragma: nocover 

126 

127 

128def test_no_multipart_installed_form_file(monkeypatch): 1abcde

129 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1YZ012

130 with warnings.catch_warnings(record=True): 1YZ012

131 warnings.simplefilter("always") 1YZ012

132 monkeypatch.delattr("multipart.__version__", raising=False) 1YZ012

133 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1YZ012

134 app = FastAPI() 1YZ012

135 

136 @app.post("/") 1YZ012

137 async def root(username: str = Form(), f: UploadFile = File()): 1YZ012

138 return username # pragma: nocover 

139 

140 

141def test_old_multipart_installed(monkeypatch): 1abcde

142 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 134567

143 with warnings.catch_warnings(record=True): 134567

144 warnings.simplefilter("always") 134567

145 app = FastAPI() 134567

146 

147 @app.post("/") 134567

148 async def root(username: str = Form()): 134567

149 return username # pragma: nocover