From 92ca4ce8413360a2ffdd1509ca28e500e6d4eaa5 Mon Sep 17 00:00:00 2001 From: Eduardo Chiarotti Date: Thu, 22 Aug 2024 20:06:11 -0300 Subject: [PATCH] feat: add tests --- src/crewai/cli/deploy/api.py | 9 ++-- tests/cli/deploy/test_api.py | 102 +++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 tests/cli/deploy/test_api.py diff --git a/src/crewai/cli/deploy/api.py b/src/crewai/cli/deploy/api.py index 71acee5b5..942fc487e 100644 --- a/src/crewai/cli/deploy/api.py +++ b/src/crewai/cli/deploy/api.py @@ -8,19 +8,18 @@ class CrewAPI: CrewAPI class to interact with the crewAI+ API. """ - CREWAI_BASE_URL = getenv( - "CREWAI_BASE_URL", "https://dev.crewai.com/crewai_plus/api/v1/crews" - ) - def __init__(self, api_key: str) -> None: self.api_key = api_key self.headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } + self.base_url = getenv( + "CREWAI_BASE_URL", "https://dev.crewai.com/crewai_plus/api/v1/crews" + ) def _make_request(self, method: str, endpoint: str, **kwargs) -> requests.Response: - url = f"{self.CREWAI_BASE_URL}/{endpoint}" + url = f"{self.base_url}/{endpoint}" return requests.request(method, url, headers=self.headers, **kwargs) # Deploy diff --git a/tests/cli/deploy/test_api.py b/tests/cli/deploy/test_api.py new file mode 100644 index 000000000..f1a6c573d --- /dev/null +++ b/tests/cli/deploy/test_api.py @@ -0,0 +1,102 @@ +import unittest +from os import environ +from unittest.mock import MagicMock, patch + +from crewai.cli.deploy.api import CrewAPI + + +class TestCrewAPI(unittest.TestCase): + def setUp(self): + self.api_key = "test_api_key" + self.api = CrewAPI(self.api_key) + + def test_init(self): + self.assertEqual(self.api.api_key, self.api_key) + self.assertEqual( + self.api.headers, + { + "Authorization": f"Bearer {self.api_key}", + "Content-Type": "application/json", + }, + ) + + @patch("crewai.cli.deploy.api.requests.request") + def test_make_request(self, mock_request): + mock_response = MagicMock() + mock_request.return_value = mock_response + + response = self.api._make_request("GET", "test_endpoint") + + mock_request.assert_called_once_with( + "GET", f"{self.api.base_url}/test_endpoint", headers=self.api.headers + ) + self.assertEqual(response, mock_response) + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_deploy_by_name(self, mock_make_request): + self.api.deploy_by_name("test_project") + mock_make_request.assert_called_once_with("POST", "by-name/test_project/deploy") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_deploy_by_uuid(self, mock_make_request): + self.api.deploy_by_uuid("test_uuid") + mock_make_request.assert_called_once_with("POST", "test_uuid/deploy") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_status_by_name(self, mock_make_request): + self.api.status_by_name("test_project") + mock_make_request.assert_called_once_with("GET", "by-name/test_project/status") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_status_by_uuid(self, mock_make_request): + self.api.status_by_uuid("test_uuid") + mock_make_request.assert_called_once_with("GET", "test_uuid/status") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_logs_by_name(self, mock_make_request): + self.api.logs_by_name("test_project") + mock_make_request.assert_called_once_with( + "GET", "by-name/test_project/logs/deployment" + ) + + self.api.logs_by_name("test_project", "custom_log") + mock_make_request.assert_called_with( + "GET", "by-name/test_project/logs/custom_log" + ) + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_logs_by_uuid(self, mock_make_request): + self.api.logs_by_uuid("test_uuid") + mock_make_request.assert_called_once_with("GET", "test_uuid/logs/deployment") + + self.api.logs_by_uuid("test_uuid", "custom_log") + mock_make_request.assert_called_with("GET", "test_uuid/logs/custom_log") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_delete_by_name(self, mock_make_request): + self.api.delete_by_name("test_project") + mock_make_request.assert_called_once_with("DELETE", "by-name/test_project") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_delete_by_uuid(self, mock_make_request): + self.api.delete_by_uuid("test_uuid") + mock_make_request.assert_called_once_with("DELETE", "test_uuid") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_list_crews(self, mock_make_request): + self.api.list_crews() + mock_make_request.assert_called_once_with("GET", "") + + @patch("crewai.cli.deploy.api.CrewAPI._make_request") + def test_create_crew(self, mock_make_request): + payload = {"name": "test_crew"} + self.api.create_crew(payload) + mock_make_request.assert_called_once_with("POST", "", json=payload) + + @patch.dict(environ, {"CREWAI_BASE_URL": "https://custom-url.com/api"}) + def test_custom_base_url(self): + custom_api = CrewAPI("test_key") + self.assertEqual( + custom_api.base_url, + "https://custom-url.com/api", + )