diff --git a/pyproject.toml b/pyproject.toml index 6fcbd31..2f0c047 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,7 +77,7 @@ disallow_any_unimported = false [tool.ruff] fix = true line-length = 88 -include = ["mailchimp_api/**/*.py", "mailchimp_api/**/*.pyi", "pyproject.toml"] +include = ["mailchimp_api/**/*.py", "mailchimp_api/**/*.pyi", "tests/**/*.py", "pyproject.toml"] exclude = [] [tool.ruff.lint] diff --git a/tests/test_mailchimp_openapi_spec.py b/tests/test_mailchimp_openapi_spec.py index ade7074..6771c51 100644 --- a/tests/test_mailchimp_openapi_spec.py +++ b/tests/test_mailchimp_openapi_spec.py @@ -1,31 +1,38 @@ -from collections.abc import Iterator +from unittest.mock import MagicMock, patch +import pytest from autogen import UserProxyAgent from fastagency.api.openapi.client import OpenAPI from mailchimp_api.utils.create_api import create_api -from unittest.mock import MagicMock, patch -import pytest + +@pytest.fixture(scope="class") +def setup_api() -> OpenAPI: + return create_api(mailchimp_api_key="test Key") # pragma: allowlist secret + class TestMailchimpOpenapiSpec: @pytest.fixture(autouse=True) - def setup( + def _setup( self, - ) -> Iterator[None]: - self.api = create_api(mailchimp_api_key="test Key") - yield - + ) -> None: + self.headers = { + "Content-Type": "application/json", + "Authorization": "Basic YW55c3RyaW5nOnRlc3QgS2V5", + } + return - def test_create_api(self) -> None: - assert isinstance(self.api, OpenAPI) - url = self.api.servers[0]["url"] + def test_create_api(self, setup_api: OpenAPI) -> None: + assert isinstance(setup_api, OpenAPI) + url = setup_api.servers[0]["url"] assert url == "https://us14.api.mailchimp.com/3.0", url - @patch("fastagency.api.openapi.client.requests.get") - def test_mailchimp_api( + def _test_mailchimp_api( self, mock_post: MagicMock, + setup_api: OpenAPI, + function: str, ) -> None: mock_response = MagicMock() mock_response.status_code = 200 @@ -38,19 +45,45 @@ def test_mailchimp_api( code_execution_config=False, ) - functions = ["get_ping"] - self.api._register_for_execution(user_proxy, functions=functions) + setup_api._register_for_execution(user_proxy, functions=[function]) + + assert tuple(user_proxy._function_map.keys()) == (function,) - assert tuple(user_proxy._function_map.keys()) == ("get_ping",) + func = user_proxy._function_map[function] + func(body={}) - get_ping = user_proxy._function_map["get_ping"] - get_ping(body={}) + @patch("fastagency.api.openapi.client.requests.get") + def test_mailchimp_api_ping( + self, + mock_post: MagicMock, + setup_api: OpenAPI, + ) -> None: + self._test_mailchimp_api( + mock_post=mock_post, + setup_api=setup_api, + function="get_ping", + ) + + mock_post.assert_called_once_with( + f"{setup_api.servers[0]['url']}/ping", + params={}, + headers=self.headers, + ) + + @patch("fastagency.api.openapi.client.requests.get") + def test_mailchimp_api_get_lists( + self, + mock_post: MagicMock, + setup_api: OpenAPI, + ) -> None: + self._test_mailchimp_api( + mock_post=mock_post, + setup_api=setup_api, + function="get_lists", + ) mock_post.assert_called_once_with( - f"{self.api.servers[0]['url']}/ping", + f"{setup_api.servers[0]['url']}/lists", params={}, - headers={ - 'Content-Type': 'application/json', - 'Authorization': 'Basic YW55c3RyaW5nOnRlc3QgS2V5', - }, + headers=self.headers, )