ยปCore Development>Code coverage>Lib/unittest/test/testmock/testwith.py

Python code coverage for Lib/unittest/test/testmock/testwith.py

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