Some checks failed
Build Simulation and Test / Run All Tests (push) Failing after 1m46s
195 lines
5.5 KiB
Python
195 lines
5.5 KiB
Python
"""Tests for output collection system."""
|
|
|
|
import json
|
|
import tempfile
|
|
import os
|
|
from unittest.mock import Mock
|
|
|
|
from output.collectors.metrics_collector import MetricsCollector
|
|
from output.collectors.entity_collector import EntityCollector
|
|
from output.formatters.json_formatter import JSONFormatter
|
|
from output.formatters.csv_formatter import CSVFormatter
|
|
from output.writers.file_writer import FileWriter
|
|
|
|
|
|
class TestMetricsCollector:
|
|
"""Test metrics collector functionality."""
|
|
|
|
def test_collect_data(self):
|
|
"""Test metrics data collection."""
|
|
collector = MetricsCollector(collection_interval=100)
|
|
|
|
# Create mock simulation core
|
|
mock_sim_core = Mock()
|
|
mock_sim_core.get_world_state.return_value = {
|
|
'tick_count': 1500,
|
|
'actual_tps': 58.5,
|
|
'target_tps': 60.0,
|
|
'speed_multiplier': 1.0,
|
|
'is_paused': False,
|
|
'sprint_mode': False,
|
|
'world_buffer': 1,
|
|
'entity_counts': {
|
|
'total': 25,
|
|
'cells': 20,
|
|
'food': 5
|
|
}
|
|
}
|
|
|
|
# Mock timing
|
|
mock_sim_core.timing = Mock()
|
|
mock_sim_core.timing.last_tick_time = 1234567890.5
|
|
|
|
data = collector.collect(mock_sim_core)
|
|
|
|
assert data['tick_count'] == 1500
|
|
assert data['actual_tps'] == 58.5
|
|
assert data['target_tps'] == 60.0
|
|
assert data['speed_multiplier'] == 1.0
|
|
assert data['is_paused'] == False
|
|
assert data['sprint_mode'] == False
|
|
assert data['world_buffer'] == 1
|
|
assert data['entity_counts'] == {
|
|
'total': 25,
|
|
'cells': 20,
|
|
'food': 5
|
|
}
|
|
assert data['collection_type'] == 'metrics'
|
|
assert data['timestamp'] == 1234567890.5
|
|
|
|
|
|
class TestEntityCollector:
|
|
"""Test entity collector functionality."""
|
|
|
|
def test_collect_cells_only(self):
|
|
"""Test collecting only cell entities."""
|
|
collector = EntityCollector(
|
|
collection_interval=1000,
|
|
include_cells=True,
|
|
include_food=False
|
|
)
|
|
|
|
# Create mock simulation core with entity states
|
|
mock_sim_core = Mock()
|
|
mock_sim_core.get_world_state.return_value = {
|
|
'tick_count': 1000
|
|
}
|
|
mock_sim_core.get_entity_states.return_value = [
|
|
{
|
|
'id': 1,
|
|
'type': 'cell',
|
|
'position': {'x': 10, 'y': 20},
|
|
'energy': 75.5,
|
|
'age': 150,
|
|
'generation': 3,
|
|
'neural_network': {'layer_sizes': [4, 6, 2]}
|
|
},
|
|
{
|
|
'id': 2,
|
|
'type': 'food',
|
|
'position': {'x': 30, 'y': 40},
|
|
'decay': 50,
|
|
'max_decay': 100
|
|
}
|
|
]
|
|
|
|
data = collector.collect(mock_sim_core)
|
|
|
|
assert len(data['entities']) == 1 # Only cell included
|
|
assert data['entities'][0]['type'] == 'cell'
|
|
assert data['entities'][0]['id'] == 1
|
|
assert data['collection_type'] == 'entities'
|
|
|
|
|
|
|
|
class TestJSONFormatter:
|
|
"""Test JSON formatter functionality."""
|
|
|
|
def test_format_data(self):
|
|
"""Test JSON data formatting."""
|
|
formatter = JSONFormatter()
|
|
|
|
test_data = {
|
|
'tick_count': 1000,
|
|
'actual_tps': 58.5,
|
|
'entity_counts': {
|
|
'cells': 20,
|
|
'food': 5
|
|
}
|
|
}
|
|
|
|
formatted = formatter.format(test_data)
|
|
|
|
assert isinstance(formatted, str)
|
|
|
|
# Verify it's valid JSON
|
|
parsed = json.loads(formatted)
|
|
assert parsed['tick_count'] == 1000
|
|
assert parsed['actual_tps'] == 58.5
|
|
assert parsed['entity_counts']['cells'] == 20
|
|
|
|
|
|
class TestCSVFormatter:
|
|
"""Test CSV formatter functionality."""
|
|
|
|
def test_format_simple_data(self):
|
|
"""Test CSV formatting for simple data."""
|
|
formatter = CSVFormatter()
|
|
|
|
test_data = {
|
|
'tick_count': 1000,
|
|
'actual_tps': 58.5,
|
|
'is_paused': False
|
|
}
|
|
|
|
formatted = formatter.format(test_data)
|
|
|
|
assert isinstance(formatted, str)
|
|
lines = formatted.strip().split('\n')
|
|
|
|
# Should have header and one data row
|
|
assert len(lines) == 2
|
|
|
|
# Check header
|
|
header = lines[0]
|
|
assert 'tick_count' in header
|
|
assert 'actual_tps' in header
|
|
assert 'is_paused' in header
|
|
|
|
# Check data row
|
|
data_row = lines[1]
|
|
assert '1000' in data_row
|
|
assert '58.5' in data_row
|
|
|
|
def test_get_file_extension(self):
|
|
"""Test file extension."""
|
|
formatter = CSVFormatter()
|
|
assert formatter.get_file_extension() == 'csv'
|
|
|
|
|
|
class TestFileWriter:
|
|
"""Test file writer functionality."""
|
|
|
|
def test_write_data(self):
|
|
"""Test writing data to file."""
|
|
# Create temporary directory
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
writer = FileWriter(temp_dir)
|
|
|
|
# Should be ready now
|
|
assert writer.is_ready() == True
|
|
|
|
# Write test data
|
|
test_data = '{"tick_count": 1000, "actual_tps": 58.5}'
|
|
filename = "test_data.json"
|
|
|
|
writer.write(test_data, filename)
|
|
|
|
# Verify file was created
|
|
file_path = os.path.join(temp_dir, filename)
|
|
assert os.path.exists(file_path)
|
|
|
|
# Verify file content
|
|
with open(file_path, 'r') as f:
|
|
content = f.read()
|
|
assert content == test_data |