test_compat.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. from typing import Union
  3. from unittest import TestCase
  4. from yarl import URL
  5. from aioresponses.compat import merge_params
  6. def get_url(url: str, as_str: bool) -> Union[URL, str]:
  7. return url if as_str else URL(url)
  8. class CompatTestCase(TestCase):
  9. use_default_loop = False
  10. def setUp(self):
  11. self.url_with_parameters = 'http://example.com/api?foo=bar#fragment'
  12. self.url_without_parameters = 'http://example.com/api?#fragment'
  13. def test_no_params_returns_same_url__as_str(self):
  14. for as_str in (True, False):
  15. with self.subTest():
  16. url = get_url(self.url_with_parameters, as_str)
  17. self.assertEqual(
  18. merge_params(url, None), URL(self.url_with_parameters)
  19. )
  20. def test_empty_params_returns_same_url__as_str(self):
  21. for as_str in (True, False):
  22. with self.subTest():
  23. url = get_url(self.url_with_parameters, as_str)
  24. self.assertEqual(merge_params(url, {}), URL(self.url_with_parameters))
  25. def test_both_with_params_returns_corrected_url__as_str(self):
  26. for as_str in (True, False):
  27. with self.subTest():
  28. url = get_url(self.url_with_parameters, as_str)
  29. self.assertEqual(
  30. merge_params(url, {'x': 42}),
  31. URL('http://example.com/api?foo=bar&x=42#fragment'),
  32. )
  33. def test_base_without_params_returns_corrected_url__as_str(self):
  34. for as_str in (True, False):
  35. with self.subTest():
  36. expected_url = URL('http://example.com/api?x=42#fragment')
  37. url = get_url(self.url_without_parameters, as_str)
  38. self.assertEqual(merge_params(url, {'x': 42}), expected_url)