Skip to content

Commit

Permalink
test: Ut 10888 backend (#40)
Browse files Browse the repository at this point in the history
* edit 1

* edit 2

* Repository Governance

* added stale-bot

* stalebot test

* chNGED TO 1 DAY

* edit 3

* Create test

* edit1

* edit2

* edit 4

* edit 5

* edit 6

* edit 6

* edit 7

* edit 8

* edit 5

* edit 10

* edit 11

* edit 12

* edit 13

* Update stale-bot.yml

* edit 14

* edit 15

* edit 16

* edit 17

* Update stale-bot.yml

* Update CODEOWNER

* Create nwetest

* added pylint

* changed flak loc

* edit 1

* add backend test workflow

* added test file

* edit 2

* added  sample test file

* changed py version

* made edits to workflow

* pass even if test filesa r not present

* removed test file

* added sample test file

* Create docker-build-and-push.yml

* Delete test

* Delete src/backend/middleware/test_health_check.py

* Delete .github/nwetest

* Delete .github/workflows/CI.yml

* updated docker

* Testcases

* Testcases

* Testcases

* Update test.yml

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Pylint issues fixed

* pylint issues fixed

* Pylint issues fixed

* Pylint issues fixed

* pylint fixed for line

* fix for test cases failure

* added agentutils test

* updated test utils file

* Testcases

* Testcases

* Testcases

* Testcases

* added genric test file

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* solving pylint errors

* resolved pylint errors

* edit 2

* resolving pylint

* edit 3

* resolved issue

* resolve edit 5

* resoleving pylint edit 6

* pylint error edit 7

* Update test.yml

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Update test.yml

* Update test.yml

* Update test.yml

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testing

* Testing

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* added test_base_agent file

* Testcases

* Testcases

* Testcases

* Testcases

* added the test_agentutils and test_base_agent

* deleted agentutils.py

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* Testcases

* edited flak

* resolved pylint issues

* Test case Group_chat_manager

* Test case Group_chat_manager

* updated test_agentutils.py

* editv1

* resolving pylint issue

* edit 2

* Testcases

* Testcases

* Testcases

* Testcases

* docker changes reverted

---------

Co-authored-by: Harmanpreet Kaur <[email protected]>
Co-authored-by: Harmanpreet-Microsoft <[email protected]>
Co-authored-by: Roopan P M <[email protected]>
Co-authored-by: Pradheep-Microsoft <[email protected]>
  • Loading branch information
5 people authored Feb 3, 2025
1 parent be39487 commit f8640d9
Show file tree
Hide file tree
Showing 52 changed files with 3,621 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
max-line-length = 88
extend-ignore = E501
exclude = .venv, frontend
ignore = E203, W503, G004, G200
ignore = E203, W503, G004, G200, E402
5 changes: 1 addition & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r src/backend/requirements.txt
pip install pytest-cov
pip install pytest-asyncio
- name: Check if test files exist
id: check_tests
Expand All @@ -50,7 +48,6 @@ jobs:
echo "Test files found, running tests."
echo "skip_tests=false" >> $GITHUB_ENV
fi
- name: Run tests with coverage
if: env.skip_tests == 'false'
run: |
Expand All @@ -59,4 +56,4 @@ jobs:
- name: Skip coverage report if no tests
if: env.skip_tests == 'true'
run: |
echo "Skipping coverage report because no tests were found."
echo "Skipping coverage report because no tests were found."
Empty file added src/__init__.py
Empty file.
Empty file added src/backend/__init__.py
Empty file.
Empty file added src/backend/agents/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions src/backend/agents/agentutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
)
from pydantic import BaseModel

from context.cosmos_memory import CosmosBufferedChatCompletionContext
from models.messages import Step
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.models.messages import Step

common_agent_system_message = "If you do not have the information for the arguments of the function you need to call, do not call the function. Instead, respond back to the user requesting further information. You must not hallucinate or invent any of the information used as arguments in the function. For example, if you need to call a function that requires a delivery address, you must not generate 123 Example St. You must skip calling functions and return a clarification message along the lines of: Sorry, I'm missing some information I need to help you with that. Could you please provide the delivery address so I can do that for you?"

Expand Down
6 changes: 3 additions & 3 deletions src/backend/agents/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
from autogen_core.components.tool_agent import tool_agent_caller_loop
from autogen_core.components.tools import Tool

from context.cosmos_memory import CosmosBufferedChatCompletionContext
from models.messages import (
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.models.messages import (
ActionRequest,
ActionResponse,
AgentMessage,
Step,
StepStatus,
)
from event_utils import track_event_if_configured
from src.backend.event_utils import track_event_if_configured


class BaseAgent(RoutedAgent):
Expand Down
4 changes: 2 additions & 2 deletions src/backend/agents/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from autogen_core.components.models import AzureOpenAIChatCompletionClient
from autogen_core.components.tools import FunctionTool, Tool

from agents.base_agent import BaseAgent
from context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.agents.base_agent import BaseAgent
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext


async def dummy_function() -> str:
Expand Down
6 changes: 3 additions & 3 deletions src/backend/agents/group_chat_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from autogen_core.components import RoutedAgent, default_subscription, message_handler
from autogen_core.components.models import AzureOpenAIChatCompletionClient

from context.cosmos_memory import CosmosBufferedChatCompletionContext
from models.messages import (
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.models.messages import (
ActionRequest,
AgentMessage,
BAgentType,
Expand All @@ -22,7 +22,7 @@
StepStatus,
)

from event_utils import track_event_if_configured
from src.backend.event_utils import track_event_if_configured


@default_subscription
Expand Down
4 changes: 2 additions & 2 deletions src/backend/agents/hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from autogen_core.components.tools import FunctionTool, Tool
from typing_extensions import Annotated

from agents.base_agent import BaseAgent
from context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.agents.base_agent import BaseAgent
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext

formatting_instructions = "Instructions: returning the output of this function call verbatim to the user in markdown. Then write AGENT SUMMARY: and then include a summary of what you did."

Expand Down
6 changes: 3 additions & 3 deletions src/backend/agents/human.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from autogen_core.base import AgentId, MessageContext
from autogen_core.components import RoutedAgent, default_subscription, message_handler

from context.cosmos_memory import CosmosBufferedChatCompletionContext
from models.messages import (
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.models.messages import (
ApprovalRequest,
HumanFeedback,
StepStatus,
AgentMessage,
Step,
)
from event_utils import track_event_if_configured
from src.backend.event_utils import track_event_if_configured


@default_subscription
Expand Down
4 changes: 2 additions & 2 deletions src/backend/agents/marketing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from autogen_core.components.models import AzureOpenAIChatCompletionClient
from autogen_core.components.tools import FunctionTool, Tool

from agents.base_agent import BaseAgent
from context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.agents.base_agent import BaseAgent
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext


# Define new Marketing tools (functions)
Expand Down
6 changes: 3 additions & 3 deletions src/backend/agents/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
)
from pydantic import BaseModel

from context.cosmos_memory import CosmosBufferedChatCompletionContext
from models.messages import (
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.models.messages import (
AgentMessage,
HumanClarification,
BAgentType,
Expand All @@ -26,7 +26,7 @@
HumanFeedbackStatus,
)

from event_utils import track_event_if_configured
from src.backend.event_utils import track_event_if_configured


@default_subscription
Expand Down
4 changes: 2 additions & 2 deletions src/backend/agents/procurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from autogen_core.components.tools import FunctionTool, Tool
from typing_extensions import Annotated

from agents.base_agent import BaseAgent
from context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.agents.base_agent import BaseAgent
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext


# Define new Procurement tools (functions)
Expand Down
4 changes: 2 additions & 2 deletions src/backend/agents/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from autogen_core.components.tools import FunctionTool, Tool
from typing_extensions import Annotated

from agents.base_agent import BaseAgent
from context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.agents.base_agent import BaseAgent
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext

formatting_instructions = "Instructions: returning the output of this function call verbatim to the user in markdown. Then write AGENT SUMMARY: and then include a summary of what you did."

Expand Down
5 changes: 2 additions & 3 deletions src/backend/agents/tech_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from autogen_core.components.tools import FunctionTool, Tool
from typing_extensions import Annotated

from agents.base_agent import BaseAgent
from context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.agents.base_agent import BaseAgent
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext

formatting_instructions = "Instructions: returning the output of this function call verbatim to the user in markdown. Then write AGENT SUMMARY: and then include a summary of what you did."

Expand Down Expand Up @@ -523,7 +523,6 @@ async def get_tech_information(
Document Name: Contoso's IT Policy and Procedure Manual
Domain: IT Policy
Description: A comprehensive guide detailing the IT policies and procedures at Contoso, including acceptable use, security protocols, and incident reporting.
At Contoso, we prioritize the security and efficiency of our IT infrastructure. All employees are required to adhere to the following policies:
- Use strong passwords and change them every 90 days.
- Report any suspicious emails to the IT department immediately.
Expand Down
14 changes: 7 additions & 7 deletions src/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import os
import uuid
from typing import List, Optional
from middleware.health_check import HealthCheckMiddleware
from src.backend.middleware.health_check import HealthCheckMiddleware
from autogen_core.base import AgentId
from fastapi import FastAPI, HTTPException, Query, Request
from auth.auth_utils import get_authenticated_user_details
from config import Config
from context.cosmos_memory import CosmosBufferedChatCompletionContext
from models.messages import (
from src.backend.auth.auth_utils import get_authenticated_user_details
from src.backend.config import Config
from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext
from src.backend.models.messages import (
HumanFeedback,
HumanClarification,
InputTask,
Expand All @@ -19,8 +19,8 @@
AgentMessage,
PlanWithSteps,
)
from utils import initialize_runtime_and_context, retrieve_all_agent_tools, rai_success
from event_utils import track_event_if_configured
from src.backend.utils import initialize_runtime_and_context, retrieve_all_agent_tools, rai_success
from src.backend.event_utils import track_event_if_configured
from fastapi.middleware.cors import CORSMiddleware
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
Expand Down
4 changes: 2 additions & 2 deletions src/backend/context/cosmos_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
)
from azure.cosmos.partition_key import PartitionKey

from config import Config
from models.messages import BaseDataModel, Plan, Session, Step, AgentMessage
from src.backend.config import Config
from src.backend.models.messages import BaseDataModel, Plan, Session, Step, AgentMessage


class CosmosBufferedChatCompletionContext(BufferedChatCompletionContext):
Expand Down
Empty file.
4 changes: 3 additions & 1 deletion src/backend/handlers/runtime_interrupt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from autogen_core.base import AgentId
from autogen_core.base.intervention import DefaultInterventionHandler

from models.messages import GetHumanInputMessage, GroupChatMessage
from src.backend.models.messages import GroupChatMessage

from src.backend.models.messages import GetHumanInputMessage


class NeedsUserInputHandler(DefaultInterventionHandler):
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions src/backend/models/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,11 @@ class RequestToSpeak(BaseModel):

def to_dict(self):
return self.model_dump()


class GetHumanInputMessage:
def __init__(self, message):
self.message = message

def __str__(self):
return f"GetHumanInputMessage: {self.message}"
5 changes: 5 additions & 0 deletions src/backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ opentelemetry-instrumentation-fastapi
opentelemetry-instrumentation-openai
opentelemetry-exporter-otlp-proto-http
opentelemetry-exporter-otlp-proto-grpc

# Testing tools
pytest>=8.2,<9 # Compatible version for pytest-asyncio
pytest-asyncio==0.24.0
pytest-cov==5.0.0
Empty file added src/backend/tests/__init__.py
Empty file.
Empty file.
54 changes: 54 additions & 0 deletions src/backend/tests/agents/test_agentutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# pylint: disable=import-error, wrong-import-position, missing-module-docstring
import os
import sys
from unittest.mock import MagicMock
import pytest
from pydantic import ValidationError

# Environment and module setup
sys.modules["azure.monitor.events.extension"] = MagicMock()

os.environ["COSMOSDB_ENDPOINT"] = "https://mock-endpoint"
os.environ["COSMOSDB_KEY"] = "mock-key"
os.environ["COSMOSDB_DATABASE"] = "mock-database"
os.environ["COSMOSDB_CONTAINER"] = "mock-container"
os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "mock-deployment-name"
os.environ["AZURE_OPENAI_API_VERSION"] = "2023-01-01"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://mock-openai-endpoint"

from src.backend.agents.agentutils import extract_and_update_transition_states # noqa: F401, C0413
from src.backend.models.messages import Step # noqa: F401, C0413


def test_step_initialization():
"""Test Step initialization with valid data."""
step = Step(
data_type="step",
plan_id="test_plan",
action="test_action",
agent="HumanAgent",
session_id="test_session",
user_id="test_user",
agent_reply="test_reply",
)

assert step.data_type == "step"
assert step.plan_id == "test_plan"
assert step.action == "test_action"
assert step.agent == "HumanAgent"
assert step.session_id == "test_session"
assert step.user_id == "test_user"
assert step.agent_reply == "test_reply"
assert step.status == "planned"
assert step.human_approval_status == "requested"


def test_step_missing_required_fields():
"""Test Step initialization with missing required fields."""
with pytest.raises(ValidationError):
Step(
data_type="step",
action="test_action",
agent="test_agent",
session_id="test_session",
)
Loading

0 comments on commit f8640d9

Please sign in to comment.