aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/aioresponses/tests/test_compat.py
blob: 56571c82b9cafae0bcbd9d1e386958008ee18b57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
from typing import Union
from unittest import TestCase

from yarl import URL

from aioresponses.compat import merge_params


def get_url(url: str, as_str: bool) -> Union[URL, str]:
    return url if as_str else URL(url)


class CompatTestCase(TestCase):
    use_default_loop = False

    def setUp(self):
        self.url_with_parameters = 'http://example.com/api?foo=bar#fragment'
        self.url_without_parameters = 'http://example.com/api?#fragment'

    def test_no_params_returns_same_url__as_str(self):
        for as_str in (True, False):
            with self.subTest():
                url = get_url(self.url_with_parameters, as_str)
                self.assertEqual(
                    merge_params(url, None), URL(self.url_with_parameters)
                )

    def test_empty_params_returns_same_url__as_str(self):
        for as_str in (True, False):
            with self.subTest():
                url = get_url(self.url_with_parameters, as_str)
                self.assertEqual(merge_params(url, {}), URL(self.url_with_parameters))

    def test_both_with_params_returns_corrected_url__as_str(self):
        for as_str in (True, False):
            with self.subTest():
                url = get_url(self.url_with_parameters, as_str)
                self.assertEqual(
                    merge_params(url, {'x': 42}),
                    URL('http://example.com/api?foo=bar&x=42#fragment'),
                )

    def test_base_without_params_returns_corrected_url__as_str(self):
        for as_str in (True, False):
            with self.subTest():
                expected_url = URL('http://example.com/api?x=42#fragment')
                url = get_url(self.url_without_parameters, as_str)

                self.assertEqual(merge_params(url, {'x': 42}), expected_url)