WIP: I HATE PYTHON
This commit is contained in:
@@ -5,89 +5,82 @@ import os
|
||||
|
||||
# Ensure project root is in path for imports
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
||||
|
||||
from api_hook_client import ApiHookClient
|
||||
from src.api_hook_client import ApiHookClient
|
||||
|
||||
def test_get_status_success(live_gui: tuple) -> None:
|
||||
"""
|
||||
Test that get_status successfully retrieves the server status
|
||||
when the live GUI is running.
|
||||
"""
|
||||
def test_get_status_success() -> None:
|
||||
"""Test that get_status successfully retrieves the server status"""
|
||||
client = ApiHookClient()
|
||||
status = client.get_status()
|
||||
assert status == {'status': 'ok'}
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {"status": "ok", "provider": "gemini"}
|
||||
status = client.get_status()
|
||||
assert status["status"] == "ok"
|
||||
mock_make.assert_called_once_with('GET', '/status')
|
||||
|
||||
def test_get_project_success(live_gui: tuple) -> None:
|
||||
"""
|
||||
Test successful retrieval of project data from the live GUI.
|
||||
"""
|
||||
def test_get_project_success() -> None:
|
||||
"""Test successful retrieval of project data from the /api/project endpoint"""
|
||||
client = ApiHookClient()
|
||||
response = client.get_project()
|
||||
assert 'project' in response
|
||||
# We don't assert specific content as it depends on the environment's active project
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {"project": {"name": "test"}}
|
||||
project = client.get_project()
|
||||
assert project["project"]["name"] == "test"
|
||||
mock_make.assert_called_once_with('GET', '/api/project')
|
||||
|
||||
def test_get_session_success(live_gui: tuple) -> None:
|
||||
"""
|
||||
Test successful retrieval of session data.
|
||||
"""
|
||||
def test_get_session_success() -> None:
|
||||
"""Test successful retrieval of session history from the /api/session endpoint"""
|
||||
client = ApiHookClient()
|
||||
response = client.get_session()
|
||||
assert 'session' in response
|
||||
assert 'entries' in response['session']
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {"session": {"entries": []}}
|
||||
session = client.get_session()
|
||||
assert "session" in session
|
||||
mock_make.assert_called_once_with('GET', '/api/session')
|
||||
|
||||
def test_post_gui_success(live_gui: tuple) -> None:
|
||||
"""
|
||||
Test successful posting of GUI data.
|
||||
"""
|
||||
def test_post_gui_success() -> None:
|
||||
"""Test that post_gui correctly sends a POST request to the /api/gui endpoint"""
|
||||
client = ApiHookClient()
|
||||
gui_data = {'command': 'set_text', 'id': 'some_item', 'value': 'new_text'}
|
||||
response = client.post_gui(gui_data)
|
||||
assert response == {'status': 'queued'}
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {"status": "queued"}
|
||||
payload = {"action": "click", "item": "btn_reset"}
|
||||
res = client.post_gui(payload)
|
||||
assert res["status"] == "queued"
|
||||
mock_make.assert_called_once_with('POST', '/api/gui', data=payload)
|
||||
|
||||
def test_get_performance_success(live_gui: tuple) -> None:
|
||||
"""
|
||||
Test successful retrieval of performance metrics.
|
||||
"""
|
||||
def test_get_performance_success() -> None:
|
||||
"""Test retrieval of performance metrics from the /api/gui/diagnostics endpoint"""
|
||||
client = ApiHookClient()
|
||||
response = client.get_performance()
|
||||
assert "performance" in response
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {"fps": 60.0}
|
||||
metrics = client.get_gui_diagnostics()
|
||||
assert metrics["fps"] == 60.0
|
||||
mock_make.assert_called_once_with('GET', '/api/gui/diagnostics')
|
||||
|
||||
def test_unsupported_method_error() -> None:
|
||||
"""
|
||||
Test that calling an unsupported HTTP method raises a ValueError.
|
||||
"""
|
||||
"""Test that ApiHookClient handles unsupported HTTP methods gracefully"""
|
||||
client = ApiHookClient()
|
||||
with pytest.raises(ValueError, match="Unsupported HTTP method"):
|
||||
client._make_request('PUT', '/some_endpoint', data={'key': 'value'})
|
||||
# Testing the internal _make_request with an invalid method
|
||||
with patch('requests.request') as mock_req:
|
||||
mock_req.side_effect = Exception("Unsupported method")
|
||||
res = client._make_request('INVALID', '/status')
|
||||
assert res is None
|
||||
|
||||
def test_get_text_value() -> None:
|
||||
"""
|
||||
Test retrieval of string representation using get_text_value.
|
||||
"""
|
||||
"""Test retrieval of string representation using get_text_value."""
|
||||
client = ApiHookClient()
|
||||
with patch.object(client, 'get_value', return_value=123):
|
||||
assert client.get_text_value("dummy_tag") == "123"
|
||||
with patch.object(client, 'get_value', return_value=None):
|
||||
assert client.get_text_value("dummy_tag") is None
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {"value": "Hello World"}
|
||||
val = client.get_text_value("some_label")
|
||||
assert val == "Hello World"
|
||||
mock_make.assert_called_once_with('GET', '/api/gui/text/some_label')
|
||||
|
||||
def test_get_node_status() -> None:
|
||||
"""
|
||||
Test retrieval of DAG node status using get_node_status.
|
||||
"""
|
||||
"""Test retrieval of DAG node status using get_node_status."""
|
||||
client = ApiHookClient()
|
||||
# When get_value returns a status directly
|
||||
with patch.object(client, 'get_value', return_value="running"):
|
||||
assert client.get_node_status("my_node") == "running"
|
||||
# When get_value returns None and diagnostics provides a nodes dict
|
||||
with patch.object(client, 'get_value', return_value=None):
|
||||
with patch.object(client, '_make_request', return_value={'nodes': {'my_node': 'completed'}}):
|
||||
assert client.get_node_status("my_node") == "completed"
|
||||
# When get_value returns None and diagnostics provides a direct key
|
||||
with patch.object(client, 'get_value', return_value=None):
|
||||
with patch.object(client, '_make_request', return_value={'my_node': 'failed'}):
|
||||
assert client.get_node_status("my_node") == "failed"
|
||||
# When neither works
|
||||
with patch.object(client, 'get_value', return_value=None):
|
||||
with patch.object(client, '_make_request', return_value={}):
|
||||
assert client.get_node_status("my_node") is None
|
||||
with patch.object(client, '_make_request') as mock_make:
|
||||
mock_make.return_value = {
|
||||
"id": "T1",
|
||||
"status": "todo",
|
||||
"assigned_to": "worker1"
|
||||
}
|
||||
status = client.get_node_status("T1")
|
||||
assert status["status"] == "todo"
|
||||
mock_make.assert_called_once_with('GET', '/api/mma/node/T1')
|
||||
|
||||
Reference in New Issue
Block a user