Coverage for tests/test_multipart_installation.py: 100%

101 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import warnings 1abcdef

2 

3import pytest 1abcdef

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

5from fastapi.dependencies.utils import ( 1abcdef

6 multipart_incorrect_install_error, 

7 multipart_not_installed_error, 

8) 

9 

10 

11def test_incorrect_multipart_installed_form(monkeypatch): 1abcdef

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

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

14 warnings.simplefilter("always") 1ghijkl

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

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

17 app = FastAPI() 1ghijkl

18 

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

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

21 return username # pragma: nocover 

22 

23 

24def test_incorrect_multipart_installed_file_upload(monkeypatch): 1abcdef

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

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

27 warnings.simplefilter("always") 1mnopqr

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

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

30 app = FastAPI() 1mnopqr

31 

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

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

34 return f # pragma: nocover 

35 

36 

37def test_incorrect_multipart_installed_file_bytes(monkeypatch): 1abcdef

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

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

40 warnings.simplefilter("always") 1stuvwx

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

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

43 app = FastAPI() 1stuvwx

44 

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

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

47 return f # pragma: nocover 

48 

49 

50def test_incorrect_multipart_installed_multi_form(monkeypatch): 1abcdef

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

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

53 warnings.simplefilter("always") 1yzABCD

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

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

56 app = FastAPI() 1yzABCD

57 

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

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

60 return username # pragma: nocover 

61 

62 

63def test_incorrect_multipart_installed_form_file(monkeypatch): 1abcdef

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

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

66 warnings.simplefilter("always") 1EFGHIJ

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

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

69 app = FastAPI() 1EFGHIJ

70 

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

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

73 return username # pragma: nocover 

74 

75 

76def test_no_multipart_installed(monkeypatch): 1abcdef

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

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

79 warnings.simplefilter("always") 1KLMNOP

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

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

82 app = FastAPI() 1KLMNOP

83 

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

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

86 return username # pragma: nocover 

87 

88 

89def test_no_multipart_installed_file(monkeypatch): 1abcdef

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

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

92 warnings.simplefilter("always") 1QRSTUV

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

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

95 app = FastAPI() 1QRSTUV

96 

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

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

99 return f # pragma: nocover 

100 

101 

102def test_no_multipart_installed_file_bytes(monkeypatch): 1abcdef

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

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

105 warnings.simplefilter("always") 1WXYZ01

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

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

108 app = FastAPI() 1WXYZ01

109 

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

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

112 return f # pragma: nocover 

113 

114 

115def test_no_multipart_installed_multi_form(monkeypatch): 1abcdef

116 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 1234567

117 with warnings.catch_warnings(record=True): 1234567

118 warnings.simplefilter("always") 1234567

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

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

121 app = FastAPI() 1234567

122 

123 @app.post("/") 1234567

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

125 return username # pragma: nocover 

126 

127 

128def test_no_multipart_installed_form_file(monkeypatch): 1abcdef

129 monkeypatch.setattr("python_multipart.__version__", "0.0.12") 189!#$%

130 with warnings.catch_warnings(record=True): 189!#$%

131 warnings.simplefilter("always") 189!#$%

132 monkeypatch.delattr("multipart.__version__", raising=False) 189!#$%

133 with pytest.raises(RuntimeError, match=multipart_not_installed_error): 189!#$%

134 app = FastAPI() 189!#$%

135 

136 @app.post("/") 189!#$%

137 async def root(username: str = Form(), f: UploadFile = File()): 189!#$%

138 return username # pragma: nocover 

139 

140 

141def test_old_multipart_installed(monkeypatch): 1abcdef

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