Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
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 | import os |
michael@0 | 6 | import sys |
michael@0 | 7 | |
michael@0 | 8 | from tests import support |
michael@0 | 9 | from tests.support import unittest2, inPy3k, SomeClass, is_instance, callable |
michael@0 | 10 | |
michael@0 | 11 | from mock import ( |
michael@0 | 12 | NonCallableMock, CallableMixin, patch, sentinel, |
michael@0 | 13 | MagicMock, Mock, NonCallableMagicMock, patch, _patch, |
michael@0 | 14 | DEFAULT, call, _get_target |
michael@0 | 15 | ) |
michael@0 | 16 | |
michael@0 | 17 | builtin_string = '__builtin__' |
michael@0 | 18 | if inPy3k: |
michael@0 | 19 | builtin_string = 'builtins' |
michael@0 | 20 | unicode = str |
michael@0 | 21 | |
michael@0 | 22 | PTModule = sys.modules[__name__] |
michael@0 | 23 | MODNAME = '%s.PTModule' % __name__ |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | def _get_proxy(obj, get_only=True): |
michael@0 | 27 | class Proxy(object): |
michael@0 | 28 | def __getattr__(self, name): |
michael@0 | 29 | return getattr(obj, name) |
michael@0 | 30 | if not get_only: |
michael@0 | 31 | def __setattr__(self, name, value): |
michael@0 | 32 | setattr(obj, name, value) |
michael@0 | 33 | def __delattr__(self, name): |
michael@0 | 34 | delattr(obj, name) |
michael@0 | 35 | Proxy.__setattr__ = __setattr__ |
michael@0 | 36 | Proxy.__delattr__ = __delattr__ |
michael@0 | 37 | return Proxy() |
michael@0 | 38 | |
michael@0 | 39 | |
michael@0 | 40 | # for use in the test |
michael@0 | 41 | something = sentinel.Something |
michael@0 | 42 | something_else = sentinel.SomethingElse |
michael@0 | 43 | |
michael@0 | 44 | |
michael@0 | 45 | class Foo(object): |
michael@0 | 46 | def __init__(self, a): |
michael@0 | 47 | pass |
michael@0 | 48 | def f(self, a): |
michael@0 | 49 | pass |
michael@0 | 50 | def g(self): |
michael@0 | 51 | pass |
michael@0 | 52 | foo = 'bar' |
michael@0 | 53 | |
michael@0 | 54 | class Bar(object): |
michael@0 | 55 | def a(self): |
michael@0 | 56 | pass |
michael@0 | 57 | |
michael@0 | 58 | foo_name = '%s.Foo' % __name__ |
michael@0 | 59 | |
michael@0 | 60 | |
michael@0 | 61 | def function(a, b=Foo): |
michael@0 | 62 | pass |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | class Container(object): |
michael@0 | 66 | def __init__(self): |
michael@0 | 67 | self.values = {} |
michael@0 | 68 | |
michael@0 | 69 | def __getitem__(self, name): |
michael@0 | 70 | return self.values[name] |
michael@0 | 71 | |
michael@0 | 72 | def __setitem__(self, name, value): |
michael@0 | 73 | self.values[name] = value |
michael@0 | 74 | |
michael@0 | 75 | def __delitem__(self, name): |
michael@0 | 76 | del self.values[name] |
michael@0 | 77 | |
michael@0 | 78 | def __iter__(self): |
michael@0 | 79 | return iter(self.values) |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | |
michael@0 | 83 | class PatchTest(unittest2.TestCase): |
michael@0 | 84 | |
michael@0 | 85 | def assertNotCallable(self, obj, magic=True): |
michael@0 | 86 | MockClass = NonCallableMagicMock |
michael@0 | 87 | if not magic: |
michael@0 | 88 | MockClass = NonCallableMock |
michael@0 | 89 | |
michael@0 | 90 | self.assertRaises(TypeError, obj) |
michael@0 | 91 | self.assertTrue(is_instance(obj, MockClass)) |
michael@0 | 92 | self.assertFalse(is_instance(obj, CallableMixin)) |
michael@0 | 93 | |
michael@0 | 94 | |
michael@0 | 95 | def test_single_patchobject(self): |
michael@0 | 96 | class Something(object): |
michael@0 | 97 | attribute = sentinel.Original |
michael@0 | 98 | |
michael@0 | 99 | @patch.object(Something, 'attribute', sentinel.Patched) |
michael@0 | 100 | def test(): |
michael@0 | 101 | self.assertEqual(Something.attribute, sentinel.Patched, "unpatched") |
michael@0 | 102 | |
michael@0 | 103 | test() |
michael@0 | 104 | self.assertEqual(Something.attribute, sentinel.Original, |
michael@0 | 105 | "patch not restored") |
michael@0 | 106 | |
michael@0 | 107 | |
michael@0 | 108 | def test_patchobject_with_none(self): |
michael@0 | 109 | class Something(object): |
michael@0 | 110 | attribute = sentinel.Original |
michael@0 | 111 | |
michael@0 | 112 | @patch.object(Something, 'attribute', None) |
michael@0 | 113 | def test(): |
michael@0 | 114 | self.assertIsNone(Something.attribute, "unpatched") |
michael@0 | 115 | |
michael@0 | 116 | test() |
michael@0 | 117 | self.assertEqual(Something.attribute, sentinel.Original, |
michael@0 | 118 | "patch not restored") |
michael@0 | 119 | |
michael@0 | 120 | |
michael@0 | 121 | def test_multiple_patchobject(self): |
michael@0 | 122 | class Something(object): |
michael@0 | 123 | attribute = sentinel.Original |
michael@0 | 124 | next_attribute = sentinel.Original2 |
michael@0 | 125 | |
michael@0 | 126 | @patch.object(Something, 'attribute', sentinel.Patched) |
michael@0 | 127 | @patch.object(Something, 'next_attribute', sentinel.Patched2) |
michael@0 | 128 | def test(): |
michael@0 | 129 | self.assertEqual(Something.attribute, sentinel.Patched, |
michael@0 | 130 | "unpatched") |
michael@0 | 131 | self.assertEqual(Something.next_attribute, sentinel.Patched2, |
michael@0 | 132 | "unpatched") |
michael@0 | 133 | |
michael@0 | 134 | test() |
michael@0 | 135 | self.assertEqual(Something.attribute, sentinel.Original, |
michael@0 | 136 | "patch not restored") |
michael@0 | 137 | self.assertEqual(Something.next_attribute, sentinel.Original2, |
michael@0 | 138 | "patch not restored") |
michael@0 | 139 | |
michael@0 | 140 | |
michael@0 | 141 | def test_object_lookup_is_quite_lazy(self): |
michael@0 | 142 | global something |
michael@0 | 143 | original = something |
michael@0 | 144 | @patch('%s.something' % __name__, sentinel.Something2) |
michael@0 | 145 | def test(): |
michael@0 | 146 | pass |
michael@0 | 147 | |
michael@0 | 148 | try: |
michael@0 | 149 | something = sentinel.replacement_value |
michael@0 | 150 | test() |
michael@0 | 151 | self.assertEqual(something, sentinel.replacement_value) |
michael@0 | 152 | finally: |
michael@0 | 153 | something = original |
michael@0 | 154 | |
michael@0 | 155 | |
michael@0 | 156 | def test_patch(self): |
michael@0 | 157 | @patch('%s.something' % __name__, sentinel.Something2) |
michael@0 | 158 | def test(): |
michael@0 | 159 | self.assertEqual(PTModule.something, sentinel.Something2, |
michael@0 | 160 | "unpatched") |
michael@0 | 161 | |
michael@0 | 162 | test() |
michael@0 | 163 | self.assertEqual(PTModule.something, sentinel.Something, |
michael@0 | 164 | "patch not restored") |
michael@0 | 165 | |
michael@0 | 166 | @patch('%s.something' % __name__, sentinel.Something2) |
michael@0 | 167 | @patch('%s.something_else' % __name__, sentinel.SomethingElse) |
michael@0 | 168 | def test(): |
michael@0 | 169 | self.assertEqual(PTModule.something, sentinel.Something2, |
michael@0 | 170 | "unpatched") |
michael@0 | 171 | self.assertEqual(PTModule.something_else, sentinel.SomethingElse, |
michael@0 | 172 | "unpatched") |
michael@0 | 173 | |
michael@0 | 174 | self.assertEqual(PTModule.something, sentinel.Something, |
michael@0 | 175 | "patch not restored") |
michael@0 | 176 | self.assertEqual(PTModule.something_else, sentinel.SomethingElse, |
michael@0 | 177 | "patch not restored") |
michael@0 | 178 | |
michael@0 | 179 | # Test the patching and restoring works a second time |
michael@0 | 180 | test() |
michael@0 | 181 | |
michael@0 | 182 | self.assertEqual(PTModule.something, sentinel.Something, |
michael@0 | 183 | "patch not restored") |
michael@0 | 184 | self.assertEqual(PTModule.something_else, sentinel.SomethingElse, |
michael@0 | 185 | "patch not restored") |
michael@0 | 186 | |
michael@0 | 187 | mock = Mock() |
michael@0 | 188 | mock.return_value = sentinel.Handle |
michael@0 | 189 | @patch('%s.open' % builtin_string, mock) |
michael@0 | 190 | def test(): |
michael@0 | 191 | self.assertEqual(open('filename', 'r'), sentinel.Handle, |
michael@0 | 192 | "open not patched") |
michael@0 | 193 | test() |
michael@0 | 194 | test() |
michael@0 | 195 | |
michael@0 | 196 | self.assertNotEqual(open, mock, "patch not restored") |
michael@0 | 197 | |
michael@0 | 198 | |
michael@0 | 199 | def test_patch_class_attribute(self): |
michael@0 | 200 | @patch('%s.SomeClass.class_attribute' % __name__, |
michael@0 | 201 | sentinel.ClassAttribute) |
michael@0 | 202 | def test(): |
michael@0 | 203 | self.assertEqual(PTModule.SomeClass.class_attribute, |
michael@0 | 204 | sentinel.ClassAttribute, "unpatched") |
michael@0 | 205 | test() |
michael@0 | 206 | |
michael@0 | 207 | self.assertIsNone(PTModule.SomeClass.class_attribute, |
michael@0 | 208 | "patch not restored") |
michael@0 | 209 | |
michael@0 | 210 | |
michael@0 | 211 | def test_patchobject_with_default_mock(self): |
michael@0 | 212 | class Test(object): |
michael@0 | 213 | something = sentinel.Original |
michael@0 | 214 | something2 = sentinel.Original2 |
michael@0 | 215 | |
michael@0 | 216 | @patch.object(Test, 'something') |
michael@0 | 217 | def test(mock): |
michael@0 | 218 | self.assertEqual(mock, Test.something, |
michael@0 | 219 | "Mock not passed into test function") |
michael@0 | 220 | self.assertIsInstance(mock, MagicMock, |
michael@0 | 221 | "patch with two arguments did not create a mock") |
michael@0 | 222 | |
michael@0 | 223 | test() |
michael@0 | 224 | |
michael@0 | 225 | @patch.object(Test, 'something') |
michael@0 | 226 | @patch.object(Test, 'something2') |
michael@0 | 227 | def test(this1, this2, mock1, mock2): |
michael@0 | 228 | self.assertEqual(this1, sentinel.this1, |
michael@0 | 229 | "Patched function didn't receive initial argument") |
michael@0 | 230 | self.assertEqual(this2, sentinel.this2, |
michael@0 | 231 | "Patched function didn't receive second argument") |
michael@0 | 232 | self.assertEqual(mock1, Test.something2, |
michael@0 | 233 | "Mock not passed into test function") |
michael@0 | 234 | self.assertEqual(mock2, Test.something, |
michael@0 | 235 | "Second Mock not passed into test function") |
michael@0 | 236 | self.assertIsInstance(mock2, MagicMock, |
michael@0 | 237 | "patch with two arguments did not create a mock") |
michael@0 | 238 | self.assertIsInstance(mock2, MagicMock, |
michael@0 | 239 | "patch with two arguments did not create a mock") |
michael@0 | 240 | |
michael@0 | 241 | # A hack to test that new mocks are passed the second time |
michael@0 | 242 | self.assertNotEqual(outerMock1, mock1, "unexpected value for mock1") |
michael@0 | 243 | self.assertNotEqual(outerMock2, mock2, "unexpected value for mock1") |
michael@0 | 244 | return mock1, mock2 |
michael@0 | 245 | |
michael@0 | 246 | outerMock1 = outerMock2 = None |
michael@0 | 247 | outerMock1, outerMock2 = test(sentinel.this1, sentinel.this2) |
michael@0 | 248 | |
michael@0 | 249 | # Test that executing a second time creates new mocks |
michael@0 | 250 | test(sentinel.this1, sentinel.this2) |
michael@0 | 251 | |
michael@0 | 252 | |
michael@0 | 253 | def test_patch_with_spec(self): |
michael@0 | 254 | @patch('%s.SomeClass' % __name__, spec=SomeClass) |
michael@0 | 255 | def test(MockSomeClass): |
michael@0 | 256 | self.assertEqual(SomeClass, MockSomeClass) |
michael@0 | 257 | self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) |
michael@0 | 258 | self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) |
michael@0 | 259 | |
michael@0 | 260 | test() |
michael@0 | 261 | |
michael@0 | 262 | |
michael@0 | 263 | def test_patchobject_with_spec(self): |
michael@0 | 264 | @patch.object(SomeClass, 'class_attribute', spec=SomeClass) |
michael@0 | 265 | def test(MockAttribute): |
michael@0 | 266 | self.assertEqual(SomeClass.class_attribute, MockAttribute) |
michael@0 | 267 | self.assertTrue(is_instance(SomeClass.class_attribute.wibble, |
michael@0 | 268 | MagicMock)) |
michael@0 | 269 | self.assertRaises(AttributeError, |
michael@0 | 270 | lambda: SomeClass.class_attribute.not_wibble) |
michael@0 | 271 | |
michael@0 | 272 | test() |
michael@0 | 273 | |
michael@0 | 274 | |
michael@0 | 275 | def test_patch_with_spec_as_list(self): |
michael@0 | 276 | @patch('%s.SomeClass' % __name__, spec=['wibble']) |
michael@0 | 277 | def test(MockSomeClass): |
michael@0 | 278 | self.assertEqual(SomeClass, MockSomeClass) |
michael@0 | 279 | self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) |
michael@0 | 280 | self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) |
michael@0 | 281 | |
michael@0 | 282 | test() |
michael@0 | 283 | |
michael@0 | 284 | |
michael@0 | 285 | def test_patchobject_with_spec_as_list(self): |
michael@0 | 286 | @patch.object(SomeClass, 'class_attribute', spec=['wibble']) |
michael@0 | 287 | def test(MockAttribute): |
michael@0 | 288 | self.assertEqual(SomeClass.class_attribute, MockAttribute) |
michael@0 | 289 | self.assertTrue(is_instance(SomeClass.class_attribute.wibble, |
michael@0 | 290 | MagicMock)) |
michael@0 | 291 | self.assertRaises(AttributeError, |
michael@0 | 292 | lambda: SomeClass.class_attribute.not_wibble) |
michael@0 | 293 | |
michael@0 | 294 | test() |
michael@0 | 295 | |
michael@0 | 296 | |
michael@0 | 297 | def test_nested_patch_with_spec_as_list(self): |
michael@0 | 298 | # regression test for nested decorators |
michael@0 | 299 | @patch('%s.open' % builtin_string) |
michael@0 | 300 | @patch('%s.SomeClass' % __name__, spec=['wibble']) |
michael@0 | 301 | def test(MockSomeClass, MockOpen): |
michael@0 | 302 | self.assertEqual(SomeClass, MockSomeClass) |
michael@0 | 303 | self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) |
michael@0 | 304 | self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) |
michael@0 | 305 | test() |
michael@0 | 306 | |
michael@0 | 307 | |
michael@0 | 308 | def test_patch_with_spec_as_boolean(self): |
michael@0 | 309 | @patch('%s.SomeClass' % __name__, spec=True) |
michael@0 | 310 | def test(MockSomeClass): |
michael@0 | 311 | self.assertEqual(SomeClass, MockSomeClass) |
michael@0 | 312 | # Should not raise attribute error |
michael@0 | 313 | MockSomeClass.wibble |
michael@0 | 314 | |
michael@0 | 315 | self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble) |
michael@0 | 316 | |
michael@0 | 317 | test() |
michael@0 | 318 | |
michael@0 | 319 | |
michael@0 | 320 | def test_patch_object_with_spec_as_boolean(self): |
michael@0 | 321 | @patch.object(PTModule, 'SomeClass', spec=True) |
michael@0 | 322 | def test(MockSomeClass): |
michael@0 | 323 | self.assertEqual(SomeClass, MockSomeClass) |
michael@0 | 324 | # Should not raise attribute error |
michael@0 | 325 | MockSomeClass.wibble |
michael@0 | 326 | |
michael@0 | 327 | self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble) |
michael@0 | 328 | |
michael@0 | 329 | test() |
michael@0 | 330 | |
michael@0 | 331 | |
michael@0 | 332 | def test_patch_class_acts_with_spec_is_inherited(self): |
michael@0 | 333 | @patch('%s.SomeClass' % __name__, spec=True) |
michael@0 | 334 | def test(MockSomeClass): |
michael@0 | 335 | self.assertTrue(is_instance(MockSomeClass, MagicMock)) |
michael@0 | 336 | instance = MockSomeClass() |
michael@0 | 337 | self.assertNotCallable(instance) |
michael@0 | 338 | # Should not raise attribute error |
michael@0 | 339 | instance.wibble |
michael@0 | 340 | |
michael@0 | 341 | self.assertRaises(AttributeError, lambda: instance.not_wibble) |
michael@0 | 342 | |
michael@0 | 343 | test() |
michael@0 | 344 | |
michael@0 | 345 | |
michael@0 | 346 | def test_patch_with_create_mocks_non_existent_attributes(self): |
michael@0 | 347 | @patch('%s.frooble' % builtin_string, sentinel.Frooble, create=True) |
michael@0 | 348 | def test(): |
michael@0 | 349 | self.assertEqual(frooble, sentinel.Frooble) |
michael@0 | 350 | |
michael@0 | 351 | test() |
michael@0 | 352 | self.assertRaises(NameError, lambda: frooble) |
michael@0 | 353 | |
michael@0 | 354 | |
michael@0 | 355 | def test_patchobject_with_create_mocks_non_existent_attributes(self): |
michael@0 | 356 | @patch.object(SomeClass, 'frooble', sentinel.Frooble, create=True) |
michael@0 | 357 | def test(): |
michael@0 | 358 | self.assertEqual(SomeClass.frooble, sentinel.Frooble) |
michael@0 | 359 | |
michael@0 | 360 | test() |
michael@0 | 361 | self.assertFalse(hasattr(SomeClass, 'frooble')) |
michael@0 | 362 | |
michael@0 | 363 | |
michael@0 | 364 | def test_patch_wont_create_by_default(self): |
michael@0 | 365 | try: |
michael@0 | 366 | @patch('%s.frooble' % builtin_string, sentinel.Frooble) |
michael@0 | 367 | def test(): |
michael@0 | 368 | self.assertEqual(frooble, sentinel.Frooble) |
michael@0 | 369 | |
michael@0 | 370 | test() |
michael@0 | 371 | except AttributeError: |
michael@0 | 372 | pass |
michael@0 | 373 | else: |
michael@0 | 374 | self.fail('Patching non existent attributes should fail') |
michael@0 | 375 | |
michael@0 | 376 | self.assertRaises(NameError, lambda: frooble) |
michael@0 | 377 | |
michael@0 | 378 | |
michael@0 | 379 | def test_patchobject_wont_create_by_default(self): |
michael@0 | 380 | try: |
michael@0 | 381 | @patch.object(SomeClass, 'frooble', sentinel.Frooble) |
michael@0 | 382 | def test(): |
michael@0 | 383 | self.fail('Patching non existent attributes should fail') |
michael@0 | 384 | |
michael@0 | 385 | test() |
michael@0 | 386 | except AttributeError: |
michael@0 | 387 | pass |
michael@0 | 388 | else: |
michael@0 | 389 | self.fail('Patching non existent attributes should fail') |
michael@0 | 390 | self.assertFalse(hasattr(SomeClass, 'frooble')) |
michael@0 | 391 | |
michael@0 | 392 | |
michael@0 | 393 | def test_patch_with_static_methods(self): |
michael@0 | 394 | class Foo(object): |
michael@0 | 395 | @staticmethod |
michael@0 | 396 | def woot(): |
michael@0 | 397 | return sentinel.Static |
michael@0 | 398 | |
michael@0 | 399 | @patch.object(Foo, 'woot', staticmethod(lambda: sentinel.Patched)) |
michael@0 | 400 | def anonymous(): |
michael@0 | 401 | self.assertEqual(Foo.woot(), sentinel.Patched) |
michael@0 | 402 | anonymous() |
michael@0 | 403 | |
michael@0 | 404 | self.assertEqual(Foo.woot(), sentinel.Static) |
michael@0 | 405 | |
michael@0 | 406 | |
michael@0 | 407 | def test_patch_local(self): |
michael@0 | 408 | foo = sentinel.Foo |
michael@0 | 409 | @patch.object(sentinel, 'Foo', 'Foo') |
michael@0 | 410 | def anonymous(): |
michael@0 | 411 | self.assertEqual(sentinel.Foo, 'Foo') |
michael@0 | 412 | anonymous() |
michael@0 | 413 | |
michael@0 | 414 | self.assertEqual(sentinel.Foo, foo) |
michael@0 | 415 | |
michael@0 | 416 | |
michael@0 | 417 | def test_patch_slots(self): |
michael@0 | 418 | class Foo(object): |
michael@0 | 419 | __slots__ = ('Foo',) |
michael@0 | 420 | |
michael@0 | 421 | foo = Foo() |
michael@0 | 422 | foo.Foo = sentinel.Foo |
michael@0 | 423 | |
michael@0 | 424 | @patch.object(foo, 'Foo', 'Foo') |
michael@0 | 425 | def anonymous(): |
michael@0 | 426 | self.assertEqual(foo.Foo, 'Foo') |
michael@0 | 427 | anonymous() |
michael@0 | 428 | |
michael@0 | 429 | self.assertEqual(foo.Foo, sentinel.Foo) |
michael@0 | 430 | |
michael@0 | 431 | |
michael@0 | 432 | def test_patchobject_class_decorator(self): |
michael@0 | 433 | class Something(object): |
michael@0 | 434 | attribute = sentinel.Original |
michael@0 | 435 | |
michael@0 | 436 | class Foo(object): |
michael@0 | 437 | def test_method(other_self): |
michael@0 | 438 | self.assertEqual(Something.attribute, sentinel.Patched, |
michael@0 | 439 | "unpatched") |
michael@0 | 440 | def not_test_method(other_self): |
michael@0 | 441 | self.assertEqual(Something.attribute, sentinel.Original, |
michael@0 | 442 | "non-test method patched") |
michael@0 | 443 | |
michael@0 | 444 | Foo = patch.object(Something, 'attribute', sentinel.Patched)(Foo) |
michael@0 | 445 | |
michael@0 | 446 | f = Foo() |
michael@0 | 447 | f.test_method() |
michael@0 | 448 | f.not_test_method() |
michael@0 | 449 | |
michael@0 | 450 | self.assertEqual(Something.attribute, sentinel.Original, |
michael@0 | 451 | "patch not restored") |
michael@0 | 452 | |
michael@0 | 453 | |
michael@0 | 454 | def test_patch_class_decorator(self): |
michael@0 | 455 | class Something(object): |
michael@0 | 456 | attribute = sentinel.Original |
michael@0 | 457 | |
michael@0 | 458 | class Foo(object): |
michael@0 | 459 | def test_method(other_self, mock_something): |
michael@0 | 460 | self.assertEqual(PTModule.something, mock_something, |
michael@0 | 461 | "unpatched") |
michael@0 | 462 | def not_test_method(other_self): |
michael@0 | 463 | self.assertEqual(PTModule.something, sentinel.Something, |
michael@0 | 464 | "non-test method patched") |
michael@0 | 465 | Foo = patch('%s.something' % __name__)(Foo) |
michael@0 | 466 | |
michael@0 | 467 | f = Foo() |
michael@0 | 468 | f.test_method() |
michael@0 | 469 | f.not_test_method() |
michael@0 | 470 | |
michael@0 | 471 | self.assertEqual(Something.attribute, sentinel.Original, |
michael@0 | 472 | "patch not restored") |
michael@0 | 473 | self.assertEqual(PTModule.something, sentinel.Something, |
michael@0 | 474 | "patch not restored") |
michael@0 | 475 | |
michael@0 | 476 | |
michael@0 | 477 | def test_patchobject_twice(self): |
michael@0 | 478 | class Something(object): |
michael@0 | 479 | attribute = sentinel.Original |
michael@0 | 480 | next_attribute = sentinel.Original2 |
michael@0 | 481 | |
michael@0 | 482 | @patch.object(Something, 'attribute', sentinel.Patched) |
michael@0 | 483 | @patch.object(Something, 'attribute', sentinel.Patched) |
michael@0 | 484 | def test(): |
michael@0 | 485 | self.assertEqual(Something.attribute, sentinel.Patched, "unpatched") |
michael@0 | 486 | |
michael@0 | 487 | test() |
michael@0 | 488 | |
michael@0 | 489 | self.assertEqual(Something.attribute, sentinel.Original, |
michael@0 | 490 | "patch not restored") |
michael@0 | 491 | |
michael@0 | 492 | |
michael@0 | 493 | def test_patch_dict(self): |
michael@0 | 494 | foo = {'initial': object(), 'other': 'something'} |
michael@0 | 495 | original = foo.copy() |
michael@0 | 496 | |
michael@0 | 497 | @patch.dict(foo) |
michael@0 | 498 | def test(): |
michael@0 | 499 | foo['a'] = 3 |
michael@0 | 500 | del foo['initial'] |
michael@0 | 501 | foo['other'] = 'something else' |
michael@0 | 502 | |
michael@0 | 503 | test() |
michael@0 | 504 | |
michael@0 | 505 | self.assertEqual(foo, original) |
michael@0 | 506 | |
michael@0 | 507 | @patch.dict(foo, {'a': 'b'}) |
michael@0 | 508 | def test(): |
michael@0 | 509 | self.assertEqual(len(foo), 3) |
michael@0 | 510 | self.assertEqual(foo['a'], 'b') |
michael@0 | 511 | |
michael@0 | 512 | test() |
michael@0 | 513 | |
michael@0 | 514 | self.assertEqual(foo, original) |
michael@0 | 515 | |
michael@0 | 516 | @patch.dict(foo, [('a', 'b')]) |
michael@0 | 517 | def test(): |
michael@0 | 518 | self.assertEqual(len(foo), 3) |
michael@0 | 519 | self.assertEqual(foo['a'], 'b') |
michael@0 | 520 | |
michael@0 | 521 | test() |
michael@0 | 522 | |
michael@0 | 523 | self.assertEqual(foo, original) |
michael@0 | 524 | |
michael@0 | 525 | |
michael@0 | 526 | def test_patch_dict_with_container_object(self): |
michael@0 | 527 | foo = Container() |
michael@0 | 528 | foo['initial'] = object() |
michael@0 | 529 | foo['other'] = 'something' |
michael@0 | 530 | |
michael@0 | 531 | original = foo.values.copy() |
michael@0 | 532 | |
michael@0 | 533 | @patch.dict(foo) |
michael@0 | 534 | def test(): |
michael@0 | 535 | foo['a'] = 3 |
michael@0 | 536 | del foo['initial'] |
michael@0 | 537 | foo['other'] = 'something else' |
michael@0 | 538 | |
michael@0 | 539 | test() |
michael@0 | 540 | |
michael@0 | 541 | self.assertEqual(foo.values, original) |
michael@0 | 542 | |
michael@0 | 543 | @patch.dict(foo, {'a': 'b'}) |
michael@0 | 544 | def test(): |
michael@0 | 545 | self.assertEqual(len(foo.values), 3) |
michael@0 | 546 | self.assertEqual(foo['a'], 'b') |
michael@0 | 547 | |
michael@0 | 548 | test() |
michael@0 | 549 | |
michael@0 | 550 | self.assertEqual(foo.values, original) |
michael@0 | 551 | |
michael@0 | 552 | |
michael@0 | 553 | def test_patch_dict_with_clear(self): |
michael@0 | 554 | foo = {'initial': object(), 'other': 'something'} |
michael@0 | 555 | original = foo.copy() |
michael@0 | 556 | |
michael@0 | 557 | @patch.dict(foo, clear=True) |
michael@0 | 558 | def test(): |
michael@0 | 559 | self.assertEqual(foo, {}) |
michael@0 | 560 | foo['a'] = 3 |
michael@0 | 561 | foo['other'] = 'something else' |
michael@0 | 562 | |
michael@0 | 563 | test() |
michael@0 | 564 | |
michael@0 | 565 | self.assertEqual(foo, original) |
michael@0 | 566 | |
michael@0 | 567 | @patch.dict(foo, {'a': 'b'}, clear=True) |
michael@0 | 568 | def test(): |
michael@0 | 569 | self.assertEqual(foo, {'a': 'b'}) |
michael@0 | 570 | |
michael@0 | 571 | test() |
michael@0 | 572 | |
michael@0 | 573 | self.assertEqual(foo, original) |
michael@0 | 574 | |
michael@0 | 575 | @patch.dict(foo, [('a', 'b')], clear=True) |
michael@0 | 576 | def test(): |
michael@0 | 577 | self.assertEqual(foo, {'a': 'b'}) |
michael@0 | 578 | |
michael@0 | 579 | test() |
michael@0 | 580 | |
michael@0 | 581 | self.assertEqual(foo, original) |
michael@0 | 582 | |
michael@0 | 583 | |
michael@0 | 584 | def test_patch_dict_with_container_object_and_clear(self): |
michael@0 | 585 | foo = Container() |
michael@0 | 586 | foo['initial'] = object() |
michael@0 | 587 | foo['other'] = 'something' |
michael@0 | 588 | |
michael@0 | 589 | original = foo.values.copy() |
michael@0 | 590 | |
michael@0 | 591 | @patch.dict(foo, clear=True) |
michael@0 | 592 | def test(): |
michael@0 | 593 | self.assertEqual(foo.values, {}) |
michael@0 | 594 | foo['a'] = 3 |
michael@0 | 595 | foo['other'] = 'something else' |
michael@0 | 596 | |
michael@0 | 597 | test() |
michael@0 | 598 | |
michael@0 | 599 | self.assertEqual(foo.values, original) |
michael@0 | 600 | |
michael@0 | 601 | @patch.dict(foo, {'a': 'b'}, clear=True) |
michael@0 | 602 | def test(): |
michael@0 | 603 | self.assertEqual(foo.values, {'a': 'b'}) |
michael@0 | 604 | |
michael@0 | 605 | test() |
michael@0 | 606 | |
michael@0 | 607 | self.assertEqual(foo.values, original) |
michael@0 | 608 | |
michael@0 | 609 | |
michael@0 | 610 | def test_name_preserved(self): |
michael@0 | 611 | foo = {} |
michael@0 | 612 | |
michael@0 | 613 | @patch('%s.SomeClass' % __name__, object()) |
michael@0 | 614 | @patch('%s.SomeClass' % __name__, object(), autospec=True) |
michael@0 | 615 | @patch.object(SomeClass, object()) |
michael@0 | 616 | @patch.dict(foo) |
michael@0 | 617 | def some_name(): |
michael@0 | 618 | pass |
michael@0 | 619 | |
michael@0 | 620 | self.assertEqual(some_name.__name__, 'some_name') |
michael@0 | 621 | |
michael@0 | 622 | |
michael@0 | 623 | def test_patch_with_exception(self): |
michael@0 | 624 | foo = {} |
michael@0 | 625 | |
michael@0 | 626 | @patch.dict(foo, {'a': 'b'}) |
michael@0 | 627 | def test(): |
michael@0 | 628 | raise NameError('Konrad') |
michael@0 | 629 | try: |
michael@0 | 630 | test() |
michael@0 | 631 | except NameError: |
michael@0 | 632 | pass |
michael@0 | 633 | else: |
michael@0 | 634 | self.fail('NameError not raised by test') |
michael@0 | 635 | |
michael@0 | 636 | self.assertEqual(foo, {}) |
michael@0 | 637 | |
michael@0 | 638 | |
michael@0 | 639 | def test_patch_dict_with_string(self): |
michael@0 | 640 | @patch.dict('os.environ', {'konrad_delong': 'some value'}) |
michael@0 | 641 | def test(): |
michael@0 | 642 | self.assertIn('konrad_delong', os.environ) |
michael@0 | 643 | |
michael@0 | 644 | test() |
michael@0 | 645 | |
michael@0 | 646 | |
michael@0 | 647 | @unittest2.expectedFailure |
michael@0 | 648 | def test_patch_descriptor(self): |
michael@0 | 649 | # would be some effort to fix this - we could special case the |
michael@0 | 650 | # builtin descriptors: classmethod, property, staticmethod |
michael@0 | 651 | class Nothing(object): |
michael@0 | 652 | foo = None |
michael@0 | 653 | |
michael@0 | 654 | class Something(object): |
michael@0 | 655 | foo = {} |
michael@0 | 656 | |
michael@0 | 657 | @patch.object(Nothing, 'foo', 2) |
michael@0 | 658 | @classmethod |
michael@0 | 659 | def klass(cls): |
michael@0 | 660 | self.assertIs(cls, Something) |
michael@0 | 661 | |
michael@0 | 662 | @patch.object(Nothing, 'foo', 2) |
michael@0 | 663 | @staticmethod |
michael@0 | 664 | def static(arg): |
michael@0 | 665 | return arg |
michael@0 | 666 | |
michael@0 | 667 | @patch.dict(foo) |
michael@0 | 668 | @classmethod |
michael@0 | 669 | def klass_dict(cls): |
michael@0 | 670 | self.assertIs(cls, Something) |
michael@0 | 671 | |
michael@0 | 672 | @patch.dict(foo) |
michael@0 | 673 | @staticmethod |
michael@0 | 674 | def static_dict(arg): |
michael@0 | 675 | return arg |
michael@0 | 676 | |
michael@0 | 677 | # these will raise exceptions if patching descriptors is broken |
michael@0 | 678 | self.assertEqual(Something.static('f00'), 'f00') |
michael@0 | 679 | Something.klass() |
michael@0 | 680 | self.assertEqual(Something.static_dict('f00'), 'f00') |
michael@0 | 681 | Something.klass_dict() |
michael@0 | 682 | |
michael@0 | 683 | something = Something() |
michael@0 | 684 | self.assertEqual(something.static('f00'), 'f00') |
michael@0 | 685 | something.klass() |
michael@0 | 686 | self.assertEqual(something.static_dict('f00'), 'f00') |
michael@0 | 687 | something.klass_dict() |
michael@0 | 688 | |
michael@0 | 689 | |
michael@0 | 690 | def test_patch_spec_set(self): |
michael@0 | 691 | @patch('%s.SomeClass' % __name__, spec_set=SomeClass) |
michael@0 | 692 | def test(MockClass): |
michael@0 | 693 | MockClass.z = 'foo' |
michael@0 | 694 | |
michael@0 | 695 | self.assertRaises(AttributeError, test) |
michael@0 | 696 | |
michael@0 | 697 | @patch.object(support, 'SomeClass', spec_set=SomeClass) |
michael@0 | 698 | def test(MockClass): |
michael@0 | 699 | MockClass.z = 'foo' |
michael@0 | 700 | |
michael@0 | 701 | self.assertRaises(AttributeError, test) |
michael@0 | 702 | @patch('%s.SomeClass' % __name__, spec_set=True) |
michael@0 | 703 | def test(MockClass): |
michael@0 | 704 | MockClass.z = 'foo' |
michael@0 | 705 | |
michael@0 | 706 | self.assertRaises(AttributeError, test) |
michael@0 | 707 | |
michael@0 | 708 | @patch.object(support, 'SomeClass', spec_set=True) |
michael@0 | 709 | def test(MockClass): |
michael@0 | 710 | MockClass.z = 'foo' |
michael@0 | 711 | |
michael@0 | 712 | self.assertRaises(AttributeError, test) |
michael@0 | 713 | |
michael@0 | 714 | |
michael@0 | 715 | def test_spec_set_inherit(self): |
michael@0 | 716 | @patch('%s.SomeClass' % __name__, spec_set=True) |
michael@0 | 717 | def test(MockClass): |
michael@0 | 718 | instance = MockClass() |
michael@0 | 719 | instance.z = 'foo' |
michael@0 | 720 | |
michael@0 | 721 | self.assertRaises(AttributeError, test) |
michael@0 | 722 | |
michael@0 | 723 | |
michael@0 | 724 | def test_patch_start_stop(self): |
michael@0 | 725 | original = something |
michael@0 | 726 | patcher = patch('%s.something' % __name__) |
michael@0 | 727 | self.assertIs(something, original) |
michael@0 | 728 | mock = patcher.start() |
michael@0 | 729 | try: |
michael@0 | 730 | self.assertIsNot(mock, original) |
michael@0 | 731 | self.assertIs(something, mock) |
michael@0 | 732 | finally: |
michael@0 | 733 | patcher.stop() |
michael@0 | 734 | self.assertIs(something, original) |
michael@0 | 735 | |
michael@0 | 736 | |
michael@0 | 737 | def test_stop_without_start(self): |
michael@0 | 738 | patcher = patch(foo_name, 'bar', 3) |
michael@0 | 739 | |
michael@0 | 740 | # calling stop without start used to produce a very obscure error |
michael@0 | 741 | self.assertRaises(RuntimeError, patcher.stop) |
michael@0 | 742 | |
michael@0 | 743 | |
michael@0 | 744 | def test_patchobject_start_stop(self): |
michael@0 | 745 | original = something |
michael@0 | 746 | patcher = patch.object(PTModule, 'something', 'foo') |
michael@0 | 747 | self.assertIs(something, original) |
michael@0 | 748 | replaced = patcher.start() |
michael@0 | 749 | try: |
michael@0 | 750 | self.assertEqual(replaced, 'foo') |
michael@0 | 751 | self.assertIs(something, replaced) |
michael@0 | 752 | finally: |
michael@0 | 753 | patcher.stop() |
michael@0 | 754 | self.assertIs(something, original) |
michael@0 | 755 | |
michael@0 | 756 | |
michael@0 | 757 | def test_patch_dict_start_stop(self): |
michael@0 | 758 | d = {'foo': 'bar'} |
michael@0 | 759 | original = d.copy() |
michael@0 | 760 | patcher = patch.dict(d, [('spam', 'eggs')], clear=True) |
michael@0 | 761 | self.assertEqual(d, original) |
michael@0 | 762 | |
michael@0 | 763 | patcher.start() |
michael@0 | 764 | try: |
michael@0 | 765 | self.assertEqual(d, {'spam': 'eggs'}) |
michael@0 | 766 | finally: |
michael@0 | 767 | patcher.stop() |
michael@0 | 768 | self.assertEqual(d, original) |
michael@0 | 769 | |
michael@0 | 770 | |
michael@0 | 771 | def test_patch_dict_class_decorator(self): |
michael@0 | 772 | this = self |
michael@0 | 773 | d = {'spam': 'eggs'} |
michael@0 | 774 | original = d.copy() |
michael@0 | 775 | |
michael@0 | 776 | class Test(object): |
michael@0 | 777 | def test_first(self): |
michael@0 | 778 | this.assertEqual(d, {'foo': 'bar'}) |
michael@0 | 779 | def test_second(self): |
michael@0 | 780 | this.assertEqual(d, {'foo': 'bar'}) |
michael@0 | 781 | |
michael@0 | 782 | Test = patch.dict(d, {'foo': 'bar'}, clear=True)(Test) |
michael@0 | 783 | self.assertEqual(d, original) |
michael@0 | 784 | |
michael@0 | 785 | test = Test() |
michael@0 | 786 | |
michael@0 | 787 | test.test_first() |
michael@0 | 788 | self.assertEqual(d, original) |
michael@0 | 789 | |
michael@0 | 790 | test.test_second() |
michael@0 | 791 | self.assertEqual(d, original) |
michael@0 | 792 | |
michael@0 | 793 | test = Test() |
michael@0 | 794 | |
michael@0 | 795 | test.test_first() |
michael@0 | 796 | self.assertEqual(d, original) |
michael@0 | 797 | |
michael@0 | 798 | test.test_second() |
michael@0 | 799 | self.assertEqual(d, original) |
michael@0 | 800 | |
michael@0 | 801 | |
michael@0 | 802 | def test_get_only_proxy(self): |
michael@0 | 803 | class Something(object): |
michael@0 | 804 | foo = 'foo' |
michael@0 | 805 | class SomethingElse: |
michael@0 | 806 | foo = 'foo' |
michael@0 | 807 | |
michael@0 | 808 | for thing in Something, SomethingElse, Something(), SomethingElse: |
michael@0 | 809 | proxy = _get_proxy(thing) |
michael@0 | 810 | |
michael@0 | 811 | @patch.object(proxy, 'foo', 'bar') |
michael@0 | 812 | def test(): |
michael@0 | 813 | self.assertEqual(proxy.foo, 'bar') |
michael@0 | 814 | test() |
michael@0 | 815 | self.assertEqual(proxy.foo, 'foo') |
michael@0 | 816 | self.assertEqual(thing.foo, 'foo') |
michael@0 | 817 | self.assertNotIn('foo', proxy.__dict__) |
michael@0 | 818 | |
michael@0 | 819 | |
michael@0 | 820 | def test_get_set_delete_proxy(self): |
michael@0 | 821 | class Something(object): |
michael@0 | 822 | foo = 'foo' |
michael@0 | 823 | class SomethingElse: |
michael@0 | 824 | foo = 'foo' |
michael@0 | 825 | |
michael@0 | 826 | for thing in Something, SomethingElse, Something(), SomethingElse: |
michael@0 | 827 | proxy = _get_proxy(Something, get_only=False) |
michael@0 | 828 | |
michael@0 | 829 | @patch.object(proxy, 'foo', 'bar') |
michael@0 | 830 | def test(): |
michael@0 | 831 | self.assertEqual(proxy.foo, 'bar') |
michael@0 | 832 | test() |
michael@0 | 833 | self.assertEqual(proxy.foo, 'foo') |
michael@0 | 834 | self.assertEqual(thing.foo, 'foo') |
michael@0 | 835 | self.assertNotIn('foo', proxy.__dict__) |
michael@0 | 836 | |
michael@0 | 837 | |
michael@0 | 838 | def test_patch_keyword_args(self): |
michael@0 | 839 | kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, |
michael@0 | 840 | 'foo': MagicMock()} |
michael@0 | 841 | |
michael@0 | 842 | patcher = patch(foo_name, **kwargs) |
michael@0 | 843 | mock = patcher.start() |
michael@0 | 844 | patcher.stop() |
michael@0 | 845 | |
michael@0 | 846 | self.assertRaises(KeyError, mock) |
michael@0 | 847 | self.assertEqual(mock.foo.bar(), 33) |
michael@0 | 848 | self.assertIsInstance(mock.foo, MagicMock) |
michael@0 | 849 | |
michael@0 | 850 | |
michael@0 | 851 | def test_patch_object_keyword_args(self): |
michael@0 | 852 | kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, |
michael@0 | 853 | 'foo': MagicMock()} |
michael@0 | 854 | |
michael@0 | 855 | patcher = patch.object(Foo, 'f', **kwargs) |
michael@0 | 856 | mock = patcher.start() |
michael@0 | 857 | patcher.stop() |
michael@0 | 858 | |
michael@0 | 859 | self.assertRaises(KeyError, mock) |
michael@0 | 860 | self.assertEqual(mock.foo.bar(), 33) |
michael@0 | 861 | self.assertIsInstance(mock.foo, MagicMock) |
michael@0 | 862 | |
michael@0 | 863 | |
michael@0 | 864 | def test_patch_dict_keyword_args(self): |
michael@0 | 865 | original = {'foo': 'bar'} |
michael@0 | 866 | copy = original.copy() |
michael@0 | 867 | |
michael@0 | 868 | patcher = patch.dict(original, foo=3, bar=4, baz=5) |
michael@0 | 869 | patcher.start() |
michael@0 | 870 | |
michael@0 | 871 | try: |
michael@0 | 872 | self.assertEqual(original, dict(foo=3, bar=4, baz=5)) |
michael@0 | 873 | finally: |
michael@0 | 874 | patcher.stop() |
michael@0 | 875 | |
michael@0 | 876 | self.assertEqual(original, copy) |
michael@0 | 877 | |
michael@0 | 878 | |
michael@0 | 879 | def test_autospec(self): |
michael@0 | 880 | class Boo(object): |
michael@0 | 881 | def __init__(self, a): |
michael@0 | 882 | pass |
michael@0 | 883 | def f(self, a): |
michael@0 | 884 | pass |
michael@0 | 885 | def g(self): |
michael@0 | 886 | pass |
michael@0 | 887 | foo = 'bar' |
michael@0 | 888 | |
michael@0 | 889 | class Bar(object): |
michael@0 | 890 | def a(self): |
michael@0 | 891 | pass |
michael@0 | 892 | |
michael@0 | 893 | def _test(mock): |
michael@0 | 894 | mock(1) |
michael@0 | 895 | mock.assert_called_with(1) |
michael@0 | 896 | self.assertRaises(TypeError, mock) |
michael@0 | 897 | |
michael@0 | 898 | def _test2(mock): |
michael@0 | 899 | mock.f(1) |
michael@0 | 900 | mock.f.assert_called_with(1) |
michael@0 | 901 | self.assertRaises(TypeError, mock.f) |
michael@0 | 902 | |
michael@0 | 903 | mock.g() |
michael@0 | 904 | mock.g.assert_called_with() |
michael@0 | 905 | self.assertRaises(TypeError, mock.g, 1) |
michael@0 | 906 | |
michael@0 | 907 | self.assertRaises(AttributeError, getattr, mock, 'h') |
michael@0 | 908 | |
michael@0 | 909 | mock.foo.lower() |
michael@0 | 910 | mock.foo.lower.assert_called_with() |
michael@0 | 911 | self.assertRaises(AttributeError, getattr, mock.foo, 'bar') |
michael@0 | 912 | |
michael@0 | 913 | mock.Bar() |
michael@0 | 914 | mock.Bar.assert_called_with() |
michael@0 | 915 | |
michael@0 | 916 | mock.Bar.a() |
michael@0 | 917 | mock.Bar.a.assert_called_with() |
michael@0 | 918 | self.assertRaises(TypeError, mock.Bar.a, 1) |
michael@0 | 919 | |
michael@0 | 920 | mock.Bar().a() |
michael@0 | 921 | mock.Bar().a.assert_called_with() |
michael@0 | 922 | self.assertRaises(TypeError, mock.Bar().a, 1) |
michael@0 | 923 | |
michael@0 | 924 | self.assertRaises(AttributeError, getattr, mock.Bar, 'b') |
michael@0 | 925 | self.assertRaises(AttributeError, getattr, mock.Bar(), 'b') |
michael@0 | 926 | |
michael@0 | 927 | def function(mock): |
michael@0 | 928 | _test(mock) |
michael@0 | 929 | _test2(mock) |
michael@0 | 930 | _test2(mock(1)) |
michael@0 | 931 | self.assertIs(mock, Foo) |
michael@0 | 932 | return mock |
michael@0 | 933 | |
michael@0 | 934 | test = patch(foo_name, autospec=True)(function) |
michael@0 | 935 | |
michael@0 | 936 | mock = test() |
michael@0 | 937 | self.assertIsNot(Foo, mock) |
michael@0 | 938 | # test patching a second time works |
michael@0 | 939 | test() |
michael@0 | 940 | |
michael@0 | 941 | module = sys.modules[__name__] |
michael@0 | 942 | test = patch.object(module, 'Foo', autospec=True)(function) |
michael@0 | 943 | |
michael@0 | 944 | mock = test() |
michael@0 | 945 | self.assertIsNot(Foo, mock) |
michael@0 | 946 | # test patching a second time works |
michael@0 | 947 | test() |
michael@0 | 948 | |
michael@0 | 949 | |
michael@0 | 950 | def test_autospec_function(self): |
michael@0 | 951 | @patch('%s.function' % __name__, autospec=True) |
michael@0 | 952 | def test(mock): |
michael@0 | 953 | function(1) |
michael@0 | 954 | function.assert_called_with(1) |
michael@0 | 955 | function(2, 3) |
michael@0 | 956 | function.assert_called_with(2, 3) |
michael@0 | 957 | |
michael@0 | 958 | self.assertRaises(TypeError, function) |
michael@0 | 959 | self.assertRaises(AttributeError, getattr, function, 'foo') |
michael@0 | 960 | |
michael@0 | 961 | test() |
michael@0 | 962 | |
michael@0 | 963 | |
michael@0 | 964 | def test_autospec_keywords(self): |
michael@0 | 965 | @patch('%s.function' % __name__, autospec=True, |
michael@0 | 966 | return_value=3) |
michael@0 | 967 | def test(mock_function): |
michael@0 | 968 | #self.assertEqual(function.abc, 'foo') |
michael@0 | 969 | return function(1, 2) |
michael@0 | 970 | |
michael@0 | 971 | result = test() |
michael@0 | 972 | self.assertEqual(result, 3) |
michael@0 | 973 | |
michael@0 | 974 | |
michael@0 | 975 | def test_autospec_with_new(self): |
michael@0 | 976 | patcher = patch('%s.function' % __name__, new=3, autospec=True) |
michael@0 | 977 | self.assertRaises(TypeError, patcher.start) |
michael@0 | 978 | |
michael@0 | 979 | module = sys.modules[__name__] |
michael@0 | 980 | patcher = patch.object(module, 'function', new=3, autospec=True) |
michael@0 | 981 | self.assertRaises(TypeError, patcher.start) |
michael@0 | 982 | |
michael@0 | 983 | |
michael@0 | 984 | def test_autospec_with_object(self): |
michael@0 | 985 | class Bar(Foo): |
michael@0 | 986 | extra = [] |
michael@0 | 987 | |
michael@0 | 988 | patcher = patch(foo_name, autospec=Bar) |
michael@0 | 989 | mock = patcher.start() |
michael@0 | 990 | try: |
michael@0 | 991 | self.assertIsInstance(mock, Bar) |
michael@0 | 992 | self.assertIsInstance(mock.extra, list) |
michael@0 | 993 | finally: |
michael@0 | 994 | patcher.stop() |
michael@0 | 995 | |
michael@0 | 996 | |
michael@0 | 997 | def test_autospec_inherits(self): |
michael@0 | 998 | FooClass = Foo |
michael@0 | 999 | patcher = patch(foo_name, autospec=True) |
michael@0 | 1000 | mock = patcher.start() |
michael@0 | 1001 | try: |
michael@0 | 1002 | self.assertIsInstance(mock, FooClass) |
michael@0 | 1003 | self.assertIsInstance(mock(3), FooClass) |
michael@0 | 1004 | finally: |
michael@0 | 1005 | patcher.stop() |
michael@0 | 1006 | |
michael@0 | 1007 | |
michael@0 | 1008 | def test_autospec_name(self): |
michael@0 | 1009 | patcher = patch(foo_name, autospec=True) |
michael@0 | 1010 | mock = patcher.start() |
michael@0 | 1011 | |
michael@0 | 1012 | try: |
michael@0 | 1013 | self.assertIn(" name='Foo'", repr(mock)) |
michael@0 | 1014 | self.assertIn(" name='Foo.f'", repr(mock.f)) |
michael@0 | 1015 | self.assertIn(" name='Foo()'", repr(mock(None))) |
michael@0 | 1016 | self.assertIn(" name='Foo().f'", repr(mock(None).f)) |
michael@0 | 1017 | finally: |
michael@0 | 1018 | patcher.stop() |
michael@0 | 1019 | |
michael@0 | 1020 | |
michael@0 | 1021 | def test_tracebacks(self): |
michael@0 | 1022 | @patch.object(Foo, 'f', object()) |
michael@0 | 1023 | def test(): |
michael@0 | 1024 | raise AssertionError |
michael@0 | 1025 | try: |
michael@0 | 1026 | test() |
michael@0 | 1027 | except: |
michael@0 | 1028 | err = sys.exc_info() |
michael@0 | 1029 | |
michael@0 | 1030 | result = unittest2.TextTestResult(None, None, 0) |
michael@0 | 1031 | traceback = result._exc_info_to_string(err, self) |
michael@0 | 1032 | self.assertIn('raise AssertionError', traceback) |
michael@0 | 1033 | |
michael@0 | 1034 | |
michael@0 | 1035 | def test_new_callable_patch(self): |
michael@0 | 1036 | patcher = patch(foo_name, new_callable=NonCallableMagicMock) |
michael@0 | 1037 | |
michael@0 | 1038 | m1 = patcher.start() |
michael@0 | 1039 | patcher.stop() |
michael@0 | 1040 | m2 = patcher.start() |
michael@0 | 1041 | patcher.stop() |
michael@0 | 1042 | |
michael@0 | 1043 | self.assertIsNot(m1, m2) |
michael@0 | 1044 | for mock in m1, m2: |
michael@0 | 1045 | self.assertNotCallable(m1) |
michael@0 | 1046 | |
michael@0 | 1047 | |
michael@0 | 1048 | def test_new_callable_patch_object(self): |
michael@0 | 1049 | patcher = patch.object(Foo, 'f', new_callable=NonCallableMagicMock) |
michael@0 | 1050 | |
michael@0 | 1051 | m1 = patcher.start() |
michael@0 | 1052 | patcher.stop() |
michael@0 | 1053 | m2 = patcher.start() |
michael@0 | 1054 | patcher.stop() |
michael@0 | 1055 | |
michael@0 | 1056 | self.assertIsNot(m1, m2) |
michael@0 | 1057 | for mock in m1, m2: |
michael@0 | 1058 | self.assertNotCallable(m1) |
michael@0 | 1059 | |
michael@0 | 1060 | |
michael@0 | 1061 | def test_new_callable_keyword_arguments(self): |
michael@0 | 1062 | class Bar(object): |
michael@0 | 1063 | kwargs = None |
michael@0 | 1064 | def __init__(self, **kwargs): |
michael@0 | 1065 | Bar.kwargs = kwargs |
michael@0 | 1066 | |
michael@0 | 1067 | patcher = patch(foo_name, new_callable=Bar, arg1=1, arg2=2) |
michael@0 | 1068 | m = patcher.start() |
michael@0 | 1069 | try: |
michael@0 | 1070 | self.assertIs(type(m), Bar) |
michael@0 | 1071 | self.assertEqual(Bar.kwargs, dict(arg1=1, arg2=2)) |
michael@0 | 1072 | finally: |
michael@0 | 1073 | patcher.stop() |
michael@0 | 1074 | |
michael@0 | 1075 | |
michael@0 | 1076 | def test_new_callable_spec(self): |
michael@0 | 1077 | class Bar(object): |
michael@0 | 1078 | kwargs = None |
michael@0 | 1079 | def __init__(self, **kwargs): |
michael@0 | 1080 | Bar.kwargs = kwargs |
michael@0 | 1081 | |
michael@0 | 1082 | patcher = patch(foo_name, new_callable=Bar, spec=Bar) |
michael@0 | 1083 | patcher.start() |
michael@0 | 1084 | try: |
michael@0 | 1085 | self.assertEqual(Bar.kwargs, dict(spec=Bar)) |
michael@0 | 1086 | finally: |
michael@0 | 1087 | patcher.stop() |
michael@0 | 1088 | |
michael@0 | 1089 | patcher = patch(foo_name, new_callable=Bar, spec_set=Bar) |
michael@0 | 1090 | patcher.start() |
michael@0 | 1091 | try: |
michael@0 | 1092 | self.assertEqual(Bar.kwargs, dict(spec_set=Bar)) |
michael@0 | 1093 | finally: |
michael@0 | 1094 | patcher.stop() |
michael@0 | 1095 | |
michael@0 | 1096 | |
michael@0 | 1097 | def test_new_callable_create(self): |
michael@0 | 1098 | non_existent_attr = '%s.weeeee' % foo_name |
michael@0 | 1099 | p = patch(non_existent_attr, new_callable=NonCallableMock) |
michael@0 | 1100 | self.assertRaises(AttributeError, p.start) |
michael@0 | 1101 | |
michael@0 | 1102 | p = patch(non_existent_attr, new_callable=NonCallableMock, |
michael@0 | 1103 | create=True) |
michael@0 | 1104 | m = p.start() |
michael@0 | 1105 | try: |
michael@0 | 1106 | self.assertNotCallable(m, magic=False) |
michael@0 | 1107 | finally: |
michael@0 | 1108 | p.stop() |
michael@0 | 1109 | |
michael@0 | 1110 | |
michael@0 | 1111 | def test_new_callable_incompatible_with_new(self): |
michael@0 | 1112 | self.assertRaises( |
michael@0 | 1113 | ValueError, patch, foo_name, new=object(), new_callable=MagicMock |
michael@0 | 1114 | ) |
michael@0 | 1115 | self.assertRaises( |
michael@0 | 1116 | ValueError, patch.object, Foo, 'f', new=object(), |
michael@0 | 1117 | new_callable=MagicMock |
michael@0 | 1118 | ) |
michael@0 | 1119 | |
michael@0 | 1120 | |
michael@0 | 1121 | def test_new_callable_incompatible_with_autospec(self): |
michael@0 | 1122 | self.assertRaises( |
michael@0 | 1123 | ValueError, patch, foo_name, new_callable=MagicMock, |
michael@0 | 1124 | autospec=True |
michael@0 | 1125 | ) |
michael@0 | 1126 | self.assertRaises( |
michael@0 | 1127 | ValueError, patch.object, Foo, 'f', new_callable=MagicMock, |
michael@0 | 1128 | autospec=True |
michael@0 | 1129 | ) |
michael@0 | 1130 | |
michael@0 | 1131 | |
michael@0 | 1132 | def test_new_callable_inherit_for_mocks(self): |
michael@0 | 1133 | class MockSub(Mock): |
michael@0 | 1134 | pass |
michael@0 | 1135 | |
michael@0 | 1136 | MockClasses = ( |
michael@0 | 1137 | NonCallableMock, NonCallableMagicMock, MagicMock, Mock, MockSub |
michael@0 | 1138 | ) |
michael@0 | 1139 | for Klass in MockClasses: |
michael@0 | 1140 | for arg in 'spec', 'spec_set': |
michael@0 | 1141 | kwargs = {arg: True} |
michael@0 | 1142 | p = patch(foo_name, new_callable=Klass, **kwargs) |
michael@0 | 1143 | m = p.start() |
michael@0 | 1144 | try: |
michael@0 | 1145 | instance = m.return_value |
michael@0 | 1146 | self.assertRaises(AttributeError, getattr, instance, 'x') |
michael@0 | 1147 | finally: |
michael@0 | 1148 | p.stop() |
michael@0 | 1149 | |
michael@0 | 1150 | |
michael@0 | 1151 | def test_new_callable_inherit_non_mock(self): |
michael@0 | 1152 | class NotAMock(object): |
michael@0 | 1153 | def __init__(self, spec): |
michael@0 | 1154 | self.spec = spec |
michael@0 | 1155 | |
michael@0 | 1156 | p = patch(foo_name, new_callable=NotAMock, spec=True) |
michael@0 | 1157 | m = p.start() |
michael@0 | 1158 | try: |
michael@0 | 1159 | self.assertTrue(is_instance(m, NotAMock)) |
michael@0 | 1160 | self.assertRaises(AttributeError, getattr, m, 'return_value') |
michael@0 | 1161 | finally: |
michael@0 | 1162 | p.stop() |
michael@0 | 1163 | |
michael@0 | 1164 | self.assertEqual(m.spec, Foo) |
michael@0 | 1165 | |
michael@0 | 1166 | |
michael@0 | 1167 | def test_new_callable_class_decorating(self): |
michael@0 | 1168 | test = self |
michael@0 | 1169 | original = Foo |
michael@0 | 1170 | class SomeTest(object): |
michael@0 | 1171 | |
michael@0 | 1172 | def _test(self, mock_foo): |
michael@0 | 1173 | test.assertIsNot(Foo, original) |
michael@0 | 1174 | test.assertIs(Foo, mock_foo) |
michael@0 | 1175 | test.assertIsInstance(Foo, SomeClass) |
michael@0 | 1176 | |
michael@0 | 1177 | def test_two(self, mock_foo): |
michael@0 | 1178 | self._test(mock_foo) |
michael@0 | 1179 | def test_one(self, mock_foo): |
michael@0 | 1180 | self._test(mock_foo) |
michael@0 | 1181 | |
michael@0 | 1182 | SomeTest = patch(foo_name, new_callable=SomeClass)(SomeTest) |
michael@0 | 1183 | SomeTest().test_one() |
michael@0 | 1184 | SomeTest().test_two() |
michael@0 | 1185 | self.assertIs(Foo, original) |
michael@0 | 1186 | |
michael@0 | 1187 | |
michael@0 | 1188 | def test_patch_multiple(self): |
michael@0 | 1189 | original_foo = Foo |
michael@0 | 1190 | original_f = Foo.f |
michael@0 | 1191 | original_g = Foo.g |
michael@0 | 1192 | |
michael@0 | 1193 | patcher1 = patch.multiple(foo_name, f=1, g=2) |
michael@0 | 1194 | patcher2 = patch.multiple(Foo, f=1, g=2) |
michael@0 | 1195 | |
michael@0 | 1196 | for patcher in patcher1, patcher2: |
michael@0 | 1197 | patcher.start() |
michael@0 | 1198 | try: |
michael@0 | 1199 | self.assertIs(Foo, original_foo) |
michael@0 | 1200 | self.assertEqual(Foo.f, 1) |
michael@0 | 1201 | self.assertEqual(Foo.g, 2) |
michael@0 | 1202 | finally: |
michael@0 | 1203 | patcher.stop() |
michael@0 | 1204 | |
michael@0 | 1205 | self.assertIs(Foo, original_foo) |
michael@0 | 1206 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1207 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1208 | |
michael@0 | 1209 | |
michael@0 | 1210 | @patch.multiple(foo_name, f=3, g=4) |
michael@0 | 1211 | def test(): |
michael@0 | 1212 | self.assertIs(Foo, original_foo) |
michael@0 | 1213 | self.assertEqual(Foo.f, 3) |
michael@0 | 1214 | self.assertEqual(Foo.g, 4) |
michael@0 | 1215 | |
michael@0 | 1216 | test() |
michael@0 | 1217 | |
michael@0 | 1218 | |
michael@0 | 1219 | def test_patch_multiple_no_kwargs(self): |
michael@0 | 1220 | self.assertRaises(ValueError, patch.multiple, foo_name) |
michael@0 | 1221 | self.assertRaises(ValueError, patch.multiple, Foo) |
michael@0 | 1222 | |
michael@0 | 1223 | |
michael@0 | 1224 | def test_patch_multiple_create_mocks(self): |
michael@0 | 1225 | original_foo = Foo |
michael@0 | 1226 | original_f = Foo.f |
michael@0 | 1227 | original_g = Foo.g |
michael@0 | 1228 | |
michael@0 | 1229 | @patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT) |
michael@0 | 1230 | def test(f, foo): |
michael@0 | 1231 | self.assertIs(Foo, original_foo) |
michael@0 | 1232 | self.assertIs(Foo.f, f) |
michael@0 | 1233 | self.assertEqual(Foo.g, 3) |
michael@0 | 1234 | self.assertIs(Foo.foo, foo) |
michael@0 | 1235 | self.assertTrue(is_instance(f, MagicMock)) |
michael@0 | 1236 | self.assertTrue(is_instance(foo, MagicMock)) |
michael@0 | 1237 | |
michael@0 | 1238 | test() |
michael@0 | 1239 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1240 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1241 | |
michael@0 | 1242 | |
michael@0 | 1243 | def test_patch_multiple_create_mocks_different_order(self): |
michael@0 | 1244 | # bug revealed by Jython! |
michael@0 | 1245 | original_f = Foo.f |
michael@0 | 1246 | original_g = Foo.g |
michael@0 | 1247 | |
michael@0 | 1248 | patcher = patch.object(Foo, 'f', 3) |
michael@0 | 1249 | patcher.attribute_name = 'f' |
michael@0 | 1250 | |
michael@0 | 1251 | other = patch.object(Foo, 'g', DEFAULT) |
michael@0 | 1252 | other.attribute_name = 'g' |
michael@0 | 1253 | patcher.additional_patchers = [other] |
michael@0 | 1254 | |
michael@0 | 1255 | @patcher |
michael@0 | 1256 | def test(g): |
michael@0 | 1257 | self.assertIs(Foo.g, g) |
michael@0 | 1258 | self.assertEqual(Foo.f, 3) |
michael@0 | 1259 | |
michael@0 | 1260 | test() |
michael@0 | 1261 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1262 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1263 | |
michael@0 | 1264 | |
michael@0 | 1265 | def test_patch_multiple_stacked_decorators(self): |
michael@0 | 1266 | original_foo = Foo |
michael@0 | 1267 | original_f = Foo.f |
michael@0 | 1268 | original_g = Foo.g |
michael@0 | 1269 | |
michael@0 | 1270 | @patch.multiple(foo_name, f=DEFAULT) |
michael@0 | 1271 | @patch.multiple(foo_name, foo=DEFAULT) |
michael@0 | 1272 | @patch(foo_name + '.g') |
michael@0 | 1273 | def test1(g, **kwargs): |
michael@0 | 1274 | _test(g, **kwargs) |
michael@0 | 1275 | |
michael@0 | 1276 | @patch.multiple(foo_name, f=DEFAULT) |
michael@0 | 1277 | @patch(foo_name + '.g') |
michael@0 | 1278 | @patch.multiple(foo_name, foo=DEFAULT) |
michael@0 | 1279 | def test2(g, **kwargs): |
michael@0 | 1280 | _test(g, **kwargs) |
michael@0 | 1281 | |
michael@0 | 1282 | @patch(foo_name + '.g') |
michael@0 | 1283 | @patch.multiple(foo_name, f=DEFAULT) |
michael@0 | 1284 | @patch.multiple(foo_name, foo=DEFAULT) |
michael@0 | 1285 | def test3(g, **kwargs): |
michael@0 | 1286 | _test(g, **kwargs) |
michael@0 | 1287 | |
michael@0 | 1288 | def _test(g, **kwargs): |
michael@0 | 1289 | f = kwargs.pop('f') |
michael@0 | 1290 | foo = kwargs.pop('foo') |
michael@0 | 1291 | self.assertFalse(kwargs) |
michael@0 | 1292 | |
michael@0 | 1293 | self.assertIs(Foo, original_foo) |
michael@0 | 1294 | self.assertIs(Foo.f, f) |
michael@0 | 1295 | self.assertIs(Foo.g, g) |
michael@0 | 1296 | self.assertIs(Foo.foo, foo) |
michael@0 | 1297 | self.assertTrue(is_instance(f, MagicMock)) |
michael@0 | 1298 | self.assertTrue(is_instance(g, MagicMock)) |
michael@0 | 1299 | self.assertTrue(is_instance(foo, MagicMock)) |
michael@0 | 1300 | |
michael@0 | 1301 | test1() |
michael@0 | 1302 | test2() |
michael@0 | 1303 | test3() |
michael@0 | 1304 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1305 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1306 | |
michael@0 | 1307 | |
michael@0 | 1308 | def test_patch_multiple_create_mocks_patcher(self): |
michael@0 | 1309 | original_foo = Foo |
michael@0 | 1310 | original_f = Foo.f |
michael@0 | 1311 | original_g = Foo.g |
michael@0 | 1312 | |
michael@0 | 1313 | patcher = patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT) |
michael@0 | 1314 | |
michael@0 | 1315 | result = patcher.start() |
michael@0 | 1316 | try: |
michael@0 | 1317 | f = result['f'] |
michael@0 | 1318 | foo = result['foo'] |
michael@0 | 1319 | self.assertEqual(set(result), set(['f', 'foo'])) |
michael@0 | 1320 | |
michael@0 | 1321 | self.assertIs(Foo, original_foo) |
michael@0 | 1322 | self.assertIs(Foo.f, f) |
michael@0 | 1323 | self.assertIs(Foo.foo, foo) |
michael@0 | 1324 | self.assertTrue(is_instance(f, MagicMock)) |
michael@0 | 1325 | self.assertTrue(is_instance(foo, MagicMock)) |
michael@0 | 1326 | finally: |
michael@0 | 1327 | patcher.stop() |
michael@0 | 1328 | |
michael@0 | 1329 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1330 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1331 | |
michael@0 | 1332 | |
michael@0 | 1333 | def test_patch_multiple_decorating_class(self): |
michael@0 | 1334 | test = self |
michael@0 | 1335 | original_foo = Foo |
michael@0 | 1336 | original_f = Foo.f |
michael@0 | 1337 | original_g = Foo.g |
michael@0 | 1338 | |
michael@0 | 1339 | class SomeTest(object): |
michael@0 | 1340 | |
michael@0 | 1341 | def _test(self, f, foo): |
michael@0 | 1342 | test.assertIs(Foo, original_foo) |
michael@0 | 1343 | test.assertIs(Foo.f, f) |
michael@0 | 1344 | test.assertEqual(Foo.g, 3) |
michael@0 | 1345 | test.assertIs(Foo.foo, foo) |
michael@0 | 1346 | test.assertTrue(is_instance(f, MagicMock)) |
michael@0 | 1347 | test.assertTrue(is_instance(foo, MagicMock)) |
michael@0 | 1348 | |
michael@0 | 1349 | def test_two(self, f, foo): |
michael@0 | 1350 | self._test(f, foo) |
michael@0 | 1351 | def test_one(self, f, foo): |
michael@0 | 1352 | self._test(f, foo) |
michael@0 | 1353 | |
michael@0 | 1354 | SomeTest = patch.multiple( |
michael@0 | 1355 | foo_name, f=DEFAULT, g=3, foo=DEFAULT |
michael@0 | 1356 | )(SomeTest) |
michael@0 | 1357 | |
michael@0 | 1358 | thing = SomeTest() |
michael@0 | 1359 | thing.test_one() |
michael@0 | 1360 | thing.test_two() |
michael@0 | 1361 | |
michael@0 | 1362 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1363 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1364 | |
michael@0 | 1365 | |
michael@0 | 1366 | def test_patch_multiple_create(self): |
michael@0 | 1367 | patcher = patch.multiple(Foo, blam='blam') |
michael@0 | 1368 | self.assertRaises(AttributeError, patcher.start) |
michael@0 | 1369 | |
michael@0 | 1370 | patcher = patch.multiple(Foo, blam='blam', create=True) |
michael@0 | 1371 | patcher.start() |
michael@0 | 1372 | try: |
michael@0 | 1373 | self.assertEqual(Foo.blam, 'blam') |
michael@0 | 1374 | finally: |
michael@0 | 1375 | patcher.stop() |
michael@0 | 1376 | |
michael@0 | 1377 | self.assertFalse(hasattr(Foo, 'blam')) |
michael@0 | 1378 | |
michael@0 | 1379 | |
michael@0 | 1380 | def test_patch_multiple_spec_set(self): |
michael@0 | 1381 | # if spec_set works then we can assume that spec and autospec also |
michael@0 | 1382 | # work as the underlying machinery is the same |
michael@0 | 1383 | patcher = patch.multiple(Foo, foo=DEFAULT, spec_set=['a', 'b']) |
michael@0 | 1384 | result = patcher.start() |
michael@0 | 1385 | try: |
michael@0 | 1386 | self.assertEqual(Foo.foo, result['foo']) |
michael@0 | 1387 | Foo.foo.a(1) |
michael@0 | 1388 | Foo.foo.b(2) |
michael@0 | 1389 | Foo.foo.a.assert_called_with(1) |
michael@0 | 1390 | Foo.foo.b.assert_called_with(2) |
michael@0 | 1391 | self.assertRaises(AttributeError, setattr, Foo.foo, 'c', None) |
michael@0 | 1392 | finally: |
michael@0 | 1393 | patcher.stop() |
michael@0 | 1394 | |
michael@0 | 1395 | |
michael@0 | 1396 | def test_patch_multiple_new_callable(self): |
michael@0 | 1397 | class Thing(object): |
michael@0 | 1398 | pass |
michael@0 | 1399 | |
michael@0 | 1400 | patcher = patch.multiple( |
michael@0 | 1401 | Foo, f=DEFAULT, g=DEFAULT, new_callable=Thing |
michael@0 | 1402 | ) |
michael@0 | 1403 | result = patcher.start() |
michael@0 | 1404 | try: |
michael@0 | 1405 | self.assertIs(Foo.f, result['f']) |
michael@0 | 1406 | self.assertIs(Foo.g, result['g']) |
michael@0 | 1407 | self.assertIsInstance(Foo.f, Thing) |
michael@0 | 1408 | self.assertIsInstance(Foo.g, Thing) |
michael@0 | 1409 | self.assertIsNot(Foo.f, Foo.g) |
michael@0 | 1410 | finally: |
michael@0 | 1411 | patcher.stop() |
michael@0 | 1412 | |
michael@0 | 1413 | |
michael@0 | 1414 | def test_nested_patch_failure(self): |
michael@0 | 1415 | original_f = Foo.f |
michael@0 | 1416 | original_g = Foo.g |
michael@0 | 1417 | |
michael@0 | 1418 | @patch.object(Foo, 'g', 1) |
michael@0 | 1419 | @patch.object(Foo, 'missing', 1) |
michael@0 | 1420 | @patch.object(Foo, 'f', 1) |
michael@0 | 1421 | def thing1(): |
michael@0 | 1422 | pass |
michael@0 | 1423 | |
michael@0 | 1424 | @patch.object(Foo, 'missing', 1) |
michael@0 | 1425 | @patch.object(Foo, 'g', 1) |
michael@0 | 1426 | @patch.object(Foo, 'f', 1) |
michael@0 | 1427 | def thing2(): |
michael@0 | 1428 | pass |
michael@0 | 1429 | |
michael@0 | 1430 | @patch.object(Foo, 'g', 1) |
michael@0 | 1431 | @patch.object(Foo, 'f', 1) |
michael@0 | 1432 | @patch.object(Foo, 'missing', 1) |
michael@0 | 1433 | def thing3(): |
michael@0 | 1434 | pass |
michael@0 | 1435 | |
michael@0 | 1436 | for func in thing1, thing2, thing3: |
michael@0 | 1437 | self.assertRaises(AttributeError, func) |
michael@0 | 1438 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1439 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1440 | |
michael@0 | 1441 | |
michael@0 | 1442 | def test_new_callable_failure(self): |
michael@0 | 1443 | original_f = Foo.f |
michael@0 | 1444 | original_g = Foo.g |
michael@0 | 1445 | original_foo = Foo.foo |
michael@0 | 1446 | |
michael@0 | 1447 | def crasher(): |
michael@0 | 1448 | raise NameError('crasher') |
michael@0 | 1449 | |
michael@0 | 1450 | @patch.object(Foo, 'g', 1) |
michael@0 | 1451 | @patch.object(Foo, 'foo', new_callable=crasher) |
michael@0 | 1452 | @patch.object(Foo, 'f', 1) |
michael@0 | 1453 | def thing1(): |
michael@0 | 1454 | pass |
michael@0 | 1455 | |
michael@0 | 1456 | @patch.object(Foo, 'foo', new_callable=crasher) |
michael@0 | 1457 | @patch.object(Foo, 'g', 1) |
michael@0 | 1458 | @patch.object(Foo, 'f', 1) |
michael@0 | 1459 | def thing2(): |
michael@0 | 1460 | pass |
michael@0 | 1461 | |
michael@0 | 1462 | @patch.object(Foo, 'g', 1) |
michael@0 | 1463 | @patch.object(Foo, 'f', 1) |
michael@0 | 1464 | @patch.object(Foo, 'foo', new_callable=crasher) |
michael@0 | 1465 | def thing3(): |
michael@0 | 1466 | pass |
michael@0 | 1467 | |
michael@0 | 1468 | for func in thing1, thing2, thing3: |
michael@0 | 1469 | self.assertRaises(NameError, func) |
michael@0 | 1470 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1471 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1472 | self.assertEqual(Foo.foo, original_foo) |
michael@0 | 1473 | |
michael@0 | 1474 | |
michael@0 | 1475 | def test_patch_multiple_failure(self): |
michael@0 | 1476 | original_f = Foo.f |
michael@0 | 1477 | original_g = Foo.g |
michael@0 | 1478 | |
michael@0 | 1479 | patcher = patch.object(Foo, 'f', 1) |
michael@0 | 1480 | patcher.attribute_name = 'f' |
michael@0 | 1481 | |
michael@0 | 1482 | good = patch.object(Foo, 'g', 1) |
michael@0 | 1483 | good.attribute_name = 'g' |
michael@0 | 1484 | |
michael@0 | 1485 | bad = patch.object(Foo, 'missing', 1) |
michael@0 | 1486 | bad.attribute_name = 'missing' |
michael@0 | 1487 | |
michael@0 | 1488 | for additionals in [good, bad], [bad, good]: |
michael@0 | 1489 | patcher.additional_patchers = additionals |
michael@0 | 1490 | |
michael@0 | 1491 | @patcher |
michael@0 | 1492 | def func(): |
michael@0 | 1493 | pass |
michael@0 | 1494 | |
michael@0 | 1495 | self.assertRaises(AttributeError, func) |
michael@0 | 1496 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1497 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1498 | |
michael@0 | 1499 | |
michael@0 | 1500 | def test_patch_multiple_new_callable_failure(self): |
michael@0 | 1501 | original_f = Foo.f |
michael@0 | 1502 | original_g = Foo.g |
michael@0 | 1503 | original_foo = Foo.foo |
michael@0 | 1504 | |
michael@0 | 1505 | def crasher(): |
michael@0 | 1506 | raise NameError('crasher') |
michael@0 | 1507 | |
michael@0 | 1508 | patcher = patch.object(Foo, 'f', 1) |
michael@0 | 1509 | patcher.attribute_name = 'f' |
michael@0 | 1510 | |
michael@0 | 1511 | good = patch.object(Foo, 'g', 1) |
michael@0 | 1512 | good.attribute_name = 'g' |
michael@0 | 1513 | |
michael@0 | 1514 | bad = patch.object(Foo, 'foo', new_callable=crasher) |
michael@0 | 1515 | bad.attribute_name = 'foo' |
michael@0 | 1516 | |
michael@0 | 1517 | for additionals in [good, bad], [bad, good]: |
michael@0 | 1518 | patcher.additional_patchers = additionals |
michael@0 | 1519 | |
michael@0 | 1520 | @patcher |
michael@0 | 1521 | def func(): |
michael@0 | 1522 | pass |
michael@0 | 1523 | |
michael@0 | 1524 | self.assertRaises(NameError, func) |
michael@0 | 1525 | self.assertEqual(Foo.f, original_f) |
michael@0 | 1526 | self.assertEqual(Foo.g, original_g) |
michael@0 | 1527 | self.assertEqual(Foo.foo, original_foo) |
michael@0 | 1528 | |
michael@0 | 1529 | |
michael@0 | 1530 | def test_patch_multiple_string_subclasses(self): |
michael@0 | 1531 | for base in (str, unicode): |
michael@0 | 1532 | Foo = type('Foo', (base,), {'fish': 'tasty'}) |
michael@0 | 1533 | foo = Foo() |
michael@0 | 1534 | @patch.multiple(foo, fish='nearly gone') |
michael@0 | 1535 | def test(): |
michael@0 | 1536 | self.assertEqual(foo.fish, 'nearly gone') |
michael@0 | 1537 | |
michael@0 | 1538 | test() |
michael@0 | 1539 | self.assertEqual(foo.fish, 'tasty') |
michael@0 | 1540 | |
michael@0 | 1541 | |
michael@0 | 1542 | @patch('mock.patch.TEST_PREFIX', 'foo') |
michael@0 | 1543 | def test_patch_test_prefix(self): |
michael@0 | 1544 | class Foo(object): |
michael@0 | 1545 | thing = 'original' |
michael@0 | 1546 | |
michael@0 | 1547 | def foo_one(self): |
michael@0 | 1548 | return self.thing |
michael@0 | 1549 | def foo_two(self): |
michael@0 | 1550 | return self.thing |
michael@0 | 1551 | def test_one(self): |
michael@0 | 1552 | return self.thing |
michael@0 | 1553 | def test_two(self): |
michael@0 | 1554 | return self.thing |
michael@0 | 1555 | |
michael@0 | 1556 | Foo = patch.object(Foo, 'thing', 'changed')(Foo) |
michael@0 | 1557 | |
michael@0 | 1558 | foo = Foo() |
michael@0 | 1559 | self.assertEqual(foo.foo_one(), 'changed') |
michael@0 | 1560 | self.assertEqual(foo.foo_two(), 'changed') |
michael@0 | 1561 | self.assertEqual(foo.test_one(), 'original') |
michael@0 | 1562 | self.assertEqual(foo.test_two(), 'original') |
michael@0 | 1563 | |
michael@0 | 1564 | |
michael@0 | 1565 | @patch('mock.patch.TEST_PREFIX', 'bar') |
michael@0 | 1566 | def test_patch_dict_test_prefix(self): |
michael@0 | 1567 | class Foo(object): |
michael@0 | 1568 | def bar_one(self): |
michael@0 | 1569 | return dict(the_dict) |
michael@0 | 1570 | def bar_two(self): |
michael@0 | 1571 | return dict(the_dict) |
michael@0 | 1572 | def test_one(self): |
michael@0 | 1573 | return dict(the_dict) |
michael@0 | 1574 | def test_two(self): |
michael@0 | 1575 | return dict(the_dict) |
michael@0 | 1576 | |
michael@0 | 1577 | the_dict = {'key': 'original'} |
michael@0 | 1578 | Foo = patch.dict(the_dict, key='changed')(Foo) |
michael@0 | 1579 | |
michael@0 | 1580 | foo =Foo() |
michael@0 | 1581 | self.assertEqual(foo.bar_one(), {'key': 'changed'}) |
michael@0 | 1582 | self.assertEqual(foo.bar_two(), {'key': 'changed'}) |
michael@0 | 1583 | self.assertEqual(foo.test_one(), {'key': 'original'}) |
michael@0 | 1584 | self.assertEqual(foo.test_two(), {'key': 'original'}) |
michael@0 | 1585 | |
michael@0 | 1586 | |
michael@0 | 1587 | def test_patch_with_spec_mock_repr(self): |
michael@0 | 1588 | for arg in ('spec', 'autospec', 'spec_set'): |
michael@0 | 1589 | p = patch('%s.SomeClass' % __name__, **{arg: True}) |
michael@0 | 1590 | m = p.start() |
michael@0 | 1591 | try: |
michael@0 | 1592 | self.assertIn(" name='SomeClass'", repr(m)) |
michael@0 | 1593 | self.assertIn(" name='SomeClass.class_attribute'", |
michael@0 | 1594 | repr(m.class_attribute)) |
michael@0 | 1595 | self.assertIn(" name='SomeClass()'", repr(m())) |
michael@0 | 1596 | self.assertIn(" name='SomeClass().class_attribute'", |
michael@0 | 1597 | repr(m().class_attribute)) |
michael@0 | 1598 | finally: |
michael@0 | 1599 | p.stop() |
michael@0 | 1600 | |
michael@0 | 1601 | |
michael@0 | 1602 | def test_patch_nested_autospec_repr(self): |
michael@0 | 1603 | p = patch('tests.support', autospec=True) |
michael@0 | 1604 | m = p.start() |
michael@0 | 1605 | try: |
michael@0 | 1606 | self.assertIn(" name='support.SomeClass.wibble()'", |
michael@0 | 1607 | repr(m.SomeClass.wibble())) |
michael@0 | 1608 | self.assertIn(" name='support.SomeClass().wibble()'", |
michael@0 | 1609 | repr(m.SomeClass().wibble())) |
michael@0 | 1610 | finally: |
michael@0 | 1611 | p.stop() |
michael@0 | 1612 | |
michael@0 | 1613 | |
michael@0 | 1614 | def test_mock_calls_with_patch(self): |
michael@0 | 1615 | for arg in ('spec', 'autospec', 'spec_set'): |
michael@0 | 1616 | p = patch('%s.SomeClass' % __name__, **{arg: True}) |
michael@0 | 1617 | m = p.start() |
michael@0 | 1618 | try: |
michael@0 | 1619 | m.wibble() |
michael@0 | 1620 | |
michael@0 | 1621 | kalls = [call.wibble()] |
michael@0 | 1622 | self.assertEqual(m.mock_calls, kalls) |
michael@0 | 1623 | self.assertEqual(m.method_calls, kalls) |
michael@0 | 1624 | self.assertEqual(m.wibble.mock_calls, [call()]) |
michael@0 | 1625 | |
michael@0 | 1626 | result = m() |
michael@0 | 1627 | kalls.append(call()) |
michael@0 | 1628 | self.assertEqual(m.mock_calls, kalls) |
michael@0 | 1629 | |
michael@0 | 1630 | result.wibble() |
michael@0 | 1631 | kalls.append(call().wibble()) |
michael@0 | 1632 | self.assertEqual(m.mock_calls, kalls) |
michael@0 | 1633 | |
michael@0 | 1634 | self.assertEqual(result.mock_calls, [call.wibble()]) |
michael@0 | 1635 | self.assertEqual(result.wibble.mock_calls, [call()]) |
michael@0 | 1636 | self.assertEqual(result.method_calls, [call.wibble()]) |
michael@0 | 1637 | finally: |
michael@0 | 1638 | p.stop() |
michael@0 | 1639 | |
michael@0 | 1640 | |
michael@0 | 1641 | def test_patch_imports_lazily(self): |
michael@0 | 1642 | sys.modules.pop('squizz', None) |
michael@0 | 1643 | |
michael@0 | 1644 | p1 = patch('squizz.squozz') |
michael@0 | 1645 | self.assertRaises(ImportError, p1.start) |
michael@0 | 1646 | |
michael@0 | 1647 | squizz = Mock() |
michael@0 | 1648 | squizz.squozz = 6 |
michael@0 | 1649 | sys.modules['squizz'] = squizz |
michael@0 | 1650 | p1 = patch('squizz.squozz') |
michael@0 | 1651 | squizz.squozz = 3 |
michael@0 | 1652 | p1.start() |
michael@0 | 1653 | p1.stop() |
michael@0 | 1654 | self.assertEqual(squizz.squozz, 3) |
michael@0 | 1655 | |
michael@0 | 1656 | |
michael@0 | 1657 | def test_patch_propogrates_exc_on_exit(self): |
michael@0 | 1658 | class holder: |
michael@0 | 1659 | exc_info = None, None, None |
michael@0 | 1660 | |
michael@0 | 1661 | class custom_patch(_patch): |
michael@0 | 1662 | def __exit__(self, etype=None, val=None, tb=None): |
michael@0 | 1663 | _patch.__exit__(self, etype, val, tb) |
michael@0 | 1664 | holder.exc_info = etype, val, tb |
michael@0 | 1665 | stop = __exit__ |
michael@0 | 1666 | |
michael@0 | 1667 | def with_custom_patch(target): |
michael@0 | 1668 | getter, attribute = _get_target(target) |
michael@0 | 1669 | return custom_patch( |
michael@0 | 1670 | getter, attribute, DEFAULT, None, False, None, |
michael@0 | 1671 | None, None, {} |
michael@0 | 1672 | ) |
michael@0 | 1673 | |
michael@0 | 1674 | @with_custom_patch('squizz.squozz') |
michael@0 | 1675 | def test(mock): |
michael@0 | 1676 | raise RuntimeError |
michael@0 | 1677 | |
michael@0 | 1678 | self.assertRaises(RuntimeError, test) |
michael@0 | 1679 | self.assertIs(holder.exc_info[0], RuntimeError) |
michael@0 | 1680 | self.assertIsNotNone(holder.exc_info[1], |
michael@0 | 1681 | 'exception value not propgated') |
michael@0 | 1682 | self.assertIsNotNone(holder.exc_info[2], |
michael@0 | 1683 | 'exception traceback not propgated') |
michael@0 | 1684 | |
michael@0 | 1685 | |
michael@0 | 1686 | def test_create_and_specs(self): |
michael@0 | 1687 | for kwarg in ('spec', 'spec_set', 'autospec'): |
michael@0 | 1688 | p = patch('%s.doesnotexist' % __name__, create=True, |
michael@0 | 1689 | **{kwarg: True}) |
michael@0 | 1690 | self.assertRaises(TypeError, p.start) |
michael@0 | 1691 | self.assertRaises(NameError, lambda: doesnotexist) |
michael@0 | 1692 | |
michael@0 | 1693 | # check that spec with create is innocuous if the original exists |
michael@0 | 1694 | p = patch(MODNAME, create=True, **{kwarg: True}) |
michael@0 | 1695 | p.start() |
michael@0 | 1696 | p.stop() |
michael@0 | 1697 | |
michael@0 | 1698 | |
michael@0 | 1699 | def test_multiple_specs(self): |
michael@0 | 1700 | original = PTModule |
michael@0 | 1701 | for kwarg in ('spec', 'spec_set'): |
michael@0 | 1702 | p = patch(MODNAME, autospec=0, **{kwarg: 0}) |
michael@0 | 1703 | self.assertRaises(TypeError, p.start) |
michael@0 | 1704 | self.assertIs(PTModule, original) |
michael@0 | 1705 | |
michael@0 | 1706 | for kwarg in ('spec', 'autospec'): |
michael@0 | 1707 | p = patch(MODNAME, spec_set=0, **{kwarg: 0}) |
michael@0 | 1708 | self.assertRaises(TypeError, p.start) |
michael@0 | 1709 | self.assertIs(PTModule, original) |
michael@0 | 1710 | |
michael@0 | 1711 | for kwarg in ('spec_set', 'autospec'): |
michael@0 | 1712 | p = patch(MODNAME, spec=0, **{kwarg: 0}) |
michael@0 | 1713 | self.assertRaises(TypeError, p.start) |
michael@0 | 1714 | self.assertIs(PTModule, original) |
michael@0 | 1715 | |
michael@0 | 1716 | |
michael@0 | 1717 | def test_specs_false_instead_of_none(self): |
michael@0 | 1718 | p = patch(MODNAME, spec=False, spec_set=False, autospec=False) |
michael@0 | 1719 | mock = p.start() |
michael@0 | 1720 | try: |
michael@0 | 1721 | # no spec should have been set, so attribute access should not fail |
michael@0 | 1722 | mock.does_not_exist |
michael@0 | 1723 | mock.does_not_exist = 3 |
michael@0 | 1724 | finally: |
michael@0 | 1725 | p.stop() |
michael@0 | 1726 | |
michael@0 | 1727 | |
michael@0 | 1728 | def test_falsey_spec(self): |
michael@0 | 1729 | for kwarg in ('spec', 'autospec', 'spec_set'): |
michael@0 | 1730 | p = patch(MODNAME, **{kwarg: 0}) |
michael@0 | 1731 | m = p.start() |
michael@0 | 1732 | try: |
michael@0 | 1733 | self.assertRaises(AttributeError, getattr, m, 'doesnotexit') |
michael@0 | 1734 | finally: |
michael@0 | 1735 | p.stop() |
michael@0 | 1736 | |
michael@0 | 1737 | |
michael@0 | 1738 | def test_spec_set_true(self): |
michael@0 | 1739 | for kwarg in ('spec', 'autospec'): |
michael@0 | 1740 | p = patch(MODNAME, spec_set=True, **{kwarg: True}) |
michael@0 | 1741 | m = p.start() |
michael@0 | 1742 | try: |
michael@0 | 1743 | self.assertRaises(AttributeError, setattr, m, |
michael@0 | 1744 | 'doesnotexist', 'something') |
michael@0 | 1745 | self.assertRaises(AttributeError, getattr, m, 'doesnotexist') |
michael@0 | 1746 | finally: |
michael@0 | 1747 | p.stop() |
michael@0 | 1748 | |
michael@0 | 1749 | |
michael@0 | 1750 | def test_callable_spec_as_list(self): |
michael@0 | 1751 | spec = ('__call__',) |
michael@0 | 1752 | p = patch(MODNAME, spec=spec) |
michael@0 | 1753 | m = p.start() |
michael@0 | 1754 | try: |
michael@0 | 1755 | self.assertTrue(callable(m)) |
michael@0 | 1756 | finally: |
michael@0 | 1757 | p.stop() |
michael@0 | 1758 | |
michael@0 | 1759 | |
michael@0 | 1760 | def test_not_callable_spec_as_list(self): |
michael@0 | 1761 | spec = ('foo', 'bar') |
michael@0 | 1762 | p = patch(MODNAME, spec=spec) |
michael@0 | 1763 | m = p.start() |
michael@0 | 1764 | try: |
michael@0 | 1765 | self.assertFalse(callable(m)) |
michael@0 | 1766 | finally: |
michael@0 | 1767 | p.stop() |
michael@0 | 1768 | |
michael@0 | 1769 | |
michael@0 | 1770 | def test_patch_stopall(self): |
michael@0 | 1771 | unlink = os.unlink |
michael@0 | 1772 | chdir = os.chdir |
michael@0 | 1773 | path = os.path |
michael@0 | 1774 | patch('os.unlink', something).start() |
michael@0 | 1775 | patch('os.chdir', something_else).start() |
michael@0 | 1776 | |
michael@0 | 1777 | @patch('os.path') |
michael@0 | 1778 | def patched(mock_path): |
michael@0 | 1779 | patch.stopall() |
michael@0 | 1780 | self.assertIs(os.path, mock_path) |
michael@0 | 1781 | self.assertIs(os.unlink, unlink) |
michael@0 | 1782 | self.assertIs(os.chdir, chdir) |
michael@0 | 1783 | |
michael@0 | 1784 | patched() |
michael@0 | 1785 | self.assertIs(os.path, path) |
michael@0 | 1786 | |
michael@0 | 1787 | |
michael@0 | 1788 | |
michael@0 | 1789 | if __name__ == '__main__': |
michael@0 | 1790 | unittest2.main() |