refactor: update tests to use async mocks and improve readability
CI / lint-test-build (push) Failing after 12s
CI / lint-test-build (push) Failing after 12s
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Unit tests for configuration repositories."""
|
||||
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -8,7 +8,6 @@ from arbitrade.config.service import (
|
||||
ConfigPairing,
|
||||
ConfigSetting,
|
||||
)
|
||||
from arbitrade.storage.pg_store import PgStore
|
||||
from arbitrade.storage.repositories import (
|
||||
ConfigBacktestingDefaultsRepository,
|
||||
ConfigPairingRepository,
|
||||
@@ -18,202 +17,150 @@ from arbitrade.storage.repositories import (
|
||||
|
||||
@pytest.fixture
|
||||
def mock_store():
|
||||
"""Create a mock database store."""
|
||||
store = Mock(spec=PgStore)
|
||||
"""Create a mock database store with async pool."""
|
||||
store = MagicMock()
|
||||
conn = AsyncMock()
|
||||
conn.fetchone = AsyncMock(return_value=None)
|
||||
conn.fetchall = AsyncMock(return_value=[])
|
||||
conn.fetch = AsyncMock(return_value=[])
|
||||
conn.execute = AsyncMock(return_value=conn)
|
||||
store.pool = MagicMock()
|
||||
cm = AsyncMock()
|
||||
cm.__aenter__.return_value = conn
|
||||
store.pool.acquire.return_value = cm
|
||||
return store
|
||||
|
||||
|
||||
def _make_row(mapping: dict):
|
||||
row = MagicMock()
|
||||
row.__getitem__.side_effect = lambda k: mapping[k]
|
||||
return row
|
||||
|
||||
|
||||
SETTING_ROW = {
|
||||
"key": "test_key",
|
||||
"section": "test_section",
|
||||
"value_json": "test_value",
|
||||
"value_type": "str",
|
||||
"is_secret": False,
|
||||
"is_runtime_reloadable": False,
|
||||
"updated_at": "2023-01-01T00:00:00",
|
||||
"updated_by": "test_user",
|
||||
}
|
||||
|
||||
PAIRING_ROW = {
|
||||
"id": 1,
|
||||
"base_asset": "BTC",
|
||||
"quote_asset": "USD",
|
||||
"enabled": True,
|
||||
"source": "Kraken",
|
||||
"created_at": "2023-01-01T00:00:00",
|
||||
"updated_at": "2023-01-01T00:00:00",
|
||||
}
|
||||
|
||||
|
||||
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):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_setting_repository_create_setting(mock_store):
|
||||
"""Test creating a configuration setting."""
|
||||
repo = ConfigSettingRepository(mock_store)
|
||||
conn = await mock_store.pool.acquire().__aenter__()
|
||||
conn.fetchrow = AsyncMock(return_value=_make_row(SETTING_ROW))
|
||||
|
||||
# Mock database connection
|
||||
with patch.object(mock_store, "connect") as mock_connect:
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.execute.return_value = mock_cursor
|
||||
mock_connect.return_value.__enter__.return_value = mock_cursor
|
||||
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",
|
||||
)
|
||||
|
||||
# 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",
|
||||
]
|
||||
result = await repo.create_setting(setting)
|
||||
|
||||
# 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"
|
||||
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):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_setting_repository_get_setting(mock_store):
|
||||
"""Test getting a configuration setting."""
|
||||
repo = ConfigSettingRepository(mock_store)
|
||||
conn = await mock_store.pool.acquire().__aenter__()
|
||||
conn.fetchrow = AsyncMock(return_value=_make_row(SETTING_ROW))
|
||||
|
||||
# Mock database connection
|
||||
with patch.object(mock_store, "connect") as mock_connect:
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.execute.return_value = mock_cursor
|
||||
mock_connect.return_value.__enter__.return_value = mock_cursor
|
||||
result = await repo.get_setting("test_key")
|
||||
|
||||
# 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"
|
||||
assert result is not None
|
||||
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):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_setting_repository_update_setting(mock_store):
|
||||
"""Test updating a configuration setting."""
|
||||
repo = ConfigSettingRepository(mock_store)
|
||||
conn = await mock_store.pool.acquire().__aenter__()
|
||||
conn.fetchrow = AsyncMock(return_value=_make_row(SETTING_ROW))
|
||||
|
||||
# Mock database connection
|
||||
with patch.object(mock_store, "connect") as mock_connect:
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.execute.return_value = mock_cursor
|
||||
mock_connect.return_value.__enter__.return_value = mock_cursor
|
||||
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",
|
||||
)
|
||||
|
||||
# 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",
|
||||
]
|
||||
result = await repo.update_setting("test_key", setting)
|
||||
|
||||
# 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"
|
||||
assert result.key == "test_key"
|
||||
|
||||
|
||||
def test_config_setting_repository_list_settings(mock_store):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_setting_repository_list_settings(mock_store):
|
||||
"""Test listing configuration settings."""
|
||||
repo = ConfigSettingRepository(mock_store)
|
||||
conn = await mock_store.pool.acquire().__aenter__()
|
||||
|
||||
# Mock database connection
|
||||
with patch.object(mock_store, "connect") as mock_connect:
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.execute.return_value = mock_cursor
|
||||
mock_connect.return_value.__enter__.return_value = mock_cursor
|
||||
row1 = _make_row({**SETTING_ROW, "key": "test_key1",
|
||||
"value_json": "test_value1"})
|
||||
row2 = _make_row({**SETTING_ROW, "key": "test_key2",
|
||||
"value_json": "test_value2"})
|
||||
conn.fetch = AsyncMock(return_value=[row1, row2])
|
||||
|
||||
# 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",
|
||||
],
|
||||
]
|
||||
result = await repo.list_settings()
|
||||
|
||||
# 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"
|
||||
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):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_setting_repository_get_latest_updated_at(mock_store):
|
||||
"""Test getting latest updated timestamp."""
|
||||
repo = ConfigSettingRepository(mock_store)
|
||||
conn = await mock_store.pool.acquire().__aenter__()
|
||||
|
||||
# Mock database connection
|
||||
with patch.object(mock_store, "connect") as mock_connect:
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.execute.return_value = mock_cursor
|
||||
mock_connect.return_value.__enter__.return_value = mock_cursor
|
||||
row = _make_row({"latest_updated_at": "2023-01-01T00:00:00"})
|
||||
conn.fetchrow = AsyncMock(return_value=row)
|
||||
|
||||
# Mock the return value
|
||||
mock_cursor.fetchone.return_value = ["2023-01-01T00:00:00"]
|
||||
result = await repo.get_latest_updated_at()
|
||||
|
||||
# Get latest updated at
|
||||
result = repo.get_latest_updated_at()
|
||||
|
||||
# Verify database call
|
||||
mock_cursor.execute.assert_called_once()
|
||||
assert result is not None
|
||||
assert result is not None
|
||||
|
||||
|
||||
def test_config_pairing_repository_initialization(mock_store):
|
||||
@@ -222,71 +169,38 @@ def test_config_pairing_repository_initialization(mock_store):
|
||||
assert repo._store == mock_store
|
||||
|
||||
|
||||
def test_config_pairing_repository_create_pairing(mock_store):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_pairing_repository_create_pairing(mock_store):
|
||||
"""Test creating a currency pairing."""
|
||||
repo = ConfigPairingRepository(mock_store)
|
||||
conn = await mock_store.pool.acquire().__aenter__()
|
||||
conn.fetchrow = AsyncMock(return_value=_make_row(PAIRING_ROW))
|
||||
|
||||
# Mock database connection
|
||||
with patch.object(mock_store, "connect") as mock_connect:
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.execute.return_value = mock_cursor
|
||||
mock_connect.return_value.__enter__.return_value = mock_cursor
|
||||
pairing = ConfigPairing(
|
||||
base_asset="BTC", quote_asset="USD", enabled=True, source="Kraken"
|
||||
)
|
||||
|
||||
# Mock the return value
|
||||
mock_cursor.fetchone.return_value = [
|
||||
1,
|
||||
"BTC",
|
||||
"USD",
|
||||
True,
|
||||
"Kraken",
|
||||
"2023-01-01T00:00:00",
|
||||
"2023-01-01T00:00:00",
|
||||
]
|
||||
result = await repo.create_pairing(pairing)
|
||||
|
||||
# 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"
|
||||
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):
|
||||
@pytest.mark.asyncio
|
||||
async def test_config_pairing_repository_get_pairing(mock_store):
|
||||
"""Test getting a currency pairing."""
|
||||
repo = ConfigPairingRepository(mock_store)
|
||||
conn = await mock_store.pool.acquire().__aenter__()
|
||||
conn.fetchrow = AsyncMock(return_value=_make_row(PAIRING_ROW))
|
||||
|
||||
# Mock database connection
|
||||
with patch.object(mock_store, "connect") as mock_connect:
|
||||
mock_cursor = Mock()
|
||||
mock_cursor.execute.return_value = mock_cursor
|
||||
mock_connect.return_value.__enter__.return_value = mock_cursor
|
||||
result = await repo.get_pairing("BTC", "USD")
|
||||
|
||||
# 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"
|
||||
assert result.base_asset == "BTC"
|
||||
assert result.quote_asset == "USD"
|
||||
assert result.enabled is True
|
||||
assert result.source == "Kraken"
|
||||
|
||||
|
||||
def test_config_backtesting_defaults_repository_initialization(mock_store):
|
||||
|
||||
Reference in New Issue
Block a user