Plex plugin to to play various online streams (mostly Latvian).

testhelpers.py 28KB

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