Spaces:
Sleeping
Sleeping
Add test_agent.py with unit tests for AgentRunner functionality, including tests for question handling and basic math calculations. Implement logging for better traceability during tests.
Browse files- test_agent.py +142 -0
test_agent.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import pytest
|
| 3 |
+
import requests
|
| 4 |
+
from agent import AgentRunner
|
| 5 |
+
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO,
|
| 9 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 10 |
+
)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
# Suppress specific warnings
|
| 14 |
+
pytestmark = pytest.mark.filterwarnings(
|
| 15 |
+
"ignore::DeprecationWarning:httpx._models"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Constants
|
| 19 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
+
QUESTIONS_URL = f"{DEFAULT_API_URL}/questions"
|
| 21 |
+
|
| 22 |
+
@pytest.fixture(scope="session")
|
| 23 |
+
def agent():
|
| 24 |
+
"""Fixture to create and return an AgentRunner instance."""
|
| 25 |
+
logger.info("Creating AgentRunner instance")
|
| 26 |
+
return AgentRunner()
|
| 27 |
+
|
| 28 |
+
# @pytest.fixture(scope="session")
|
| 29 |
+
# def questions_data():
|
| 30 |
+
# """Fixture to fetch questions from the API."""
|
| 31 |
+
# logger.info(f"Fetching questions from: {QUESTIONS_URL}")
|
| 32 |
+
# try:
|
| 33 |
+
# response = requests.get(QUESTIONS_URL, timeout=15)
|
| 34 |
+
# response.raise_for_status()
|
| 35 |
+
# data = response.json()
|
| 36 |
+
# if not data:
|
| 37 |
+
# logger.error("Fetched questions list is empty.")
|
| 38 |
+
# return []
|
| 39 |
+
# logger.info(f"Fetched {len(data)} questions.")
|
| 40 |
+
# return data
|
| 41 |
+
# except requests.exceptions.RequestException as e:
|
| 42 |
+
# logger.error(f"Error fetching questions: {e}")
|
| 43 |
+
# return []
|
| 44 |
+
# except requests.exceptions.JSONDecodeError as e:
|
| 45 |
+
# logger.error(f"Error decoding JSON response from questions endpoint: {e}")
|
| 46 |
+
# return []
|
| 47 |
+
# except Exception as e:
|
| 48 |
+
# logger.error(f"An unexpected error occurred fetching questions: {e}")
|
| 49 |
+
# return []
|
| 50 |
+
#
|
| 51 |
+
# class TestAppQuestions:
|
| 52 |
+
# """Test cases for questions from the app."""
|
| 53 |
+
#
|
| 54 |
+
# def test_first_app_question(self, agent, questions_data):
|
| 55 |
+
# """Test the agent's response to the first app question."""
|
| 56 |
+
# if not questions_data:
|
| 57 |
+
# pytest.skip("No questions available from API")
|
| 58 |
+
#
|
| 59 |
+
# first_question = questions_data[0]
|
| 60 |
+
# question_text = first_question.get("question")
|
| 61 |
+
# task_id = first_question.get("task_id")
|
| 62 |
+
#
|
| 63 |
+
# if not question_text or not task_id:
|
| 64 |
+
# pytest.skip("First question is missing required fields")
|
| 65 |
+
#
|
| 66 |
+
# logger.info(f"Testing with app question: {question_text}")
|
| 67 |
+
#
|
| 68 |
+
# response = agent(question_text)
|
| 69 |
+
# logger.info(f"Agent response: {response}")
|
| 70 |
+
#
|
| 71 |
+
# # Check that the response contains the expected information
|
| 72 |
+
# assert "Mercedes Sosa" in response, "Response should mention Mercedes Sosa"
|
| 73 |
+
# assert "studio albums" in response.lower(), "Response should mention studio albums"
|
| 74 |
+
# assert "2000" in response and "2009" in response, "Response should mention the year range"
|
| 75 |
+
#
|
| 76 |
+
# # Verify that a number is mentioned (either as word or digit)
|
| 77 |
+
# import re
|
| 78 |
+
# number_pattern = r'\b(one|two|three|four|five|six|seven|eight|nine|ten|\d+)\b'
|
| 79 |
+
# has_number = bool(re.search(number_pattern, response.lower()))
|
| 80 |
+
# assert has_number, "Response should include the number of albums"
|
| 81 |
+
#
|
| 82 |
+
# # Check for album names in the response
|
| 83 |
+
# known_albums = [
|
| 84 |
+
# "Corazón Libre",
|
| 85 |
+
# "Cantora",
|
| 86 |
+
# "Hermano",
|
| 87 |
+
# "Acústico",
|
| 88 |
+
# "Argentina quiere cantar"
|
| 89 |
+
# ]
|
| 90 |
+
# found_albums = [album for album in known_albums if album in response]
|
| 91 |
+
# assert len(found_albums) > 0, "Response should mention at least some of the known albums"
|
| 92 |
+
#
|
| 93 |
+
# # Check for a structured response
|
| 94 |
+
# assert re.search(r'\d+\.\s+[^(]+\(\d{4}\)', response), \
|
| 95 |
+
# "Response should list albums with years"
|
| 96 |
+
|
| 97 |
+
class TestBasicCodeAgentCapabilities:
|
| 98 |
+
"""Test cases for basic CodeAgent capabilities using examples from the YAML file."""
|
| 99 |
+
|
| 100 |
+
def test_simple_math_calculation(self, agent):
|
| 101 |
+
"""Test the agent's ability to perform basic mathematical operations."""
|
| 102 |
+
# Test the second example from code_agent.yaml
|
| 103 |
+
question = "What is the result of the following operation: 5 + 3 + 1294.678?"
|
| 104 |
+
|
| 105 |
+
logger.info("Testing simple math calculation capabilities")
|
| 106 |
+
logger.info(f"Question: {question}")
|
| 107 |
+
|
| 108 |
+
response = agent(question)
|
| 109 |
+
logger.info(f"Agent response: {response}")
|
| 110 |
+
|
| 111 |
+
# Verify the response contains the correct result
|
| 112 |
+
expected_result = str(5 + 3 + 1294.678)
|
| 113 |
+
assert expected_result in response, f"Response should contain the result {expected_result}"
|
| 114 |
+
|
| 115 |
+
# Check that the response is a clear answer
|
| 116 |
+
assert "answer" in response.lower(), "Response should indicate it's providing an answer"
|
| 117 |
+
|
| 118 |
+
def test_document_qa_and_image_generation(self, agent):
|
| 119 |
+
"""Test the agent's ability to process a document QA task and generate an image."""
|
| 120 |
+
# Test the first example from code_agent.yaml
|
| 121 |
+
question = "Generate an image of the oldest person in this document."
|
| 122 |
+
|
| 123 |
+
logger.info("Testing document QA and image generation capabilities")
|
| 124 |
+
logger.info(f"Question: {question}")
|
| 125 |
+
|
| 126 |
+
response = agent(question)
|
| 127 |
+
logger.info(f"Agent response: {response}")
|
| 128 |
+
|
| 129 |
+
# Verify the response contains key elements
|
| 130 |
+
assert "Bob Wilson" in response, "Response should identify Bob Wilson as the oldest person"
|
| 131 |
+
assert "60" in response, "Response should mention the age 60"
|
| 132 |
+
assert "engineer" in response, "Response should mention the profession"
|
| 133 |
+
assert "Vancouver" in response, "Response should mention the location"
|
| 134 |
+
|
| 135 |
+
# Check for image generation related content
|
| 136 |
+
assert "image" in response.lower() or "portrait" in response.lower(), \
|
| 137 |
+
"Response should indicate image generation"
|
| 138 |
+
assert "description" in response.lower(), \
|
| 139 |
+
"Response should include a description of the image"
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
pytest.main([__file__, "-v", "-x"])
|