Exemplo n.º 1
0
    def test_session_property(self, base_absolut_url):
        requester = APIRequester(base_url=base_absolut_url)

        assert requester.session == requester._session

        with pytest.raises(AttributeError):
            requester.session = None
Exemplo n.º 2
0
    def test_close(self, m_session, base_absolut_url):
        session = Mock()
        session.headers = {}
        m_session.return_value = session

        requester = APIRequester(base_url=base_absolut_url)
        requester.close()
        session.close.assert_called_once()
Exemplo n.º 3
0
    def test__request_wrong_response(self, requests_mock, base_absolut_url):
        path = "test"
        requester = APIRequester(base_url=base_absolut_url)
        requests_mock.get(urljoin(base_absolut_url, path),
                          text="{ is not JSON ]")

        with pytest.raises(ParsingError):
            requester._request(method="get", path=path)
Exemplo n.º 4
0
    def test__request_with_success_status(self, status_code, requests_mock,
                                          base_absolut_url):
        path = "test"
        requester = APIRequester(base_url=base_absolut_url)
        requests_mock.get(urljoin(base_absolut_url, path),
                          status_code=status_code,
                          text='{"test": 1}')

        assert requester._request(method="get", path=path) == {"test": 1}
Exemplo n.º 5
0
    def test__request_with_not_success_status(self, status_code, exception,
                                              requests_mock, base_absolut_url):
        path = "test"
        requester = APIRequester(base_url=base_absolut_url)
        requests_mock.get(urljoin(base_absolut_url, path),
                          status_code=status_code,
                          text="{}")

        with pytest.raises(exception):
            requester._request(method="get", path=path)
Exemplo n.º 6
0
    def test_init_custom_params(self, m_session, base_absolut_url):
        session = Mock()
        m_session.return_value = session

        default_headers = {"some_custom_header": "value"}
        timeout = 100
        retry_delay = 1  # 1s
        max_retries = 5

        requester = APIRequester(
            base_url=base_absolut_url,
            timeout=timeout,
            retry_delay=retry_delay,  # 1s
            max_retries=max_retries,
            default_headers=default_headers,
        )

        headers = copy(requester.default_headers)
        headers.update(default_headers)

        session.headers.update.assert_called_once_with(headers)
        assert requester.base_url == base_absolut_url
        assert requester._timeout == timeout
        assert requester._retry_delay == retry_delay
        assert requester._max_retries == max_retries
Exemplo n.º 7
0
    def test_init_default_params(self, m_session, base_absolut_url):
        session = Mock()
        m_session.return_value = session

        requester = APIRequester(base_url=base_absolut_url)

        session.headers.update.assert_called_once_with(
            requester.default_headers)
        assert requester.base_url == base_absolut_url
Exemplo n.º 8
0
    def test_destructor(self, m_close, base_absolut_url):
        m_close.return_value = Mock()

        _requester = APIRequester(base_url=base_absolut_url)

        m_close.assert_not_called()

        _requester = None  # noqa F841
        m_close.assert_called_once()
Exemplo n.º 9
0
    def test_request(
        self,
        m_request,
        m_sleep,
        base_absolut_url,
        should_retry,
        max_retries,
        retry_delay,
        num_retries,
    ):
        class SomeException(APIException):
            def __init__(self, headers=None, context=None):
                super().__init__(
                    context=context,
                    headers=headers,
                    http_status=None,
                    should_retry=should_retry,
                )

        m_request.side_effect = SomeException

        requester = APIRequester(
            base_url=base_absolut_url,
            max_retries=max_retries,
            retry_delay=retry_delay,
            timeout=0,
        )

        kw = {
            "method": "get",
            "path": "test",
            "params": {
                "test": "value"
            },
            "request_data": {
                "should_retry": should_retry
            },
        }

        with pytest.raises(APIException):
            requester.request(**kw)

        assert m_request.call_count == num_retries
        assert m_sleep.call_count == num_retries - 1
Exemplo n.º 10
0
 def get_resource(self, base_absolut_url):
     return self.resource_class(requester=APIRequester(
         base_url=base_absolut_url))
Exemplo n.º 11
0
 def test_kwargs_in_requester(self, m_request, base_absolut_url, kwargs):
     m_request.return_value = "response"
     _requester = APIRequester(base_url=base_absolut_url)
     _requester.request('get', 'test', **kwargs)
     m_request.assert_called_once_with('get', 'test', **kwargs)
Exemplo n.º 12
0
 def test__clear_data(self, in_data, out_data, base_absolut_url):
     requester = APIRequester(base_url=base_absolut_url)
     assert requester._clear_data(in_data) == out_data