1 | n/a | import unittest |
---|
2 | n/a | from warnings import catch_warnings |
---|
3 | n/a | |
---|
4 | n/a | from unittest.test.testmock.support import is_instance |
---|
5 | n/a | from unittest.mock import MagicMock, Mock, patch, sentinel, mock_open, call |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | something = sentinel.Something |
---|
10 | n/a | something_else = sentinel.SomethingElse |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | |
---|
14 | n/a | class WithTest(unittest.TestCase): |
---|
15 | n/a | |
---|
16 | n/a | def test_with_statement(self): |
---|
17 | n/a | with patch('%s.something' % __name__, sentinel.Something2): |
---|
18 | n/a | self.assertEqual(something, sentinel.Something2, "unpatched") |
---|
19 | n/a | self.assertEqual(something, sentinel.Something) |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | def test_with_statement_exception(self): |
---|
23 | n/a | try: |
---|
24 | n/a | with patch('%s.something' % __name__, sentinel.Something2): |
---|
25 | n/a | self.assertEqual(something, sentinel.Something2, "unpatched") |
---|
26 | n/a | raise Exception('pow') |
---|
27 | n/a | except Exception: |
---|
28 | n/a | pass |
---|
29 | n/a | else: |
---|
30 | n/a | self.fail("patch swallowed exception") |
---|
31 | n/a | self.assertEqual(something, sentinel.Something) |
---|
32 | n/a | |
---|
33 | n/a | |
---|
34 | n/a | def test_with_statement_as(self): |
---|
35 | n/a | with patch('%s.something' % __name__) as mock_something: |
---|
36 | n/a | self.assertEqual(something, mock_something, "unpatched") |
---|
37 | n/a | self.assertTrue(is_instance(mock_something, MagicMock), |
---|
38 | n/a | "patching wrong type") |
---|
39 | n/a | self.assertEqual(something, sentinel.Something) |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | def test_patch_object_with_statement(self): |
---|
43 | n/a | class Foo(object): |
---|
44 | n/a | something = 'foo' |
---|
45 | n/a | original = Foo.something |
---|
46 | n/a | with patch.object(Foo, 'something'): |
---|
47 | n/a | self.assertNotEqual(Foo.something, original, "unpatched") |
---|
48 | n/a | self.assertEqual(Foo.something, original) |
---|
49 | n/a | |
---|
50 | n/a | |
---|
51 | n/a | def test_with_statement_nested(self): |
---|
52 | n/a | with catch_warnings(record=True): |
---|
53 | n/a | with patch('%s.something' % __name__) as mock_something, patch('%s.something_else' % __name__) as mock_something_else: |
---|
54 | n/a | self.assertEqual(something, mock_something, "unpatched") |
---|
55 | n/a | self.assertEqual(something_else, mock_something_else, |
---|
56 | n/a | "unpatched") |
---|
57 | n/a | |
---|
58 | n/a | self.assertEqual(something, sentinel.Something) |
---|
59 | n/a | self.assertEqual(something_else, sentinel.SomethingElse) |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | def test_with_statement_specified(self): |
---|
63 | n/a | with patch('%s.something' % __name__, sentinel.Patched) as mock_something: |
---|
64 | n/a | self.assertEqual(something, mock_something, "unpatched") |
---|
65 | n/a | self.assertEqual(mock_something, sentinel.Patched, "wrong patch") |
---|
66 | n/a | self.assertEqual(something, sentinel.Something) |
---|
67 | n/a | |
---|
68 | n/a | |
---|
69 | n/a | def testContextManagerMocking(self): |
---|
70 | n/a | mock = Mock() |
---|
71 | n/a | mock.__enter__ = Mock() |
---|
72 | n/a | mock.__exit__ = Mock() |
---|
73 | n/a | mock.__exit__.return_value = False |
---|
74 | n/a | |
---|
75 | n/a | with mock as m: |
---|
76 | n/a | self.assertEqual(m, mock.__enter__.return_value) |
---|
77 | n/a | mock.__enter__.assert_called_with() |
---|
78 | n/a | mock.__exit__.assert_called_with(None, None, None) |
---|
79 | n/a | |
---|
80 | n/a | |
---|
81 | n/a | def test_context_manager_with_magic_mock(self): |
---|
82 | n/a | mock = MagicMock() |
---|
83 | n/a | |
---|
84 | n/a | with self.assertRaises(TypeError): |
---|
85 | n/a | with mock: |
---|
86 | n/a | 'foo' + 3 |
---|
87 | n/a | mock.__enter__.assert_called_with() |
---|
88 | n/a | self.assertTrue(mock.__exit__.called) |
---|
89 | n/a | |
---|
90 | n/a | |
---|
91 | n/a | def test_with_statement_same_attribute(self): |
---|
92 | n/a | with patch('%s.something' % __name__, sentinel.Patched) as mock_something: |
---|
93 | n/a | self.assertEqual(something, mock_something, "unpatched") |
---|
94 | n/a | |
---|
95 | n/a | with patch('%s.something' % __name__) as mock_again: |
---|
96 | n/a | self.assertEqual(something, mock_again, "unpatched") |
---|
97 | n/a | |
---|
98 | n/a | self.assertEqual(something, mock_something, |
---|
99 | n/a | "restored with wrong instance") |
---|
100 | n/a | |
---|
101 | n/a | self.assertEqual(something, sentinel.Something, "not restored") |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | def test_with_statement_imbricated(self): |
---|
105 | n/a | with patch('%s.something' % __name__) as mock_something: |
---|
106 | n/a | self.assertEqual(something, mock_something, "unpatched") |
---|
107 | n/a | |
---|
108 | n/a | with patch('%s.something_else' % __name__) as mock_something_else: |
---|
109 | n/a | self.assertEqual(something_else, mock_something_else, |
---|
110 | n/a | "unpatched") |
---|
111 | n/a | |
---|
112 | n/a | self.assertEqual(something, sentinel.Something) |
---|
113 | n/a | self.assertEqual(something_else, sentinel.SomethingElse) |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | def test_dict_context_manager(self): |
---|
117 | n/a | foo = {} |
---|
118 | n/a | with patch.dict(foo, {'a': 'b'}): |
---|
119 | n/a | self.assertEqual(foo, {'a': 'b'}) |
---|
120 | n/a | self.assertEqual(foo, {}) |
---|
121 | n/a | |
---|
122 | n/a | with self.assertRaises(NameError): |
---|
123 | n/a | with patch.dict(foo, {'a': 'b'}): |
---|
124 | n/a | self.assertEqual(foo, {'a': 'b'}) |
---|
125 | n/a | raise NameError('Konrad') |
---|
126 | n/a | |
---|
127 | n/a | self.assertEqual(foo, {}) |
---|
128 | n/a | |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | class TestMockOpen(unittest.TestCase): |
---|
132 | n/a | |
---|
133 | n/a | def test_mock_open(self): |
---|
134 | n/a | mock = mock_open() |
---|
135 | n/a | with patch('%s.open' % __name__, mock, create=True) as patched: |
---|
136 | n/a | self.assertIs(patched, mock) |
---|
137 | n/a | open('foo') |
---|
138 | n/a | |
---|
139 | n/a | mock.assert_called_once_with('foo') |
---|
140 | n/a | |
---|
141 | n/a | |
---|
142 | n/a | def test_mock_open_context_manager(self): |
---|
143 | n/a | mock = mock_open() |
---|
144 | n/a | handle = mock.return_value |
---|
145 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
146 | n/a | with open('foo') as f: |
---|
147 | n/a | f.read() |
---|
148 | n/a | |
---|
149 | n/a | expected_calls = [call('foo'), call().__enter__(), call().read(), |
---|
150 | n/a | call().__exit__(None, None, None)] |
---|
151 | n/a | self.assertEqual(mock.mock_calls, expected_calls) |
---|
152 | n/a | self.assertIs(f, handle) |
---|
153 | n/a | |
---|
154 | n/a | def test_mock_open_context_manager_multiple_times(self): |
---|
155 | n/a | mock = mock_open() |
---|
156 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
157 | n/a | with open('foo') as f: |
---|
158 | n/a | f.read() |
---|
159 | n/a | with open('bar') as f: |
---|
160 | n/a | f.read() |
---|
161 | n/a | |
---|
162 | n/a | expected_calls = [ |
---|
163 | n/a | call('foo'), call().__enter__(), call().read(), |
---|
164 | n/a | call().__exit__(None, None, None), |
---|
165 | n/a | call('bar'), call().__enter__(), call().read(), |
---|
166 | n/a | call().__exit__(None, None, None)] |
---|
167 | n/a | self.assertEqual(mock.mock_calls, expected_calls) |
---|
168 | n/a | |
---|
169 | n/a | def test_explicit_mock(self): |
---|
170 | n/a | mock = MagicMock() |
---|
171 | n/a | mock_open(mock) |
---|
172 | n/a | |
---|
173 | n/a | with patch('%s.open' % __name__, mock, create=True) as patched: |
---|
174 | n/a | self.assertIs(patched, mock) |
---|
175 | n/a | open('foo') |
---|
176 | n/a | |
---|
177 | n/a | mock.assert_called_once_with('foo') |
---|
178 | n/a | |
---|
179 | n/a | |
---|
180 | n/a | def test_read_data(self): |
---|
181 | n/a | mock = mock_open(read_data='foo') |
---|
182 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
183 | n/a | h = open('bar') |
---|
184 | n/a | result = h.read() |
---|
185 | n/a | |
---|
186 | n/a | self.assertEqual(result, 'foo') |
---|
187 | n/a | |
---|
188 | n/a | |
---|
189 | n/a | def test_readline_data(self): |
---|
190 | n/a | # Check that readline will return all the lines from the fake file |
---|
191 | n/a | mock = mock_open(read_data='foo\nbar\nbaz\n') |
---|
192 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
193 | n/a | h = open('bar') |
---|
194 | n/a | line1 = h.readline() |
---|
195 | n/a | line2 = h.readline() |
---|
196 | n/a | line3 = h.readline() |
---|
197 | n/a | self.assertEqual(line1, 'foo\n') |
---|
198 | n/a | self.assertEqual(line2, 'bar\n') |
---|
199 | n/a | self.assertEqual(line3, 'baz\n') |
---|
200 | n/a | |
---|
201 | n/a | # Check that we properly emulate a file that doesn't end in a newline |
---|
202 | n/a | mock = mock_open(read_data='foo') |
---|
203 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
204 | n/a | h = open('bar') |
---|
205 | n/a | result = h.readline() |
---|
206 | n/a | self.assertEqual(result, 'foo') |
---|
207 | n/a | |
---|
208 | n/a | |
---|
209 | n/a | def test_readlines_data(self): |
---|
210 | n/a | # Test that emulating a file that ends in a newline character works |
---|
211 | n/a | mock = mock_open(read_data='foo\nbar\nbaz\n') |
---|
212 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
213 | n/a | h = open('bar') |
---|
214 | n/a | result = h.readlines() |
---|
215 | n/a | self.assertEqual(result, ['foo\n', 'bar\n', 'baz\n']) |
---|
216 | n/a | |
---|
217 | n/a | # Test that files without a final newline will also be correctly |
---|
218 | n/a | # emulated |
---|
219 | n/a | mock = mock_open(read_data='foo\nbar\nbaz') |
---|
220 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
221 | n/a | h = open('bar') |
---|
222 | n/a | result = h.readlines() |
---|
223 | n/a | |
---|
224 | n/a | self.assertEqual(result, ['foo\n', 'bar\n', 'baz']) |
---|
225 | n/a | |
---|
226 | n/a | |
---|
227 | n/a | def test_read_bytes(self): |
---|
228 | n/a | mock = mock_open(read_data=b'\xc6') |
---|
229 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
230 | n/a | with open('abc', 'rb') as f: |
---|
231 | n/a | result = f.read() |
---|
232 | n/a | self.assertEqual(result, b'\xc6') |
---|
233 | n/a | |
---|
234 | n/a | |
---|
235 | n/a | def test_readline_bytes(self): |
---|
236 | n/a | m = mock_open(read_data=b'abc\ndef\nghi\n') |
---|
237 | n/a | with patch('%s.open' % __name__, m, create=True): |
---|
238 | n/a | with open('abc', 'rb') as f: |
---|
239 | n/a | line1 = f.readline() |
---|
240 | n/a | line2 = f.readline() |
---|
241 | n/a | line3 = f.readline() |
---|
242 | n/a | self.assertEqual(line1, b'abc\n') |
---|
243 | n/a | self.assertEqual(line2, b'def\n') |
---|
244 | n/a | self.assertEqual(line3, b'ghi\n') |
---|
245 | n/a | |
---|
246 | n/a | |
---|
247 | n/a | def test_readlines_bytes(self): |
---|
248 | n/a | m = mock_open(read_data=b'abc\ndef\nghi\n') |
---|
249 | n/a | with patch('%s.open' % __name__, m, create=True): |
---|
250 | n/a | with open('abc', 'rb') as f: |
---|
251 | n/a | result = f.readlines() |
---|
252 | n/a | self.assertEqual(result, [b'abc\n', b'def\n', b'ghi\n']) |
---|
253 | n/a | |
---|
254 | n/a | |
---|
255 | n/a | def test_mock_open_read_with_argument(self): |
---|
256 | n/a | # At one point calling read with an argument was broken |
---|
257 | n/a | # for mocks returned by mock_open |
---|
258 | n/a | some_data = 'foo\nbar\nbaz' |
---|
259 | n/a | mock = mock_open(read_data=some_data) |
---|
260 | n/a | self.assertEqual(mock().read(10), some_data) |
---|
261 | n/a | |
---|
262 | n/a | |
---|
263 | n/a | def test_interleaved_reads(self): |
---|
264 | n/a | # Test that calling read, readline, and readlines pulls data |
---|
265 | n/a | # sequentially from the data we preload with |
---|
266 | n/a | mock = mock_open(read_data='foo\nbar\nbaz\n') |
---|
267 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
268 | n/a | h = open('bar') |
---|
269 | n/a | line1 = h.readline() |
---|
270 | n/a | rest = h.readlines() |
---|
271 | n/a | self.assertEqual(line1, 'foo\n') |
---|
272 | n/a | self.assertEqual(rest, ['bar\n', 'baz\n']) |
---|
273 | n/a | |
---|
274 | n/a | mock = mock_open(read_data='foo\nbar\nbaz\n') |
---|
275 | n/a | with patch('%s.open' % __name__, mock, create=True): |
---|
276 | n/a | h = open('bar') |
---|
277 | n/a | line1 = h.readline() |
---|
278 | n/a | rest = h.read() |
---|
279 | n/a | self.assertEqual(line1, 'foo\n') |
---|
280 | n/a | self.assertEqual(rest, 'bar\nbaz\n') |
---|
281 | n/a | |
---|
282 | n/a | |
---|
283 | n/a | def test_overriding_return_values(self): |
---|
284 | n/a | mock = mock_open(read_data='foo') |
---|
285 | n/a | handle = mock() |
---|
286 | n/a | |
---|
287 | n/a | handle.read.return_value = 'bar' |
---|
288 | n/a | handle.readline.return_value = 'bar' |
---|
289 | n/a | handle.readlines.return_value = ['bar'] |
---|
290 | n/a | |
---|
291 | n/a | self.assertEqual(handle.read(), 'bar') |
---|
292 | n/a | self.assertEqual(handle.readline(), 'bar') |
---|
293 | n/a | self.assertEqual(handle.readlines(), ['bar']) |
---|
294 | n/a | |
---|
295 | n/a | # call repeatedly to check that a StopIteration is not propagated |
---|
296 | n/a | self.assertEqual(handle.readline(), 'bar') |
---|
297 | n/a | self.assertEqual(handle.readline(), 'bar') |
---|
298 | n/a | |
---|
299 | n/a | |
---|
300 | n/a | if __name__ == '__main__': |
---|
301 | n/a | unittest.main() |
---|