-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing APM test case
- Loading branch information
Showing
7 changed files
with
67 additions
and
50 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,36 @@ | ||
import pytest | ||
from unittest.mock import MagicMock | ||
from superagi.models.events import Event | ||
from superagi.apm.analytics_helper import AnalyticsHelper | ||
from sqlalchemy.orm import Session | ||
|
||
@pytest.fixture | ||
def mock_session(): | ||
return MagicMock(spec=Session) | ||
from unittest.mock import MagicMock | ||
|
||
@pytest.fixture | ||
def organisation_id(): | ||
return 1 | ||
|
||
@pytest.fixture | ||
def mock_session(): | ||
return MagicMock() | ||
|
||
@pytest.fixture | ||
def analytics_helper(mock_session, organisation_id): | ||
return AnalyticsHelper(mock_session, organisation_id) | ||
|
||
def test_calculate_run_completed_metrics(analytics_helper, mock_session): | ||
analytics_helper.calculate_run_completed_metrics = MagicMock(return_value = {}) | ||
mock_session.query().all.return_value = [MagicMock()] | ||
result = analytics_helper.calculate_run_completed_metrics() | ||
assert isinstance(result, dict) | ||
analytics_helper.calculate_run_completed_metrics.assert_called() | ||
|
||
def test_fetch_agent_data(analytics_helper, mock_session): | ||
analytics_helper.fetch_agent_data = MagicMock(return_value = {}) | ||
mock_session.query().all.return_value = [MagicMock()] | ||
result = analytics_helper.fetch_agent_data() | ||
assert isinstance(result, dict) | ||
analytics_helper.fetch_agent_data.assert_called() | ||
|
||
def test_fetch_agent_runs(analytics_helper, mock_session): | ||
agent_id = 1 | ||
analytics_helper.fetch_agent_runs = MagicMock(return_value = []) | ||
result = analytics_helper.fetch_agent_runs(agent_id) | ||
mock_session.query().all.return_value = [MagicMock()] | ||
result = analytics_helper.fetch_agent_runs(1) | ||
assert isinstance(result, list) | ||
analytics_helper.fetch_agent_runs.assert_called_with(agent_id) | ||
|
||
def test_get_active_runs(analytics_helper, mock_session): | ||
analytics_helper.get_active_runs = MagicMock(return_value = []) | ||
mock_session.query().all.return_value = [MagicMock()] | ||
result = analytics_helper.get_active_runs() | ||
assert isinstance(result, list) | ||
analytics_helper.get_active_runs.assert_called() | ||
assert isinstance(result, list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
import pytest | ||
from unittest.mock import MagicMock | ||
from sqlalchemy.orm import Session | ||
from superagi.apm.tools_handler import ToolsHandler | ||
|
||
@pytest.fixture | ||
def mock_session(): | ||
return MagicMock(spec=Session) | ||
from superagi.apm.tools_handler import ToolsHandler | ||
from sqlalchemy.orm import Session | ||
|
||
@pytest.fixture | ||
def organisation_id(): | ||
return 1 | ||
|
||
@pytest.fixture | ||
def mock_session(): | ||
return MagicMock() | ||
|
||
@pytest.fixture | ||
def tools_handler(mock_session, organisation_id): | ||
return ToolsHandler(mock_session, organisation_id) | ||
|
||
def test_calculate_tool_usage(tools_handler): | ||
tools_handler.calculate_tool_usage = MagicMock(return_value=[]) | ||
def test_calculate_tool_usage(tools_handler, mock_session): | ||
mock_session.query().all.return_value = [MagicMock()] | ||
result = tools_handler.calculate_tool_usage() | ||
assert isinstance(result, list) | ||
tools_handler.calculate_tool_usage.assert_called() | ||
assert isinstance(result, list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,61 @@ | ||
from unittest.mock import patch | ||
from unittest.mock import patch, MagicMock | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
from main import app | ||
|
||
client = TestClient(app) | ||
|
||
def test_get_metrics_success(): | ||
with patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper: | ||
@patch('superagi.controllers.analytics.db') | ||
def test_get_metrics_success(mock_get_db): | ||
with patch('superagi.helper.auth.get_user_organisation') as mock_get_user_org, \ | ||
patch('superagi.controllers.analytics.db') as mock_db, \ | ||
patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper, \ | ||
patch('superagi.helper.auth.db') as mock_auth_db: | ||
mock_helper().calculate_run_completed_metrics.return_value = {'total_tokens': 10, 'total_calls': 5, 'runs_completed': 2} | ||
response = client.get("analytics/metrics") | ||
response = client.get("/analytics/metrics") | ||
assert response.status_code == 200 | ||
assert response.json() == {'total_tokens': 10, 'total_calls': 5, 'runs_completed': 2} | ||
|
||
def test_get_agents_success(): | ||
with patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper: | ||
@patch('superagi.controllers.analytics.db') | ||
def test_get_agents_success(mock_get_db): | ||
with patch('superagi.helper.auth.get_user_organisation') as mock_get_user_org, \ | ||
patch('superagi.controllers.analytics.db') as mock_db, \ | ||
patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper, \ | ||
patch('superagi.helper.auth.db') as mock_auth_db: | ||
mock_helper().fetch_agent_data.return_value = {"agent_details": "mock_details", "model_info": "mock_info"} | ||
response = client.get("analytics/agents/all") | ||
response = client.get("/analytics/agents/all") | ||
assert response.status_code == 200 | ||
assert response.json() == {"agent_details": "mock_details", "model_info": "mock_info"} | ||
|
||
def test_get_agent_runs_success(): | ||
with patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper: | ||
@patch('superagi.controllers.analytics.db') | ||
def test_get_agent_runs_success(mock_get_db): | ||
with patch('superagi.helper.auth.get_user_organisation') as mock_get_user_org, \ | ||
patch('superagi.controllers.analytics.db') as mock_db, \ | ||
patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper, \ | ||
patch('superagi.helper.auth.db') as mock_auth_db: | ||
mock_helper().fetch_agent_runs.return_value = "mock_agent_runs" | ||
response = client.get("analytics/agents/1") | ||
response = client.get("/analytics/agents/1") | ||
assert response.status_code == 200 | ||
assert response.json() == "mock_agent_runs" | ||
|
||
def test_get_active_runs_success(): | ||
with patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper: | ||
@patch('superagi.controllers.analytics.db') | ||
def test_get_active_runs_success(mock_get_db): | ||
with patch('superagi.helper.auth.get_user_organisation') as mock_get_user_org, \ | ||
patch('superagi.controllers.analytics.db') as mock_db, \ | ||
patch('superagi.controllers.analytics.AnalyticsHelper') as mock_helper, \ | ||
patch('superagi.helper.auth.db') as mock_auth_db: | ||
mock_helper().get_active_runs.return_value = ["mock_run_1", "mock_run_2"] | ||
response = client.get("analytics/runs/active") | ||
response = client.get("/analytics/runs/active") | ||
assert response.status_code == 200 | ||
assert response.json() == ["mock_run_1", "mock_run_2"] | ||
|
||
def test_get_tools_user_success(): | ||
with patch('superagi.controllers.analytics.ToolsHandler') as mock_handler: | ||
@patch('superagi.controllers.analytics.db') | ||
def test_get_tools_user_success(mock_get_db): | ||
with patch('superagi.helper.auth.get_user_organisation') as mock_get_user_org, \ | ||
patch('superagi.controllers.analytics.db') as mock_db, \ | ||
patch('superagi.controllers.analytics.ToolsHandler') as mock_handler, \ | ||
patch('superagi.helper.auth.db') as mock_auth_db: | ||
mock_handler().calculate_tool_usage.return_value = ["tool1", "tool2"] | ||
response = client.get("analytics/tools/used") | ||
response = client.get("/analytics/tools/used") | ||
assert response.status_code == 200 | ||
assert response.json() == ["tool1", "tool2"] |