247 lines
8.1 KiB
Python
247 lines
8.1 KiB
Python
"""Unit tests for configuration repositories."""
|
|
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
|
|
from arbitrade.storage.repositories import (
|
|
ConfigSettingRepository,
|
|
ConfigPairingRepository,
|
|
ConfigPairFeeRepository,
|
|
ConfigBacktestingDefaultsRepository
|
|
)
|
|
from arbitrade.config.service import ConfigSetting, ConfigPairing, ConfigPairFee, ConfigBacktestingDefaults
|
|
from arbitrade.storage.db import DuckDBStore
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_store():
|
|
"""Create a mock database store."""
|
|
store = Mock(spec=DuckDBStore)
|
|
return store
|
|
|
|
|
|
def test_config_setting_repository_initialization(mock_store):
|
|
"""Test ConfigSettingRepository initialization."""
|
|
repo = ConfigSettingRepository(mock_store)
|
|
assert repo._store == mock_store
|
|
|
|
|
|
def test_config_setting_repository_create_setting(mock_store):
|
|
"""Test creating a configuration setting."""
|
|
repo = ConfigSettingRepository(mock_store)
|
|
|
|
# Mock database connection
|
|
with patch.object(mock_store, 'connect') as mock_connect:
|
|
mock_cursor = Mock()
|
|
mock_connect.return_value.__enter__.return_value = mock_cursor
|
|
|
|
# Mock the return value
|
|
mock_cursor.fetchone.return_value = [
|
|
"test_key", "test_section", "test_value", "str", False, False, "2023-01-01T00:00:00", "test_user"
|
|
]
|
|
|
|
# Create setting
|
|
setting = ConfigSetting(
|
|
key="test_key",
|
|
section="test_section",
|
|
value_json="test_value",
|
|
value_type="str",
|
|
is_secret=False,
|
|
is_runtime_reloadable=False,
|
|
updated_by="test_user"
|
|
)
|
|
|
|
result = repo.create_setting(setting)
|
|
|
|
# Verify database call
|
|
mock_cursor.execute.assert_called_once()
|
|
assert result.key == "test_key"
|
|
assert result.section == "test_section"
|
|
assert result.value_json == "test_value"
|
|
assert result.value_type == "str"
|
|
assert result.updated_by == "test_user"
|
|
|
|
|
|
def test_config_setting_repository_get_setting(mock_store):
|
|
"""Test getting a configuration setting."""
|
|
repo = ConfigSettingRepository(mock_store)
|
|
|
|
# Mock database connection
|
|
with patch.object(mock_store, 'connect') as mock_connect:
|
|
mock_cursor = Mock()
|
|
mock_connect.return_value.__enter__.return_value = mock_cursor
|
|
|
|
# Mock the return value
|
|
mock_cursor.fetchone.return_value = [
|
|
"test_key", "test_section", "test_value", "str", False, False, "2023-01-01T00:00:00", "test_user"
|
|
]
|
|
|
|
# Get setting
|
|
result = repo.get_setting("test_key")
|
|
|
|
# Verify database call
|
|
mock_cursor.execute.assert_called_once()
|
|
assert result.key == "test_key"
|
|
assert result.section == "test_section"
|
|
assert result.value_json == "test_value"
|
|
assert result.value_type == "str"
|
|
assert result.updated_by == "test_user"
|
|
|
|
|
|
def test_config_setting_repository_update_setting(mock_store):
|
|
"""Test updating a configuration setting."""
|
|
repo = ConfigSettingRepository(mock_store)
|
|
|
|
# Mock database connection
|
|
with patch.object(mock_store, 'connect') as mock_connect:
|
|
mock_cursor = Mock()
|
|
mock_connect.return_value.__enter__.return_value = mock_cursor
|
|
|
|
# Mock the return value
|
|
mock_cursor.fetchone.return_value = [
|
|
"test_key", "test_section", "updated_value", "str", False, False, "2023-01-01T00:00:00", "test_user"
|
|
]
|
|
|
|
# Update setting
|
|
setting = ConfigSetting(
|
|
key="test_key",
|
|
section="test_section",
|
|
value_json="updated_value",
|
|
value_type="str",
|
|
is_secret=False,
|
|
is_runtime_reloadable=False,
|
|
updated_by="test_user"
|
|
)
|
|
|
|
result = repo.update_setting("test_key", setting)
|
|
|
|
# Verify database call
|
|
mock_cursor.execute.assert_called_once()
|
|
assert result.key == "test_key"
|
|
assert result.section == "test_section"
|
|
assert result.value_json == "updated_value"
|
|
assert result.value_type == "str"
|
|
assert result.updated_by == "test_user"
|
|
|
|
|
|
def test_config_setting_repository_list_settings(mock_store):
|
|
"""Test listing configuration settings."""
|
|
repo = ConfigSettingRepository(mock_store)
|
|
|
|
# Mock database connection
|
|
with patch.object(mock_store, 'connect') as mock_connect:
|
|
mock_cursor = Mock()
|
|
mock_connect.return_value.__enter__.return_value = mock_cursor
|
|
|
|
# Mock the return value
|
|
mock_cursor.fetchall.return_value = [
|
|
["test_key1", "test_section", "test_value1", "str",
|
|
False, False, "2023-01-01T00:00:00", "test_user"],
|
|
["test_key2", "test_section", "test_value2", "str",
|
|
False, False, "2023-01-01T00:00:00", "test_user"]
|
|
]
|
|
|
|
# List settings
|
|
result = repo.list_settings()
|
|
|
|
# Verify database call
|
|
mock_cursor.execute.assert_called_once()
|
|
assert len(result) == 2
|
|
assert result[0].key == "test_key1"
|
|
assert result[1].key == "test_key2"
|
|
|
|
|
|
def test_config_setting_repository_get_latest_updated_at(mock_store):
|
|
"""Test getting latest updated timestamp."""
|
|
repo = ConfigSettingRepository(mock_store)
|
|
|
|
# Mock database connection
|
|
with patch.object(mock_store, 'connect') as mock_connect:
|
|
mock_cursor = Mock()
|
|
mock_connect.return_value.__enter__.return_value = mock_cursor
|
|
|
|
# Mock the return value
|
|
mock_cursor.fetchone.return_value = ["2023-01-01T00:00:00"]
|
|
|
|
# Get latest updated at
|
|
result = repo.get_latest_updated_at()
|
|
|
|
# Verify database call
|
|
mock_cursor.execute.assert_called_once()
|
|
assert result is not None
|
|
|
|
|
|
def test_config_pairing_repository_initialization(mock_store):
|
|
"""Test ConfigPairingRepository initialization."""
|
|
repo = ConfigPairingRepository(mock_store)
|
|
assert repo._store == mock_store
|
|
|
|
|
|
def test_config_pairing_repository_create_pairing(mock_store):
|
|
"""Test creating a currency pairing."""
|
|
repo = ConfigPairingRepository(mock_store)
|
|
|
|
# Mock database connection
|
|
with patch.object(mock_store, 'connect') as mock_connect:
|
|
mock_cursor = Mock()
|
|
mock_connect.return_value.__enter__.return_value = mock_cursor
|
|
|
|
# Mock the return value
|
|
mock_cursor.fetchone.return_value = [
|
|
1, "BTC", "USD", True, "Kraken", "2023-01-01T00:00:00", "2023-01-01T00:00:00"
|
|
]
|
|
|
|
# Create pairing
|
|
pairing = ConfigPairing(
|
|
base_asset="BTC",
|
|
quote_asset="USD",
|
|
enabled=True,
|
|
source="Kraken"
|
|
)
|
|
|
|
result = repo.create_pairing(pairing)
|
|
|
|
# Verify database call
|
|
mock_cursor.execute.assert_called_once()
|
|
assert result.base_asset == "BTC"
|
|
assert result.quote_asset == "USD"
|
|
assert result.enabled is True
|
|
assert result.source == "Kraken"
|
|
|
|
|
|
def test_config_pairing_repository_get_pairing(mock_store):
|
|
"""Test getting a currency pairing."""
|
|
repo = ConfigPairingRepository(mock_store)
|
|
|
|
# Mock database connection
|
|
with patch.object(mock_store, 'connect') as mock_connect:
|
|
mock_cursor = Mock()
|
|
mock_connect.return_value.__enter__.return_value = mock_cursor
|
|
|
|
# Mock the return value
|
|
mock_cursor.fetchone.return_value = [
|
|
1, "BTC", "USD", True, "Kraken", "2023-01-01T00:00:00", "2023-01-01T00:00:00"
|
|
]
|
|
|
|
# Get pairing
|
|
result = repo.get_pairing("BTC", "USD")
|
|
|
|
# Verify database call
|
|
mock_cursor.execute.assert_called_once()
|
|
assert result.base_asset == "BTC"
|
|
assert result.quote_asset == "USD"
|
|
assert result.enabled is True
|
|
assert result.source == "Kraken"
|
|
|
|
|
|
def test_config_pair_fee_repository_initialization(mock_store):
|
|
"""Test ConfigPairFeeRepository initialization."""
|
|
repo = ConfigPairFeeRepository(mock_store)
|
|
assert repo._store == mock_store
|
|
|
|
|
|
def test_config_backtesting_defaults_repository_initialization(mock_store):
|
|
"""Test ConfigBacktestingDefaultsRepository initialization."""
|
|
repo = ConfigBacktestingDefaultsRepository(mock_store)
|
|
assert repo._store == mock_store
|