"""Tests for DataManager functionality."""
import pytest

from engine.data_manager import DataManager
from engine.market import MarketType


class TestDataManager:
    """Test suite for DataManager class."""
    
    def test_init_creates_data_dir(self, tmp_path):
        """Test that DataManager creates data directory on init."""
        data_dir = tmp_path / "test_data"
        dm = DataManager(data_dir=str(data_dir))
        assert data_dir.exists()
    
    def test_get_data_path_spot(self, tmp_path):
        """Test data path generation for spot market."""
        dm = DataManager(data_dir=str(tmp_path))
        path = dm._get_data_path("okx", "BTC/USDT", "1m", MarketType.SPOT)
        
        expected = tmp_path / "okx" / "spot" / "BTC-USDT" / "1m.csv"
        assert path == expected
    
    def test_get_data_path_perpetual(self, tmp_path):
        """Test data path generation for perpetual market."""
        dm = DataManager(data_dir=str(tmp_path))
        path = dm._get_data_path("okx", "BTC/USDT", "1h", MarketType.PERPETUAL)
        
        expected = tmp_path / "okx" / "perpetual" / "BTC-USDT" / "1h.csv"
        assert path == expected
    
    def test_load_data_file_not_found(self, tmp_path):
        """Test that load_data raises FileNotFoundError for missing data."""
        dm = DataManager(data_dir=str(tmp_path))
        
        with pytest.raises(FileNotFoundError):
            dm.load_data("okx", "BTC/USDT", "1m", MarketType.SPOT)


@pytest.mark.network
class TestDataManagerDownload:
    """Tests requiring network access (marked for selective running)."""
    
    def test_download_spot_data(self):
        """Test downloading spot market data."""
        dm = DataManager()
        df = dm.download_data(
            "okx", "BTC/USDT", "1m", 
            start_date="2025-01-01", 
            market_type=MarketType.SPOT
        )
        
        assert df is not None
        assert 'open' in df.columns
        assert 'close' in df.columns
    
    def test_download_perpetual_data(self):
        """Test downloading perpetual market data."""
        dm = DataManager()
        df = dm.download_data(
            "okx", "BTC/USDT", "1m", 
            start_date="2025-01-01", 
            market_type=MarketType.PERPETUAL
        )
        
        assert df is not None
        assert 'open' in df.columns
        assert 'close' in df.columns
