|
1 # Copyright (C) 2007-2012 Michael Foord & the mock team |
|
2 # E-mail: fuzzyman AT voidspace DOT org DOT uk |
|
3 # http://www.voidspace.org.uk/python/mock/ |
|
4 |
|
5 from __future__ import with_statement |
|
6 |
|
7 from tests.support import unittest2, is_instance |
|
8 |
|
9 from mock import MagicMock, Mock, patch, sentinel, mock_open, call |
|
10 |
|
11 from tests.support_with import catch_warnings, nested |
|
12 |
|
13 something = sentinel.Something |
|
14 something_else = sentinel.SomethingElse |
|
15 |
|
16 |
|
17 |
|
18 class WithTest(unittest2.TestCase): |
|
19 |
|
20 def test_with_statement(self): |
|
21 with patch('tests._testwith.something', sentinel.Something2): |
|
22 self.assertEqual(something, sentinel.Something2, "unpatched") |
|
23 self.assertEqual(something, sentinel.Something) |
|
24 |
|
25 |
|
26 def test_with_statement_exception(self): |
|
27 try: |
|
28 with patch('tests._testwith.something', sentinel.Something2): |
|
29 self.assertEqual(something, sentinel.Something2, "unpatched") |
|
30 raise Exception('pow') |
|
31 except Exception: |
|
32 pass |
|
33 else: |
|
34 self.fail("patch swallowed exception") |
|
35 self.assertEqual(something, sentinel.Something) |
|
36 |
|
37 |
|
38 def test_with_statement_as(self): |
|
39 with patch('tests._testwith.something') as mock_something: |
|
40 self.assertEqual(something, mock_something, "unpatched") |
|
41 self.assertTrue(is_instance(mock_something, MagicMock), |
|
42 "patching wrong type") |
|
43 self.assertEqual(something, sentinel.Something) |
|
44 |
|
45 |
|
46 def test_patch_object_with_statement(self): |
|
47 class Foo(object): |
|
48 something = 'foo' |
|
49 original = Foo.something |
|
50 with patch.object(Foo, 'something'): |
|
51 self.assertNotEqual(Foo.something, original, "unpatched") |
|
52 self.assertEqual(Foo.something, original) |
|
53 |
|
54 |
|
55 def test_with_statement_nested(self): |
|
56 with catch_warnings(record=True): |
|
57 # nested is deprecated in Python 2.7 |
|
58 with nested(patch('tests._testwith.something'), |
|
59 patch('tests._testwith.something_else')) as (mock_something, mock_something_else): |
|
60 self.assertEqual(something, mock_something, "unpatched") |
|
61 self.assertEqual(something_else, mock_something_else, |
|
62 "unpatched") |
|
63 self.assertEqual(something, sentinel.Something) |
|
64 self.assertEqual(something_else, sentinel.SomethingElse) |
|
65 |
|
66 |
|
67 def test_with_statement_specified(self): |
|
68 with patch('tests._testwith.something', sentinel.Patched) as mock_something: |
|
69 self.assertEqual(something, mock_something, "unpatched") |
|
70 self.assertEqual(mock_something, sentinel.Patched, "wrong patch") |
|
71 self.assertEqual(something, sentinel.Something) |
|
72 |
|
73 |
|
74 def testContextManagerMocking(self): |
|
75 mock = Mock() |
|
76 mock.__enter__ = Mock() |
|
77 mock.__exit__ = Mock() |
|
78 mock.__exit__.return_value = False |
|
79 |
|
80 with mock as m: |
|
81 self.assertEqual(m, mock.__enter__.return_value) |
|
82 mock.__enter__.assert_called_with() |
|
83 mock.__exit__.assert_called_with(None, None, None) |
|
84 |
|
85 |
|
86 def test_context_manager_with_magic_mock(self): |
|
87 mock = MagicMock() |
|
88 |
|
89 with self.assertRaises(TypeError): |
|
90 with mock: |
|
91 'foo' + 3 |
|
92 mock.__enter__.assert_called_with() |
|
93 self.assertTrue(mock.__exit__.called) |
|
94 |
|
95 |
|
96 def test_with_statement_same_attribute(self): |
|
97 with patch('tests._testwith.something', sentinel.Patched) as mock_something: |
|
98 self.assertEqual(something, mock_something, "unpatched") |
|
99 |
|
100 with patch('tests._testwith.something') as mock_again: |
|
101 self.assertEqual(something, mock_again, "unpatched") |
|
102 |
|
103 self.assertEqual(something, mock_something, |
|
104 "restored with wrong instance") |
|
105 |
|
106 self.assertEqual(something, sentinel.Something, "not restored") |
|
107 |
|
108 |
|
109 def test_with_statement_imbricated(self): |
|
110 with patch('tests._testwith.something') as mock_something: |
|
111 self.assertEqual(something, mock_something, "unpatched") |
|
112 |
|
113 with patch('tests._testwith.something_else') as mock_something_else: |
|
114 self.assertEqual(something_else, mock_something_else, |
|
115 "unpatched") |
|
116 |
|
117 self.assertEqual(something, sentinel.Something) |
|
118 self.assertEqual(something_else, sentinel.SomethingElse) |
|
119 |
|
120 |
|
121 def test_dict_context_manager(self): |
|
122 foo = {} |
|
123 with patch.dict(foo, {'a': 'b'}): |
|
124 self.assertEqual(foo, {'a': 'b'}) |
|
125 self.assertEqual(foo, {}) |
|
126 |
|
127 with self.assertRaises(NameError): |
|
128 with patch.dict(foo, {'a': 'b'}): |
|
129 self.assertEqual(foo, {'a': 'b'}) |
|
130 raise NameError('Konrad') |
|
131 |
|
132 self.assertEqual(foo, {}) |
|
133 |
|
134 |
|
135 |
|
136 class TestMockOpen(unittest2.TestCase): |
|
137 |
|
138 def test_mock_open(self): |
|
139 mock = mock_open() |
|
140 with patch('%s.open' % __name__, mock, create=True) as patched: |
|
141 self.assertIs(patched, mock) |
|
142 open('foo') |
|
143 |
|
144 mock.assert_called_once_with('foo') |
|
145 |
|
146 |
|
147 def test_mock_open_context_manager(self): |
|
148 mock = mock_open() |
|
149 handle = mock.return_value |
|
150 with patch('%s.open' % __name__, mock, create=True): |
|
151 with open('foo') as f: |
|
152 f.read() |
|
153 |
|
154 expected_calls = [call('foo'), call().__enter__(), call().read(), |
|
155 call().__exit__(None, None, None)] |
|
156 self.assertEqual(mock.mock_calls, expected_calls) |
|
157 self.assertIs(f, handle) |
|
158 |
|
159 |
|
160 def test_explicit_mock(self): |
|
161 mock = MagicMock() |
|
162 mock_open(mock) |
|
163 |
|
164 with patch('%s.open' % __name__, mock, create=True) as patched: |
|
165 self.assertIs(patched, mock) |
|
166 open('foo') |
|
167 |
|
168 mock.assert_called_once_with('foo') |
|
169 |
|
170 |
|
171 def test_read_data(self): |
|
172 mock = mock_open(read_data='foo') |
|
173 with patch('%s.open' % __name__, mock, create=True): |
|
174 h = open('bar') |
|
175 result = h.read() |
|
176 |
|
177 self.assertEqual(result, 'foo') |
|
178 |
|
179 |
|
180 if __name__ == '__main__': |
|
181 unittest2.main() |