diff --git a/autonomous_agent_api/backend/app/controllers/agent_router.py b/autonomous_agent_api/backend/app/controllers/agent_router.py index 24c60e4b5..29fc6d145 100644 --- a/autonomous_agent_api/backend/app/controllers/agent_router.py +++ b/autonomous_agent_api/backend/app/controllers/agent_router.py @@ -4,9 +4,9 @@ from classy_fastapi import Routable, get, post, put, delete from fastapi import HTTPException -from backend.app.models.AgentDto.agent_dto import AgentCreateDTO +from backend.app.models.agent.agent_dto import AgentCreateDTO from backend.app.services.agent_service import AgentService -from backend.app.models.AgentDto.response_dto import AgentResponse +from backend.app.models.agent.response_dto import AgentResponse from backend.dependency import get_agent_service diff --git a/autonomous_agent_api/backend/app/controllers/trigger_router.py b/autonomous_agent_api/backend/app/controllers/trigger_router.py index 164d5413e..bbbe13fe2 100644 --- a/autonomous_agent_api/backend/app/controllers/trigger_router.py +++ b/autonomous_agent_api/backend/app/controllers/trigger_router.py @@ -4,8 +4,8 @@ from classy_fastapi import Routable, get, post, put, delete from fastapi import HTTPException -from backend.app.models.TriggerDto.resposne_dto import TriggerResponse -from backend.app.models.TriggerDto.trigger_dto import TriggerCreateDTO, TriggerCreateDTO +from backend.app.models import TriggerResponse +from backend.app.models import TriggerCreateDTO from backend.app.services.trigger_service import TriggerService from backend.dependency import get_trigger_service @@ -27,16 +27,16 @@ async def list_triggers_by_agent_id(self, agent_id: str): @get("/triggers/{trigger_id}", response_model=TriggerResponse) async def list_trigger_by_trigger_id(self, trigger_id: str): - trigger = await self.trigger_service.list_trigger_by_trigger_id(trigger_id) + trigger = await self.trigger_service.list_trigger_by_id(trigger_id) return trigger @put("/triggers/{trigger_id}", response_model=TriggerResponse) - async def update_trigger_by_agent_id_and_trigger_id(self, trigger_id: str, trigger_data: TriggerCreateDTO): - trigger = await self.trigger_service.update_trigger_by_trigger_id(trigger_id, trigger_data) + async def update_trigger_by_trigger_id(self, trigger_id: str, trigger_data: TriggerCreateDTO): + trigger = await self.trigger_service.update_trigger_by_id(trigger_id, trigger_data) return trigger @delete("/triggers/{trigger_id}", status_code=HTTPStatus.NO_CONTENT) async def delete_trigger_by_trigger_id(self, trigger_id: str): - success = await self.trigger_service.delete_trigger_by_trigger_id(trigger_id) + success = await self.trigger_service.delete_by_id(trigger_id) if not success: raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Trigger not found") diff --git a/autonomous_agent_api/backend/app/models/TriggerDto/__init__.py b/autonomous_agent_api/backend/app/models/TriggerDto/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/autonomous_agent_api/backend/app/models/__init__.py b/autonomous_agent_api/backend/app/models/__init__.py index 468f57eaa..2c19f0a78 100644 --- a/autonomous_agent_api/backend/app/models/__init__.py +++ b/autonomous_agent_api/backend/app/models/__init__.py @@ -1 +1,4 @@ -"""Application implementation - models.""" +# backend/app/models/__init__.py + +from .trigger.resposne_dto import TriggerResponse +from .trigger.trigger_dto import TriggerCreateDTO, TopicTriggerDTO, CronTriggerDTO diff --git a/autonomous_agent_api/backend/app/models/AgentDto/__init__.py b/autonomous_agent_api/backend/app/models/agent/__init__.py similarity index 100% rename from autonomous_agent_api/backend/app/models/AgentDto/__init__.py rename to autonomous_agent_api/backend/app/models/agent/__init__.py diff --git a/autonomous_agent_api/backend/app/models/AgentDto/agent_dto.py b/autonomous_agent_api/backend/app/models/agent/agent_dto.py similarity index 100% rename from autonomous_agent_api/backend/app/models/AgentDto/agent_dto.py rename to autonomous_agent_api/backend/app/models/agent/agent_dto.py diff --git a/autonomous_agent_api/backend/app/models/AgentDto/response_dto.py b/autonomous_agent_api/backend/app/models/agent/response_dto.py similarity index 100% rename from autonomous_agent_api/backend/app/models/AgentDto/response_dto.py rename to autonomous_agent_api/backend/app/models/agent/response_dto.py diff --git a/autonomous_agent_api/backend/app/models/trigger/__init__.py b/autonomous_agent_api/backend/app/models/trigger/__init__.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/autonomous_agent_api/backend/app/models/trigger/__init__.py @@ -0,0 +1 @@ + diff --git a/autonomous_agent_api/backend/app/models/TriggerDto/resposne_dto.py b/autonomous_agent_api/backend/app/models/trigger/resposne_dto.py similarity index 78% rename from autonomous_agent_api/backend/app/models/TriggerDto/resposne_dto.py rename to autonomous_agent_api/backend/app/models/trigger/resposne_dto.py index bdaceffbf..61ca76c8a 100644 --- a/autonomous_agent_api/backend/app/models/TriggerDto/resposne_dto.py +++ b/autonomous_agent_api/backend/app/models/trigger/resposne_dto.py @@ -1,7 +1,7 @@ from pydantic import BaseModel from typing import Union -from backend.app.models.TriggerDto.trigger_dto import CronTriggerDTO, TopicTriggerDTO +from backend.app.models.trigger.trigger_dto import CronTriggerDTO, TopicTriggerDTO class TriggerResponse(BaseModel): diff --git a/autonomous_agent_api/backend/app/models/TriggerDto/trigger_dto.py b/autonomous_agent_api/backend/app/models/trigger/trigger_dto.py similarity index 100% rename from autonomous_agent_api/backend/app/models/TriggerDto/trigger_dto.py rename to autonomous_agent_api/backend/app/models/trigger/trigger_dto.py diff --git a/autonomous_agent_api/backend/app/repositories/agent_repository.py b/autonomous_agent_api/backend/app/repositories/agent_repository.py index e37328bf3..67726dae2 100644 --- a/autonomous_agent_api/backend/app/repositories/agent_repository.py +++ b/autonomous_agent_api/backend/app/repositories/agent_repository.py @@ -2,8 +2,8 @@ from datetime import datetime, timezone from typing import List, Optional from fastapi import HTTPException -from backend.app.models.AgentDto.agent_dto import AgentCreateDTO -from backend.app.models.AgentDto.response_dto import AgentResponse +from backend.app.models.agent.agent_dto import AgentCreateDTO +from backend.app.models.agent.response_dto import AgentResponse from backend.config.database import prisma_connection diff --git a/autonomous_agent_api/backend/app/repositories/trigger_repository.py b/autonomous_agent_api/backend/app/repositories/trigger_repository.py index 6e4213f7c..2d3387531 100644 --- a/autonomous_agent_api/backend/app/repositories/trigger_repository.py +++ b/autonomous_agent_api/backend/app/repositories/trigger_repository.py @@ -4,8 +4,8 @@ from typing import List, Optional, Union from fastapi import HTTPException -from backend.app.models.TriggerDto.resposne_dto import TriggerResponse -from backend.app.models.TriggerDto.trigger_dto import ( +from backend.app.models.trigger.resposne_dto import TriggerResponse +from backend.app.models.trigger.trigger_dto import ( TriggerCreateDTO, CronTriggerDTO, TopicTriggerDTO, @@ -69,7 +69,7 @@ async def remove_trigger_by_trigger_id(self, trigger_id: str) -> bool: ) return True - async def retreive_trigger_by_trigger_id(self, trigger_id: str) -> Optional[TriggerResponse]: + async def retreive_trigger_by_id(self, trigger_id: str) -> Optional[TriggerResponse]: async with self.db: trigger = await self.db.prisma.trigger.find_first(where={"id": trigger_id, "deleted_at": None}) if trigger is None: @@ -77,9 +77,7 @@ async def retreive_trigger_by_trigger_id(self, trigger_id: str) -> Optional[Trig else: return trigger - async def modify_trigger_by_trigger_id( - self, trigger_id: str, trigger_data: TriggerCreateDTO - ) -> Optional[TriggerResponse]: + async def modify_trigger_by_id(self, trigger_id: str, trigger_data: TriggerCreateDTO) -> Optional[TriggerResponse]: async with self.db: trigger = await self.db.prisma.trigger.find_first(where={"id": trigger_id}) if trigger is None or trigger.deleted_at is not None: diff --git a/autonomous_agent_api/backend/app/services/agent_service.py b/autonomous_agent_api/backend/app/services/agent_service.py index 1462db1df..8f015f7f8 100644 --- a/autonomous_agent_api/backend/app/services/agent_service.py +++ b/autonomous_agent_api/backend/app/services/agent_service.py @@ -1,8 +1,8 @@ # agent_service.py from typing import List -from backend.app.models.AgentDto.agent_dto import AgentCreateDTO -from backend.app.models.AgentDto.response_dto import AgentResponse +from backend.app.models.agent.agent_dto import AgentCreateDTO +from backend.app.models.agent.response_dto import AgentResponse from backend.app.repositories.agent_repository import AgentRepository diff --git a/autonomous_agent_api/backend/app/services/trigger_service.py b/autonomous_agent_api/backend/app/services/trigger_service.py index 51b546db7..7ac116d6d 100644 --- a/autonomous_agent_api/backend/app/services/trigger_service.py +++ b/autonomous_agent_api/backend/app/services/trigger_service.py @@ -1,7 +1,7 @@ from typing import List -from backend.app.models.TriggerDto.resposne_dto import TriggerResponse -from backend.app.models.TriggerDto.trigger_dto import TriggerCreateDTO +from backend.app.models.trigger.resposne_dto import TriggerResponse +from backend.app.models.trigger.trigger_dto import TriggerCreateDTO from backend.app.repositories.trigger_repository import TriggerRepository @@ -18,11 +18,11 @@ async def list_triggers(self) -> List[TriggerResponse]: async def list_triggers_by_agent_id(self, agent_id: str) -> List[TriggerResponse]: return await self.trigger_repository.retreive_triggers_by_agent_id(agent_id) - async def delete_trigger_by_trigger_id(self, trigger_id: str) -> bool: + async def delete_by_id(self, trigger_id: str) -> bool: return await self.trigger_repository.remove_trigger_by_trigger_id(trigger_id) - async def list_trigger_by_trigger_id(self, trigger_id: str) -> TriggerResponse: - return await self.trigger_repository.retreive_trigger_by_trigger_id(trigger_id) + async def list_trigger_by_id(self, trigger_id: str) -> TriggerResponse: + return await self.trigger_repository.retreive_trigger_by_id(trigger_id) - async def update_trigger_by_trigger_id(self, trigger_id: str, trigger_data: TriggerCreateDTO) -> TriggerResponse: - return await self.trigger_repository.modify_trigger_by_trigger_id(trigger_id, trigger_data) + async def update_trigger_by_id(self, trigger_id: str, trigger_data: TriggerCreateDTO) -> TriggerResponse: + return await self.trigger_repository.modify_trigger_by_id(trigger_id, trigger_data) diff --git a/autonomous_agent_api/prisma/migrations/20240402034446_initial_schema/migration.sql b/autonomous_agent_api/prisma/migrations/20240404083450_initial_schema/migration.sql similarity index 76% rename from autonomous_agent_api/prisma/migrations/20240402034446_initial_schema/migration.sql rename to autonomous_agent_api/prisma/migrations/20240404083450_initial_schema/migration.sql index b76fe985e..196aae208 100644 --- a/autonomous_agent_api/prisma/migrations/20240402034446_initial_schema/migration.sql +++ b/autonomous_agent_api/prisma/migrations/20240404083450_initial_schema/migration.sql @@ -6,8 +6,8 @@ CREATE TABLE "Agent" ( "id" TEXT NOT NULL, "name" TEXT NOT NULL, "action" TEXT[] DEFAULT ARRAY[]::TEXT[], - "created_at" TIMESTAMP(3), - "updated_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL, + "updated_at" TIMESTAMP(3) NOT NULL, "deleted_at" TIMESTAMP(3), CONSTRAINT "Agent_pkey" PRIMARY KEY ("id") @@ -19,8 +19,8 @@ CREATE TABLE "Trigger" ( "agent_id" TEXT NOT NULL, "type" "TriggerType" NOT NULL, "data" JSONB NOT NULL, - "created_at" TIMESTAMP(3), - "updated_at" TIMESTAMP(3), + "created_at" TIMESTAMP(3) NOT NULL, + "updated_at" TIMESTAMP(3) NOT NULL, "deleted_at" TIMESTAMP(3), CONSTRAINT "Trigger_pkey" PRIMARY KEY ("id") diff --git a/autonomous_agent_api/prisma/schema.prisma b/autonomous_agent_api/prisma/schema.prisma index 981bbe02e..63ee09013 100644 --- a/autonomous_agent_api/prisma/schema.prisma +++ b/autonomous_agent_api/prisma/schema.prisma @@ -14,8 +14,8 @@ model Agent { id String @id // UUID name String action String[] @default([]) - created_at DateTime? - updated_at DateTime? + created_at DateTime + updated_at DateTime deleted_at DateTime? // Soft deletion field } @@ -24,8 +24,8 @@ model Trigger { agent_id String type TriggerType data Json - created_at DateTime? - updated_at DateTime? + created_at DateTime + updated_at DateTime deleted_at DateTime? } diff --git a/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_agent_list.py b/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_agent_list.py index 6f37e83d6..53533b37d 100644 --- a/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_agent_list.py +++ b/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_agent_list.py @@ -1,7 +1,7 @@ from unittest.mock import MagicMock, AsyncMock from backend.app.controllers.agent_router import AgentRouter -from backend.app.models.AgentDto.response_dto import AgentResponse +from backend.app.models.agent.response_dto import AgentResponse from backend.app.services.agent_service import AgentService import pytest diff --git a/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_create_agent.py b/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_create_agent.py index 6f37e83d6..5d4dfb09d 100644 --- a/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_create_agent.py +++ b/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_create_agent.py @@ -1,46 +1,49 @@ -from unittest.mock import MagicMock, AsyncMock +from pydantic import ValidationError from backend.app.controllers.agent_router import AgentRouter -from backend.app.models.AgentDto.response_dto import AgentResponse +from backend.app.models.agent.agent_dto import AgentCreateDTO +from backend.app.models.agent.response_dto import AgentResponse from backend.app.services.agent_service import AgentService - +from unittest.mock import MagicMock, AsyncMock import pytest -@pytest.fixture -def agent_service(): - mock_service = MagicMock(spec=AgentService) - return mock_service +@pytest.mark.asyncio +class TestAgentEditRouter: + @pytest.fixture + def agent_service(self): + return MagicMock(spec=AgentService) + @pytest.fixture + def agent_router(self, agent_service): + return AgentRouter(agent_service) -@pytest.fixture -def agent_router(agent_service): - return AgentRouter(agent_service) + @pytest.mark.asyncio + async def test_create_agent_with_valid_details(self, agent_service, agent_router): + agent_id = "018e8909-549b-7b9f-8fab-5499f53a8244" + agent_data = AgentCreateDTO(name="Agent", action=["Test Description"]) + created_agent = AgentResponse(id=agent_id, name="Agent", action=["Test Description"]) + agent_service.update_agent = AsyncMock(return_value=created_agent) -# test for listing the agents -@pytest.mark.asyncio -async def test_list_agents_with_two_agents(agent_service, agent_router): - # Mocking the list_agents method to return a list of mock agents - mock_agents = [ - AgentResponse( - id="018e8908-5dc9-78c9-b71a-37ebd2149394", - name="Agent 1", - action=["Description 1"], - ), - AgentResponse( - id="018e8908-7563-7e8a-b87c-b33ac6e6c872", - name="Agent 2", - action=["Description 2"], - ), - ] - agent_service.list_agents = AsyncMock(return_value=mock_agents) - - # Call the list_agents method - agents = await agent_router.list_agents() - - # calling method - agent_service.list_agents.assert_called_once() - - # Assert that the returned agents match the mock_agents - assert agents == mock_agents + result = await agent_router.create_agent(agent_id, agent_data) + + agent_service.update_agent.assert_called_once_with(agent_id, agent_data) + + assert result == created_agent + + @pytest.mark.asyncio + async def test_create_agent_should_fail_with_invalid_details(self, agent_service, agent_router): + with pytest.raises(ValidationError): + # Mock data + agent_id = "018e8909-549b-7b9f-8fab-5499f53a8244" + agent_data = AgentCreateDTO(name="", action=["Test Description"]) + created_agent = AgentResponse(id=agent_id, name="Agent", action=["Test Description"]) + + agent_service.update_agent = AsyncMock(return_value=created_agent) + + result = await agent_router.update_agent(agent_id, agent_data) + + agent_service.update_agent.assert_called_once_with(agent_id, agent_data) + + assert result == created_agent diff --git a/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_editing_agent.py b/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_editing_agent.py index 0b5d15bcc..4bebe90a1 100644 --- a/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_editing_agent.py +++ b/autonomous_agent_api/tests/unit/app/controllers/agent_router_test/test_editing_agent.py @@ -1,8 +1,8 @@ from pydantic import ValidationError from backend.app.controllers.agent_router import AgentRouter -from backend.app.models.AgentDto.agent_dto import AgentCreateDTO -from backend.app.models.AgentDto.response_dto import AgentResponse +from backend.app.models.agent.agent_dto import AgentCreateDTO +from backend.app.models.agent.response_dto import AgentResponse from backend.app.services.agent_service import AgentService from unittest.mock import MagicMock, AsyncMock import pytest