python/mock-1.0.0/tests/testhelpers.py

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 # Copyright (C) 2007-2012 Michael Foord & the mock team
michael@0 2 # E-mail: fuzzyman AT voidspace DOT org DOT uk
michael@0 3 # http://www.voidspace.org.uk/python/mock/
michael@0 4
michael@0 5 from tests.support import unittest2, inPy3k
michael@0 6
michael@0 7 from mock import (
michael@0 8 call, _Call, create_autospec, MagicMock,
michael@0 9 Mock, ANY, _CallList, patch, PropertyMock
michael@0 10 )
michael@0 11
michael@0 12 from datetime import datetime
michael@0 13
michael@0 14 class SomeClass(object):
michael@0 15 def one(self, a, b):
michael@0 16 pass
michael@0 17 def two(self):
michael@0 18 pass
michael@0 19 def three(self, a=None):
michael@0 20 pass
michael@0 21
michael@0 22
michael@0 23
michael@0 24 class AnyTest(unittest2.TestCase):
michael@0 25
michael@0 26 def test_any(self):
michael@0 27 self.assertEqual(ANY, object())
michael@0 28
michael@0 29 mock = Mock()
michael@0 30 mock(ANY)
michael@0 31 mock.assert_called_with(ANY)
michael@0 32
michael@0 33 mock = Mock()
michael@0 34 mock(foo=ANY)
michael@0 35 mock.assert_called_with(foo=ANY)
michael@0 36
michael@0 37 def test_repr(self):
michael@0 38 self.assertEqual(repr(ANY), '<ANY>')
michael@0 39 self.assertEqual(str(ANY), '<ANY>')
michael@0 40
michael@0 41
michael@0 42 def test_any_and_datetime(self):
michael@0 43 mock = Mock()
michael@0 44 mock(datetime.now(), foo=datetime.now())
michael@0 45
michael@0 46 mock.assert_called_with(ANY, foo=ANY)
michael@0 47
michael@0 48
michael@0 49 def test_any_mock_calls_comparison_order(self):
michael@0 50 mock = Mock()
michael@0 51 d = datetime.now()
michael@0 52 class Foo(object):
michael@0 53 def __eq__(self, other):
michael@0 54 return False
michael@0 55 def __ne__(self, other):
michael@0 56 return True
michael@0 57
michael@0 58 for d in datetime.now(), Foo():
michael@0 59 mock.reset_mock()
michael@0 60
michael@0 61 mock(d, foo=d, bar=d)
michael@0 62 mock.method(d, zinga=d, alpha=d)
michael@0 63 mock().method(a1=d, z99=d)
michael@0 64
michael@0 65 expected = [
michael@0 66 call(ANY, foo=ANY, bar=ANY),
michael@0 67 call.method(ANY, zinga=ANY, alpha=ANY),
michael@0 68 call(), call().method(a1=ANY, z99=ANY)
michael@0 69 ]
michael@0 70 self.assertEqual(expected, mock.mock_calls)
michael@0 71 self.assertEqual(mock.mock_calls, expected)
michael@0 72
michael@0 73
michael@0 74
michael@0 75 class CallTest(unittest2.TestCase):
michael@0 76
michael@0 77 def test_call_with_call(self):
michael@0 78 kall = _Call()
michael@0 79 self.assertEqual(kall, _Call())
michael@0 80 self.assertEqual(kall, _Call(('',)))
michael@0 81 self.assertEqual(kall, _Call(((),)))
michael@0 82 self.assertEqual(kall, _Call(({},)))
michael@0 83 self.assertEqual(kall, _Call(('', ())))
michael@0 84 self.assertEqual(kall, _Call(('', {})))
michael@0 85 self.assertEqual(kall, _Call(('', (), {})))
michael@0 86 self.assertEqual(kall, _Call(('foo',)))
michael@0 87 self.assertEqual(kall, _Call(('bar', ())))
michael@0 88 self.assertEqual(kall, _Call(('baz', {})))
michael@0 89 self.assertEqual(kall, _Call(('spam', (), {})))
michael@0 90
michael@0 91 kall = _Call(((1, 2, 3),))
michael@0 92 self.assertEqual(kall, _Call(((1, 2, 3),)))
michael@0 93 self.assertEqual(kall, _Call(('', (1, 2, 3))))
michael@0 94 self.assertEqual(kall, _Call(((1, 2, 3), {})))
michael@0 95 self.assertEqual(kall, _Call(('', (1, 2, 3), {})))
michael@0 96
michael@0 97 kall = _Call(((1, 2, 4),))
michael@0 98 self.assertNotEqual(kall, _Call(('', (1, 2, 3))))
michael@0 99 self.assertNotEqual(kall, _Call(('', (1, 2, 3), {})))
michael@0 100
michael@0 101 kall = _Call(('foo', (1, 2, 4),))
michael@0 102 self.assertNotEqual(kall, _Call(('', (1, 2, 4))))
michael@0 103 self.assertNotEqual(kall, _Call(('', (1, 2, 4), {})))
michael@0 104 self.assertNotEqual(kall, _Call(('bar', (1, 2, 4))))
michael@0 105 self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {})))
michael@0 106
michael@0 107 kall = _Call(({'a': 3},))
michael@0 108 self.assertEqual(kall, _Call(('', (), {'a': 3})))
michael@0 109 self.assertEqual(kall, _Call(('', {'a': 3})))
michael@0 110 self.assertEqual(kall, _Call(((), {'a': 3})))
michael@0 111 self.assertEqual(kall, _Call(({'a': 3},)))
michael@0 112
michael@0 113
michael@0 114 def test_empty__Call(self):
michael@0 115 args = _Call()
michael@0 116
michael@0 117 self.assertEqual(args, ())
michael@0 118 self.assertEqual(args, ('foo',))
michael@0 119 self.assertEqual(args, ((),))
michael@0 120 self.assertEqual(args, ('foo', ()))
michael@0 121 self.assertEqual(args, ('foo',(), {}))
michael@0 122 self.assertEqual(args, ('foo', {}))
michael@0 123 self.assertEqual(args, ({},))
michael@0 124
michael@0 125
michael@0 126 def test_named_empty_call(self):
michael@0 127 args = _Call(('foo', (), {}))
michael@0 128
michael@0 129 self.assertEqual(args, ('foo',))
michael@0 130 self.assertEqual(args, ('foo', ()))
michael@0 131 self.assertEqual(args, ('foo',(), {}))
michael@0 132 self.assertEqual(args, ('foo', {}))
michael@0 133
michael@0 134 self.assertNotEqual(args, ((),))
michael@0 135 self.assertNotEqual(args, ())
michael@0 136 self.assertNotEqual(args, ({},))
michael@0 137 self.assertNotEqual(args, ('bar',))
michael@0 138 self.assertNotEqual(args, ('bar', ()))
michael@0 139 self.assertNotEqual(args, ('bar', {}))
michael@0 140
michael@0 141
michael@0 142 def test_call_with_args(self):
michael@0 143 args = _Call(((1, 2, 3), {}))
michael@0 144
michael@0 145 self.assertEqual(args, ((1, 2, 3),))
michael@0 146 self.assertEqual(args, ('foo', (1, 2, 3)))
michael@0 147 self.assertEqual(args, ('foo', (1, 2, 3), {}))
michael@0 148 self.assertEqual(args, ((1, 2, 3), {}))
michael@0 149
michael@0 150
michael@0 151 def test_named_call_with_args(self):
michael@0 152 args = _Call(('foo', (1, 2, 3), {}))
michael@0 153
michael@0 154 self.assertEqual(args, ('foo', (1, 2, 3)))
michael@0 155 self.assertEqual(args, ('foo', (1, 2, 3), {}))
michael@0 156
michael@0 157 self.assertNotEqual(args, ((1, 2, 3),))
michael@0 158 self.assertNotEqual(args, ((1, 2, 3), {}))
michael@0 159
michael@0 160
michael@0 161 def test_call_with_kwargs(self):
michael@0 162 args = _Call(((), dict(a=3, b=4)))
michael@0 163
michael@0 164 self.assertEqual(args, (dict(a=3, b=4),))
michael@0 165 self.assertEqual(args, ('foo', dict(a=3, b=4)))
michael@0 166 self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
michael@0 167 self.assertEqual(args, ((), dict(a=3, b=4)))
michael@0 168
michael@0 169
michael@0 170 def test_named_call_with_kwargs(self):
michael@0 171 args = _Call(('foo', (), dict(a=3, b=4)))
michael@0 172
michael@0 173 self.assertEqual(args, ('foo', dict(a=3, b=4)))
michael@0 174 self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
michael@0 175
michael@0 176 self.assertNotEqual(args, (dict(a=3, b=4),))
michael@0 177 self.assertNotEqual(args, ((), dict(a=3, b=4)))
michael@0 178
michael@0 179
michael@0 180 def test_call_with_args_call_empty_name(self):
michael@0 181 args = _Call(((1, 2, 3), {}))
michael@0 182 self.assertEqual(args, call(1, 2, 3))
michael@0 183 self.assertEqual(call(1, 2, 3), args)
michael@0 184 self.assertTrue(call(1, 2, 3) in [args])
michael@0 185
michael@0 186
michael@0 187 def test_call_ne(self):
michael@0 188 self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2))
michael@0 189 self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3))
michael@0 190 self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3))
michael@0 191
michael@0 192
michael@0 193 def test_call_non_tuples(self):
michael@0 194 kall = _Call(((1, 2, 3),))
michael@0 195 for value in 1, None, self, int:
michael@0 196 self.assertNotEqual(kall, value)
michael@0 197 self.assertFalse(kall == value)
michael@0 198
michael@0 199
michael@0 200 def test_repr(self):
michael@0 201 self.assertEqual(repr(_Call()), 'call()')
michael@0 202 self.assertEqual(repr(_Call(('foo',))), 'call.foo()')
michael@0 203
michael@0 204 self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))),
michael@0 205 "call(1, 2, 3, a='b')")
michael@0 206 self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))),
michael@0 207 "call.bar(1, 2, 3, a='b')")
michael@0 208
michael@0 209 self.assertEqual(repr(call), 'call')
michael@0 210 self.assertEqual(str(call), 'call')
michael@0 211
michael@0 212 self.assertEqual(repr(call()), 'call()')
michael@0 213 self.assertEqual(repr(call(1)), 'call(1)')
michael@0 214 self.assertEqual(repr(call(zz='thing')), "call(zz='thing')")
michael@0 215
michael@0 216 self.assertEqual(repr(call().foo), 'call().foo')
michael@0 217 self.assertEqual(repr(call(1).foo.bar(a=3).bing),
michael@0 218 'call().foo.bar().bing')
michael@0 219 self.assertEqual(
michael@0 220 repr(call().foo(1, 2, a=3)),
michael@0 221 "call().foo(1, 2, a=3)"
michael@0 222 )
michael@0 223 self.assertEqual(repr(call()()), "call()()")
michael@0 224 self.assertEqual(repr(call(1)(2)), "call()(2)")
michael@0 225 self.assertEqual(
michael@0 226 repr(call()().bar().baz.beep(1)),
michael@0 227 "call()().bar().baz.beep(1)"
michael@0 228 )
michael@0 229
michael@0 230
michael@0 231 def test_call(self):
michael@0 232 self.assertEqual(call(), ('', (), {}))
michael@0 233 self.assertEqual(call('foo', 'bar', one=3, two=4),
michael@0 234 ('', ('foo', 'bar'), {'one': 3, 'two': 4}))
michael@0 235
michael@0 236 mock = Mock()
michael@0 237 mock(1, 2, 3)
michael@0 238 mock(a=3, b=6)
michael@0 239 self.assertEqual(mock.call_args_list,
michael@0 240 [call(1, 2, 3), call(a=3, b=6)])
michael@0 241
michael@0 242 def test_attribute_call(self):
michael@0 243 self.assertEqual(call.foo(1), ('foo', (1,), {}))
michael@0 244 self.assertEqual(call.bar.baz(fish='eggs'),
michael@0 245 ('bar.baz', (), {'fish': 'eggs'}))
michael@0 246
michael@0 247 mock = Mock()
michael@0 248 mock.foo(1, 2 ,3)
michael@0 249 mock.bar.baz(a=3, b=6)
michael@0 250 self.assertEqual(mock.method_calls,
michael@0 251 [call.foo(1, 2, 3), call.bar.baz(a=3, b=6)])
michael@0 252
michael@0 253
michael@0 254 def test_extended_call(self):
michael@0 255 result = call(1).foo(2).bar(3, a=4)
michael@0 256 self.assertEqual(result, ('().foo().bar', (3,), dict(a=4)))
michael@0 257
michael@0 258 mock = MagicMock()
michael@0 259 mock(1, 2, a=3, b=4)
michael@0 260 self.assertEqual(mock.call_args, call(1, 2, a=3, b=4))
michael@0 261 self.assertNotEqual(mock.call_args, call(1, 2, 3))
michael@0 262
michael@0 263 self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)])
michael@0 264 self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)])
michael@0 265
michael@0 266 mock = MagicMock()
michael@0 267 mock.foo(1).bar()().baz.beep(a=6)
michael@0 268
michael@0 269 last_call = call.foo(1).bar()().baz.beep(a=6)
michael@0 270 self.assertEqual(mock.mock_calls[-1], last_call)
michael@0 271 self.assertEqual(mock.mock_calls, last_call.call_list())
michael@0 272
michael@0 273
michael@0 274 def test_call_list(self):
michael@0 275 mock = MagicMock()
michael@0 276 mock(1)
michael@0 277 self.assertEqual(call(1).call_list(), mock.mock_calls)
michael@0 278
michael@0 279 mock = MagicMock()
michael@0 280 mock(1).method(2)
michael@0 281 self.assertEqual(call(1).method(2).call_list(),
michael@0 282 mock.mock_calls)
michael@0 283
michael@0 284 mock = MagicMock()
michael@0 285 mock(1).method(2)(3)
michael@0 286 self.assertEqual(call(1).method(2)(3).call_list(),
michael@0 287 mock.mock_calls)
michael@0 288
michael@0 289 mock = MagicMock()
michael@0 290 int(mock(1).method(2)(3).foo.bar.baz(4)(5))
michael@0 291 kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__()
michael@0 292 self.assertEqual(kall.call_list(), mock.mock_calls)
michael@0 293
michael@0 294
michael@0 295 def test_call_any(self):
michael@0 296 self.assertEqual(call, ANY)
michael@0 297
michael@0 298 m = MagicMock()
michael@0 299 int(m)
michael@0 300 self.assertEqual(m.mock_calls, [ANY])
michael@0 301 self.assertEqual([ANY], m.mock_calls)
michael@0 302
michael@0 303
michael@0 304 def test_two_args_call(self):
michael@0 305 args = _Call(((1, 2), {'a': 3}), two=True)
michael@0 306 self.assertEqual(len(args), 2)
michael@0 307 self.assertEqual(args[0], (1, 2))
michael@0 308 self.assertEqual(args[1], {'a': 3})
michael@0 309
michael@0 310 other_args = _Call(((1, 2), {'a': 3}))
michael@0 311 self.assertEqual(args, other_args)
michael@0 312
michael@0 313
michael@0 314 class SpecSignatureTest(unittest2.TestCase):
michael@0 315
michael@0 316 def _check_someclass_mock(self, mock):
michael@0 317 self.assertRaises(AttributeError, getattr, mock, 'foo')
michael@0 318 mock.one(1, 2)
michael@0 319 mock.one.assert_called_with(1, 2)
michael@0 320 self.assertRaises(AssertionError,
michael@0 321 mock.one.assert_called_with, 3, 4)
michael@0 322 self.assertRaises(TypeError, mock.one, 1)
michael@0 323
michael@0 324 mock.two()
michael@0 325 mock.two.assert_called_with()
michael@0 326 self.assertRaises(AssertionError,
michael@0 327 mock.two.assert_called_with, 3)
michael@0 328 self.assertRaises(TypeError, mock.two, 1)
michael@0 329
michael@0 330 mock.three()
michael@0 331 mock.three.assert_called_with()
michael@0 332 self.assertRaises(AssertionError,
michael@0 333 mock.three.assert_called_with, 3)
michael@0 334 self.assertRaises(TypeError, mock.three, 3, 2)
michael@0 335
michael@0 336 mock.three(1)
michael@0 337 mock.three.assert_called_with(1)
michael@0 338
michael@0 339 mock.three(a=1)
michael@0 340 mock.three.assert_called_with(a=1)
michael@0 341
michael@0 342
michael@0 343 def test_basic(self):
michael@0 344 for spec in (SomeClass, SomeClass()):
michael@0 345 mock = create_autospec(spec)
michael@0 346 self._check_someclass_mock(mock)
michael@0 347
michael@0 348
michael@0 349 def test_create_autospec_return_value(self):
michael@0 350 def f():
michael@0 351 pass
michael@0 352 mock = create_autospec(f, return_value='foo')
michael@0 353 self.assertEqual(mock(), 'foo')
michael@0 354
michael@0 355 class Foo(object):
michael@0 356 pass
michael@0 357
michael@0 358 mock = create_autospec(Foo, return_value='foo')
michael@0 359 self.assertEqual(mock(), 'foo')
michael@0 360
michael@0 361
michael@0 362 def test_autospec_reset_mock(self):
michael@0 363 m = create_autospec(int)
michael@0 364 int(m)
michael@0 365 m.reset_mock()
michael@0 366 self.assertEqual(m.__int__.call_count, 0)
michael@0 367
michael@0 368
michael@0 369 def test_mocking_unbound_methods(self):
michael@0 370 class Foo(object):
michael@0 371 def foo(self, foo):
michael@0 372 pass
michael@0 373 p = patch.object(Foo, 'foo')
michael@0 374 mock_foo = p.start()
michael@0 375 Foo().foo(1)
michael@0 376
michael@0 377 mock_foo.assert_called_with(1)
michael@0 378
michael@0 379
michael@0 380 @unittest2.expectedFailure
michael@0 381 def test_create_autospec_unbound_methods(self):
michael@0 382 # see issue 128
michael@0 383 class Foo(object):
michael@0 384 def foo(self):
michael@0 385 pass
michael@0 386
michael@0 387 klass = create_autospec(Foo)
michael@0 388 instance = klass()
michael@0 389 self.assertRaises(TypeError, instance.foo, 1)
michael@0 390
michael@0 391 # Note: no type checking on the "self" parameter
michael@0 392 klass.foo(1)
michael@0 393 klass.foo.assert_called_with(1)
michael@0 394 self.assertRaises(TypeError, klass.foo)
michael@0 395
michael@0 396
michael@0 397 def test_create_autospec_keyword_arguments(self):
michael@0 398 class Foo(object):
michael@0 399 a = 3
michael@0 400 m = create_autospec(Foo, a='3')
michael@0 401 self.assertEqual(m.a, '3')
michael@0 402
michael@0 403 @unittest2.skipUnless(inPy3k, "Keyword only arguments Python 3 specific")
michael@0 404 def test_create_autospec_keyword_only_arguments(self):
michael@0 405 func_def = "def foo(a, *, b=None):\n pass\n"
michael@0 406 namespace = {}
michael@0 407 exec (func_def, namespace)
michael@0 408 foo = namespace['foo']
michael@0 409
michael@0 410 m = create_autospec(foo)
michael@0 411 m(1)
michael@0 412 m.assert_called_with(1)
michael@0 413 self.assertRaises(TypeError, m, 1, 2)
michael@0 414
michael@0 415 m(2, b=3)
michael@0 416 m.assert_called_with(2, b=3)
michael@0 417
michael@0 418 def test_function_as_instance_attribute(self):
michael@0 419 obj = SomeClass()
michael@0 420 def f(a):
michael@0 421 pass
michael@0 422 obj.f = f
michael@0 423
michael@0 424 mock = create_autospec(obj)
michael@0 425 mock.f('bing')
michael@0 426 mock.f.assert_called_with('bing')
michael@0 427
michael@0 428
michael@0 429 def test_spec_as_list(self):
michael@0 430 # because spec as a list of strings in the mock constructor means
michael@0 431 # something very different we treat a list instance as the type.
michael@0 432 mock = create_autospec([])
michael@0 433 mock.append('foo')
michael@0 434 mock.append.assert_called_with('foo')
michael@0 435
michael@0 436 self.assertRaises(AttributeError, getattr, mock, 'foo')
michael@0 437
michael@0 438 class Foo(object):
michael@0 439 foo = []
michael@0 440
michael@0 441 mock = create_autospec(Foo)
michael@0 442 mock.foo.append(3)
michael@0 443 mock.foo.append.assert_called_with(3)
michael@0 444 self.assertRaises(AttributeError, getattr, mock.foo, 'foo')
michael@0 445
michael@0 446
michael@0 447 def test_attributes(self):
michael@0 448 class Sub(SomeClass):
michael@0 449 attr = SomeClass()
michael@0 450
michael@0 451 sub_mock = create_autospec(Sub)
michael@0 452
michael@0 453 for mock in (sub_mock, sub_mock.attr):
michael@0 454 self._check_someclass_mock(mock)
michael@0 455
michael@0 456
michael@0 457 def test_builtin_functions_types(self):
michael@0 458 # we could replace builtin functions / methods with a function
michael@0 459 # with *args / **kwargs signature. Using the builtin method type
michael@0 460 # as a spec seems to work fairly well though.
michael@0 461 class BuiltinSubclass(list):
michael@0 462 def bar(self, arg):
michael@0 463 pass
michael@0 464 sorted = sorted
michael@0 465 attr = {}
michael@0 466
michael@0 467 mock = create_autospec(BuiltinSubclass)
michael@0 468 mock.append(3)
michael@0 469 mock.append.assert_called_with(3)
michael@0 470 self.assertRaises(AttributeError, getattr, mock.append, 'foo')
michael@0 471
michael@0 472 mock.bar('foo')
michael@0 473 mock.bar.assert_called_with('foo')
michael@0 474 self.assertRaises(TypeError, mock.bar, 'foo', 'bar')
michael@0 475 self.assertRaises(AttributeError, getattr, mock.bar, 'foo')
michael@0 476
michael@0 477 mock.sorted([1, 2])
michael@0 478 mock.sorted.assert_called_with([1, 2])
michael@0 479 self.assertRaises(AttributeError, getattr, mock.sorted, 'foo')
michael@0 480
michael@0 481 mock.attr.pop(3)
michael@0 482 mock.attr.pop.assert_called_with(3)
michael@0 483 self.assertRaises(AttributeError, getattr, mock.attr, 'foo')
michael@0 484
michael@0 485
michael@0 486 def test_method_calls(self):
michael@0 487 class Sub(SomeClass):
michael@0 488 attr = SomeClass()
michael@0 489
michael@0 490 mock = create_autospec(Sub)
michael@0 491 mock.one(1, 2)
michael@0 492 mock.two()
michael@0 493 mock.three(3)
michael@0 494
michael@0 495 expected = [call.one(1, 2), call.two(), call.three(3)]
michael@0 496 self.assertEqual(mock.method_calls, expected)
michael@0 497
michael@0 498 mock.attr.one(1, 2)
michael@0 499 mock.attr.two()
michael@0 500 mock.attr.three(3)
michael@0 501
michael@0 502 expected.extend(
michael@0 503 [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)]
michael@0 504 )
michael@0 505 self.assertEqual(mock.method_calls, expected)
michael@0 506
michael@0 507
michael@0 508 def test_magic_methods(self):
michael@0 509 class BuiltinSubclass(list):
michael@0 510 attr = {}
michael@0 511
michael@0 512 mock = create_autospec(BuiltinSubclass)
michael@0 513 self.assertEqual(list(mock), [])
michael@0 514 self.assertRaises(TypeError, int, mock)
michael@0 515 self.assertRaises(TypeError, int, mock.attr)
michael@0 516 self.assertEqual(list(mock), [])
michael@0 517
michael@0 518 self.assertIsInstance(mock['foo'], MagicMock)
michael@0 519 self.assertIsInstance(mock.attr['foo'], MagicMock)
michael@0 520
michael@0 521
michael@0 522 def test_spec_set(self):
michael@0 523 class Sub(SomeClass):
michael@0 524 attr = SomeClass()
michael@0 525
michael@0 526 for spec in (Sub, Sub()):
michael@0 527 mock = create_autospec(spec, spec_set=True)
michael@0 528 self._check_someclass_mock(mock)
michael@0 529
michael@0 530 self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar')
michael@0 531 self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar')
michael@0 532
michael@0 533
michael@0 534 def test_descriptors(self):
michael@0 535 class Foo(object):
michael@0 536 @classmethod
michael@0 537 def f(cls, a, b):
michael@0 538 pass
michael@0 539 @staticmethod
michael@0 540 def g(a, b):
michael@0 541 pass
michael@0 542
michael@0 543 class Bar(Foo):
michael@0 544 pass
michael@0 545
michael@0 546 class Baz(SomeClass, Bar):
michael@0 547 pass
michael@0 548
michael@0 549 for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()):
michael@0 550 mock = create_autospec(spec)
michael@0 551 mock.f(1, 2)
michael@0 552 mock.f.assert_called_once_with(1, 2)
michael@0 553
michael@0 554 mock.g(3, 4)
michael@0 555 mock.g.assert_called_once_with(3, 4)
michael@0 556
michael@0 557
michael@0 558 @unittest2.skipIf(inPy3k, "No old style classes in Python 3")
michael@0 559 def test_old_style_classes(self):
michael@0 560 class Foo:
michael@0 561 def f(self, a, b):
michael@0 562 pass
michael@0 563
michael@0 564 class Bar(Foo):
michael@0 565 g = Foo()
michael@0 566
michael@0 567 for spec in (Foo, Foo(), Bar, Bar()):
michael@0 568 mock = create_autospec(spec)
michael@0 569 mock.f(1, 2)
michael@0 570 mock.f.assert_called_once_with(1, 2)
michael@0 571
michael@0 572 self.assertRaises(AttributeError, getattr, mock, 'foo')
michael@0 573 self.assertRaises(AttributeError, getattr, mock.f, 'foo')
michael@0 574
michael@0 575 mock.g.f(1, 2)
michael@0 576 mock.g.f.assert_called_once_with(1, 2)
michael@0 577 self.assertRaises(AttributeError, getattr, mock.g, 'foo')
michael@0 578
michael@0 579
michael@0 580 def test_recursive(self):
michael@0 581 class A(object):
michael@0 582 def a(self):
michael@0 583 pass
michael@0 584 foo = 'foo bar baz'
michael@0 585 bar = foo
michael@0 586
michael@0 587 A.B = A
michael@0 588 mock = create_autospec(A)
michael@0 589
michael@0 590 mock()
michael@0 591 self.assertFalse(mock.B.called)
michael@0 592
michael@0 593 mock.a()
michael@0 594 mock.B.a()
michael@0 595 self.assertEqual(mock.method_calls, [call.a(), call.B.a()])
michael@0 596
michael@0 597 self.assertIs(A.foo, A.bar)
michael@0 598 self.assertIsNot(mock.foo, mock.bar)
michael@0 599 mock.foo.lower()
michael@0 600 self.assertRaises(AssertionError, mock.bar.lower.assert_called_with)
michael@0 601
michael@0 602
michael@0 603 def test_spec_inheritance_for_classes(self):
michael@0 604 class Foo(object):
michael@0 605 def a(self):
michael@0 606 pass
michael@0 607 class Bar(object):
michael@0 608 def f(self):
michael@0 609 pass
michael@0 610
michael@0 611 class_mock = create_autospec(Foo)
michael@0 612
michael@0 613 self.assertIsNot(class_mock, class_mock())
michael@0 614
michael@0 615 for this_mock in class_mock, class_mock():
michael@0 616 this_mock.a()
michael@0 617 this_mock.a.assert_called_with()
michael@0 618 self.assertRaises(TypeError, this_mock.a, 'foo')
michael@0 619 self.assertRaises(AttributeError, getattr, this_mock, 'b')
michael@0 620
michael@0 621 instance_mock = create_autospec(Foo())
michael@0 622 instance_mock.a()
michael@0 623 instance_mock.a.assert_called_with()
michael@0 624 self.assertRaises(TypeError, instance_mock.a, 'foo')
michael@0 625 self.assertRaises(AttributeError, getattr, instance_mock, 'b')
michael@0 626
michael@0 627 # The return value isn't isn't callable
michael@0 628 self.assertRaises(TypeError, instance_mock)
michael@0 629
michael@0 630 instance_mock.Bar.f()
michael@0 631 instance_mock.Bar.f.assert_called_with()
michael@0 632 self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g')
michael@0 633
michael@0 634 instance_mock.Bar().f()
michael@0 635 instance_mock.Bar().f.assert_called_with()
michael@0 636 self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g')
michael@0 637
michael@0 638
michael@0 639 def test_inherit(self):
michael@0 640 class Foo(object):
michael@0 641 a = 3
michael@0 642
michael@0 643 Foo.Foo = Foo
michael@0 644
michael@0 645 # class
michael@0 646 mock = create_autospec(Foo)
michael@0 647 instance = mock()
michael@0 648 self.assertRaises(AttributeError, getattr, instance, 'b')
michael@0 649
michael@0 650 attr_instance = mock.Foo()
michael@0 651 self.assertRaises(AttributeError, getattr, attr_instance, 'b')
michael@0 652
michael@0 653 # instance
michael@0 654 mock = create_autospec(Foo())
michael@0 655 self.assertRaises(AttributeError, getattr, mock, 'b')
michael@0 656 self.assertRaises(TypeError, mock)
michael@0 657
michael@0 658 # attribute instance
michael@0 659 call_result = mock.Foo()
michael@0 660 self.assertRaises(AttributeError, getattr, call_result, 'b')
michael@0 661
michael@0 662
michael@0 663 def test_builtins(self):
michael@0 664 # used to fail with infinite recursion
michael@0 665 create_autospec(1)
michael@0 666
michael@0 667 create_autospec(int)
michael@0 668 create_autospec('foo')
michael@0 669 create_autospec(str)
michael@0 670 create_autospec({})
michael@0 671 create_autospec(dict)
michael@0 672 create_autospec([])
michael@0 673 create_autospec(list)
michael@0 674 create_autospec(set())
michael@0 675 create_autospec(set)
michael@0 676 create_autospec(1.0)
michael@0 677 create_autospec(float)
michael@0 678 create_autospec(1j)
michael@0 679 create_autospec(complex)
michael@0 680 create_autospec(False)
michael@0 681 create_autospec(True)
michael@0 682
michael@0 683
michael@0 684 def test_function(self):
michael@0 685 def f(a, b):
michael@0 686 pass
michael@0 687
michael@0 688 mock = create_autospec(f)
michael@0 689 self.assertRaises(TypeError, mock)
michael@0 690 mock(1, 2)
michael@0 691 mock.assert_called_with(1, 2)
michael@0 692
michael@0 693 f.f = f
michael@0 694 mock = create_autospec(f)
michael@0 695 self.assertRaises(TypeError, mock.f)
michael@0 696 mock.f(3, 4)
michael@0 697 mock.f.assert_called_with(3, 4)
michael@0 698
michael@0 699
michael@0 700 def test_skip_attributeerrors(self):
michael@0 701 class Raiser(object):
michael@0 702 def __get__(self, obj, type=None):
michael@0 703 if obj is None:
michael@0 704 raise AttributeError('Can only be accessed via an instance')
michael@0 705
michael@0 706 class RaiserClass(object):
michael@0 707 raiser = Raiser()
michael@0 708
michael@0 709 @staticmethod
michael@0 710 def existing(a, b):
michael@0 711 return a + b
michael@0 712
michael@0 713 s = create_autospec(RaiserClass)
michael@0 714 self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3))
michael@0 715 s.existing(1, 2)
michael@0 716 self.assertRaises(AttributeError, lambda: s.nonexisting)
michael@0 717
michael@0 718 # check we can fetch the raiser attribute and it has no spec
michael@0 719 obj = s.raiser
michael@0 720 obj.foo, obj.bar
michael@0 721
michael@0 722
michael@0 723 def test_signature_class(self):
michael@0 724 class Foo(object):
michael@0 725 def __init__(self, a, b=3):
michael@0 726 pass
michael@0 727
michael@0 728 mock = create_autospec(Foo)
michael@0 729
michael@0 730 self.assertRaises(TypeError, mock)
michael@0 731 mock(1)
michael@0 732 mock.assert_called_once_with(1)
michael@0 733
michael@0 734 mock(4, 5)
michael@0 735 mock.assert_called_with(4, 5)
michael@0 736
michael@0 737
michael@0 738 @unittest2.skipIf(inPy3k, 'no old style classes in Python 3')
michael@0 739 def test_signature_old_style_class(self):
michael@0 740 class Foo:
michael@0 741 def __init__(self, a, b=3):
michael@0 742 pass
michael@0 743
michael@0 744 mock = create_autospec(Foo)
michael@0 745
michael@0 746 self.assertRaises(TypeError, mock)
michael@0 747 mock(1)
michael@0 748 mock.assert_called_once_with(1)
michael@0 749
michael@0 750 mock(4, 5)
michael@0 751 mock.assert_called_with(4, 5)
michael@0 752
michael@0 753
michael@0 754 def test_class_with_no_init(self):
michael@0 755 # this used to raise an exception
michael@0 756 # due to trying to get a signature from object.__init__
michael@0 757 class Foo(object):
michael@0 758 pass
michael@0 759 create_autospec(Foo)
michael@0 760
michael@0 761
michael@0 762 @unittest2.skipIf(inPy3k, 'no old style classes in Python 3')
michael@0 763 def test_old_style_class_with_no_init(self):
michael@0 764 # this used to raise an exception
michael@0 765 # due to Foo.__init__ raising an AttributeError
michael@0 766 class Foo:
michael@0 767 pass
michael@0 768 create_autospec(Foo)
michael@0 769
michael@0 770
michael@0 771 def test_signature_callable(self):
michael@0 772 class Callable(object):
michael@0 773 def __init__(self):
michael@0 774 pass
michael@0 775 def __call__(self, a):
michael@0 776 pass
michael@0 777
michael@0 778 mock = create_autospec(Callable)
michael@0 779 mock()
michael@0 780 mock.assert_called_once_with()
michael@0 781 self.assertRaises(TypeError, mock, 'a')
michael@0 782
michael@0 783 instance = mock()
michael@0 784 self.assertRaises(TypeError, instance)
michael@0 785 instance(a='a')
michael@0 786 instance.assert_called_once_with(a='a')
michael@0 787 instance('a')
michael@0 788 instance.assert_called_with('a')
michael@0 789
michael@0 790 mock = create_autospec(Callable())
michael@0 791 mock(a='a')
michael@0 792 mock.assert_called_once_with(a='a')
michael@0 793 self.assertRaises(TypeError, mock)
michael@0 794 mock('a')
michael@0 795 mock.assert_called_with('a')
michael@0 796
michael@0 797
michael@0 798 def test_signature_noncallable(self):
michael@0 799 class NonCallable(object):
michael@0 800 def __init__(self):
michael@0 801 pass
michael@0 802
michael@0 803 mock = create_autospec(NonCallable)
michael@0 804 instance = mock()
michael@0 805 mock.assert_called_once_with()
michael@0 806 self.assertRaises(TypeError, mock, 'a')
michael@0 807 self.assertRaises(TypeError, instance)
michael@0 808 self.assertRaises(TypeError, instance, 'a')
michael@0 809
michael@0 810 mock = create_autospec(NonCallable())
michael@0 811 self.assertRaises(TypeError, mock)
michael@0 812 self.assertRaises(TypeError, mock, 'a')
michael@0 813
michael@0 814
michael@0 815 def test_create_autospec_none(self):
michael@0 816 class Foo(object):
michael@0 817 bar = None
michael@0 818
michael@0 819 mock = create_autospec(Foo)
michael@0 820 none = mock.bar
michael@0 821 self.assertNotIsInstance(none, type(None))
michael@0 822
michael@0 823 none.foo()
michael@0 824 none.foo.assert_called_once_with()
michael@0 825
michael@0 826
michael@0 827 def test_autospec_functions_with_self_in_odd_place(self):
michael@0 828 class Foo(object):
michael@0 829 def f(a, self):
michael@0 830 pass
michael@0 831
michael@0 832 a = create_autospec(Foo)
michael@0 833 a.f(self=10)
michael@0 834 a.f.assert_called_with(self=10)
michael@0 835
michael@0 836
michael@0 837 def test_autospec_property(self):
michael@0 838 class Foo(object):
michael@0 839 @property
michael@0 840 def foo(self):
michael@0 841 return 3
michael@0 842
michael@0 843 foo = create_autospec(Foo)
michael@0 844 mock_property = foo.foo
michael@0 845
michael@0 846 # no spec on properties
michael@0 847 self.assertTrue(isinstance(mock_property, MagicMock))
michael@0 848 mock_property(1, 2, 3)
michael@0 849 mock_property.abc(4, 5, 6)
michael@0 850 mock_property.assert_called_once_with(1, 2, 3)
michael@0 851 mock_property.abc.assert_called_once_with(4, 5, 6)
michael@0 852
michael@0 853
michael@0 854 def test_autospec_slots(self):
michael@0 855 class Foo(object):
michael@0 856 __slots__ = ['a']
michael@0 857
michael@0 858 foo = create_autospec(Foo)
michael@0 859 mock_slot = foo.a
michael@0 860
michael@0 861 # no spec on slots
michael@0 862 mock_slot(1, 2, 3)
michael@0 863 mock_slot.abc(4, 5, 6)
michael@0 864 mock_slot.assert_called_once_with(1, 2, 3)
michael@0 865 mock_slot.abc.assert_called_once_with(4, 5, 6)
michael@0 866
michael@0 867
michael@0 868 class TestCallList(unittest2.TestCase):
michael@0 869
michael@0 870 def test_args_list_contains_call_list(self):
michael@0 871 mock = Mock()
michael@0 872 self.assertIsInstance(mock.call_args_list, _CallList)
michael@0 873
michael@0 874 mock(1, 2)
michael@0 875 mock(a=3)
michael@0 876 mock(3, 4)
michael@0 877 mock(b=6)
michael@0 878
michael@0 879 for kall in call(1, 2), call(a=3), call(3, 4), call(b=6):
michael@0 880 self.assertTrue(kall in mock.call_args_list)
michael@0 881
michael@0 882 calls = [call(a=3), call(3, 4)]
michael@0 883 self.assertTrue(calls in mock.call_args_list)
michael@0 884 calls = [call(1, 2), call(a=3)]
michael@0 885 self.assertTrue(calls in mock.call_args_list)
michael@0 886 calls = [call(3, 4), call(b=6)]
michael@0 887 self.assertTrue(calls in mock.call_args_list)
michael@0 888 calls = [call(3, 4)]
michael@0 889 self.assertTrue(calls in mock.call_args_list)
michael@0 890
michael@0 891 self.assertFalse(call('fish') in mock.call_args_list)
michael@0 892 self.assertFalse([call('fish')] in mock.call_args_list)
michael@0 893
michael@0 894
michael@0 895 def test_call_list_str(self):
michael@0 896 mock = Mock()
michael@0 897 mock(1, 2)
michael@0 898 mock.foo(a=3)
michael@0 899 mock.foo.bar().baz('fish', cat='dog')
michael@0 900
michael@0 901 expected = (
michael@0 902 "[call(1, 2),\n"
michael@0 903 " call.foo(a=3),\n"
michael@0 904 " call.foo.bar(),\n"
michael@0 905 " call.foo.bar().baz('fish', cat='dog')]"
michael@0 906 )
michael@0 907 self.assertEqual(str(mock.mock_calls), expected)
michael@0 908
michael@0 909
michael@0 910 def test_propertymock(self):
michael@0 911 p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock)
michael@0 912 mock = p.start()
michael@0 913 try:
michael@0 914 SomeClass.one
michael@0 915 mock.assert_called_once_with()
michael@0 916
michael@0 917 s = SomeClass()
michael@0 918 s.one
michael@0 919 mock.assert_called_with()
michael@0 920 self.assertEqual(mock.mock_calls, [call(), call()])
michael@0 921
michael@0 922 s.one = 3
michael@0 923 self.assertEqual(mock.mock_calls, [call(), call(), call(3)])
michael@0 924 finally:
michael@0 925 p.stop()
michael@0 926
michael@0 927
michael@0 928 def test_propertymock_returnvalue(self):
michael@0 929 m = MagicMock()
michael@0 930 p = PropertyMock()
michael@0 931 type(m).foo = p
michael@0 932
michael@0 933 returned = m.foo
michael@0 934 p.assert_called_once_with()
michael@0 935 self.assertIsInstance(returned, MagicMock)
michael@0 936 self.assertNotIsInstance(returned, PropertyMock)
michael@0 937
michael@0 938
michael@0 939 if __name__ == '__main__':
michael@0 940 unittest2.main()

mercurial