Skip to content

Commit

Permalink
fix: Add test for request error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle committed Jun 16, 2024
1 parent e8a5438 commit 45ba9fb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
6 changes: 5 additions & 1 deletion tests/Functional/Client/test_Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from requests.exceptions import RequestException, HTTPError

from tests.Utils.TestClient import mock_requests, test_client
from tests.Utils.TestLogger import test_log_handler, LogHandler


class TestClient:
Expand Down Expand Up @@ -83,6 +83,7 @@ class TestClient:
def test_post_raw_returns_error_response(
self,
test_client,
test_log_handler: LogHandler,
mock_requests,
expected_response_status,
expected_reason,
Expand Down Expand Up @@ -132,3 +133,6 @@ def test_post_raw_returns_error_response(
f"docs_url='{api_error.docs_url}', "
f"field_errors={api_error.field_errors}"
")")

assert test_log_handler.get_logs()[0].message == \
f"Request failed: {expected_response_status} Client Error: {expected_reason} for url: {expected_request_url}. {repr(api_error)}"
15 changes: 10 additions & 5 deletions tests/Utils/TestClient.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
from os import getenv
from pytest import fixture
from requests_mock import Mocker
from logging import Logger

from paddle_billing import Client, Environment, Options

from tests.Utils.TestLogger import test_logger


class TestClient:
def __init__(
self,
client: Client | None = None,
api_secret_key: str | None = None,
environment: Environment = Environment.SANDBOX
environment: Environment = Environment.SANDBOX,
logger: Logger | None = None,
):
self._environment = environment
self._base_url = self._environment.base_url
self._client = client or self.create_client(api_secret_key)
self._client = client or self.create_client(api_secret_key, logger)


@property
Expand All @@ -30,16 +34,17 @@ def environment(self):
return self._environment


def create_client(self, api_secret_key: str | None = None):
def create_client(self, api_secret_key: str | None = None, logger: Logger | None = None):
return Client(
getenv('PADDLE_API_SECRET_KEY') if api_secret_key is None else api_secret_key,
options = Options(self._environment),
logger=logger
)


@fixture(autouse=True)
def test_client():
return TestClient(environment=Environment.SANDBOX)
def test_client(test_logger):
return TestClient(environment=Environment.SANDBOX, logger=test_logger)


@fixture
Expand Down
28 changes: 28 additions & 0 deletions tests/Utils/TestLogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from pytest import fixture
from logging import getLogger, Logger, Handler, LogRecord


class LogHandler(Handler):
def __init__(self):
super().__init__()
self._logs = []


def emit(self, record):
self._logs.append(record)


def get_logs(self) -> list[LogRecord]:
return self._logs


@fixture
def test_log_handler() -> LogHandler:
return LogHandler()


@fixture
def test_logger(test_log_handler) -> Logger:
logger = getLogger('paddle_test_logger')
logger.addHandler(test_log_handler)
return logger
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from tests.Utils.TestClient import mock_requests, test_client
from tests.Utils.TestLogger import test_log_handler, test_logger

0 comments on commit 45ba9fb

Please sign in to comment.