#!/usr/bin/env python3 """ Test script for Mice Game Score API """ import requests import json import time API_BASE_URL = "http://localhost:8000" def test_api(): """Test all API endpoints""" print("Testing Mice Game Score API...") print("=" * 50) # Test device and user IDs device_id = "DEV-TEST001" user1 = "TestUser1" user2 = "TestUser2" try: # 1. Test root endpoint print("\n1. Testing root endpoint:") response = requests.get(f"{API_BASE_URL}/") print(f"Status: {response.status_code}") print(f"Response: {response.json()}") # 2. Test user signup print(f"\n2. Testing user signup for {user1}:") response = requests.post(f"{API_BASE_URL}/signup/{device_id}/{user1}") print(f"Status: {response.status_code}") print(f"Response: {response.json()}") print(f"\n3. Testing user signup for {user2}:") response = requests.post(f"{API_BASE_URL}/signup/{device_id}/{user2}") print(f"Status: {response.status_code}") print(f"Response: {response.json()}") # 4. Test duplicate signup (should fail) print(f"\n4. Testing duplicate signup for {user1} (should fail):") response = requests.post(f"{API_BASE_URL}/signup/{device_id}/{user1}") print(f"Status: {response.status_code}") print(f"Response: {response.json()}") # 5. Test getting users for device print(f"\n5. Testing get users for device {device_id}:") response = requests.get(f"{API_BASE_URL}/users/{device_id}") print(f"Status: {response.status_code}") print(f"Response: {response.json()}") # 6. Test score submission print(f"\n6. Testing score submission for {user1}:") score_data = { "user_id": user1, "device_id": device_id, "score": 1500, "game_completed": True } response = requests.post( f"{API_BASE_URL}/score/{device_id}/{user1}", json=score_data ) print(f"Status: {response.status_code}") print(f"Response: {response.json()}") # 7. Test more score submissions scores_to_test = [ (user1, 2000, True), (user1, 1200, False), (user2, 1800, True), (user2, 2500, True) ] print(f"\n7. Testing multiple score submissions:") for user, score, completed in scores_to_test: score_data = { "user_id": user, "device_id": device_id, "score": score, "game_completed": completed } response = requests.post( f"{API_BASE_URL}/score/{device_id}/{user}", json=score_data ) print(f" {user} - Score: {score}, Completed: {completed} -> Status: {response.status_code}") time.sleep(0.1) # Small delay between requests # 8. Test score submission for non-registered user (should fail) print(f"\n8. Testing score submission for non-registered user (should fail):") score_data = { "user_id": "NonExistentUser", "device_id": device_id, "score": 1000, "game_completed": True } response = requests.post( f"{API_BASE_URL}/score/{device_id}/NonExistentUser", json=score_data ) print(f"Status: {response.status_code}") print(f"Response: {response.json()}") # 9. Test getting user scores print(f"\n9. Testing get scores for {user1}:") response = requests.get(f"{API_BASE_URL}/scores/{device_id}/{user1}") print(f"Status: {response.status_code}") scores = response.json() print(f"Number of scores: {len(scores)}") for score in scores: print(f" Score: {score['score']}, Completed: {score['game_completed']}, Time: {score['timestamp']}") # 10. Test leaderboard print(f"\n10. Testing leaderboard for device {device_id}:") response = requests.get(f"{API_BASE_URL}/leaderboard/{device_id}") print(f"Status: {response.status_code}") leaderboard = response.json() print("Leaderboard:") for entry in leaderboard: print(f" Rank {entry['rank']}: {entry['user_id']} - Best Score: {entry['best_score']} ({entry['total_games']} games)") # 11. Test final user list print(f"\n11. Final user list for device {device_id}:") response = requests.get(f"{API_BASE_URL}/users/{device_id}") users = response.json() for user in users: print(f" {user['user_id']}: Best Score: {user['best_score']}, Total Games: {user['total_scores']}") print("\n" + "=" * 50) print("API Testing completed successfully!") except requests.exceptions.ConnectionError: print("ERROR: Could not connect to API server.") print("Make sure the API server is running with: python score_api.py") except Exception as e: print(f"ERROR: {e}") if __name__ == "__main__": test_api()