Skip to content

Commit

Permalink
made changes according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
roshan00017 committed Apr 4, 2024
1 parent 297e021 commit 76ed018
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 74 deletions.
4 changes: 2 additions & 2 deletions autonomous_agent_api/backend/app/controllers/agent_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
12 changes: 6 additions & 6 deletions autonomous_agent_api/backend/app/controllers/trigger_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Empty file.
5 changes: 4 additions & 1 deletion autonomous_agent_api/backend/app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -69,17 +69,15 @@ 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:
raise HTTPException(status_code=404, detail="Trigger not found")
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:
Expand Down
4 changes: 2 additions & 2 deletions autonomous_agent_api/backend/app/services/agent_service.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
14 changes: 7 additions & 7 deletions autonomous_agent_api/backend/app/services/trigger_service.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions autonomous_agent_api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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?
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 76ed018

Please sign in to comment.