Coverage for tests/test_multipart_installation.py: 100%

101 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import warnings 1abcdefg

2 

3import pytest 1abcdefg

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

5from fastapi.dependencies.utils import ( 1abcdefg

6 multipart_incorrect_install_error, 

7 multipart_not_installed_error, 

8) 

9 

10 

11def test_incorrect_multipart_installed_form(monkeypatch): 1abcdefg

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

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

14 warnings.simplefilter("always") 1hijklmn

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

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

17 app = FastAPI() 1hijklmn

18 

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

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

21 return username # pragma: nocover 

22 

23 

24def test_incorrect_multipart_installed_file_upload(monkeypatch): 1abcdefg

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

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

27 warnings.simplefilter("always") 1opqrstu

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

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

30 app = FastAPI() 1opqrstu

31 

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

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

34 return f # pragma: nocover 

35 

36 

37def test_incorrect_multipart_installed_file_bytes(monkeypatch): 1abcdefg

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

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

40 warnings.simplefilter("always") 1vwxyzAB

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

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

43 app = FastAPI() 1vwxyzAB

44 

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

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

47 return f # pragma: nocover 

48 

49 

50def test_incorrect_multipart_installed_multi_form(monkeypatch): 1abcdefg

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

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

53 warnings.simplefilter("always") 1CDEFGHI

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

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

56 app = FastAPI() 1CDEFGHI

57 

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

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

60 return username # pragma: nocover 

61 

62 

63def test_incorrect_multipart_installed_form_file(monkeypatch): 1abcdefg

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

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

66 warnings.simplefilter("always") 1JKLMNOP

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

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

69 app = FastAPI() 1JKLMNOP

70 

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

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

73 return username # pragma: nocover 

74 

75 

76def test_no_multipart_installed(monkeypatch): 1abcdefg

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

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

79 warnings.simplefilter("always") 1QRSTUVW

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

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

82 app = FastAPI() 1QRSTUVW

83 

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

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

86 return username # pragma: nocover 

87 

88 

89def test_no_multipart_installed_file(monkeypatch): 1abcdefg

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

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

92 warnings.simplefilter("always") 1XYZ0123

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

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

95 app = FastAPI() 1XYZ0123

96 

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

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

99 return f # pragma: nocover 

100 

101 

102def test_no_multipart_installed_file_bytes(monkeypatch): 1abcdefg

103 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1456789!

104 with warnings.catch_warnings(record=True): 1456789!

105 warnings.simplefilter("always") 1456789!

106 monkeypatch.delattr("multipart.__version__", raising=False) 1456789!

107 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1456789!

108 app = FastAPI() 1456789!

109 

110 @app.post("/") 1456789!

111 async def root(f: bytes = File()): 1456789!

112 return f # pragma: nocover 

113 

114 

115def test_no_multipart_installed_multi_form(monkeypatch): 1abcdefg

116 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1#$%'()*

117 with warnings.catch_warnings(record=True): 1#$%'()*

118 warnings.simplefilter("always") 1#$%'()*

119 monkeypatch.delattr("multipart.__version__", raising=False) 1#$%'()*

120 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1#$%'()*

121 app = FastAPI() 1#$%'()*

122 

123 @app.post("/") 1#$%'()*

124 async def root(username: str = Form(), password: str = Form()): 1#$%'()*

125 return username # pragma: nocover 

126 

127 

128def test_no_multipart_installed_form_file(monkeypatch): 1abcdefg

129 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1+,-./:;

130 with warnings.catch_warnings(record=True): 1+,-./:;

131 warnings.simplefilter("always") 1+,-./:;

132 monkeypatch.delattr("multipart.__version__", raising=False) 1+,-./:;

133 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 1+,-./:;

134 app = FastAPI() 1+,-./:;

135 

136 @app.post("/") 1+,-./:;

137 async def root(username: str = Form(), f: UploadFile = File()): 1+,-./:;

138 return username # pragma: nocover 

139 

140 

141def test_old_multipart_installed(monkeypatch): 1abcdefg

142 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1=?@[]^_

143 with warnings.catch_warnings(record=True): 1=?@[]^_

144 warnings.simplefilter("always") 1=?@[]^_

145 app = FastAPI() 1=?@[]^_

146 

147 @app.post("/") 1=?@[]^_

148 async def root(username: str = Form()): 1=?@[]^_

149 return username # pragma: nocover