test_server.py•4.21 kB
import sys
import os
import unittest
from unittest.mock import patch, MagicMock
import json
import pandas as pd
# Add parent directory to path to import scraper and server modules
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# Import functions to test
import server
class TestServer(unittest.TestCase):
"""Test cases for the NFL transactions MCP server"""
def test_list_tools(self):
"""Test the listTools method"""
result = server.listTools()
# Check that we have the expected tools
self.assertIn("tools", result)
tools = result["tools"]
# Verify we have at least 3 tools
self.assertTrue(len(tools) >= 3)
# Check for specific tool names
tool_names = [t["name"] for t in tools]
self.assertIn("fetch_transactions", tool_names)
self.assertIn("list_teams", tool_names)
self.assertIn("list_transaction_types", tool_names)
@patch('server.fetch_all_transactions')
def test_fetch_transactions(self, mock_fetch):
"""Test the fetch_transactions method"""
# Create a test DataFrame to return
test_df = pd.DataFrame({
'Date': ['2023-01-01', '2023-01-02'],
'Team': ['Patriots', 'Patriots'],
'Acquired': ['Player A', 'Player B'],
'Relinquished': ['', 'Player C'],
'Notes': ['Signed as free agent', 'Traded'],
'transaction_type': ['Player', 'Player']
})
mock_fetch.return_value = test_df
# Call the method with test params
result = server.fetch_transactions(
start_date="2023-01-01",
end_date="2023-12-31",
transaction_type="Player",
team="Patriots"
)
# Verify the result
self.assertEqual(result["status"], "success")
self.assertIn("data", result)
self.assertEqual(len(result["data"]), 2)
# Test with empty DataFrame
mock_fetch.return_value = pd.DataFrame()
result = server.fetch_transactions(
start_date="2023-01-01",
end_date="2023-12-31"
)
self.assertEqual(result["status"], "success")
self.assertEqual(result["message"], "No transactions found")
# Test with exception
mock_fetch.side_effect = ValueError("Invalid date")
result = server.fetch_transactions(
start_date="invalid",
end_date="2023-12-31"
)
self.assertIsInstance(result, server.Error)
@patch('server.get_all_nfl_teams')
def test_list_teams(self, mock_teams):
"""Test the list_teams method"""
# Mock the teams function
mock_teams.return_value = ["Patriots", "Chiefs", "Bills"]
# Call the method
result = server.list_teams()
# Verify the result
self.assertEqual(result["status"], "success")
self.assertIn("teams", result)
self.assertEqual(len(result["teams"]), 3)
self.assertIn("Patriots", result["teams"])
# Test with exception
mock_teams.side_effect = Exception("Test error")
result = server.list_teams()
self.assertIsInstance(result, server.Error)
@patch('server.get_available_transaction_types')
def test_list_transaction_types(self, mock_types):
"""Test the list_transaction_types method"""
# Mock the transaction types function
mock_types.return_value = ["Player", "Injury", "Legal", "All"]
# Call the method
result = server.list_transaction_types()
# Verify the result
self.assertEqual(result["status"], "success")
self.assertIn("transaction_types", result)
self.assertEqual(len(result["transaction_types"]), 4)
self.assertIn("Player", result["transaction_types"])
# Test with exception
mock_types.side_effect = Exception("Test error")
result = server.list_transaction_types()
self.assertIsInstance(result, server.Error)
if __name__ == '__main__':
unittest.main()