Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| Test Data Generators and Fixtures | |
| Provides reusable test data for the MMORPG test suite. | |
| """ | |
| import random | |
| import string | |
| from typing import Dict, List, Any, Optional | |
| from datetime import datetime, timedelta | |
| from dataclasses import dataclass | |
| class TestDataGenerator: | |
| """Generates test data for MMORPG components.""" | |
| def random_string(length: int = 10) -> str: | |
| """Generate a random string of specified length.""" | |
| return ''.join(random.choices(string.ascii_letters + string.digits, k=length)) | |
| def random_player_name() -> str: | |
| """Generate a random player name.""" | |
| prefixes = ["Hero", "Knight", "Mage", "Warrior", "Rogue", "Paladin", "Archer"] | |
| suffixes = ["Bold", "Swift", "Wise", "Strong", "Brave", "Noble", "Quick"] | |
| return f"{random.choice(prefixes)}{random.choice(suffixes)}{random.randint(1, 999)}" | |
| def random_coordinates() -> Dict[str, int]: | |
| """Generate random valid coordinates.""" | |
| return { | |
| "x": random.randint(10, 790), | |
| "y": random.randint(10, 590) | |
| } | |
| def create_player_data( | |
| name: Optional[str] = None, | |
| player_type: str = "human", | |
| level: Optional[int] = None, | |
| **kwargs | |
| ) -> Dict[str, Any]: | |
| """Create test player data.""" | |
| coords = TestDataGenerator.random_coordinates() | |
| data = { | |
| "name": name or TestDataGenerator.random_player_name(), | |
| "type": player_type, | |
| "level": level or random.randint(1, 10), | |
| "x": coords["x"], | |
| "y": coords["y"], | |
| "health": random.randint(80, 100), | |
| "max_health": 100, | |
| "experience": random.randint(0, 100), | |
| "gold": random.randint(0, 500), | |
| "session_hash": TestDataGenerator.random_string(32), | |
| "joined_at": datetime.now().isoformat(), | |
| **kwargs | |
| } | |
| return data | |
| def create_npc_data( | |
| npc_id: Optional[str] = None, | |
| name: Optional[str] = None, | |
| **kwargs | |
| ) -> Dict[str, Any]: | |
| """Create test NPC data.""" | |
| coords = TestDataGenerator.random_coordinates() | |
| npc_types = [ | |
| {"char": "🏪", "name": "Trader", "personality": "merchant"}, | |
| {"char": "🛡️", "name": "Guard", "personality": "protective"}, | |
| {"char": "🧙", "name": "Wizard", "personality": "mysterious"}, | |
| {"char": "📮", "name": "Mailbox", "personality": "neutral"} | |
| ] | |
| npc_type = random.choice(npc_types) | |
| data = { | |
| "id": npc_id or f"npc_{TestDataGenerator.random_string(8)}", | |
| "name": name or f"{npc_type['name']} {TestDataGenerator.random_string(4)}", | |
| "char": npc_type["char"], | |
| "x": coords["x"], | |
| "y": coords["y"], | |
| "personality": npc_type["personality"], | |
| "health": 100, | |
| "level": random.randint(5, 20), | |
| **kwargs | |
| } | |
| return data | |
| def create_chat_message( | |
| sender: Optional[str] = None, | |
| message_type: str = "public", | |
| **kwargs | |
| ) -> Dict[str, Any]: | |
| """Create test chat message data.""" | |
| messages = [ | |
| "Hello everyone!", | |
| "How is everyone doing?", | |
| "Nice weather today!", | |
| "Anyone want to trade?", | |
| "Looking for a party!", | |
| "This game is awesome!", | |
| "Where can I find the merchant?", | |
| "Thanks for the help!" | |
| ] | |
| data = { | |
| "sender": sender or TestDataGenerator.random_player_name(), | |
| "message": random.choice(messages), | |
| "type": message_type, | |
| "timestamp": datetime.now().isoformat(), | |
| **kwargs | |
| } | |
| if message_type == "private": | |
| data["recipient"] = TestDataGenerator.random_player_name() | |
| return data | |
| def create_plugin_data( | |
| plugin_id: Optional[str] = None, | |
| **kwargs | |
| ) -> Dict[str, Any]: | |
| """Create test plugin data.""" | |
| plugin_types = ["trading", "weather", "chat", "npc", "utility"] | |
| data = { | |
| "id": plugin_id or f"plugin_{TestDataGenerator.random_string(8)}", | |
| "name": f"Test {random.choice(plugin_types).title()} Plugin", | |
| "version": f"{random.randint(1, 3)}.{random.randint(0, 9)}.{random.randint(0, 9)}", | |
| "type": random.choice(plugin_types), | |
| "active": random.choice([True, False]), | |
| "dependencies": [], | |
| "config": {}, | |
| **kwargs | |
| } | |
| return data | |
| class TestScenarios: | |
| """Predefined test scenarios for complex testing.""" | |
| def multiplayer_scenario(player_count: int = 3) -> Dict[str, Any]: | |
| """Create a multiplayer test scenario.""" | |
| players = [] | |
| for i in range(player_count): | |
| player_data = TestDataGenerator.create_player_data( | |
| name=f"Player{i+1}", | |
| level=i+1 | |
| ) | |
| players.append(player_data) | |
| npcs = [] | |
| for i in range(2): | |
| npc_data = TestDataGenerator.create_npc_data() | |
| npcs.append(npc_data) | |
| chat_messages = [] | |
| for i in range(5): | |
| message = TestDataGenerator.create_chat_message( | |
| sender=random.choice(players)["name"] | |
| ) | |
| chat_messages.append(message) | |
| return { | |
| "players": players, | |
| "npcs": npcs, | |
| "chat_messages": chat_messages, | |
| "world_size": {"width": 800, "height": 600} | |
| } | |
| def private_chat_scenario() -> Dict[str, Any]: | |
| """Create a private chat test scenario.""" | |
| player1 = TestDataGenerator.create_player_data(name="Alice") | |
| player2 = TestDataGenerator.create_player_data(name="Bob") | |
| # Position players near each other | |
| player2["x"] = player1["x"] + 30 | |
| player2["y"] = player1["y"] + 20 | |
| private_messages = [ | |
| { | |
| "sender": "Alice", | |
| "recipient": "Bob", | |
| "message": "Hey, want to team up?", | |
| "type": "private", | |
| "timestamp": (datetime.now() - timedelta(minutes=2)).isoformat() | |
| }, | |
| { | |
| "sender": "Bob", | |
| "recipient": "Alice", | |
| "message": "Sure! Let's go to the forest.", | |
| "type": "private", | |
| "timestamp": (datetime.now() - timedelta(minutes=1)).isoformat() | |
| } | |
| ] | |
| return { | |
| "players": [player1, player2], | |
| "private_messages": private_messages, | |
| "proximity_range": 50 | |
| } | |
| def npc_interaction_scenario() -> Dict[str, Any]: | |
| """Create an NPC interaction test scenario.""" | |
| player = TestDataGenerator.create_player_data(name="TestHero") | |
| # Create Donald the Trader NPC | |
| donald_npc = { | |
| "id": "donald_trader", | |
| "name": "Donald the Trader", | |
| "char": "🏪", | |
| "x": player["x"] + 25, # Near the player | |
| "y": player["y"] + 15, | |
| "personality": "trump_like", | |
| "health": 100, | |
| "level": 10, | |
| "inventory": ["sword", "shield", "potion"], | |
| "responses": { | |
| "greeting": "Welcome to my tremendous store! The best deals, believe me!", | |
| "trade": "You want to make a deal? I make the best deals!", | |
| "goodbye": "Come back soon! Nobody has better stuff than me!" | |
| } | |
| } | |
| # Create interaction sequence | |
| interactions = [ | |
| {"player": "Hello", "expected_response": "greeting"}, | |
| {"player": "I want to trade", "expected_response": "trade"}, | |
| {"player": "Goodbye", "expected_response": "goodbye"} | |
| ] | |
| return { | |
| "player": player, | |
| "npc": donald_npc, | |
| "interactions": interactions, | |
| "interaction_range": 50 | |
| } | |
| def plugin_system_scenario() -> Dict[str, Any]: | |
| """Create a plugin system test scenario.""" | |
| plugins = [ | |
| TestDataGenerator.create_plugin_data( | |
| plugin_id="trading_system", | |
| name="Trading System", | |
| type="trading", | |
| active=True | |
| ), | |
| TestDataGenerator.create_plugin_data( | |
| plugin_id="weather_service", | |
| name="Weather Service", | |
| type="weather", | |
| active=True, | |
| dependencies=["http_client"] | |
| ), | |
| TestDataGenerator.create_plugin_data( | |
| plugin_id="chat_enhancer", | |
| name="Chat Enhancer", | |
| type="chat", | |
| active=False | |
| ) | |
| ] | |
| plugin_events = [ | |
| {"type": "load", "plugin_id": "trading_system", "timestamp": datetime.now().isoformat()}, | |
| {"type": "activate", "plugin_id": "trading_system", "timestamp": datetime.now().isoformat()}, | |
| {"type": "load", "plugin_id": "weather_service", "timestamp": datetime.now().isoformat()}, | |
| {"type": "error", "plugin_id": "chat_enhancer", "error": "Dependency not found", "timestamp": datetime.now().isoformat()} | |
| ] | |
| return { | |
| "plugins": plugins, | |
| "events": plugin_events, | |
| "expected_active": 2, | |
| "expected_errors": 1 | |
| } | |
| def performance_test_scenario() -> Dict[str, Any]: | |
| """Create a performance testing scenario.""" | |
| # Large number of players for load testing | |
| players = [] | |
| for i in range(20): | |
| player = TestDataGenerator.create_player_data( | |
| name=f"LoadTestPlayer{i+1}", | |
| player_type="ai_agent" if i % 3 == 0 else "human" | |
| ) | |
| players.append(player) | |
| # Many chat messages | |
| chat_messages = [] | |
| for i in range(100): | |
| message = TestDataGenerator.create_chat_message( | |
| sender=random.choice(players)["name"] | |
| ) | |
| chat_messages.append(message) | |
| # Multiple NPCs | |
| npcs = [] | |
| for i in range(10): | |
| npc = TestDataGenerator.create_npc_data() | |
| npcs.append(npc) | |
| return { | |
| "players": players, | |
| "chat_messages": chat_messages, | |
| "npcs": npcs, | |
| "expected_max_response_time": 200, # milliseconds | |
| "expected_max_memory": 512 # MB | |
| } | |
| # Predefined test data sets | |
| SAMPLE_PLAYERS = [ | |
| TestDataGenerator.create_player_data(name="Alice", level=5, x=100, y=100), | |
| TestDataGenerator.create_player_data(name="Bob", level=3, x=200, y=150), | |
| TestDataGenerator.create_player_data(name="Charlie", level=7, x=300, y=200, player_type="ai_agent") | |
| ] | |
| SAMPLE_NPCS = [ | |
| TestDataGenerator.create_npc_data(npc_id="merchant1", name="Friendly Trader", char="🏪"), | |
| TestDataGenerator.create_npc_data(npc_id="guard1", name="Town Guard", char="🛡️"), | |
| TestDataGenerator.create_npc_data(npc_id="wizard1", name="Mysterious Wizard", char="🧙") | |
| ] | |
| SAMPLE_CHAT_MESSAGES = [ | |
| TestDataGenerator.create_chat_message(sender="Alice", message="Hello everyone!"), | |
| TestDataGenerator.create_chat_message(sender="Bob", message="How is everyone?"), | |
| TestDataGenerator.create_chat_message(sender="Alice", message="Secret message", message_type="private", recipient="Bob") | |
| ] | |
| SAMPLE_PLUGINS = [ | |
| TestDataGenerator.create_plugin_data(plugin_id="core_trading", name="Core Trading System", active=True), | |
| TestDataGenerator.create_plugin_data(plugin_id="weather_api", name="Weather API", active=True), | |
| TestDataGenerator.create_plugin_data(plugin_id="test_plugin", name="Test Plugin", active=False) | |
| ] | |
| class TestingConstants: | |
| """Constants used in testing.""" | |
| # World dimensions | |
| WORLD_WIDTH = 800 | |
| WORLD_HEIGHT = 600 | |
| # Player limits | |
| MAX_PLAYERS = 20 | |
| MIN_HEALTH = 0 | |
| MAX_HEALTH = 100 | |
| # Performance thresholds | |
| MAX_RESPONSE_TIME_MS = 200 | |
| MAX_MEMORY_MB = 512 | |
| MAX_CPU_PERCENT = 50 | |
| # Proximity ranges | |
| INTERACTION_RANGE = 50 | |
| CHAT_RANGE = 100 | |
| # Test timeouts | |
| DEFAULT_TIMEOUT = 5.0 | |
| LONG_TIMEOUT = 30.0 | |
| SHORT_TIMEOUT = 1.0 | |
| # Export commonly used items | |
| __all__ = [ | |
| 'TestDataGenerator', | |
| 'TestScenarios', 'SAMPLE_PLAYERS', | |
| 'SAMPLE_NPCS', | |
| 'SAMPLE_CHAT_MESSAGES', | |
| 'SAMPLE_PLUGINS', | |
| 'TestingConstants' | |
| ] | |