Skip to content

Commit

Permalink
Update endpoint tests to reflect new endpoint count and properties
Browse files Browse the repository at this point in the history
Signed-off-by: samadpls <[email protected]>
  • Loading branch information
samadpls committed Jan 27, 2025
1 parent ecaed9f commit 2587ff4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
46 changes: 25 additions & 21 deletions core/tests/mad_hatter/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import pytest
from cat.mad_hatter.decorators import CustomEndpoint
from tests.mocks.mock_plugin.mock_endpoint import Item

def test_endpoints_discovery(mad_hatter_with_mock_plugin):

# discovered endpoints are cached
mock_plugin_endpoints = mad_hatter_with_mock_plugin.plugins["mock_plugin"].endpoints
assert mock_plugin_endpoints == mad_hatter_with_mock_plugin.endpoints

# discovered endpoints
assert len(mock_plugin_endpoints) == 4
assert len(mock_plugin_endpoints) == 6

# basic properties
for h in mock_plugin_endpoints:
Expand All @@ -18,54 +17,59 @@ def test_endpoints_discovery(mad_hatter_with_mock_plugin):


def test_endpoint_decorator(client, mad_hatter_with_mock_plugin):

endpoint = mad_hatter_with_mock_plugin.endpoints[0]
# Find specific endpoint by path/prefix/method
endpoints = mad_hatter_with_mock_plugin.endpoints
endpoint = next(e for e in endpoints
if e.path == "/endpoint"
and e.prefix == "/custom"
and "GET" in e.methods)

assert endpoint.name == "/custom/endpoint"
assert endpoint.prefix == "/custom"
assert endpoint.path == "/endpoint"
assert endpoint.methods == {"GET"} # fastapi stores http verbs as a set
assert endpoint.methods == {"GET"}
assert endpoint.tags == ["Custom Endpoints"]
assert endpoint.function() == {"result":"endpoint default prefix"}


def test_endpoint_decorator_prefix(client, mad_hatter_with_mock_plugin):

endpoint = mad_hatter_with_mock_plugin.endpoints[1]
endpoints = mad_hatter_with_mock_plugin.endpoints
endpoint = next(e for e in endpoints
if e.path == "/endpoint"
and e.prefix == "/tests"
and "GET" in e.methods)

assert endpoint.name == "/tests/endpoint"
assert endpoint.prefix == "/tests"
assert endpoint.path == "/endpoint"
assert endpoint.methods == {"GET"}
assert endpoint.tags == ["Tests"]
assert endpoint.function() == {"result":"endpoint prefix tests"}


def test_get_endpoint(client, mad_hatter_with_mock_plugin):

endpoint = mad_hatter_with_mock_plugin.endpoints[2]
endpoints = mad_hatter_with_mock_plugin.endpoints
endpoint = next(e for e in endpoints
if e.path == "/crud"
and e.prefix == "/tests"
and "GET" in e.methods)

assert endpoint.name == "/tests/crud"
assert endpoint.prefix == "/tests"
assert endpoint.path == "/crud"
assert endpoint.methods == {"GET"}
assert endpoint.tags == ["Tests"]
# too complicated to simulate the request arguments here,
# see tests/routes/test_custom_endpoints.py

def test_post_endpoint(client, mad_hatter_with_mock_plugin):

endpoint = mad_hatter_with_mock_plugin.endpoints[3]
endpoints = mad_hatter_with_mock_plugin.endpoints
endpoint = next(e for e in endpoints
if e.path == "/crud"
and e.prefix == "/tests"
and "POST" in e.methods)

assert endpoint.name == "/tests/crud"
assert endpoint.prefix == "/tests"
assert endpoint.prefix == "/tests"
assert endpoint.path == "/crud"
assert endpoint.methods == {"POST"}
assert endpoint.tags == ["Tests"]

payload = Item(name="the cat", description="it's magic")
assert endpoint.function(payload) == payload.model_dump()


@pytest.mark.parametrize("switch_type", ["deactivation", "uninstall"])
def test_endpoints_deactivation_or_uninstall(switch_type, mad_hatter_with_mock_plugin):
Expand Down
2 changes: 1 addition & 1 deletion core/tests/mad_hatter/test_mad_hatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_plugin_install(mad_hatter: MadHatter, plugin_is_flat):
assert len(mad_hatter.plugins["mock_plugin"].hooks) == 3
assert len(mad_hatter.plugins["mock_plugin"].tools) == 1
assert len(mad_hatter.plugins["mock_plugin"].forms) == 1
assert len(mad_hatter.plugins["mock_plugin"].endpoints) == 4
assert len(mad_hatter.plugins["mock_plugin"].endpoints) == 6

# tool found
new_tool = mad_hatter.plugins["mock_plugin"].tools[0]
Expand Down
2 changes: 1 addition & 1 deletion core/tests/routes/plugins/test_plugin_toggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def check_active_plugin_properties(plugin):
assert len(plugin["hooks"]) == 3
assert len(plugin["tools"]) == 1
assert len(plugin["forms"]) == 1
assert len(plugin["endpoints"]) == 4
assert len(plugin["endpoints"]) == 6

def check_unactive_plugin_properties(plugin):
assert plugin["id"] == "mock_plugin"
Expand Down
1 change: 1 addition & 0 deletions core/tests/routes/test_custom_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_custom_endpoint_post(client, just_installed_plugin):
assert response.json()["name"] == "the cat"
assert response.json()["description"] == "it's magic"


def test_custom_endpoint_put(client, just_installed_plugin):
payload = {"name": "the cat", "description": "it's magic"}
response = client.put("/tests/crud", json=payload)
Expand Down

0 comments on commit 2587ff4

Please sign in to comment.