Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test few mailchimp endpoints #4

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
79 changes: 56 additions & 23 deletions tests/test_mailchimp_openapi_spec.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
)