Spaces:
Runtime error
Runtime error
Create tests/test_workflow.py
Browse files- tests/test_workflow.py +75 -0
tests/test_workflow.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for the pharmaceutical data management agent workflow.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import unittest
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
from unittest.mock import MagicMock, patch
|
| 9 |
+
|
| 10 |
+
from anthropic import Anthropic
|
| 11 |
+
from langgraph.graph import START
|
| 12 |
+
|
| 13 |
+
from agents.state import AgentState
|
| 14 |
+
from graph.workflow import create_agent_graph
|
| 15 |
+
|
| 16 |
+
class TestAgentWorkflow(unittest.TestCase):
|
| 17 |
+
"""Test case for the agent workflow."""
|
| 18 |
+
|
| 19 |
+
def setUp(self):
|
| 20 |
+
"""Set up test fixtures."""
|
| 21 |
+
# Mock the Anthropic client
|
| 22 |
+
self.anthropic_client = MagicMock()
|
| 23 |
+
self.anthropic_client.messages.create.return_value = MagicMock(
|
| 24 |
+
content=[MagicMock(text="INTENT_COMPLETE\nThis is a test intent.")]
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Mock the database
|
| 28 |
+
self.db = MagicMock()
|
| 29 |
+
self.db.get_tables.return_value = {
|
| 30 |
+
"raw_tables": ["RAW_SALES_TRANSACTIONS", "RAW_HCP_DATA", "RAW_PRODUCT_DATA"],
|
| 31 |
+
"staging_tables": ["STG_SALES", "STG_HCP", "STG_PRODUCT"],
|
| 32 |
+
"ard_tables": ["ARD_SALES_PERFORMANCE", "ARD_HCP_ENGAGEMENT", "ARD_MARKET_ANALYSIS"],
|
| 33 |
+
"data_products": ["DP_SALES_DASHBOARD", "DP_HCP_TARGETING"]
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Create a test agent graph
|
| 37 |
+
self.agent_graph, self.update_state_dict = create_agent_graph(
|
| 38 |
+
self.anthropic_client, self.db
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
def test_workflow_has_entry_point(self):
|
| 42 |
+
"""Test that the workflow has an entry point."""
|
| 43 |
+
# Get the graph builder
|
| 44 |
+
graph_builder = self.agent_graph.builder
|
| 45 |
+
|
| 46 |
+
# Check if there's an edge from START
|
| 47 |
+
start_edges = [edge for edge in graph_builder.edges if edge[0] == START]
|
| 48 |
+
|
| 49 |
+
# Assert that there's at least one edge from START
|
| 50 |
+
self.assertTrue(len(start_edges) > 0, "Workflow must have at least one edge from START")
|
| 51 |
+
|
| 52 |
+
def test_workflow_execution(self):
|
| 53 |
+
"""Test the workflow execution."""
|
| 54 |
+
# Prepare the input state
|
| 55 |
+
input_state = {
|
| 56 |
+
"messages": [{"role": "user", "content": "Create a dashboard showing sales by region"}],
|
| 57 |
+
"user_intent": {},
|
| 58 |
+
"data_context": {},
|
| 59 |
+
"pipeline_plan": {},
|
| 60 |
+
"sql_queries": [],
|
| 61 |
+
"execution_results": {},
|
| 62 |
+
"confidence_scores": {},
|
| 63 |
+
"status": "planning",
|
| 64 |
+
"current_agent": "understanding_agent"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Invoke the workflow
|
| 68 |
+
result = self.agent_graph.invoke(input_state)
|
| 69 |
+
|
| 70 |
+
# Check that the workflow updated the state
|
| 71 |
+
self.assertIn("user_intent", result)
|
| 72 |
+
self.assertTrue(result["user_intent"].get("understood", False))
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
unittest.main()
|