Skip to content

Commit

Permalink
{TESTS]
Browse files Browse the repository at this point in the history
  • Loading branch information
kyegomez committed Dec 31, 2024
1 parent 5365915 commit 79e7d20
Show file tree
Hide file tree
Showing 22 changed files with 1,948 additions and 399 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ print(agent.model_dump_json())
print(agent.model_dump_yaml())

# Ingest documents into the agent's knowledge base
agent.ingest_docs("your_pdf_path.pdf")
("your_pdf_path.pdf")

# Receive a message from a user and process it
agent.receive_message(name="agent_name", message="message")
Expand Down
212 changes: 141 additions & 71 deletions api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,77 @@

# Configure loguru
LOG_PATH = "api_tests.log"
logger.add(LOG_PATH,
logger.add(
LOG_PATH,
format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}",
rotation="1 day",
retention="7 days",
level="DEBUG"
level="DEBUG",
)

BASE_URL = (
"https://api.swarms.ai/v1" # Change this to match your server URL
)

BASE_URL = "https://api.swarms.ai/v1" # Change this to match your server URL

async def log_request_details(method: str, url: str, headers: dict, data: Any = None):
async def log_request_details(
method: str, url: str, headers: dict, data: Any = None
):
"""Log request details before sending."""
logger.debug(f"\n{'='*50}")
logger.debug(f"REQUEST: {method} {url}")
logger.debug(f"HEADERS: {json.dumps(headers, indent=2)}")
if data:
logger.debug(f"PAYLOAD: {json.dumps(data, indent=2)}")

async def log_response_details(response: aiohttp.ClientResponse, data: Any = None):

async def log_response_details(
response: aiohttp.ClientResponse, data: Any = None
):
"""Log response details after receiving."""
logger.debug(f"\nRESPONSE Status: {response.status}")
logger.debug(f"RESPONSE Headers: {json.dumps(dict(response.headers), indent=2)}")
logger.debug(
f"RESPONSE Headers: {json.dumps(dict(response.headers), indent=2)}"
)
if data:
logger.debug(f"RESPONSE Body: {json.dumps(data, indent=2)}")
logger.debug(f"{'='*50}\n")

async def test_create_user(session: aiohttp.ClientSession) -> Dict[str, str]:

async def test_create_user(
session: aiohttp.ClientSession,
) -> Dict[str, str]:
"""Test user creation endpoint."""
url = f"{BASE_URL}/users"
payload = {"username": "test_user"}

logger.info("Testing user creation...")
await log_request_details("POST", url, {}, payload)

try:
async with session.post(url, json=payload) as response:
data = await response.json()
await log_response_details(response, data)

if response.status != 200:
logger.error(f"Failed to create user. Status: {response.status}, Response: {data}")
logger.error(
f"Failed to create user. Status: {response.status}, Response: {data}"
)
sys.exit(1)

logger.success("✓ Created user successfully")
return {"user_id": data["user_id"], "api_key": data["api_key"]}
return {
"user_id": data["user_id"],
"api_key": data["api_key"],
}
except Exception as e:
logger.exception(f"Exception in user creation: {str(e)}")
sys.exit(1)

async def test_create_agent(session: aiohttp.ClientSession, api_key: str) -> str:

async def test_create_agent(
session: aiohttp.ClientSession, api_key: str
) -> str:
"""Test agent creation endpoint."""
url = f"{BASE_URL}/agent"
config = {
Expand All @@ -70,114 +92,142 @@ async def test_create_agent(session: aiohttp.ClientSession, api_key: str) -> str
"tags": ["test"],
"streaming_on": False,
"user_name": "test_user", # Added required field
"output_type": "string" # Added required field
"output_type": "string", # Added required field
}

headers = {"api-key": api_key}
logger.info("Testing agent creation...")
await log_request_details("POST", url, headers, config)

try:
async with session.post(url, headers=headers, json=config) as response:
async with session.post(
url, headers=headers, json=config
) as response:
data = await response.json()
await log_response_details(response, data)

if response.status != 200:
logger.error(f"Failed to create agent. Status: {response.status}, Response: {data}")
logger.error(
f"Failed to create agent. Status: {response.status}, Response: {data}"
)
return None

logger.success("✓ Created agent successfully")
return data["agent_id"]
except Exception as e:
logger.exception(f"Exception in agent creation: {str(e)}")
return None

async def test_agent_update(session: aiohttp.ClientSession, agent_id: str, api_key: str):

async def test_agent_update(
session: aiohttp.ClientSession, agent_id: str, api_key: str
):
"""Test agent update endpoint."""
url = f"{BASE_URL}/agent/{agent_id}"
update_data = {
"description": "Updated test agent",
"system_prompt": "Updated system prompt",
"temperature": 0.7,
"tags": ["test", "updated"]
"tags": ["test", "updated"],
}

headers = {"api-key": api_key}
logger.info(f"Testing agent update for agent {agent_id}...")
await log_request_details("PATCH", url, headers, update_data)

try:
async with session.patch(url, headers=headers, json=update_data) as response:
async with session.patch(
url, headers=headers, json=update_data
) as response:
data = await response.json()
await log_response_details(response, data)

if response.status != 200:
logger.error(f"Failed to update agent. Status: {response.status}, Response: {data}")
logger.error(
f"Failed to update agent. Status: {response.status}, Response: {data}"
)
return False

logger.success("✓ Updated agent successfully")
return True
except Exception as e:
logger.exception(f"Exception in agent update: {str(e)}")
return False

async def test_completion(session: aiohttp.ClientSession, agent_id: str, api_key: str):

async def test_completion(
session: aiohttp.ClientSession, agent_id: str, api_key: str
):
"""Test completion endpoint."""
url = f"{BASE_URL}/agent/completions"
completion_request = {
"prompt": "Hello, how are you?",
"agent_id": agent_id,
"max_tokens": 100,
"stream": False
"stream": False,
}

headers = {"api-key": api_key}
logger.info(f"Testing completion for agent {agent_id}...")
await log_request_details("POST", url, headers, completion_request)

await log_request_details(
"POST", url, headers, completion_request
)

try:
async with session.post(url, headers=headers, json=completion_request) as response:
async with session.post(
url, headers=headers, json=completion_request
) as response:
data = await response.json()
await log_response_details(response, data)

if response.status != 200:
logger.error(f"Failed to process completion. Status: {response.status}, Response: {data}")
logger.error(
f"Failed to process completion. Status: {response.status}, Response: {data}"
)
return False

logger.success("✓ Processed completion successfully")
return True
except Exception as e:
logger.exception(f"Exception in completion processing: {str(e)}")
logger.exception(
f"Exception in completion processing: {str(e)}"
)
return False

async def test_get_metrics(session: aiohttp.ClientSession, agent_id: str, api_key: str):

async def test_get_metrics(
session: aiohttp.ClientSession, agent_id: str, api_key: str
):
"""Test metrics endpoint."""
url = f"{BASE_URL}/agent/{agent_id}/metrics"
headers = {"api-key": api_key}

logger.info(f"Testing metrics retrieval for agent {agent_id}...")
await log_request_details("GET", url, headers)

try:
async with session.get(url, headers=headers) as response:
data = await response.json()
await log_response_details(response, data)

if response.status != 200:
logger.error(f"Failed to get metrics. Status: {response.status}, Response: {data}")
logger.error(
f"Failed to get metrics. Status: {response.status}, Response: {data}"
)
return False

logger.success("✓ Retrieved metrics successfully")
return True
except Exception as e:
logger.exception(f"Exception in metrics retrieval: {str(e)}")
return False


async def run_tests():
"""Run all API tests."""
logger.info("Starting API test suite...")
logger.info(f"Using base URL: {BASE_URL}")

timeout = aiohttp.ClientTimeout(total=30) # 30 second timeout
async with aiohttp.ClientSession(timeout=timeout) as session:
try:
Expand All @@ -186,37 +236,47 @@ async def run_tests():
if not user_data:
logger.error("User creation failed, stopping tests.")
return

logger.info("User created successfully, proceeding with agent tests...")
user_id = user_data["user_id"]

logger.info(
"User created successfully, proceeding with agent tests..."
)
user_data["user_id"]
api_key = user_data["api_key"]

# Create test agent
agent_id = await test_create_agent(session, api_key)
if not agent_id:
logger.error("Agent creation failed, stopping tests.")
return

logger.info("Agent created successfully, proceeding with other tests...")


logger.info(
"Agent created successfully, proceeding with other tests..."
)

# Run remaining tests
test_results = []

# Test metrics retrieval
logger.info("Testing metrics retrieval...")
metrics_result = await test_get_metrics(session, agent_id, api_key)
metrics_result = await test_get_metrics(
session, agent_id, api_key
)
test_results.append(("Metrics", metrics_result))

# Test agent update
logger.info("Testing agent update...")
update_result = await test_agent_update(session, agent_id, api_key)
update_result = await test_agent_update(
session, agent_id, api_key
)
test_results.append(("Agent Update", update_result))

# Test completion
logger.info("Testing completion...")
completion_result = await test_completion(session, agent_id, api_key)
completion_result = await test_completion(
session, agent_id, api_key
)
test_results.append(("Completion", completion_result))

# Log final results
logger.info("\nTest Results Summary:")
all_passed = True
Expand All @@ -225,25 +285,34 @@ async def run_tests():
logger.info(f"{test_name}: {status}")
if not result:
all_passed = False

if all_passed:
logger.success("\n🎉 All tests completed successfully!")
logger.success(
"\n🎉 All tests completed successfully!"
)
else:
logger.error("\n❌ Some tests failed. Check the logs for details.")

logger.info(f"\nDetailed logs available at: {os.path.abspath(LOG_PATH)}")

logger.error(
"\n❌ Some tests failed. Check the logs for details."
)

logger.info(
f"\nDetailed logs available at: {os.path.abspath(LOG_PATH)}"
)

except Exception as e:
logger.exception(f"Unexpected error during test execution: {str(e)}")
logger.exception(
f"Unexpected error during test execution: {str(e)}"
)
raise
finally:
logger.info("Test suite execution completed.")


def main():
logger.info("="*50)
logger.info("=" * 50)
logger.info("API TEST SUITE EXECUTION")
logger.info("="*50)
logger.info("=" * 50)

try:
asyncio.run(run_tests())
except KeyboardInterrupt:
Expand All @@ -253,5 +322,6 @@ def main():
finally:
logger.info("Test suite shutdown complete.")


if __name__ == "__main__":
main()
main()
Loading

0 comments on commit 79e7d20

Please sign in to comment.