1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/python/mock-1.0.0/tests/testpatch.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1790 @@ 1.4 +# Copyright (C) 2007-2012 Michael Foord & the mock team 1.5 +# E-mail: fuzzyman AT voidspace DOT org DOT uk 1.6 +# http://www.voidspace.org.uk/python/mock/ 1.7 + 1.8 +import os 1.9 +import sys 1.10 + 1.11 +from tests import support 1.12 +from tests.support import unittest2, inPy3k, SomeClass, is_instance, callable 1.13 + 1.14 +from mock import ( 1.15 + NonCallableMock, CallableMixin, patch, sentinel, 1.16 + MagicMock, Mock, NonCallableMagicMock, patch, _patch, 1.17 + DEFAULT, call, _get_target 1.18 +) 1.19 + 1.20 +builtin_string = '__builtin__' 1.21 +if inPy3k: 1.22 + builtin_string = 'builtins' 1.23 + unicode = str 1.24 + 1.25 +PTModule = sys.modules[__name__] 1.26 +MODNAME = '%s.PTModule' % __name__ 1.27 + 1.28 + 1.29 +def _get_proxy(obj, get_only=True): 1.30 + class Proxy(object): 1.31 + def __getattr__(self, name): 1.32 + return getattr(obj, name) 1.33 + if not get_only: 1.34 + def __setattr__(self, name, value): 1.35 + setattr(obj, name, value) 1.36 + def __delattr__(self, name): 1.37 + delattr(obj, name) 1.38 + Proxy.__setattr__ = __setattr__ 1.39 + Proxy.__delattr__ = __delattr__ 1.40 + return Proxy() 1.41 + 1.42 + 1.43 +# for use in the test 1.44 +something = sentinel.Something 1.45 +something_else = sentinel.SomethingElse 1.46 + 1.47 + 1.48 +class Foo(object): 1.49 + def __init__(self, a): 1.50 + pass 1.51 + def f(self, a): 1.52 + pass 1.53 + def g(self): 1.54 + pass 1.55 + foo = 'bar' 1.56 + 1.57 + class Bar(object): 1.58 + def a(self): 1.59 + pass 1.60 + 1.61 +foo_name = '%s.Foo' % __name__ 1.62 + 1.63 + 1.64 +def function(a, b=Foo): 1.65 + pass 1.66 + 1.67 + 1.68 +class Container(object): 1.69 + def __init__(self): 1.70 + self.values = {} 1.71 + 1.72 + def __getitem__(self, name): 1.73 + return self.values[name] 1.74 + 1.75 + def __setitem__(self, name, value): 1.76 + self.values[name] = value 1.77 + 1.78 + def __delitem__(self, name): 1.79 + del self.values[name] 1.80 + 1.81 + def __iter__(self): 1.82 + return iter(self.values) 1.83 + 1.84 + 1.85 + 1.86 +class PatchTest(unittest2.TestCase): 1.87 + 1.88 + def assertNotCallable(self, obj, magic=True): 1.89 + MockClass = NonCallableMagicMock 1.90 + if not magic: 1.91 + MockClass = NonCallableMock 1.92 + 1.93 + self.assertRaises(TypeError, obj) 1.94 + self.assertTrue(is_instance(obj, MockClass)) 1.95 + self.assertFalse(is_instance(obj, CallableMixin)) 1.96 + 1.97 + 1.98 + def test_single_patchobject(self): 1.99 + class Something(object): 1.100 + attribute = sentinel.Original 1.101 + 1.102 + @patch.object(Something, 'attribute', sentinel.Patched) 1.103 + def test(): 1.104 + self.assertEqual(Something.attribute, sentinel.Patched, "unpatched") 1.105 + 1.106 + test() 1.107 + self.assertEqual(Something.attribute, sentinel.Original, 1.108 + "patch not restored") 1.109 + 1.110 + 1.111 + def test_patchobject_with_none(self): 1.112 + class Something(object): 1.113 + attribute = sentinel.Original 1.114 + 1.115 + @patch.object(Something, 'attribute', None) 1.116 + def test(): 1.117 + self.assertIsNone(Something.attribute, "unpatched") 1.118 + 1.119 + test() 1.120 + self.assertEqual(Something.attribute, sentinel.Original, 1.121 + "patch not restored") 1.122 + 1.123 + 1.124 + def test_multiple_patchobject(self): 1.125 + class Something(object): 1.126 + attribute = sentinel.Original 1.127 + next_attribute = sentinel.Original2 1.128 + 1.129 + @patch.object(Something, 'attribute', sentinel.Patched) 1.130 + @patch.object(Something, 'next_attribute', sentinel.Patched2) 1.131 + def test(): 1.132 + self.assertEqual(Something.attribute, sentinel.Patched, 1.133 + "unpatched") 1.134 + self.assertEqual(Something.next_attribute, sentinel.Patched2, 1.135 + "unpatched") 1.136 + 1.137 + test() 1.138 + self.assertEqual(Something.attribute, sentinel.Original, 1.139 + "patch not restored") 1.140 + self.assertEqual(Something.next_attribute, sentinel.Original2, 1.141 + "patch not restored") 1.142 + 1.143 + 1.144 + def test_object_lookup_is_quite_lazy(self): 1.145 + global something 1.146 + original = something 1.147 + @patch('%s.something' % __name__, sentinel.Something2) 1.148 + def test(): 1.149 + pass 1.150 + 1.151 + try: 1.152 + something = sentinel.replacement_value 1.153 + test() 1.154 + self.assertEqual(something, sentinel.replacement_value) 1.155 + finally: 1.156 + something = original 1.157 + 1.158 + 1.159 + def test_patch(self): 1.160 + @patch('%s.something' % __name__, sentinel.Something2) 1.161 + def test(): 1.162 + self.assertEqual(PTModule.something, sentinel.Something2, 1.163 + "unpatched") 1.164 + 1.165 + test() 1.166 + self.assertEqual(PTModule.something, sentinel.Something, 1.167 + "patch not restored") 1.168 + 1.169 + @patch('%s.something' % __name__, sentinel.Something2) 1.170 + @patch('%s.something_else' % __name__, sentinel.SomethingElse) 1.171 + def test(): 1.172 + self.assertEqual(PTModule.something, sentinel.Something2, 1.173 + "unpatched") 1.174 + self.assertEqual(PTModule.something_else, sentinel.SomethingElse, 1.175 + "unpatched") 1.176 + 1.177 + self.assertEqual(PTModule.something, sentinel.Something, 1.178 + "patch not restored") 1.179 + self.assertEqual(PTModule.something_else, sentinel.SomethingElse, 1.180 + "patch not restored") 1.181 + 1.182 + # Test the patching and restoring works a second time 1.183 + test() 1.184 + 1.185 + self.assertEqual(PTModule.something, sentinel.Something, 1.186 + "patch not restored") 1.187 + self.assertEqual(PTModule.something_else, sentinel.SomethingElse, 1.188 + "patch not restored") 1.189 + 1.190 + mock = Mock() 1.191 + mock.return_value = sentinel.Handle 1.192 + @patch('%s.open' % builtin_string, mock) 1.193 + def test(): 1.194 + self.assertEqual(open('filename', 'r'), sentinel.Handle, 1.195 + "open not patched") 1.196 + test() 1.197 + test() 1.198 + 1.199 + self.assertNotEqual(open, mock, "patch not restored") 1.200 + 1.201 + 1.202 + def test_patch_class_attribute(self): 1.203 + @patch('%s.SomeClass.class_attribute' % __name__, 1.204 + sentinel.ClassAttribute) 1.205 + def test(): 1.206 + self.assertEqual(PTModule.SomeClass.class_attribute, 1.207 + sentinel.ClassAttribute, "unpatched") 1.208 + test() 1.209 + 1.210 + self.assertIsNone(PTModule.SomeClass.class_attribute, 1.211 + "patch not restored") 1.212 + 1.213 + 1.214 + def test_patchobject_with_default_mock(self): 1.215 + class Test(object): 1.216 + something = sentinel.Original 1.217 + something2 = sentinel.Original2 1.218 + 1.219 + @patch.object(Test, 'something') 1.220 + def test(mock): 1.221 + self.assertEqual(mock, Test.something, 1.222 + "Mock not passed into test function") 1.223 + self.assertIsInstance(mock, MagicMock, 1.224 + "patch with two arguments did not create a mock") 1.225 + 1.226 + test() 1.227 + 1.228 + @patch.object(Test, 'something') 1.229 + @patch.object(Test, 'something2') 1.230 + def test(this1, this2, mock1, mock2): 1.231 + self.assertEqual(this1, sentinel.this1, 1.232 + "Patched function didn't receive initial argument") 1.233 + self.assertEqual(this2, sentinel.this2, 1.234 + "Patched function didn't receive second argument") 1.235 + self.assertEqual(mock1, Test.something2, 1.236 + "Mock not passed into test function") 1.237 + self.assertEqual(mock2, Test.something, 1.238 + "Second Mock not passed into test function") 1.239 + self.assertIsInstance(mock2, MagicMock, 1.240 + "patch with two arguments did not create a mock") 1.241 + self.assertIsInstance(mock2, MagicMock, 1.242 + "patch with two arguments did not create a mock") 1.243 + 1.244 + # A hack to test that new mocks are passed the second time 1.245 + self.assertNotEqual(outerMock1, mock1, "unexpected value for mock1") 1.246 + self.assertNotEqual(outerMock2, mock2, "unexpected value for mock1") 1.247 + return mock1, mock2 1.248 + 1.249 + outerMock1 = outerMock2 = None 1.250 + outerMock1, outerMock2 = test(sentinel.this1, sentinel.this2) 1.251 + 1.252 + # Test that executing a second time creates new mocks 1.253 + test(sentinel.this1, sentinel.this2) 1.254 + 1.255 + 1.256 + def test_patch_with_spec(self): 1.257 + @patch('%s.SomeClass' % __name__, spec=SomeClass) 1.258 + def test(MockSomeClass): 1.259 + self.assertEqual(SomeClass, MockSomeClass) 1.260 + self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) 1.261 + self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) 1.262 + 1.263 + test() 1.264 + 1.265 + 1.266 + def test_patchobject_with_spec(self): 1.267 + @patch.object(SomeClass, 'class_attribute', spec=SomeClass) 1.268 + def test(MockAttribute): 1.269 + self.assertEqual(SomeClass.class_attribute, MockAttribute) 1.270 + self.assertTrue(is_instance(SomeClass.class_attribute.wibble, 1.271 + MagicMock)) 1.272 + self.assertRaises(AttributeError, 1.273 + lambda: SomeClass.class_attribute.not_wibble) 1.274 + 1.275 + test() 1.276 + 1.277 + 1.278 + def test_patch_with_spec_as_list(self): 1.279 + @patch('%s.SomeClass' % __name__, spec=['wibble']) 1.280 + def test(MockSomeClass): 1.281 + self.assertEqual(SomeClass, MockSomeClass) 1.282 + self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) 1.283 + self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) 1.284 + 1.285 + test() 1.286 + 1.287 + 1.288 + def test_patchobject_with_spec_as_list(self): 1.289 + @patch.object(SomeClass, 'class_attribute', spec=['wibble']) 1.290 + def test(MockAttribute): 1.291 + self.assertEqual(SomeClass.class_attribute, MockAttribute) 1.292 + self.assertTrue(is_instance(SomeClass.class_attribute.wibble, 1.293 + MagicMock)) 1.294 + self.assertRaises(AttributeError, 1.295 + lambda: SomeClass.class_attribute.not_wibble) 1.296 + 1.297 + test() 1.298 + 1.299 + 1.300 + def test_nested_patch_with_spec_as_list(self): 1.301 + # regression test for nested decorators 1.302 + @patch('%s.open' % builtin_string) 1.303 + @patch('%s.SomeClass' % __name__, spec=['wibble']) 1.304 + def test(MockSomeClass, MockOpen): 1.305 + self.assertEqual(SomeClass, MockSomeClass) 1.306 + self.assertTrue(is_instance(SomeClass.wibble, MagicMock)) 1.307 + self.assertRaises(AttributeError, lambda: SomeClass.not_wibble) 1.308 + test() 1.309 + 1.310 + 1.311 + def test_patch_with_spec_as_boolean(self): 1.312 + @patch('%s.SomeClass' % __name__, spec=True) 1.313 + def test(MockSomeClass): 1.314 + self.assertEqual(SomeClass, MockSomeClass) 1.315 + # Should not raise attribute error 1.316 + MockSomeClass.wibble 1.317 + 1.318 + self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble) 1.319 + 1.320 + test() 1.321 + 1.322 + 1.323 + def test_patch_object_with_spec_as_boolean(self): 1.324 + @patch.object(PTModule, 'SomeClass', spec=True) 1.325 + def test(MockSomeClass): 1.326 + self.assertEqual(SomeClass, MockSomeClass) 1.327 + # Should not raise attribute error 1.328 + MockSomeClass.wibble 1.329 + 1.330 + self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble) 1.331 + 1.332 + test() 1.333 + 1.334 + 1.335 + def test_patch_class_acts_with_spec_is_inherited(self): 1.336 + @patch('%s.SomeClass' % __name__, spec=True) 1.337 + def test(MockSomeClass): 1.338 + self.assertTrue(is_instance(MockSomeClass, MagicMock)) 1.339 + instance = MockSomeClass() 1.340 + self.assertNotCallable(instance) 1.341 + # Should not raise attribute error 1.342 + instance.wibble 1.343 + 1.344 + self.assertRaises(AttributeError, lambda: instance.not_wibble) 1.345 + 1.346 + test() 1.347 + 1.348 + 1.349 + def test_patch_with_create_mocks_non_existent_attributes(self): 1.350 + @patch('%s.frooble' % builtin_string, sentinel.Frooble, create=True) 1.351 + def test(): 1.352 + self.assertEqual(frooble, sentinel.Frooble) 1.353 + 1.354 + test() 1.355 + self.assertRaises(NameError, lambda: frooble) 1.356 + 1.357 + 1.358 + def test_patchobject_with_create_mocks_non_existent_attributes(self): 1.359 + @patch.object(SomeClass, 'frooble', sentinel.Frooble, create=True) 1.360 + def test(): 1.361 + self.assertEqual(SomeClass.frooble, sentinel.Frooble) 1.362 + 1.363 + test() 1.364 + self.assertFalse(hasattr(SomeClass, 'frooble')) 1.365 + 1.366 + 1.367 + def test_patch_wont_create_by_default(self): 1.368 + try: 1.369 + @patch('%s.frooble' % builtin_string, sentinel.Frooble) 1.370 + def test(): 1.371 + self.assertEqual(frooble, sentinel.Frooble) 1.372 + 1.373 + test() 1.374 + except AttributeError: 1.375 + pass 1.376 + else: 1.377 + self.fail('Patching non existent attributes should fail') 1.378 + 1.379 + self.assertRaises(NameError, lambda: frooble) 1.380 + 1.381 + 1.382 + def test_patchobject_wont_create_by_default(self): 1.383 + try: 1.384 + @patch.object(SomeClass, 'frooble', sentinel.Frooble) 1.385 + def test(): 1.386 + self.fail('Patching non existent attributes should fail') 1.387 + 1.388 + test() 1.389 + except AttributeError: 1.390 + pass 1.391 + else: 1.392 + self.fail('Patching non existent attributes should fail') 1.393 + self.assertFalse(hasattr(SomeClass, 'frooble')) 1.394 + 1.395 + 1.396 + def test_patch_with_static_methods(self): 1.397 + class Foo(object): 1.398 + @staticmethod 1.399 + def woot(): 1.400 + return sentinel.Static 1.401 + 1.402 + @patch.object(Foo, 'woot', staticmethod(lambda: sentinel.Patched)) 1.403 + def anonymous(): 1.404 + self.assertEqual(Foo.woot(), sentinel.Patched) 1.405 + anonymous() 1.406 + 1.407 + self.assertEqual(Foo.woot(), sentinel.Static) 1.408 + 1.409 + 1.410 + def test_patch_local(self): 1.411 + foo = sentinel.Foo 1.412 + @patch.object(sentinel, 'Foo', 'Foo') 1.413 + def anonymous(): 1.414 + self.assertEqual(sentinel.Foo, 'Foo') 1.415 + anonymous() 1.416 + 1.417 + self.assertEqual(sentinel.Foo, foo) 1.418 + 1.419 + 1.420 + def test_patch_slots(self): 1.421 + class Foo(object): 1.422 + __slots__ = ('Foo',) 1.423 + 1.424 + foo = Foo() 1.425 + foo.Foo = sentinel.Foo 1.426 + 1.427 + @patch.object(foo, 'Foo', 'Foo') 1.428 + def anonymous(): 1.429 + self.assertEqual(foo.Foo, 'Foo') 1.430 + anonymous() 1.431 + 1.432 + self.assertEqual(foo.Foo, sentinel.Foo) 1.433 + 1.434 + 1.435 + def test_patchobject_class_decorator(self): 1.436 + class Something(object): 1.437 + attribute = sentinel.Original 1.438 + 1.439 + class Foo(object): 1.440 + def test_method(other_self): 1.441 + self.assertEqual(Something.attribute, sentinel.Patched, 1.442 + "unpatched") 1.443 + def not_test_method(other_self): 1.444 + self.assertEqual(Something.attribute, sentinel.Original, 1.445 + "non-test method patched") 1.446 + 1.447 + Foo = patch.object(Something, 'attribute', sentinel.Patched)(Foo) 1.448 + 1.449 + f = Foo() 1.450 + f.test_method() 1.451 + f.not_test_method() 1.452 + 1.453 + self.assertEqual(Something.attribute, sentinel.Original, 1.454 + "patch not restored") 1.455 + 1.456 + 1.457 + def test_patch_class_decorator(self): 1.458 + class Something(object): 1.459 + attribute = sentinel.Original 1.460 + 1.461 + class Foo(object): 1.462 + def test_method(other_self, mock_something): 1.463 + self.assertEqual(PTModule.something, mock_something, 1.464 + "unpatched") 1.465 + def not_test_method(other_self): 1.466 + self.assertEqual(PTModule.something, sentinel.Something, 1.467 + "non-test method patched") 1.468 + Foo = patch('%s.something' % __name__)(Foo) 1.469 + 1.470 + f = Foo() 1.471 + f.test_method() 1.472 + f.not_test_method() 1.473 + 1.474 + self.assertEqual(Something.attribute, sentinel.Original, 1.475 + "patch not restored") 1.476 + self.assertEqual(PTModule.something, sentinel.Something, 1.477 + "patch not restored") 1.478 + 1.479 + 1.480 + def test_patchobject_twice(self): 1.481 + class Something(object): 1.482 + attribute = sentinel.Original 1.483 + next_attribute = sentinel.Original2 1.484 + 1.485 + @patch.object(Something, 'attribute', sentinel.Patched) 1.486 + @patch.object(Something, 'attribute', sentinel.Patched) 1.487 + def test(): 1.488 + self.assertEqual(Something.attribute, sentinel.Patched, "unpatched") 1.489 + 1.490 + test() 1.491 + 1.492 + self.assertEqual(Something.attribute, sentinel.Original, 1.493 + "patch not restored") 1.494 + 1.495 + 1.496 + def test_patch_dict(self): 1.497 + foo = {'initial': object(), 'other': 'something'} 1.498 + original = foo.copy() 1.499 + 1.500 + @patch.dict(foo) 1.501 + def test(): 1.502 + foo['a'] = 3 1.503 + del foo['initial'] 1.504 + foo['other'] = 'something else' 1.505 + 1.506 + test() 1.507 + 1.508 + self.assertEqual(foo, original) 1.509 + 1.510 + @patch.dict(foo, {'a': 'b'}) 1.511 + def test(): 1.512 + self.assertEqual(len(foo), 3) 1.513 + self.assertEqual(foo['a'], 'b') 1.514 + 1.515 + test() 1.516 + 1.517 + self.assertEqual(foo, original) 1.518 + 1.519 + @patch.dict(foo, [('a', 'b')]) 1.520 + def test(): 1.521 + self.assertEqual(len(foo), 3) 1.522 + self.assertEqual(foo['a'], 'b') 1.523 + 1.524 + test() 1.525 + 1.526 + self.assertEqual(foo, original) 1.527 + 1.528 + 1.529 + def test_patch_dict_with_container_object(self): 1.530 + foo = Container() 1.531 + foo['initial'] = object() 1.532 + foo['other'] = 'something' 1.533 + 1.534 + original = foo.values.copy() 1.535 + 1.536 + @patch.dict(foo) 1.537 + def test(): 1.538 + foo['a'] = 3 1.539 + del foo['initial'] 1.540 + foo['other'] = 'something else' 1.541 + 1.542 + test() 1.543 + 1.544 + self.assertEqual(foo.values, original) 1.545 + 1.546 + @patch.dict(foo, {'a': 'b'}) 1.547 + def test(): 1.548 + self.assertEqual(len(foo.values), 3) 1.549 + self.assertEqual(foo['a'], 'b') 1.550 + 1.551 + test() 1.552 + 1.553 + self.assertEqual(foo.values, original) 1.554 + 1.555 + 1.556 + def test_patch_dict_with_clear(self): 1.557 + foo = {'initial': object(), 'other': 'something'} 1.558 + original = foo.copy() 1.559 + 1.560 + @patch.dict(foo, clear=True) 1.561 + def test(): 1.562 + self.assertEqual(foo, {}) 1.563 + foo['a'] = 3 1.564 + foo['other'] = 'something else' 1.565 + 1.566 + test() 1.567 + 1.568 + self.assertEqual(foo, original) 1.569 + 1.570 + @patch.dict(foo, {'a': 'b'}, clear=True) 1.571 + def test(): 1.572 + self.assertEqual(foo, {'a': 'b'}) 1.573 + 1.574 + test() 1.575 + 1.576 + self.assertEqual(foo, original) 1.577 + 1.578 + @patch.dict(foo, [('a', 'b')], clear=True) 1.579 + def test(): 1.580 + self.assertEqual(foo, {'a': 'b'}) 1.581 + 1.582 + test() 1.583 + 1.584 + self.assertEqual(foo, original) 1.585 + 1.586 + 1.587 + def test_patch_dict_with_container_object_and_clear(self): 1.588 + foo = Container() 1.589 + foo['initial'] = object() 1.590 + foo['other'] = 'something' 1.591 + 1.592 + original = foo.values.copy() 1.593 + 1.594 + @patch.dict(foo, clear=True) 1.595 + def test(): 1.596 + self.assertEqual(foo.values, {}) 1.597 + foo['a'] = 3 1.598 + foo['other'] = 'something else' 1.599 + 1.600 + test() 1.601 + 1.602 + self.assertEqual(foo.values, original) 1.603 + 1.604 + @patch.dict(foo, {'a': 'b'}, clear=True) 1.605 + def test(): 1.606 + self.assertEqual(foo.values, {'a': 'b'}) 1.607 + 1.608 + test() 1.609 + 1.610 + self.assertEqual(foo.values, original) 1.611 + 1.612 + 1.613 + def test_name_preserved(self): 1.614 + foo = {} 1.615 + 1.616 + @patch('%s.SomeClass' % __name__, object()) 1.617 + @patch('%s.SomeClass' % __name__, object(), autospec=True) 1.618 + @patch.object(SomeClass, object()) 1.619 + @patch.dict(foo) 1.620 + def some_name(): 1.621 + pass 1.622 + 1.623 + self.assertEqual(some_name.__name__, 'some_name') 1.624 + 1.625 + 1.626 + def test_patch_with_exception(self): 1.627 + foo = {} 1.628 + 1.629 + @patch.dict(foo, {'a': 'b'}) 1.630 + def test(): 1.631 + raise NameError('Konrad') 1.632 + try: 1.633 + test() 1.634 + except NameError: 1.635 + pass 1.636 + else: 1.637 + self.fail('NameError not raised by test') 1.638 + 1.639 + self.assertEqual(foo, {}) 1.640 + 1.641 + 1.642 + def test_patch_dict_with_string(self): 1.643 + @patch.dict('os.environ', {'konrad_delong': 'some value'}) 1.644 + def test(): 1.645 + self.assertIn('konrad_delong', os.environ) 1.646 + 1.647 + test() 1.648 + 1.649 + 1.650 + @unittest2.expectedFailure 1.651 + def test_patch_descriptor(self): 1.652 + # would be some effort to fix this - we could special case the 1.653 + # builtin descriptors: classmethod, property, staticmethod 1.654 + class Nothing(object): 1.655 + foo = None 1.656 + 1.657 + class Something(object): 1.658 + foo = {} 1.659 + 1.660 + @patch.object(Nothing, 'foo', 2) 1.661 + @classmethod 1.662 + def klass(cls): 1.663 + self.assertIs(cls, Something) 1.664 + 1.665 + @patch.object(Nothing, 'foo', 2) 1.666 + @staticmethod 1.667 + def static(arg): 1.668 + return arg 1.669 + 1.670 + @patch.dict(foo) 1.671 + @classmethod 1.672 + def klass_dict(cls): 1.673 + self.assertIs(cls, Something) 1.674 + 1.675 + @patch.dict(foo) 1.676 + @staticmethod 1.677 + def static_dict(arg): 1.678 + return arg 1.679 + 1.680 + # these will raise exceptions if patching descriptors is broken 1.681 + self.assertEqual(Something.static('f00'), 'f00') 1.682 + Something.klass() 1.683 + self.assertEqual(Something.static_dict('f00'), 'f00') 1.684 + Something.klass_dict() 1.685 + 1.686 + something = Something() 1.687 + self.assertEqual(something.static('f00'), 'f00') 1.688 + something.klass() 1.689 + self.assertEqual(something.static_dict('f00'), 'f00') 1.690 + something.klass_dict() 1.691 + 1.692 + 1.693 + def test_patch_spec_set(self): 1.694 + @patch('%s.SomeClass' % __name__, spec_set=SomeClass) 1.695 + def test(MockClass): 1.696 + MockClass.z = 'foo' 1.697 + 1.698 + self.assertRaises(AttributeError, test) 1.699 + 1.700 + @patch.object(support, 'SomeClass', spec_set=SomeClass) 1.701 + def test(MockClass): 1.702 + MockClass.z = 'foo' 1.703 + 1.704 + self.assertRaises(AttributeError, test) 1.705 + @patch('%s.SomeClass' % __name__, spec_set=True) 1.706 + def test(MockClass): 1.707 + MockClass.z = 'foo' 1.708 + 1.709 + self.assertRaises(AttributeError, test) 1.710 + 1.711 + @patch.object(support, 'SomeClass', spec_set=True) 1.712 + def test(MockClass): 1.713 + MockClass.z = 'foo' 1.714 + 1.715 + self.assertRaises(AttributeError, test) 1.716 + 1.717 + 1.718 + def test_spec_set_inherit(self): 1.719 + @patch('%s.SomeClass' % __name__, spec_set=True) 1.720 + def test(MockClass): 1.721 + instance = MockClass() 1.722 + instance.z = 'foo' 1.723 + 1.724 + self.assertRaises(AttributeError, test) 1.725 + 1.726 + 1.727 + def test_patch_start_stop(self): 1.728 + original = something 1.729 + patcher = patch('%s.something' % __name__) 1.730 + self.assertIs(something, original) 1.731 + mock = patcher.start() 1.732 + try: 1.733 + self.assertIsNot(mock, original) 1.734 + self.assertIs(something, mock) 1.735 + finally: 1.736 + patcher.stop() 1.737 + self.assertIs(something, original) 1.738 + 1.739 + 1.740 + def test_stop_without_start(self): 1.741 + patcher = patch(foo_name, 'bar', 3) 1.742 + 1.743 + # calling stop without start used to produce a very obscure error 1.744 + self.assertRaises(RuntimeError, patcher.stop) 1.745 + 1.746 + 1.747 + def test_patchobject_start_stop(self): 1.748 + original = something 1.749 + patcher = patch.object(PTModule, 'something', 'foo') 1.750 + self.assertIs(something, original) 1.751 + replaced = patcher.start() 1.752 + try: 1.753 + self.assertEqual(replaced, 'foo') 1.754 + self.assertIs(something, replaced) 1.755 + finally: 1.756 + patcher.stop() 1.757 + self.assertIs(something, original) 1.758 + 1.759 + 1.760 + def test_patch_dict_start_stop(self): 1.761 + d = {'foo': 'bar'} 1.762 + original = d.copy() 1.763 + patcher = patch.dict(d, [('spam', 'eggs')], clear=True) 1.764 + self.assertEqual(d, original) 1.765 + 1.766 + patcher.start() 1.767 + try: 1.768 + self.assertEqual(d, {'spam': 'eggs'}) 1.769 + finally: 1.770 + patcher.stop() 1.771 + self.assertEqual(d, original) 1.772 + 1.773 + 1.774 + def test_patch_dict_class_decorator(self): 1.775 + this = self 1.776 + d = {'spam': 'eggs'} 1.777 + original = d.copy() 1.778 + 1.779 + class Test(object): 1.780 + def test_first(self): 1.781 + this.assertEqual(d, {'foo': 'bar'}) 1.782 + def test_second(self): 1.783 + this.assertEqual(d, {'foo': 'bar'}) 1.784 + 1.785 + Test = patch.dict(d, {'foo': 'bar'}, clear=True)(Test) 1.786 + self.assertEqual(d, original) 1.787 + 1.788 + test = Test() 1.789 + 1.790 + test.test_first() 1.791 + self.assertEqual(d, original) 1.792 + 1.793 + test.test_second() 1.794 + self.assertEqual(d, original) 1.795 + 1.796 + test = Test() 1.797 + 1.798 + test.test_first() 1.799 + self.assertEqual(d, original) 1.800 + 1.801 + test.test_second() 1.802 + self.assertEqual(d, original) 1.803 + 1.804 + 1.805 + def test_get_only_proxy(self): 1.806 + class Something(object): 1.807 + foo = 'foo' 1.808 + class SomethingElse: 1.809 + foo = 'foo' 1.810 + 1.811 + for thing in Something, SomethingElse, Something(), SomethingElse: 1.812 + proxy = _get_proxy(thing) 1.813 + 1.814 + @patch.object(proxy, 'foo', 'bar') 1.815 + def test(): 1.816 + self.assertEqual(proxy.foo, 'bar') 1.817 + test() 1.818 + self.assertEqual(proxy.foo, 'foo') 1.819 + self.assertEqual(thing.foo, 'foo') 1.820 + self.assertNotIn('foo', proxy.__dict__) 1.821 + 1.822 + 1.823 + def test_get_set_delete_proxy(self): 1.824 + class Something(object): 1.825 + foo = 'foo' 1.826 + class SomethingElse: 1.827 + foo = 'foo' 1.828 + 1.829 + for thing in Something, SomethingElse, Something(), SomethingElse: 1.830 + proxy = _get_proxy(Something, get_only=False) 1.831 + 1.832 + @patch.object(proxy, 'foo', 'bar') 1.833 + def test(): 1.834 + self.assertEqual(proxy.foo, 'bar') 1.835 + test() 1.836 + self.assertEqual(proxy.foo, 'foo') 1.837 + self.assertEqual(thing.foo, 'foo') 1.838 + self.assertNotIn('foo', proxy.__dict__) 1.839 + 1.840 + 1.841 + def test_patch_keyword_args(self): 1.842 + kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, 1.843 + 'foo': MagicMock()} 1.844 + 1.845 + patcher = patch(foo_name, **kwargs) 1.846 + mock = patcher.start() 1.847 + patcher.stop() 1.848 + 1.849 + self.assertRaises(KeyError, mock) 1.850 + self.assertEqual(mock.foo.bar(), 33) 1.851 + self.assertIsInstance(mock.foo, MagicMock) 1.852 + 1.853 + 1.854 + def test_patch_object_keyword_args(self): 1.855 + kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33, 1.856 + 'foo': MagicMock()} 1.857 + 1.858 + patcher = patch.object(Foo, 'f', **kwargs) 1.859 + mock = patcher.start() 1.860 + patcher.stop() 1.861 + 1.862 + self.assertRaises(KeyError, mock) 1.863 + self.assertEqual(mock.foo.bar(), 33) 1.864 + self.assertIsInstance(mock.foo, MagicMock) 1.865 + 1.866 + 1.867 + def test_patch_dict_keyword_args(self): 1.868 + original = {'foo': 'bar'} 1.869 + copy = original.copy() 1.870 + 1.871 + patcher = patch.dict(original, foo=3, bar=4, baz=5) 1.872 + patcher.start() 1.873 + 1.874 + try: 1.875 + self.assertEqual(original, dict(foo=3, bar=4, baz=5)) 1.876 + finally: 1.877 + patcher.stop() 1.878 + 1.879 + self.assertEqual(original, copy) 1.880 + 1.881 + 1.882 + def test_autospec(self): 1.883 + class Boo(object): 1.884 + def __init__(self, a): 1.885 + pass 1.886 + def f(self, a): 1.887 + pass 1.888 + def g(self): 1.889 + pass 1.890 + foo = 'bar' 1.891 + 1.892 + class Bar(object): 1.893 + def a(self): 1.894 + pass 1.895 + 1.896 + def _test(mock): 1.897 + mock(1) 1.898 + mock.assert_called_with(1) 1.899 + self.assertRaises(TypeError, mock) 1.900 + 1.901 + def _test2(mock): 1.902 + mock.f(1) 1.903 + mock.f.assert_called_with(1) 1.904 + self.assertRaises(TypeError, mock.f) 1.905 + 1.906 + mock.g() 1.907 + mock.g.assert_called_with() 1.908 + self.assertRaises(TypeError, mock.g, 1) 1.909 + 1.910 + self.assertRaises(AttributeError, getattr, mock, 'h') 1.911 + 1.912 + mock.foo.lower() 1.913 + mock.foo.lower.assert_called_with() 1.914 + self.assertRaises(AttributeError, getattr, mock.foo, 'bar') 1.915 + 1.916 + mock.Bar() 1.917 + mock.Bar.assert_called_with() 1.918 + 1.919 + mock.Bar.a() 1.920 + mock.Bar.a.assert_called_with() 1.921 + self.assertRaises(TypeError, mock.Bar.a, 1) 1.922 + 1.923 + mock.Bar().a() 1.924 + mock.Bar().a.assert_called_with() 1.925 + self.assertRaises(TypeError, mock.Bar().a, 1) 1.926 + 1.927 + self.assertRaises(AttributeError, getattr, mock.Bar, 'b') 1.928 + self.assertRaises(AttributeError, getattr, mock.Bar(), 'b') 1.929 + 1.930 + def function(mock): 1.931 + _test(mock) 1.932 + _test2(mock) 1.933 + _test2(mock(1)) 1.934 + self.assertIs(mock, Foo) 1.935 + return mock 1.936 + 1.937 + test = patch(foo_name, autospec=True)(function) 1.938 + 1.939 + mock = test() 1.940 + self.assertIsNot(Foo, mock) 1.941 + # test patching a second time works 1.942 + test() 1.943 + 1.944 + module = sys.modules[__name__] 1.945 + test = patch.object(module, 'Foo', autospec=True)(function) 1.946 + 1.947 + mock = test() 1.948 + self.assertIsNot(Foo, mock) 1.949 + # test patching a second time works 1.950 + test() 1.951 + 1.952 + 1.953 + def test_autospec_function(self): 1.954 + @patch('%s.function' % __name__, autospec=True) 1.955 + def test(mock): 1.956 + function(1) 1.957 + function.assert_called_with(1) 1.958 + function(2, 3) 1.959 + function.assert_called_with(2, 3) 1.960 + 1.961 + self.assertRaises(TypeError, function) 1.962 + self.assertRaises(AttributeError, getattr, function, 'foo') 1.963 + 1.964 + test() 1.965 + 1.966 + 1.967 + def test_autospec_keywords(self): 1.968 + @patch('%s.function' % __name__, autospec=True, 1.969 + return_value=3) 1.970 + def test(mock_function): 1.971 + #self.assertEqual(function.abc, 'foo') 1.972 + return function(1, 2) 1.973 + 1.974 + result = test() 1.975 + self.assertEqual(result, 3) 1.976 + 1.977 + 1.978 + def test_autospec_with_new(self): 1.979 + patcher = patch('%s.function' % __name__, new=3, autospec=True) 1.980 + self.assertRaises(TypeError, patcher.start) 1.981 + 1.982 + module = sys.modules[__name__] 1.983 + patcher = patch.object(module, 'function', new=3, autospec=True) 1.984 + self.assertRaises(TypeError, patcher.start) 1.985 + 1.986 + 1.987 + def test_autospec_with_object(self): 1.988 + class Bar(Foo): 1.989 + extra = [] 1.990 + 1.991 + patcher = patch(foo_name, autospec=Bar) 1.992 + mock = patcher.start() 1.993 + try: 1.994 + self.assertIsInstance(mock, Bar) 1.995 + self.assertIsInstance(mock.extra, list) 1.996 + finally: 1.997 + patcher.stop() 1.998 + 1.999 + 1.1000 + def test_autospec_inherits(self): 1.1001 + FooClass = Foo 1.1002 + patcher = patch(foo_name, autospec=True) 1.1003 + mock = patcher.start() 1.1004 + try: 1.1005 + self.assertIsInstance(mock, FooClass) 1.1006 + self.assertIsInstance(mock(3), FooClass) 1.1007 + finally: 1.1008 + patcher.stop() 1.1009 + 1.1010 + 1.1011 + def test_autospec_name(self): 1.1012 + patcher = patch(foo_name, autospec=True) 1.1013 + mock = patcher.start() 1.1014 + 1.1015 + try: 1.1016 + self.assertIn(" name='Foo'", repr(mock)) 1.1017 + self.assertIn(" name='Foo.f'", repr(mock.f)) 1.1018 + self.assertIn(" name='Foo()'", repr(mock(None))) 1.1019 + self.assertIn(" name='Foo().f'", repr(mock(None).f)) 1.1020 + finally: 1.1021 + patcher.stop() 1.1022 + 1.1023 + 1.1024 + def test_tracebacks(self): 1.1025 + @patch.object(Foo, 'f', object()) 1.1026 + def test(): 1.1027 + raise AssertionError 1.1028 + try: 1.1029 + test() 1.1030 + except: 1.1031 + err = sys.exc_info() 1.1032 + 1.1033 + result = unittest2.TextTestResult(None, None, 0) 1.1034 + traceback = result._exc_info_to_string(err, self) 1.1035 + self.assertIn('raise AssertionError', traceback) 1.1036 + 1.1037 + 1.1038 + def test_new_callable_patch(self): 1.1039 + patcher = patch(foo_name, new_callable=NonCallableMagicMock) 1.1040 + 1.1041 + m1 = patcher.start() 1.1042 + patcher.stop() 1.1043 + m2 = patcher.start() 1.1044 + patcher.stop() 1.1045 + 1.1046 + self.assertIsNot(m1, m2) 1.1047 + for mock in m1, m2: 1.1048 + self.assertNotCallable(m1) 1.1049 + 1.1050 + 1.1051 + def test_new_callable_patch_object(self): 1.1052 + patcher = patch.object(Foo, 'f', new_callable=NonCallableMagicMock) 1.1053 + 1.1054 + m1 = patcher.start() 1.1055 + patcher.stop() 1.1056 + m2 = patcher.start() 1.1057 + patcher.stop() 1.1058 + 1.1059 + self.assertIsNot(m1, m2) 1.1060 + for mock in m1, m2: 1.1061 + self.assertNotCallable(m1) 1.1062 + 1.1063 + 1.1064 + def test_new_callable_keyword_arguments(self): 1.1065 + class Bar(object): 1.1066 + kwargs = None 1.1067 + def __init__(self, **kwargs): 1.1068 + Bar.kwargs = kwargs 1.1069 + 1.1070 + patcher = patch(foo_name, new_callable=Bar, arg1=1, arg2=2) 1.1071 + m = patcher.start() 1.1072 + try: 1.1073 + self.assertIs(type(m), Bar) 1.1074 + self.assertEqual(Bar.kwargs, dict(arg1=1, arg2=2)) 1.1075 + finally: 1.1076 + patcher.stop() 1.1077 + 1.1078 + 1.1079 + def test_new_callable_spec(self): 1.1080 + class Bar(object): 1.1081 + kwargs = None 1.1082 + def __init__(self, **kwargs): 1.1083 + Bar.kwargs = kwargs 1.1084 + 1.1085 + patcher = patch(foo_name, new_callable=Bar, spec=Bar) 1.1086 + patcher.start() 1.1087 + try: 1.1088 + self.assertEqual(Bar.kwargs, dict(spec=Bar)) 1.1089 + finally: 1.1090 + patcher.stop() 1.1091 + 1.1092 + patcher = patch(foo_name, new_callable=Bar, spec_set=Bar) 1.1093 + patcher.start() 1.1094 + try: 1.1095 + self.assertEqual(Bar.kwargs, dict(spec_set=Bar)) 1.1096 + finally: 1.1097 + patcher.stop() 1.1098 + 1.1099 + 1.1100 + def test_new_callable_create(self): 1.1101 + non_existent_attr = '%s.weeeee' % foo_name 1.1102 + p = patch(non_existent_attr, new_callable=NonCallableMock) 1.1103 + self.assertRaises(AttributeError, p.start) 1.1104 + 1.1105 + p = patch(non_existent_attr, new_callable=NonCallableMock, 1.1106 + create=True) 1.1107 + m = p.start() 1.1108 + try: 1.1109 + self.assertNotCallable(m, magic=False) 1.1110 + finally: 1.1111 + p.stop() 1.1112 + 1.1113 + 1.1114 + def test_new_callable_incompatible_with_new(self): 1.1115 + self.assertRaises( 1.1116 + ValueError, patch, foo_name, new=object(), new_callable=MagicMock 1.1117 + ) 1.1118 + self.assertRaises( 1.1119 + ValueError, patch.object, Foo, 'f', new=object(), 1.1120 + new_callable=MagicMock 1.1121 + ) 1.1122 + 1.1123 + 1.1124 + def test_new_callable_incompatible_with_autospec(self): 1.1125 + self.assertRaises( 1.1126 + ValueError, patch, foo_name, new_callable=MagicMock, 1.1127 + autospec=True 1.1128 + ) 1.1129 + self.assertRaises( 1.1130 + ValueError, patch.object, Foo, 'f', new_callable=MagicMock, 1.1131 + autospec=True 1.1132 + ) 1.1133 + 1.1134 + 1.1135 + def test_new_callable_inherit_for_mocks(self): 1.1136 + class MockSub(Mock): 1.1137 + pass 1.1138 + 1.1139 + MockClasses = ( 1.1140 + NonCallableMock, NonCallableMagicMock, MagicMock, Mock, MockSub 1.1141 + ) 1.1142 + for Klass in MockClasses: 1.1143 + for arg in 'spec', 'spec_set': 1.1144 + kwargs = {arg: True} 1.1145 + p = patch(foo_name, new_callable=Klass, **kwargs) 1.1146 + m = p.start() 1.1147 + try: 1.1148 + instance = m.return_value 1.1149 + self.assertRaises(AttributeError, getattr, instance, 'x') 1.1150 + finally: 1.1151 + p.stop() 1.1152 + 1.1153 + 1.1154 + def test_new_callable_inherit_non_mock(self): 1.1155 + class NotAMock(object): 1.1156 + def __init__(self, spec): 1.1157 + self.spec = spec 1.1158 + 1.1159 + p = patch(foo_name, new_callable=NotAMock, spec=True) 1.1160 + m = p.start() 1.1161 + try: 1.1162 + self.assertTrue(is_instance(m, NotAMock)) 1.1163 + self.assertRaises(AttributeError, getattr, m, 'return_value') 1.1164 + finally: 1.1165 + p.stop() 1.1166 + 1.1167 + self.assertEqual(m.spec, Foo) 1.1168 + 1.1169 + 1.1170 + def test_new_callable_class_decorating(self): 1.1171 + test = self 1.1172 + original = Foo 1.1173 + class SomeTest(object): 1.1174 + 1.1175 + def _test(self, mock_foo): 1.1176 + test.assertIsNot(Foo, original) 1.1177 + test.assertIs(Foo, mock_foo) 1.1178 + test.assertIsInstance(Foo, SomeClass) 1.1179 + 1.1180 + def test_two(self, mock_foo): 1.1181 + self._test(mock_foo) 1.1182 + def test_one(self, mock_foo): 1.1183 + self._test(mock_foo) 1.1184 + 1.1185 + SomeTest = patch(foo_name, new_callable=SomeClass)(SomeTest) 1.1186 + SomeTest().test_one() 1.1187 + SomeTest().test_two() 1.1188 + self.assertIs(Foo, original) 1.1189 + 1.1190 + 1.1191 + def test_patch_multiple(self): 1.1192 + original_foo = Foo 1.1193 + original_f = Foo.f 1.1194 + original_g = Foo.g 1.1195 + 1.1196 + patcher1 = patch.multiple(foo_name, f=1, g=2) 1.1197 + patcher2 = patch.multiple(Foo, f=1, g=2) 1.1198 + 1.1199 + for patcher in patcher1, patcher2: 1.1200 + patcher.start() 1.1201 + try: 1.1202 + self.assertIs(Foo, original_foo) 1.1203 + self.assertEqual(Foo.f, 1) 1.1204 + self.assertEqual(Foo.g, 2) 1.1205 + finally: 1.1206 + patcher.stop() 1.1207 + 1.1208 + self.assertIs(Foo, original_foo) 1.1209 + self.assertEqual(Foo.f, original_f) 1.1210 + self.assertEqual(Foo.g, original_g) 1.1211 + 1.1212 + 1.1213 + @patch.multiple(foo_name, f=3, g=4) 1.1214 + def test(): 1.1215 + self.assertIs(Foo, original_foo) 1.1216 + self.assertEqual(Foo.f, 3) 1.1217 + self.assertEqual(Foo.g, 4) 1.1218 + 1.1219 + test() 1.1220 + 1.1221 + 1.1222 + def test_patch_multiple_no_kwargs(self): 1.1223 + self.assertRaises(ValueError, patch.multiple, foo_name) 1.1224 + self.assertRaises(ValueError, patch.multiple, Foo) 1.1225 + 1.1226 + 1.1227 + def test_patch_multiple_create_mocks(self): 1.1228 + original_foo = Foo 1.1229 + original_f = Foo.f 1.1230 + original_g = Foo.g 1.1231 + 1.1232 + @patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT) 1.1233 + def test(f, foo): 1.1234 + self.assertIs(Foo, original_foo) 1.1235 + self.assertIs(Foo.f, f) 1.1236 + self.assertEqual(Foo.g, 3) 1.1237 + self.assertIs(Foo.foo, foo) 1.1238 + self.assertTrue(is_instance(f, MagicMock)) 1.1239 + self.assertTrue(is_instance(foo, MagicMock)) 1.1240 + 1.1241 + test() 1.1242 + self.assertEqual(Foo.f, original_f) 1.1243 + self.assertEqual(Foo.g, original_g) 1.1244 + 1.1245 + 1.1246 + def test_patch_multiple_create_mocks_different_order(self): 1.1247 + # bug revealed by Jython! 1.1248 + original_f = Foo.f 1.1249 + original_g = Foo.g 1.1250 + 1.1251 + patcher = patch.object(Foo, 'f', 3) 1.1252 + patcher.attribute_name = 'f' 1.1253 + 1.1254 + other = patch.object(Foo, 'g', DEFAULT) 1.1255 + other.attribute_name = 'g' 1.1256 + patcher.additional_patchers = [other] 1.1257 + 1.1258 + @patcher 1.1259 + def test(g): 1.1260 + self.assertIs(Foo.g, g) 1.1261 + self.assertEqual(Foo.f, 3) 1.1262 + 1.1263 + test() 1.1264 + self.assertEqual(Foo.f, original_f) 1.1265 + self.assertEqual(Foo.g, original_g) 1.1266 + 1.1267 + 1.1268 + def test_patch_multiple_stacked_decorators(self): 1.1269 + original_foo = Foo 1.1270 + original_f = Foo.f 1.1271 + original_g = Foo.g 1.1272 + 1.1273 + @patch.multiple(foo_name, f=DEFAULT) 1.1274 + @patch.multiple(foo_name, foo=DEFAULT) 1.1275 + @patch(foo_name + '.g') 1.1276 + def test1(g, **kwargs): 1.1277 + _test(g, **kwargs) 1.1278 + 1.1279 + @patch.multiple(foo_name, f=DEFAULT) 1.1280 + @patch(foo_name + '.g') 1.1281 + @patch.multiple(foo_name, foo=DEFAULT) 1.1282 + def test2(g, **kwargs): 1.1283 + _test(g, **kwargs) 1.1284 + 1.1285 + @patch(foo_name + '.g') 1.1286 + @patch.multiple(foo_name, f=DEFAULT) 1.1287 + @patch.multiple(foo_name, foo=DEFAULT) 1.1288 + def test3(g, **kwargs): 1.1289 + _test(g, **kwargs) 1.1290 + 1.1291 + def _test(g, **kwargs): 1.1292 + f = kwargs.pop('f') 1.1293 + foo = kwargs.pop('foo') 1.1294 + self.assertFalse(kwargs) 1.1295 + 1.1296 + self.assertIs(Foo, original_foo) 1.1297 + self.assertIs(Foo.f, f) 1.1298 + self.assertIs(Foo.g, g) 1.1299 + self.assertIs(Foo.foo, foo) 1.1300 + self.assertTrue(is_instance(f, MagicMock)) 1.1301 + self.assertTrue(is_instance(g, MagicMock)) 1.1302 + self.assertTrue(is_instance(foo, MagicMock)) 1.1303 + 1.1304 + test1() 1.1305 + test2() 1.1306 + test3() 1.1307 + self.assertEqual(Foo.f, original_f) 1.1308 + self.assertEqual(Foo.g, original_g) 1.1309 + 1.1310 + 1.1311 + def test_patch_multiple_create_mocks_patcher(self): 1.1312 + original_foo = Foo 1.1313 + original_f = Foo.f 1.1314 + original_g = Foo.g 1.1315 + 1.1316 + patcher = patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT) 1.1317 + 1.1318 + result = patcher.start() 1.1319 + try: 1.1320 + f = result['f'] 1.1321 + foo = result['foo'] 1.1322 + self.assertEqual(set(result), set(['f', 'foo'])) 1.1323 + 1.1324 + self.assertIs(Foo, original_foo) 1.1325 + self.assertIs(Foo.f, f) 1.1326 + self.assertIs(Foo.foo, foo) 1.1327 + self.assertTrue(is_instance(f, MagicMock)) 1.1328 + self.assertTrue(is_instance(foo, MagicMock)) 1.1329 + finally: 1.1330 + patcher.stop() 1.1331 + 1.1332 + self.assertEqual(Foo.f, original_f) 1.1333 + self.assertEqual(Foo.g, original_g) 1.1334 + 1.1335 + 1.1336 + def test_patch_multiple_decorating_class(self): 1.1337 + test = self 1.1338 + original_foo = Foo 1.1339 + original_f = Foo.f 1.1340 + original_g = Foo.g 1.1341 + 1.1342 + class SomeTest(object): 1.1343 + 1.1344 + def _test(self, f, foo): 1.1345 + test.assertIs(Foo, original_foo) 1.1346 + test.assertIs(Foo.f, f) 1.1347 + test.assertEqual(Foo.g, 3) 1.1348 + test.assertIs(Foo.foo, foo) 1.1349 + test.assertTrue(is_instance(f, MagicMock)) 1.1350 + test.assertTrue(is_instance(foo, MagicMock)) 1.1351 + 1.1352 + def test_two(self, f, foo): 1.1353 + self._test(f, foo) 1.1354 + def test_one(self, f, foo): 1.1355 + self._test(f, foo) 1.1356 + 1.1357 + SomeTest = patch.multiple( 1.1358 + foo_name, f=DEFAULT, g=3, foo=DEFAULT 1.1359 + )(SomeTest) 1.1360 + 1.1361 + thing = SomeTest() 1.1362 + thing.test_one() 1.1363 + thing.test_two() 1.1364 + 1.1365 + self.assertEqual(Foo.f, original_f) 1.1366 + self.assertEqual(Foo.g, original_g) 1.1367 + 1.1368 + 1.1369 + def test_patch_multiple_create(self): 1.1370 + patcher = patch.multiple(Foo, blam='blam') 1.1371 + self.assertRaises(AttributeError, patcher.start) 1.1372 + 1.1373 + patcher = patch.multiple(Foo, blam='blam', create=True) 1.1374 + patcher.start() 1.1375 + try: 1.1376 + self.assertEqual(Foo.blam, 'blam') 1.1377 + finally: 1.1378 + patcher.stop() 1.1379 + 1.1380 + self.assertFalse(hasattr(Foo, 'blam')) 1.1381 + 1.1382 + 1.1383 + def test_patch_multiple_spec_set(self): 1.1384 + # if spec_set works then we can assume that spec and autospec also 1.1385 + # work as the underlying machinery is the same 1.1386 + patcher = patch.multiple(Foo, foo=DEFAULT, spec_set=['a', 'b']) 1.1387 + result = patcher.start() 1.1388 + try: 1.1389 + self.assertEqual(Foo.foo, result['foo']) 1.1390 + Foo.foo.a(1) 1.1391 + Foo.foo.b(2) 1.1392 + Foo.foo.a.assert_called_with(1) 1.1393 + Foo.foo.b.assert_called_with(2) 1.1394 + self.assertRaises(AttributeError, setattr, Foo.foo, 'c', None) 1.1395 + finally: 1.1396 + patcher.stop() 1.1397 + 1.1398 + 1.1399 + def test_patch_multiple_new_callable(self): 1.1400 + class Thing(object): 1.1401 + pass 1.1402 + 1.1403 + patcher = patch.multiple( 1.1404 + Foo, f=DEFAULT, g=DEFAULT, new_callable=Thing 1.1405 + ) 1.1406 + result = patcher.start() 1.1407 + try: 1.1408 + self.assertIs(Foo.f, result['f']) 1.1409 + self.assertIs(Foo.g, result['g']) 1.1410 + self.assertIsInstance(Foo.f, Thing) 1.1411 + self.assertIsInstance(Foo.g, Thing) 1.1412 + self.assertIsNot(Foo.f, Foo.g) 1.1413 + finally: 1.1414 + patcher.stop() 1.1415 + 1.1416 + 1.1417 + def test_nested_patch_failure(self): 1.1418 + original_f = Foo.f 1.1419 + original_g = Foo.g 1.1420 + 1.1421 + @patch.object(Foo, 'g', 1) 1.1422 + @patch.object(Foo, 'missing', 1) 1.1423 + @patch.object(Foo, 'f', 1) 1.1424 + def thing1(): 1.1425 + pass 1.1426 + 1.1427 + @patch.object(Foo, 'missing', 1) 1.1428 + @patch.object(Foo, 'g', 1) 1.1429 + @patch.object(Foo, 'f', 1) 1.1430 + def thing2(): 1.1431 + pass 1.1432 + 1.1433 + @patch.object(Foo, 'g', 1) 1.1434 + @patch.object(Foo, 'f', 1) 1.1435 + @patch.object(Foo, 'missing', 1) 1.1436 + def thing3(): 1.1437 + pass 1.1438 + 1.1439 + for func in thing1, thing2, thing3: 1.1440 + self.assertRaises(AttributeError, func) 1.1441 + self.assertEqual(Foo.f, original_f) 1.1442 + self.assertEqual(Foo.g, original_g) 1.1443 + 1.1444 + 1.1445 + def test_new_callable_failure(self): 1.1446 + original_f = Foo.f 1.1447 + original_g = Foo.g 1.1448 + original_foo = Foo.foo 1.1449 + 1.1450 + def crasher(): 1.1451 + raise NameError('crasher') 1.1452 + 1.1453 + @patch.object(Foo, 'g', 1) 1.1454 + @patch.object(Foo, 'foo', new_callable=crasher) 1.1455 + @patch.object(Foo, 'f', 1) 1.1456 + def thing1(): 1.1457 + pass 1.1458 + 1.1459 + @patch.object(Foo, 'foo', new_callable=crasher) 1.1460 + @patch.object(Foo, 'g', 1) 1.1461 + @patch.object(Foo, 'f', 1) 1.1462 + def thing2(): 1.1463 + pass 1.1464 + 1.1465 + @patch.object(Foo, 'g', 1) 1.1466 + @patch.object(Foo, 'f', 1) 1.1467 + @patch.object(Foo, 'foo', new_callable=crasher) 1.1468 + def thing3(): 1.1469 + pass 1.1470 + 1.1471 + for func in thing1, thing2, thing3: 1.1472 + self.assertRaises(NameError, func) 1.1473 + self.assertEqual(Foo.f, original_f) 1.1474 + self.assertEqual(Foo.g, original_g) 1.1475 + self.assertEqual(Foo.foo, original_foo) 1.1476 + 1.1477 + 1.1478 + def test_patch_multiple_failure(self): 1.1479 + original_f = Foo.f 1.1480 + original_g = Foo.g 1.1481 + 1.1482 + patcher = patch.object(Foo, 'f', 1) 1.1483 + patcher.attribute_name = 'f' 1.1484 + 1.1485 + good = patch.object(Foo, 'g', 1) 1.1486 + good.attribute_name = 'g' 1.1487 + 1.1488 + bad = patch.object(Foo, 'missing', 1) 1.1489 + bad.attribute_name = 'missing' 1.1490 + 1.1491 + for additionals in [good, bad], [bad, good]: 1.1492 + patcher.additional_patchers = additionals 1.1493 + 1.1494 + @patcher 1.1495 + def func(): 1.1496 + pass 1.1497 + 1.1498 + self.assertRaises(AttributeError, func) 1.1499 + self.assertEqual(Foo.f, original_f) 1.1500 + self.assertEqual(Foo.g, original_g) 1.1501 + 1.1502 + 1.1503 + def test_patch_multiple_new_callable_failure(self): 1.1504 + original_f = Foo.f 1.1505 + original_g = Foo.g 1.1506 + original_foo = Foo.foo 1.1507 + 1.1508 + def crasher(): 1.1509 + raise NameError('crasher') 1.1510 + 1.1511 + patcher = patch.object(Foo, 'f', 1) 1.1512 + patcher.attribute_name = 'f' 1.1513 + 1.1514 + good = patch.object(Foo, 'g', 1) 1.1515 + good.attribute_name = 'g' 1.1516 + 1.1517 + bad = patch.object(Foo, 'foo', new_callable=crasher) 1.1518 + bad.attribute_name = 'foo' 1.1519 + 1.1520 + for additionals in [good, bad], [bad, good]: 1.1521 + patcher.additional_patchers = additionals 1.1522 + 1.1523 + @patcher 1.1524 + def func(): 1.1525 + pass 1.1526 + 1.1527 + self.assertRaises(NameError, func) 1.1528 + self.assertEqual(Foo.f, original_f) 1.1529 + self.assertEqual(Foo.g, original_g) 1.1530 + self.assertEqual(Foo.foo, original_foo) 1.1531 + 1.1532 + 1.1533 + def test_patch_multiple_string_subclasses(self): 1.1534 + for base in (str, unicode): 1.1535 + Foo = type('Foo', (base,), {'fish': 'tasty'}) 1.1536 + foo = Foo() 1.1537 + @patch.multiple(foo, fish='nearly gone') 1.1538 + def test(): 1.1539 + self.assertEqual(foo.fish, 'nearly gone') 1.1540 + 1.1541 + test() 1.1542 + self.assertEqual(foo.fish, 'tasty') 1.1543 + 1.1544 + 1.1545 + @patch('mock.patch.TEST_PREFIX', 'foo') 1.1546 + def test_patch_test_prefix(self): 1.1547 + class Foo(object): 1.1548 + thing = 'original' 1.1549 + 1.1550 + def foo_one(self): 1.1551 + return self.thing 1.1552 + def foo_two(self): 1.1553 + return self.thing 1.1554 + def test_one(self): 1.1555 + return self.thing 1.1556 + def test_two(self): 1.1557 + return self.thing 1.1558 + 1.1559 + Foo = patch.object(Foo, 'thing', 'changed')(Foo) 1.1560 + 1.1561 + foo = Foo() 1.1562 + self.assertEqual(foo.foo_one(), 'changed') 1.1563 + self.assertEqual(foo.foo_two(), 'changed') 1.1564 + self.assertEqual(foo.test_one(), 'original') 1.1565 + self.assertEqual(foo.test_two(), 'original') 1.1566 + 1.1567 + 1.1568 + @patch('mock.patch.TEST_PREFIX', 'bar') 1.1569 + def test_patch_dict_test_prefix(self): 1.1570 + class Foo(object): 1.1571 + def bar_one(self): 1.1572 + return dict(the_dict) 1.1573 + def bar_two(self): 1.1574 + return dict(the_dict) 1.1575 + def test_one(self): 1.1576 + return dict(the_dict) 1.1577 + def test_two(self): 1.1578 + return dict(the_dict) 1.1579 + 1.1580 + the_dict = {'key': 'original'} 1.1581 + Foo = patch.dict(the_dict, key='changed')(Foo) 1.1582 + 1.1583 + foo =Foo() 1.1584 + self.assertEqual(foo.bar_one(), {'key': 'changed'}) 1.1585 + self.assertEqual(foo.bar_two(), {'key': 'changed'}) 1.1586 + self.assertEqual(foo.test_one(), {'key': 'original'}) 1.1587 + self.assertEqual(foo.test_two(), {'key': 'original'}) 1.1588 + 1.1589 + 1.1590 + def test_patch_with_spec_mock_repr(self): 1.1591 + for arg in ('spec', 'autospec', 'spec_set'): 1.1592 + p = patch('%s.SomeClass' % __name__, **{arg: True}) 1.1593 + m = p.start() 1.1594 + try: 1.1595 + self.assertIn(" name='SomeClass'", repr(m)) 1.1596 + self.assertIn(" name='SomeClass.class_attribute'", 1.1597 + repr(m.class_attribute)) 1.1598 + self.assertIn(" name='SomeClass()'", repr(m())) 1.1599 + self.assertIn(" name='SomeClass().class_attribute'", 1.1600 + repr(m().class_attribute)) 1.1601 + finally: 1.1602 + p.stop() 1.1603 + 1.1604 + 1.1605 + def test_patch_nested_autospec_repr(self): 1.1606 + p = patch('tests.support', autospec=True) 1.1607 + m = p.start() 1.1608 + try: 1.1609 + self.assertIn(" name='support.SomeClass.wibble()'", 1.1610 + repr(m.SomeClass.wibble())) 1.1611 + self.assertIn(" name='support.SomeClass().wibble()'", 1.1612 + repr(m.SomeClass().wibble())) 1.1613 + finally: 1.1614 + p.stop() 1.1615 + 1.1616 + 1.1617 + def test_mock_calls_with_patch(self): 1.1618 + for arg in ('spec', 'autospec', 'spec_set'): 1.1619 + p = patch('%s.SomeClass' % __name__, **{arg: True}) 1.1620 + m = p.start() 1.1621 + try: 1.1622 + m.wibble() 1.1623 + 1.1624 + kalls = [call.wibble()] 1.1625 + self.assertEqual(m.mock_calls, kalls) 1.1626 + self.assertEqual(m.method_calls, kalls) 1.1627 + self.assertEqual(m.wibble.mock_calls, [call()]) 1.1628 + 1.1629 + result = m() 1.1630 + kalls.append(call()) 1.1631 + self.assertEqual(m.mock_calls, kalls) 1.1632 + 1.1633 + result.wibble() 1.1634 + kalls.append(call().wibble()) 1.1635 + self.assertEqual(m.mock_calls, kalls) 1.1636 + 1.1637 + self.assertEqual(result.mock_calls, [call.wibble()]) 1.1638 + self.assertEqual(result.wibble.mock_calls, [call()]) 1.1639 + self.assertEqual(result.method_calls, [call.wibble()]) 1.1640 + finally: 1.1641 + p.stop() 1.1642 + 1.1643 + 1.1644 + def test_patch_imports_lazily(self): 1.1645 + sys.modules.pop('squizz', None) 1.1646 + 1.1647 + p1 = patch('squizz.squozz') 1.1648 + self.assertRaises(ImportError, p1.start) 1.1649 + 1.1650 + squizz = Mock() 1.1651 + squizz.squozz = 6 1.1652 + sys.modules['squizz'] = squizz 1.1653 + p1 = patch('squizz.squozz') 1.1654 + squizz.squozz = 3 1.1655 + p1.start() 1.1656 + p1.stop() 1.1657 + self.assertEqual(squizz.squozz, 3) 1.1658 + 1.1659 + 1.1660 + def test_patch_propogrates_exc_on_exit(self): 1.1661 + class holder: 1.1662 + exc_info = None, None, None 1.1663 + 1.1664 + class custom_patch(_patch): 1.1665 + def __exit__(self, etype=None, val=None, tb=None): 1.1666 + _patch.__exit__(self, etype, val, tb) 1.1667 + holder.exc_info = etype, val, tb 1.1668 + stop = __exit__ 1.1669 + 1.1670 + def with_custom_patch(target): 1.1671 + getter, attribute = _get_target(target) 1.1672 + return custom_patch( 1.1673 + getter, attribute, DEFAULT, None, False, None, 1.1674 + None, None, {} 1.1675 + ) 1.1676 + 1.1677 + @with_custom_patch('squizz.squozz') 1.1678 + def test(mock): 1.1679 + raise RuntimeError 1.1680 + 1.1681 + self.assertRaises(RuntimeError, test) 1.1682 + self.assertIs(holder.exc_info[0], RuntimeError) 1.1683 + self.assertIsNotNone(holder.exc_info[1], 1.1684 + 'exception value not propgated') 1.1685 + self.assertIsNotNone(holder.exc_info[2], 1.1686 + 'exception traceback not propgated') 1.1687 + 1.1688 + 1.1689 + def test_create_and_specs(self): 1.1690 + for kwarg in ('spec', 'spec_set', 'autospec'): 1.1691 + p = patch('%s.doesnotexist' % __name__, create=True, 1.1692 + **{kwarg: True}) 1.1693 + self.assertRaises(TypeError, p.start) 1.1694 + self.assertRaises(NameError, lambda: doesnotexist) 1.1695 + 1.1696 + # check that spec with create is innocuous if the original exists 1.1697 + p = patch(MODNAME, create=True, **{kwarg: True}) 1.1698 + p.start() 1.1699 + p.stop() 1.1700 + 1.1701 + 1.1702 + def test_multiple_specs(self): 1.1703 + original = PTModule 1.1704 + for kwarg in ('spec', 'spec_set'): 1.1705 + p = patch(MODNAME, autospec=0, **{kwarg: 0}) 1.1706 + self.assertRaises(TypeError, p.start) 1.1707 + self.assertIs(PTModule, original) 1.1708 + 1.1709 + for kwarg in ('spec', 'autospec'): 1.1710 + p = patch(MODNAME, spec_set=0, **{kwarg: 0}) 1.1711 + self.assertRaises(TypeError, p.start) 1.1712 + self.assertIs(PTModule, original) 1.1713 + 1.1714 + for kwarg in ('spec_set', 'autospec'): 1.1715 + p = patch(MODNAME, spec=0, **{kwarg: 0}) 1.1716 + self.assertRaises(TypeError, p.start) 1.1717 + self.assertIs(PTModule, original) 1.1718 + 1.1719 + 1.1720 + def test_specs_false_instead_of_none(self): 1.1721 + p = patch(MODNAME, spec=False, spec_set=False, autospec=False) 1.1722 + mock = p.start() 1.1723 + try: 1.1724 + # no spec should have been set, so attribute access should not fail 1.1725 + mock.does_not_exist 1.1726 + mock.does_not_exist = 3 1.1727 + finally: 1.1728 + p.stop() 1.1729 + 1.1730 + 1.1731 + def test_falsey_spec(self): 1.1732 + for kwarg in ('spec', 'autospec', 'spec_set'): 1.1733 + p = patch(MODNAME, **{kwarg: 0}) 1.1734 + m = p.start() 1.1735 + try: 1.1736 + self.assertRaises(AttributeError, getattr, m, 'doesnotexit') 1.1737 + finally: 1.1738 + p.stop() 1.1739 + 1.1740 + 1.1741 + def test_spec_set_true(self): 1.1742 + for kwarg in ('spec', 'autospec'): 1.1743 + p = patch(MODNAME, spec_set=True, **{kwarg: True}) 1.1744 + m = p.start() 1.1745 + try: 1.1746 + self.assertRaises(AttributeError, setattr, m, 1.1747 + 'doesnotexist', 'something') 1.1748 + self.assertRaises(AttributeError, getattr, m, 'doesnotexist') 1.1749 + finally: 1.1750 + p.stop() 1.1751 + 1.1752 + 1.1753 + def test_callable_spec_as_list(self): 1.1754 + spec = ('__call__',) 1.1755 + p = patch(MODNAME, spec=spec) 1.1756 + m = p.start() 1.1757 + try: 1.1758 + self.assertTrue(callable(m)) 1.1759 + finally: 1.1760 + p.stop() 1.1761 + 1.1762 + 1.1763 + def test_not_callable_spec_as_list(self): 1.1764 + spec = ('foo', 'bar') 1.1765 + p = patch(MODNAME, spec=spec) 1.1766 + m = p.start() 1.1767 + try: 1.1768 + self.assertFalse(callable(m)) 1.1769 + finally: 1.1770 + p.stop() 1.1771 + 1.1772 + 1.1773 + def test_patch_stopall(self): 1.1774 + unlink = os.unlink 1.1775 + chdir = os.chdir 1.1776 + path = os.path 1.1777 + patch('os.unlink', something).start() 1.1778 + patch('os.chdir', something_else).start() 1.1779 + 1.1780 + @patch('os.path') 1.1781 + def patched(mock_path): 1.1782 + patch.stopall() 1.1783 + self.assertIs(os.path, mock_path) 1.1784 + self.assertIs(os.unlink, unlink) 1.1785 + self.assertIs(os.chdir, chdir) 1.1786 + 1.1787 + patched() 1.1788 + self.assertIs(os.path, path) 1.1789 + 1.1790 + 1.1791 + 1.1792 +if __name__ == '__main__': 1.1793 + unittest2.main()