Skip to content

Commit

Permalink
修复单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-smile committed Nov 20, 2023
1 parent 6cc2bcc commit 47af7fc
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 34 deletions.
6 changes: 3 additions & 3 deletions sdks/bkapi-client-core/bkapi_client_core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Any, Dict, Optional

from requests import Response
from requests.exceptions import HTTPError, RequestException, JSONDecodeError
from requests.exceptions import HTTPError, RequestException
from requests.sessions import merge_setting
from requests.structures import CaseInsensitiveDict

Expand Down Expand Up @@ -267,7 +267,7 @@ def _handle_exception(
if isinstance(exception, RequestException):
response = exception.response
response_headers_representer = ResponseHeadersRepresenter(response and response.headers)
logger.exception(
logger.warning(
"request bkapi failed. status_code: %s, %s\n%s",
response and response.status_code,
response_headers_representer,
Expand Down Expand Up @@ -318,7 +318,7 @@ def _handle_response_content(
# 如此,方便调用者在同一层中处理 http 状态码和 json 两个异常
try:
response_json = response.json()
except (TypeError, json.JSONDecodeError, JSONDecodeError):
except (TypeError, json.JSONDecodeError):
raise JSONResponseError(
"The response is not a valid JSON",
response=response,
Expand Down
48 changes: 38 additions & 10 deletions sdks/bkapi-client-core/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,35 @@ def test_has_apigateway_error(self, headers, expected):
headers = ResponseHeadersRepresenter(headers)
assert headers.has_apigateway_error == expected

def test_str(self, faker):
headers = ResponseHeadersRepresenter(
{
"X-Bkapi-Error-Code": faker.pystr(),
"X-Bkapi-Error-Message": faker.pystr(),
"X-Bkapi-Request-Id": faker.uuid4(),
}
)
assert "request_id:" in str(headers)
@pytest.mark.parametrize(
"headers, expected",
[
(
{
"X-Bkapi-Request-Id": "abcdef",
"X-Bkapi-Error-Code": "foo",
"X-Bkapi-Error-Message": "error",
},
"request_id: abcdef, error_code: foo, error",
),
(
{
"X-Bkapi-Request-Id": "abcdef",
"X-Bkapi-Error-Code": "foo",
},
"request_id: abcdef, error_code: foo",
),
(
{
"X-Bkapi-Request-Id": "abcdef",
},
"request_id: abcdef",
),
]
)
def test_str(self, headers, expected):
representer = ResponseHeadersRepresenter(headers)
assert str(representer) == expected


class TestBaseClient:
Expand Down Expand Up @@ -428,12 +448,20 @@ def test_handler_exception(self, mocker):
def test_handle_response_content(self, mocker):
assert self.client._handle_response_content(mocker.MagicMock(), None) is None

response = {
"headers": {"X-Bkapi-Request-Id": "abcd"},
"json.return_value": {"foo": "bar"},
"raise_for_status.return_value": None,
}
result = self.client._handle_response_content(None, mocker.MagicMock(**response))
assert result == {"foo": "bar"}

@pytest.mark.parametrize(
"response",
[
{"headers": {"X-Bkapi-Error-Code": "error"}},
{"json.side_effect": TypeError},
{"raise_for_status.side_effect": RequestException("error")},
{"raise_for_status.return_value": None, "json.side_effect": TypeError},
],
)
def test_handle_response_content_error(self, mocker, response):
Expand Down
110 changes: 96 additions & 14 deletions sdks/bkapi-client-core/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,54 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
"""
import pytest
import requests

from bkapi_client_core import exceptions
from bkapi_client_core.client import ResponseHeadersRepresenter


class TestResponseError:
def test_str(self, mocker, faker):
err = exceptions.ResponseError("error", response=None)
assert str(err) == "error"

err = exceptions.ResponseError(
"error",
response=mocker.MagicMock(),
response_headers_representer=ResponseHeadersRepresenter(
@pytest.mark.parametrize(
"error, response, headers, expected",
[
(
"error",
{
"status_code": 400,
},
{
"X-Bkapi-Error-Code": "code",
"X-Bkapi-Error-Message": "error-message",
"X-Bkapi-Request-Id": "abcdef",
},
"error, status_code: 400, request_id: abcdef, error_code: code, error-message",
),
(
"error",
None,
None,
"error",
),
(
"error",
{
"status_code": 400,
},
{
"X-Bkapi-Error-Code": faker.pystr(),
"X-Bkapi-Error-Message": faker.pystr(),
"X-Bkapi-Request-Id": faker.uuid4(),
}
"X-Bkapi-Request-Id": "abcdef",
},
"error, status_code: 400, request_id: abcdef",
),
]
)
def test_str(self, mocker, error, response, headers, expected):
err = exceptions.ResponseError(
error,
response=response and mocker.MagicMock(**response),
response_headers_representer=ResponseHeadersRepresenter(headers),
)
assert str(err).startswith("error")
assert "request_id:" in str(err)
assert str(err) == expected

def test_error_code(self, mocker):
err = exceptions.ResponseError(
Expand All @@ -54,3 +80,59 @@ def test_request_id(self, mocker):
response_headers_representer=ResponseHeadersRepresenter({"X-Bkapi-Request-Id": "abcdef"}),
)
assert err.request_id == "abcdef"

def test_curl_command(self, mocker, faker):
mocker.patch(
"bkapi_client_core.exceptions.CurlRequest.to_curl", return_value="curl -X GET http://example.com",
)
err = exceptions.ResponseError(
faker.pystr(),
response=mocker.MagicMock(),
response_headers_representer=ResponseHeadersRepresenter({"X-Bkapi-Request-Id": "abcdef"}),
)
assert err.curl_command == "curl -X GET http://example.com"

@pytest.mark.parametrize(
"request_, expected",
[
(None, None),
({"method": "GET"}, "GET"),
]
)
def test_request_method(self, mocker, faker, request_, expected):
err = exceptions.ResponseError(
faker.pystr(),
request=request_ and mocker.MagicMock(**request_),
response_headers_representer=ResponseHeadersRepresenter({"X-Bkapi-Request-Id": "abcdef"}),
)
assert err.request_method == expected

@pytest.mark.parametrize(
"request_, expected",
[
(None, ""),
({"url": "http://example.com/test?foo=bar"}, "http://example.com/test"),
]
)
def test_request_url(self, mocker, faker, request_, expected):
err = exceptions.ResponseError(
faker.pystr(),
request=request_ and mocker.MagicMock(**request_),
response_headers_representer=ResponseHeadersRepresenter({"X-Bkapi-Request-Id": "abcdef"}),
)
assert err.request_url == expected

@pytest.mark.parametrize(
"response, expected",
[
(None, None),
({"status_code": 200}, 200),
]
)
def test_request_status_code(self, mocker, faker, response, expected):
err = exceptions.ResponseError(
faker.pystr(),
response=response and mocker.MagicMock(**response),
response_headers_representer=ResponseHeadersRepresenter({"X-Bkapi-Request-Id": "abcdef"}),
)
assert err.response_status_code == expected
16 changes: 9 additions & 7 deletions sdks/bkapi-client-core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* specific language governing permissions and limitations under the License.
"""
import pytest
import requests

from bkapi_client_core import utils

Expand Down Expand Up @@ -42,10 +43,11 @@ def test_to_curl(mocker, curlify, request_, expected):


class TestCurlRequest:
def test_str(self, mocker):
curlify = mocker.patch.object(utils, "curlify")
curlify.to_curl.return_value = "curl http://example.com"

request = mocker.MagicMock(url="http://example.com")
result = utils.CurlRequest(request)
assert str(result) == "curl http://example.com"
def test_str(self):
request = requests.Request("GET", "https://example.com/get").prepare()
result = utils.CurlRequest(request).to_curl()
assert result == "curl -X GET https://example.com/get"

request = requests.Request("GET", "https://example.com/get", params={"foo": "bar"}, headers={"x-token": "test"}).prepare()
result = utils.CurlRequest(request).to_curl()
assert result == "curl -X GET 'https://example.com/get?foo=bar'"

0 comments on commit 47af7fc

Please sign in to comment.