Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
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/
5 from __future__ import with_statement
7 from tests.support import unittest2, is_instance
9 from mock import MagicMock, Mock, patch, sentinel, mock_open, call
11 from tests.support_with import catch_warnings, nested
13 something = sentinel.Something
14 something_else = sentinel.SomethingElse
18 class WithTest(unittest2.TestCase):
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)
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)
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)
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)
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)
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)
74 def testContextManagerMocking(self):
75 mock = Mock()
76 mock.__enter__ = Mock()
77 mock.__exit__ = Mock()
78 mock.__exit__.return_value = False
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)
86 def test_context_manager_with_magic_mock(self):
87 mock = MagicMock()
89 with self.assertRaises(TypeError):
90 with mock:
91 'foo' + 3
92 mock.__enter__.assert_called_with()
93 self.assertTrue(mock.__exit__.called)
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")
100 with patch('tests._testwith.something') as mock_again:
101 self.assertEqual(something, mock_again, "unpatched")
103 self.assertEqual(something, mock_something,
104 "restored with wrong instance")
106 self.assertEqual(something, sentinel.Something, "not restored")
109 def test_with_statement_imbricated(self):
110 with patch('tests._testwith.something') as mock_something:
111 self.assertEqual(something, mock_something, "unpatched")
113 with patch('tests._testwith.something_else') as mock_something_else:
114 self.assertEqual(something_else, mock_something_else,
115 "unpatched")
117 self.assertEqual(something, sentinel.Something)
118 self.assertEqual(something_else, sentinel.SomethingElse)
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, {})
127 with self.assertRaises(NameError):
128 with patch.dict(foo, {'a': 'b'}):
129 self.assertEqual(foo, {'a': 'b'})
130 raise NameError('Konrad')
132 self.assertEqual(foo, {})
136 class TestMockOpen(unittest2.TestCase):
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')
144 mock.assert_called_once_with('foo')
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()
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)
160 def test_explicit_mock(self):
161 mock = MagicMock()
162 mock_open(mock)
164 with patch('%s.open' % __name__, mock, create=True) as patched:
165 self.assertIs(patched, mock)
166 open('foo')
168 mock.assert_called_once_with('foo')
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()
177 self.assertEqual(result, 'foo')
180 if __name__ == '__main__':
181 unittest2.main()