01-fix-tests.patch 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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,7 +390,7 @@ class AIOResponsesTestCase(AsyncTestCase):
  78. assert str(exception_info.exception) == "Session is closed"
  79. async def test_address_as_instance_of_url_combined_with_pass_through(self):
  80. - external_api = 'http://httpbin.org/status/201'
  81. + external_api = str(self.external_server.make_url('/status/201'))
  82. async def doit():
  83. api_resp = await self.session.get(self.url)
  84. @@ -386,7 +407,7 @@ class AIOResponsesTestCase(AsyncTestCase):
  85. self.assertEqual(ext.status, 201)
  86. async def test_pass_through_with_origin_params(self):
  87. - external_api = 'http://httpbin.org/get'
  88. + external_api = str(self.external_server.make_url('/get'))
  89. async def doit(params):
  90. # we have to hit actual url,
  91. @@ -400,7 +421,7 @@ class AIOResponsesTestCase(AsyncTestCase):
  92. params = {'foo': 'bar'}
  93. ext = await doit(params=params)
  94. self.assertEqual(ext.status, 200)
  95. - self.assertEqual(str(ext.url), 'http://httpbin.org/get?foo=bar')
  96. + self.assertEqual(str(ext.url), external_api + '?foo=bar')
  97. @aioresponses()
  98. async def test_custom_response_class(self, m):
  99. --- contrib/python/aioresponses/tests/test_compat.py (index)
  100. +++ contrib/python/aioresponses/tests/test_compat.py (working tree)
  101. @@ -2,7 +2,6 @@
  102. from typing import Union
  103. from unittest import TestCase
  104. -from ddt import ddt, data
  105. from yarl import URL
  106. from aioresponses.compat import merge_params
  107. @@ -12,7 +11,6 @@ def get_url(url: str, as_str: bool) -> Union[URL, str]:
  108. return url if as_str else URL(url)
  109. -@ddt
  110. class CompatTestCase(TestCase):
  111. use_default_loop = False
  112. @@ -20,29 +18,33 @@ class CompatTestCase(TestCase):
  113. self.url_with_parameters = 'http://example.com/api?foo=bar#fragment'
  114. self.url_without_parameters = 'http://example.com/api?#fragment'
  115. - @data(True, False)
  116. - def test_no_params_returns_same_url__as_str(self, as_str):
  117. - url = get_url(self.url_with_parameters, as_str)
  118. - self.assertEqual(
  119. - merge_params(url, None), URL(self.url_with_parameters)
  120. - )
  121. -
  122. - @data(True, False)
  123. - def test_empty_params_returns_same_url__as_str(self, as_str):
  124. - url = get_url(self.url_with_parameters, as_str)
  125. - self.assertEqual(merge_params(url, {}), URL(self.url_with_parameters))
  126. -
  127. - @data(True, False)
  128. - def test_both_with_params_returns_corrected_url__as_str(self, as_str):
  129. - url = get_url(self.url_with_parameters, as_str)
  130. - self.assertEqual(
  131. - merge_params(url, {'x': 42}),
  132. - URL('http://example.com/api?foo=bar&x=42#fragment'),
  133. - )
  134. -
  135. - @data(True, False)
  136. - def test_base_without_params_returns_corrected_url__as_str(self, as_str):
  137. - expected_url = URL('http://example.com/api?x=42#fragment')
  138. - url = get_url(self.url_without_parameters, as_str)
  139. -
  140. - self.assertEqual(merge_params(url, {'x': 42}), expected_url)
  141. + def test_no_params_returns_same_url__as_str(self):
  142. + for as_str in (True, False):
  143. + with self.subTest():
  144. + url = get_url(self.url_with_parameters, as_str)
  145. + self.assertEqual(
  146. + merge_params(url, None), URL(self.url_with_parameters)
  147. + )
  148. +
  149. + def test_empty_params_returns_same_url__as_str(self):
  150. + for as_str in (True, False):
  151. + with self.subTest():
  152. + url = get_url(self.url_with_parameters, as_str)
  153. + self.assertEqual(merge_params(url, {}), URL(self.url_with_parameters))
  154. +
  155. + def test_both_with_params_returns_corrected_url__as_str(self):
  156. + for as_str in (True, False):
  157. + with self.subTest():
  158. + url = get_url(self.url_with_parameters, as_str)
  159. + self.assertEqual(
  160. + merge_params(url, {'x': 42}),
  161. + URL('http://example.com/api?foo=bar&x=42#fragment'),
  162. + )
  163. +
  164. + def test_base_without_params_returns_corrected_url__as_str(self):
  165. + for as_str in (True, False):
  166. + with self.subTest():
  167. + expected_url = URL('http://example.com/api?x=42#fragment')
  168. + url = get_url(self.url_without_parameters, as_str)
  169. +
  170. + self.assertEqual(merge_params(url, {'x': 42}), expected_url)