01-fix-tests.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. --- contrib/python/aioresponses/tests/test_aioresponses.py (index)
  2. +++ contrib/python/aioresponses/tests/test_aioresponses.py (working tree)
  3. @@ -8,9 +8,10 @@ from unittest.mock import patch
  4. from aiohttp import hdrs
  5. from aiohttp import http
  6. +from aiohttp import web
  7. from aiohttp.client import ClientSession
  8. from aiohttp.client_reqrep import ClientResponse
  9. -from ddt import ddt, data
  10. +from aiohttp.test_utils import TestServer
  11. from packaging.version import Version
  12. try:
  13. @@ -31,10 +32,11 @@ from aioresponses import CallbackResult, aioresponses
  14. -@ddt
  15. class AIOResponsesTestCase(AsyncTestCase):
  16. async def setup(self):
  17. self.url = 'http://example.com/api?foo=bar#fragment'
  18. self.session = ClientSession()
  19. + self.external_server = self.make_external_server()
  20. + await self.external_server.start_server()
  21. async def teardown(self):
  22. @@ -46,24 +48,43 @@ class AIOResponsesTestCase(AsyncTestCase):
  23. def run_async(self, coroutine: Union[Coroutine, Generator]):
  24. return self.loop.run_until_complete(coroutine)
  25. + def make_external_server(self):
  26. + """
  27. + В оригинальном коде для тестирования passthrough исполняются настоящие
  28. + запросы до сайта http://httpbin.org. В sandbox нет интернета, потому я заменил
  29. + httpbin на локальный сервер. Осторожнее при обновлении!
  30. + """
  31. + async def testserver_status_201(request):
  32. + return web.Response(status=201)
  33. +
  34. + async def testserver_get(request):
  35. + return web.Response()
  36. +
  37. + app = web.Application()
  38. + app.router.add_get('/status/201', testserver_status_201)
  39. + app.router.add_get('/get', testserver_get)
  40. +
  41. + return TestServer(app)
  42. +
  43. async def request(self, url: str):
  44. return await self.session.get(url)
  45. - @data(
  46. - hdrs.METH_HEAD,
  47. - hdrs.METH_GET,
  48. - hdrs.METH_POST,
  49. - hdrs.METH_PUT,
  50. - hdrs.METH_PATCH,
  51. - hdrs.METH_DELETE,
  52. - hdrs.METH_OPTIONS,
  53. - )
  54. - @patch('aioresponses.aioresponses.add')
  55. @fail_on(unused_loop=False)
  56. - def test_shortcut_method(self, http_method, mocked):
  57. - with aioresponses() as m:
  58. - getattr(m, http_method.lower())(self.url)
  59. - mocked.assert_called_once_with(self.url, method=http_method)
  60. + def test_shortcut_method(self):
  61. + for http_method in (
  62. + hdrs.METH_HEAD,
  63. + hdrs.METH_GET,
  64. + hdrs.METH_POST,
  65. + hdrs.METH_PUT,
  66. + hdrs.METH_PATCH,
  67. + hdrs.METH_DELETE,
  68. + hdrs.METH_OPTIONS,
  69. + ):
  70. + with self.subTest(), \
  71. + patch('aioresponses.aioresponses.add') as mocked, \
  72. + aioresponses() as m:
  73. + getattr(m, http_method.lower())(self.url)
  74. + mocked.assert_called_once_with(self.url, method=http_method)
  75. @aioresponses()
  76. def test_returned_instance(self, m):
  77. @@ -369,1 +390,1 @@ class AIOResponsesTestCase(AsyncTestCase):
  78. - external_api = 'http://httpbin.org/status/201'
  79. + external_api = str(self.external_server.make_url('/status/201'))
  80. @@ -386,1 +407,1 @@ class AIOResponsesTestCase(AsyncTestCase):
  81. - external_api = 'http://httpbin.org/get'
  82. + external_api = str(self.external_server.make_url('/get'))
  83. @@ -400,1 +421,1 @@ class AIOResponsesTestCase(AsyncTestCase):
  84. - self.assertEqual(str(ext.url), 'http://httpbin.org/get?foo=bar')
  85. + self.assertEqual(str(ext.url), external_api + '?foo=bar')
  86. --- contrib/python/aioresponses/tests/test_compat.py (index)
  87. +++ contrib/python/aioresponses/tests/test_compat.py (working tree)
  88. @@ -2,7 +2,6 @@
  89. from typing import Union
  90. from unittest import TestCase
  91. -from ddt import ddt, data
  92. from yarl import URL
  93. from aioresponses.compat import merge_params
  94. @@ -12,7 +11,6 @@ def get_url(url: str, as_str: bool) -> Union[URL, str]:
  95. return url if as_str else URL(url)
  96. -@ddt
  97. class CompatTestCase(TestCase):
  98. use_default_loop = False
  99. @@ -20,29 +18,33 @@ class CompatTestCase(TestCase):
  100. self.url_with_parameters = 'http://example.com/api?foo=bar#fragment'
  101. self.url_without_parameters = 'http://example.com/api?#fragment'
  102. - @data(True, False)
  103. - def test_no_params_returns_same_url__as_str(self, as_str):
  104. - url = get_url(self.url_with_parameters, as_str)
  105. - self.assertEqual(
  106. - merge_params(url, None), URL(self.url_with_parameters)
  107. - )
  108. -
  109. - @data(True, False)
  110. - def test_empty_params_returns_same_url__as_str(self, as_str):
  111. - url = get_url(self.url_with_parameters, as_str)
  112. - self.assertEqual(merge_params(url, {}), URL(self.url_with_parameters))
  113. -
  114. - @data(True, False)
  115. - def test_both_with_params_returns_corrected_url__as_str(self, as_str):
  116. - url = get_url(self.url_with_parameters, as_str)
  117. - self.assertEqual(
  118. - merge_params(url, {'x': 42}),
  119. - URL('http://example.com/api?foo=bar&x=42#fragment'),
  120. - )
  121. -
  122. - @data(True, False)
  123. - def test_base_without_params_returns_corrected_url__as_str(self, as_str):
  124. - expected_url = URL('http://example.com/api?x=42#fragment')
  125. - url = get_url(self.url_without_parameters, as_str)
  126. -
  127. - self.assertEqual(merge_params(url, {'x': 42}), expected_url)
  128. + def test_no_params_returns_same_url__as_str(self):
  129. + for as_str in (True, False):
  130. + with self.subTest():
  131. + url = get_url(self.url_with_parameters, as_str)
  132. + self.assertEqual(
  133. + merge_params(url, None), URL(self.url_with_parameters)
  134. + )
  135. +
  136. + def test_empty_params_returns_same_url__as_str(self):
  137. + for as_str in (True, False):
  138. + with self.subTest():
  139. + url = get_url(self.url_with_parameters, as_str)
  140. + self.assertEqual(merge_params(url, {}), URL(self.url_with_parameters))
  141. +
  142. + def test_both_with_params_returns_corrected_url__as_str(self):
  143. + for as_str in (True, False):
  144. + with self.subTest():
  145. + url = get_url(self.url_with_parameters, as_str)
  146. + self.assertEqual(
  147. + merge_params(url, {'x': 42}),
  148. + URL('http://example.com/api?foo=bar&x=42#fragment'),
  149. + )
  150. +
  151. + def test_base_without_params_returns_corrected_url__as_str(self):
  152. + for as_str in (True, False):
  153. + with self.subTest():
  154. + expected_url = URL('http://example.com/api?x=42#fragment')
  155. + url = get_url(self.url_without_parameters, as_str)
  156. +
  157. + self.assertEqual(merge_params(url, {'x': 42}), expected_url)
  158. --- contrib/python/aioresponses/tests/test_aioresponses.py (index)
  159. +++ contrib/python/aioresponses/tests/test_aioresponses.py (working tree)
  160. @@ -818,7 +818,7 @@ class AIOResponseRedirectTest(AsyncTestCase):
  161. self.assertEqual(len(response.history), 1)
  162. self.assertEqual(str(response.history[0].url), url)
  163. - async def test_pass_through_unmatched_requests(self):
  164. + async def _test_pass_through_unmatched_requests(self):
  165. matched_url = "https://matched_example.org"
  166. unmatched_url = "https://httpbin.org/get"
  167. params_unmatched = {'foo': 'bar'}