Skip to content

Commit

Permalink
Fixing apm (#774)
Browse files Browse the repository at this point in the history
Fixing APM test case
  • Loading branch information
jedan2506 authored Jul 17, 2023
1 parent 820ed10 commit f913608
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 50 deletions.
Empty file added superagi/apm/__init__.py
Empty file.
11 changes: 5 additions & 6 deletions superagi/apm/analytics_helper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import List, Dict, Tuple, Optional, Iterator, Union, Any
from typing import List, Dict, Union, Any

from sqlalchemy import text, func, and_
from sqlalchemy.orm import Session
from sqlalchemy.orm.query import Query

from superagi.models.events import Event
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import text, func, Integer, and_
from collections import defaultdict
import logging


class AnalyticsHelper:

Expand Down
7 changes: 5 additions & 2 deletions superagi/apm/tools_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import List, Dict
from sqlalchemy.orm import Session
from sqlalchemy.orm.query import Query

from sqlalchemy import func
from sqlalchemy.orm import Session

from superagi.models.events import Event


class ToolsHandler:

def __init__(self, session: Session, organisation_id: int):
Expand Down
Empty file.
29 changes: 12 additions & 17 deletions tests/unit_tests/apm/test_analytics_helper.py
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)
18 changes: 9 additions & 9 deletions tests/unit_tests/apm/test_tools_handler.py
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)
52 changes: 36 additions & 16 deletions tests/unit_tests/controllers/test_analytics.py
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"]

0 comments on commit f913608

Please sign in to comment.