1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/mock-1.0.0/tests/_testwith.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 1.4 +# Copyright (C) 2007-2012 Michael Foord & the mock team 1.5 +# E-mail: fuzzyman AT voidspace DOT org DOT uk 1.6 +# http://www.voidspace.org.uk/python/mock/ 1.7 + 1.8 +from __future__ import with_statement 1.9 + 1.10 +from tests.support import unittest2, is_instance 1.11 + 1.12 +from mock import MagicMock, Mock, patch, sentinel, mock_open, call 1.13 + 1.14 +from tests.support_with import catch_warnings, nested 1.15 + 1.16 +something = sentinel.Something 1.17 +something_else = sentinel.SomethingElse 1.18 + 1.19 + 1.20 + 1.21 +class WithTest(unittest2.TestCase): 1.22 + 1.23 + def test_with_statement(self): 1.24 + with patch('tests._testwith.something', sentinel.Something2): 1.25 + self.assertEqual(something, sentinel.Something2, "unpatched") 1.26 + self.assertEqual(something, sentinel.Something) 1.27 + 1.28 + 1.29 + def test_with_statement_exception(self): 1.30 + try: 1.31 + with patch('tests._testwith.something', sentinel.Something2): 1.32 + self.assertEqual(something, sentinel.Something2, "unpatched") 1.33 + raise Exception('pow') 1.34 + except Exception: 1.35 + pass 1.36 + else: 1.37 + self.fail("patch swallowed exception") 1.38 + self.assertEqual(something, sentinel.Something) 1.39 + 1.40 + 1.41 + def test_with_statement_as(self): 1.42 + with patch('tests._testwith.something') as mock_something: 1.43 + self.assertEqual(something, mock_something, "unpatched") 1.44 + self.assertTrue(is_instance(mock_something, MagicMock), 1.45 + "patching wrong type") 1.46 + self.assertEqual(something, sentinel.Something) 1.47 + 1.48 + 1.49 + def test_patch_object_with_statement(self): 1.50 + class Foo(object): 1.51 + something = 'foo' 1.52 + original = Foo.something 1.53 + with patch.object(Foo, 'something'): 1.54 + self.assertNotEqual(Foo.something, original, "unpatched") 1.55 + self.assertEqual(Foo.something, original) 1.56 + 1.57 + 1.58 + def test_with_statement_nested(self): 1.59 + with catch_warnings(record=True): 1.60 + # nested is deprecated in Python 2.7 1.61 + with nested(patch('tests._testwith.something'), 1.62 + patch('tests._testwith.something_else')) as (mock_something, mock_something_else): 1.63 + self.assertEqual(something, mock_something, "unpatched") 1.64 + self.assertEqual(something_else, mock_something_else, 1.65 + "unpatched") 1.66 + self.assertEqual(something, sentinel.Something) 1.67 + self.assertEqual(something_else, sentinel.SomethingElse) 1.68 + 1.69 + 1.70 + def test_with_statement_specified(self): 1.71 + with patch('tests._testwith.something', sentinel.Patched) as mock_something: 1.72 + self.assertEqual(something, mock_something, "unpatched") 1.73 + self.assertEqual(mock_something, sentinel.Patched, "wrong patch") 1.74 + self.assertEqual(something, sentinel.Something) 1.75 + 1.76 + 1.77 + def testContextManagerMocking(self): 1.78 + mock = Mock() 1.79 + mock.__enter__ = Mock() 1.80 + mock.__exit__ = Mock() 1.81 + mock.__exit__.return_value = False 1.82 + 1.83 + with mock as m: 1.84 + self.assertEqual(m, mock.__enter__.return_value) 1.85 + mock.__enter__.assert_called_with() 1.86 + mock.__exit__.assert_called_with(None, None, None) 1.87 + 1.88 + 1.89 + def test_context_manager_with_magic_mock(self): 1.90 + mock = MagicMock() 1.91 + 1.92 + with self.assertRaises(TypeError): 1.93 + with mock: 1.94 + 'foo' + 3 1.95 + mock.__enter__.assert_called_with() 1.96 + self.assertTrue(mock.__exit__.called) 1.97 + 1.98 + 1.99 + def test_with_statement_same_attribute(self): 1.100 + with patch('tests._testwith.something', sentinel.Patched) as mock_something: 1.101 + self.assertEqual(something, mock_something, "unpatched") 1.102 + 1.103 + with patch('tests._testwith.something') as mock_again: 1.104 + self.assertEqual(something, mock_again, "unpatched") 1.105 + 1.106 + self.assertEqual(something, mock_something, 1.107 + "restored with wrong instance") 1.108 + 1.109 + self.assertEqual(something, sentinel.Something, "not restored") 1.110 + 1.111 + 1.112 + def test_with_statement_imbricated(self): 1.113 + with patch('tests._testwith.something') as mock_something: 1.114 + self.assertEqual(something, mock_something, "unpatched") 1.115 + 1.116 + with patch('tests._testwith.something_else') as mock_something_else: 1.117 + self.assertEqual(something_else, mock_something_else, 1.118 + "unpatched") 1.119 + 1.120 + self.assertEqual(something, sentinel.Something) 1.121 + self.assertEqual(something_else, sentinel.SomethingElse) 1.122 + 1.123 + 1.124 + def test_dict_context_manager(self): 1.125 + foo = {} 1.126 + with patch.dict(foo, {'a': 'b'}): 1.127 + self.assertEqual(foo, {'a': 'b'}) 1.128 + self.assertEqual(foo, {}) 1.129 + 1.130 + with self.assertRaises(NameError): 1.131 + with patch.dict(foo, {'a': 'b'}): 1.132 + self.assertEqual(foo, {'a': 'b'}) 1.133 + raise NameError('Konrad') 1.134 + 1.135 + self.assertEqual(foo, {}) 1.136 + 1.137 + 1.138 + 1.139 +class TestMockOpen(unittest2.TestCase): 1.140 + 1.141 + def test_mock_open(self): 1.142 + mock = mock_open() 1.143 + with patch('%s.open' % __name__, mock, create=True) as patched: 1.144 + self.assertIs(patched, mock) 1.145 + open('foo') 1.146 + 1.147 + mock.assert_called_once_with('foo') 1.148 + 1.149 + 1.150 + def test_mock_open_context_manager(self): 1.151 + mock = mock_open() 1.152 + handle = mock.return_value 1.153 + with patch('%s.open' % __name__, mock, create=True): 1.154 + with open('foo') as f: 1.155 + f.read() 1.156 + 1.157 + expected_calls = [call('foo'), call().__enter__(), call().read(), 1.158 + call().__exit__(None, None, None)] 1.159 + self.assertEqual(mock.mock_calls, expected_calls) 1.160 + self.assertIs(f, handle) 1.161 + 1.162 + 1.163 + def test_explicit_mock(self): 1.164 + mock = MagicMock() 1.165 + mock_open(mock) 1.166 + 1.167 + with patch('%s.open' % __name__, mock, create=True) as patched: 1.168 + self.assertIs(patched, mock) 1.169 + open('foo') 1.170 + 1.171 + mock.assert_called_once_with('foo') 1.172 + 1.173 + 1.174 + def test_read_data(self): 1.175 + mock = mock_open(read_data='foo') 1.176 + with patch('%s.open' % __name__, mock, create=True): 1.177 + h = open('bar') 1.178 + result = h.read() 1.179 + 1.180 + self.assertEqual(result, 'foo') 1.181 + 1.182 + 1.183 +if __name__ == '__main__': 1.184 + unittest2.main()