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

testwith.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. from warnings import catch_warnings
  5. import unittest2 as unittest
  6. from mock.tests.support import is_instance
  7. from mock import MagicMock, Mock, patch, sentinel, mock_open, call
  8. something = sentinel.Something
  9. something_else = sentinel.SomethingElse
  10. class WithTest(unittest.TestCase):
  11. def test_with_statement(self):
  12. with patch('%s.something' % __name__, sentinel.Something2):
  13. self.assertEqual(something, sentinel.Something2, "unpatched")
  14. self.assertEqual(something, sentinel.Something)
  15. def test_with_statement_exception(self):
  16. try:
  17. with patch('%s.something' % __name__, sentinel.Something2):
  18. self.assertEqual(something, sentinel.Something2, "unpatched")
  19. raise Exception('pow')
  20. except Exception:
  21. pass
  22. else:
  23. self.fail("patch swallowed exception")
  24. self.assertEqual(something, sentinel.Something)
  25. def test_with_statement_as(self):
  26. with patch('%s.something' % __name__) as mock_something:
  27. self.assertEqual(something, mock_something, "unpatched")
  28. self.assertTrue(is_instance(mock_something, MagicMock),
  29. "patching wrong type")
  30. self.assertEqual(something, sentinel.Something)
  31. def test_patch_object_with_statement(self):
  32. class Foo(object):
  33. something = 'foo'
  34. original = Foo.something
  35. with patch.object(Foo, 'something'):
  36. self.assertNotEqual(Foo.something, original, "unpatched")
  37. self.assertEqual(Foo.something, original)
  38. def test_with_statement_nested(self):
  39. with catch_warnings(record=True):
  40. with patch('%s.something' % __name__) as mock_something:
  41. with patch('%s.something_else' % __name__) as mock_something_else:
  42. self.assertEqual(something, mock_something, "unpatched")
  43. self.assertEqual(something_else, mock_something_else,
  44. "unpatched")
  45. self.assertEqual(something, sentinel.Something)
  46. self.assertEqual(something_else, sentinel.SomethingElse)
  47. def test_with_statement_specified(self):
  48. with patch('%s.something' % __name__, sentinel.Patched) as mock_something:
  49. self.assertEqual(something, mock_something, "unpatched")
  50. self.assertEqual(mock_something, sentinel.Patched, "wrong patch")
  51. self.assertEqual(something, sentinel.Something)
  52. def testContextManagerMocking(self):
  53. mock = Mock()
  54. mock.__enter__ = Mock()
  55. mock.__exit__ = Mock()
  56. mock.__exit__.return_value = False
  57. with mock as m:
  58. self.assertEqual(m, mock.__enter__.return_value)
  59. mock.__enter__.assert_called_with()
  60. mock.__exit__.assert_called_with(None, None, None)
  61. def test_context_manager_with_magic_mock(self):
  62. mock = MagicMock()
  63. with self.assertRaises(TypeError):
  64. with mock:
  65. 'foo' + 3
  66. mock.__enter__.assert_called_with()
  67. self.assertTrue(mock.__exit__.called)
  68. def test_with_statement_same_attribute(self):
  69. with patch('%s.something' % __name__, sentinel.Patched) as mock_something:
  70. self.assertEqual(something, mock_something, "unpatched")
  71. with patch('%s.something' % __name__) as mock_again:
  72. self.assertEqual(something, mock_again, "unpatched")
  73. self.assertEqual(something, mock_something,
  74. "restored with wrong instance")
  75. self.assertEqual(something, sentinel.Something, "not restored")
  76. def test_with_statement_imbricated(self):
  77. with patch('%s.something' % __name__) as mock_something:
  78. self.assertEqual(something, mock_something, "unpatched")
  79. with patch('%s.something_else' % __name__) as mock_something_else:
  80. self.assertEqual(something_else, mock_something_else,
  81. "unpatched")
  82. self.assertEqual(something, sentinel.Something)
  83. self.assertEqual(something_else, sentinel.SomethingElse)
  84. def test_dict_context_manager(self):
  85. foo = {}
  86. with patch.dict(foo, {'a': 'b'}):
  87. self.assertEqual(foo, {'a': 'b'})
  88. self.assertEqual(foo, {})
  89. with self.assertRaises(NameError):
  90. with patch.dict(foo, {'a': 'b'}):
  91. self.assertEqual(foo, {'a': 'b'})
  92. raise NameError('Konrad')
  93. self.assertEqual(foo, {})
  94. class TestMockOpen(unittest.TestCase):
  95. def test_mock_open(self):
  96. mock = mock_open()
  97. with patch('%s.open' % __name__, mock, create=True) as patched:
  98. self.assertIs(patched, mock)
  99. open('foo')
  100. mock.assert_called_once_with('foo')
  101. def test_mock_open_context_manager(self):
  102. mock = mock_open()
  103. handle = mock.return_value
  104. with patch('%s.open' % __name__, mock, create=True):
  105. with open('foo') as f:
  106. f.read()
  107. expected_calls = [call('foo'), call().__enter__(), call().read(),
  108. call().__exit__(None, None, None)]
  109. self.assertEqual(mock.mock_calls, expected_calls)
  110. self.assertIs(f, handle)
  111. def test_mock_open_context_manager_multiple_times(self):
  112. mock = mock_open()
  113. with patch('%s.open' % __name__, mock, create=True):
  114. with open('foo') as f:
  115. f.read()
  116. with open('bar') as f:
  117. f.read()
  118. expected_calls = [
  119. call('foo'), call().__enter__(), call().read(),
  120. call().__exit__(None, None, None),
  121. call('bar'), call().__enter__(), call().read(),
  122. call().__exit__(None, None, None)]
  123. self.assertEqual(mock.mock_calls, expected_calls)
  124. def test_explicit_mock(self):
  125. mock = MagicMock()
  126. mock_open(mock)
  127. with patch('%s.open' % __name__, mock, create=True) as patched:
  128. self.assertIs(patched, mock)
  129. open('foo')
  130. mock.assert_called_once_with('foo')
  131. def test_read_data(self):
  132. mock = mock_open(read_data='foo')
  133. with patch('%s.open' % __name__, mock, create=True):
  134. h = open('bar')
  135. result = h.read()
  136. self.assertEqual(result, 'foo')
  137. def test_readline_data(self):
  138. # Check that readline will return all the lines from the fake file
  139. mock = mock_open(read_data='foo\nbar\nbaz\n')
  140. with patch('%s.open' % __name__, mock, create=True):
  141. h = open('bar')
  142. line1 = h.readline()
  143. line2 = h.readline()
  144. line3 = h.readline()
  145. self.assertEqual(line1, 'foo\n')
  146. self.assertEqual(line2, 'bar\n')
  147. self.assertEqual(line3, 'baz\n')
  148. # Check that we properly emulate a file that doesn't end in a newline
  149. mock = mock_open(read_data='foo')
  150. with patch('%s.open' % __name__, mock, create=True):
  151. h = open('bar')
  152. result = h.readline()
  153. self.assertEqual(result, 'foo')
  154. def test_readlines_data(self):
  155. # Test that emulating a file that ends in a newline character works
  156. mock = mock_open(read_data='foo\nbar\nbaz\n')
  157. with patch('%s.open' % __name__, mock, create=True):
  158. h = open('bar')
  159. result = h.readlines()
  160. self.assertEqual(result, ['foo\n', 'bar\n', 'baz\n'])
  161. # Test that files without a final newline will also be correctly
  162. # emulated
  163. mock = mock_open(read_data='foo\nbar\nbaz')
  164. with patch('%s.open' % __name__, mock, create=True):
  165. h = open('bar')
  166. result = h.readlines()
  167. self.assertEqual(result, ['foo\n', 'bar\n', 'baz'])
  168. def test_read_bytes(self):
  169. mock = mock_open(read_data=b'\xc6')
  170. with patch('%s.open' % __name__, mock, create=True):
  171. with open('abc', 'rb') as f:
  172. result = f.read()
  173. self.assertEqual(result, b'\xc6')
  174. def test_readline_bytes(self):
  175. m = mock_open(read_data=b'abc\ndef\nghi\n')
  176. with patch('%s.open' % __name__, m, create=True):
  177. with open('abc', 'rb') as f:
  178. line1 = f.readline()
  179. line2 = f.readline()
  180. line3 = f.readline()
  181. self.assertEqual(line1, b'abc\n')
  182. self.assertEqual(line2, b'def\n')
  183. self.assertEqual(line3, b'ghi\n')
  184. def test_readlines_bytes(self):
  185. m = mock_open(read_data=b'abc\ndef\nghi\n')
  186. with patch('%s.open' % __name__, m, create=True):
  187. with open('abc', 'rb') as f:
  188. result = f.readlines()
  189. self.assertEqual(result, [b'abc\n', b'def\n', b'ghi\n'])
  190. def test_mock_open_read_with_argument(self):
  191. # At one point calling read with an argument was broken
  192. # for mocks returned by mock_open
  193. some_data = 'foo\nbar\nbaz'
  194. mock = mock_open(read_data=some_data)
  195. self.assertEqual(mock().read(10), some_data)
  196. def test_interleaved_reads(self):
  197. # Test that calling read, readline, and readlines pulls data
  198. # sequentially from the data we preload with
  199. mock = mock_open(read_data='foo\nbar\nbaz\n')
  200. with patch('%s.open' % __name__, mock, create=True):
  201. h = open('bar')
  202. line1 = h.readline()
  203. rest = h.readlines()
  204. self.assertEqual(line1, 'foo\n')
  205. self.assertEqual(rest, ['bar\n', 'baz\n'])
  206. mock = mock_open(read_data='foo\nbar\nbaz\n')
  207. with patch('%s.open' % __name__, mock, create=True):
  208. h = open('bar')
  209. line1 = h.readline()
  210. rest = h.read()
  211. self.assertEqual(line1, 'foo\n')
  212. self.assertEqual(rest, 'bar\nbaz\n')
  213. def test_overriding_return_values(self):
  214. mock = mock_open(read_data='foo')
  215. handle = mock()
  216. handle.read.return_value = 'bar'
  217. handle.readline.return_value = 'bar'
  218. handle.readlines.return_value = ['bar']
  219. self.assertEqual(handle.read(), 'bar')
  220. self.assertEqual(handle.readline(), 'bar')
  221. self.assertEqual(handle.readlines(), ['bar'])
  222. # call repeatedly to check that a StopIteration is not propagated
  223. self.assertEqual(handle.readline(), 'bar')
  224. self.assertEqual(handle.readline(), 'bar')
  225. if __name__ == '__main__':
  226. unittest.main()