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