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