Refactor code for improved readability and consistency
CI / lint-test-build (push) Successful in 54s

- Cleaned up multiline statements and removed unnecessary line breaks in various files.
- Ensured consistent formatting in function definitions and calls across the codebase.
- Updated docstrings and comments for clarity where applicable.
- Removed trailing newlines in module docstrings.
- Enhanced logging statements for better clarity in maintenance tasks.
This commit is contained in:
2026-06-07 21:59:09 +02:00
parent f221464daa
commit dc99f1604e
25 changed files with 409 additions and 324 deletions
+1 -1
View File
@@ -1 +1 @@
"""Integration tests for PostgreSQL schema and connectivity."""
"""Integration tests for PostgreSQL schema and connectivity."""
+2 -6
View File
@@ -11,13 +11,9 @@ import pathlib
import pytest
def pytest_ignore_collect(
collection_path: pathlib.Path, config: pytest.Config
) -> bool:
def pytest_ignore_collect(collection_path: pathlib.Path, config: pytest.Config) -> bool:
"""Skip integration tests unless --integration is passed."""
if "integration" in str(collection_path) and not config.getoption(
"--integration", False
):
if "integration" in str(collection_path) and not config.getoption("--integration", False):
return True
return False
+60 -10
View File
@@ -42,9 +42,24 @@ async def test_metrics_calculator_summarizes_execution_data() -> None:
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9),
($10, $11, $12, $13, $14, $15, $16, $17, $18)
""",
"trade-1", started, finished, "filled", 12.5, 10.0, 100.0, "USD->BTC->ETH->USD", 3,
"trade-2", started_two, finished_two, "filled", -
4.5, -2.0, 200.0, "USD->ETH->BTC->USD", 3,
"trade-1",
started,
finished,
"filled",
12.5,
10.0,
100.0,
"USD->BTC->ETH->USD",
3,
"trade-2",
started_two,
finished_two,
"filled",
-4.5,
-2.0,
200.0,
"USD->ETH->BTC->USD",
3,
)
await conn.execute(
"""
@@ -53,11 +68,24 @@ async def test_metrics_calculator_summarizes_execution_data() -> None:
($7, $8, $9, $10, $11, $12),
($13, $14, $15, $16, $17, $18)
""",
started, "USD->BTC->ETH->USD", 4.0, 3.0, 0.03, True,
started_two, "USD->ETH->BTC->USD", 2.0, 1.0, 0.01, False,
started_two +
timedelta(
seconds=30), "USD->BTC->ETH->USD", 5.0, 4.0, 0.04, True,
started,
"USD->BTC->ETH->USD",
4.0,
3.0,
0.03,
True,
started_two,
"USD->ETH->BTC->USD",
2.0,
1.0,
0.01,
False,
started_two + timedelta(seconds=30),
"USD->BTC->ETH->USD",
5.0,
4.0,
0.04,
True,
)
await conn.execute(
"""
@@ -67,8 +95,30 @@ async def test_metrics_calculator_summarizes_execution_data() -> None:
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12),
($13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24)
""",
"trade-1", "order-1", 0, "BTC/USD", "buy", 2.0, 101, "closed", 2.0, 100.0, "{}", started,
"trade-2", "order-2", 0, "ETH/USD", "sell", 4.0, 202, "closed", 3.0, 200.0, "{}", started_two,
"trade-1",
"order-1",
0,
"BTC/USD",
"buy",
2.0,
101,
"closed",
2.0,
100.0,
"{}",
started,
"trade-2",
"order-2",
0,
"ETH/USD",
"sell",
4.0,
202,
"closed",
3.0,
200.0,
"{}",
started_two,
)
metrics = await MetricsCalculator(store).compute()
+99 -35
View File
@@ -25,50 +25,116 @@ EXPECTED_TABLES: dict[str, list[str]] = {
"schema_migrations": ["version", "applied_at"],
"config_sections": ["id", "name", "description", "updated_at"],
"config_settings": [
"key", "section", "value_json", "value_type", "is_secret",
"is_runtime_reloadable", "updated_at", "updated_by",
"key",
"section",
"value_json",
"value_type",
"is_secret",
"is_runtime_reloadable",
"updated_at",
"updated_by",
],
"config_pairings": [
"id", "base_asset", "quote_asset", "enabled", "source",
"created_at", "updated_at",
"id",
"base_asset",
"quote_asset",
"enabled",
"source",
"created_at",
"updated_at",
],
"config_backtesting_defaults": [
"id", "starting_balances", "trade_capital", "min_profit_threshold",
"slippage_bps", "execution_latency_ms", "fee_source",
"id",
"starting_balances",
"trade_capital",
"min_profit_threshold",
"slippage_bps",
"execution_latency_ms",
"fee_source",
],
"opportunities": [
"id", "detected_at", "cycle", "gross_pct", "net_pct",
"est_profit", "executed",
"id",
"detected_at",
"cycle",
"gross_pct",
"net_pct",
"est_profit",
"executed",
],
"trades": [
"id", "trade_ref", "started_at", "finished_at", "status",
"realized_pnl", "estimated_pnl", "capital_used", "cycle", "leg_count",
"id",
"trade_ref",
"started_at",
"finished_at",
"status",
"realized_pnl",
"estimated_pnl",
"capital_used",
"cycle",
"leg_count",
],
"orders": [
"id", "trade_ref", "order_ref", "leg_index", "pair", "side",
"volume", "user_ref", "status", "filled_volume", "avg_price",
"raw_response", "recorded_at",
"id",
"trade_ref",
"order_ref",
"leg_index",
"pair",
"side",
"volume",
"user_ref",
"status",
"filled_volume",
"avg_price",
"raw_response",
"recorded_at",
],
"pnl_events": [
"id", "trade_ref", "recorded_at", "kind", "pnl_usd", "source",
"id",
"trade_ref",
"recorded_at",
"kind",
"pnl_usd",
"source",
],
"portfolio_snapshots": ["snapshot_at", "balances", "total_value_usd"],
"market_snapshots": ["snapshot_at", "symbol", "source", "payload", "latency_ms"],
"audit_events": [
"id", "occurred_at", "actor", "event_type", "decision",
"payload", "correlation_id",
"id",
"occurred_at",
"actor",
"event_type",
"decision",
"payload",
"correlation_id",
],
"runtime_state_snapshots": [
"snapshot_at", "is_running", "kill_switch_active", "kill_switch_reason",
"open_trade_count", "last_known_balances", "note",
"snapshot_at",
"is_running",
"kill_switch_active",
"kill_switch_reason",
"open_trade_count",
"last_known_balances",
"note",
],
"kraken_account_snapshots": [
"snapshot_at", "fee_tier", "maker_fee", "taker_fee",
"thirty_day_volume", "trade_balance_raw", "fee_schedule_raw",
"snapshot_at",
"fee_tier",
"maker_fee",
"taker_fee",
"thirty_day_volume",
"trade_balance_raw",
"fee_schedule_raw",
],
"backtest_jobs": [
"id", "status", "events_path", "config", "report", "error",
"created_at", "started_at", "finished_at",
"id",
"status",
"events_path",
"config",
"report",
"error",
"created_at",
"started_at",
"finished_at",
],
}
@@ -96,6 +162,7 @@ TABLES_WITH_UNIQUE_CONSTRAINTS: dict[str, list[str]] = {
# ── fixtures ────────────────────────────────────────────────────────────────
@asynccontextmanager
async def _pg_lifecycle() -> AsyncIterator[PgStore]:
"""Connect, yield store, then disconnect."""
@@ -116,6 +183,7 @@ async def pg_fixture() -> AsyncIterator[PgStore]:
# ── helpers ─────────────────────────────────────────────────────────────────
async def _get_actual_tables(store: PgStore) -> dict[str, list[str]]:
"""Return {table_name: [column_name, ...]} for the public schema."""
actual: dict[str, list[str]] = {}
@@ -139,6 +207,7 @@ async def _table_row_count(store: PgStore, table: str) -> int:
# ── tests ───────────────────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_pg_connect(pg: PgStore) -> None:
"""Can connect to PostgreSQL and ping the server."""
@@ -165,8 +234,7 @@ async def test_schema_migration_applies(pg: PgStore) -> None:
for table in EXPECTED_TABLES:
assert table in actual, (
f"Table '{table}' missing after migration. "
f"Found tables: {sorted(actual)}"
f"Table '{table}' missing after migration. " f"Found tables: {sorted(actual)}"
)
@@ -190,8 +258,7 @@ async def test_table_columns(pg: PgStore) -> None:
actual_cols = actual.get(table, [])
for col in expected_cols:
assert col in actual_cols, (
f"Column '{col}' missing from table '{table}'. "
f"Actual columns: {actual_cols}"
f"Column '{col}' missing from table '{table}'. " f"Actual columns: {actual_cols}"
)
@@ -250,8 +317,7 @@ async def test_table_row_count_is_zero(pg: PgStore) -> None:
for table in EXPECTED_TABLES:
count = await _table_row_count(pg, table)
assert count == 0, (
f"Table '{table}' should be empty after migration, "
f"but has {count} rows"
f"Table '{table}' should be empty after migration, " f"but has {count} rows"
)
@@ -262,13 +328,10 @@ async def test_schema_migration_version_recorded(pg: PgStore) -> None:
await pg.migrate()
async with pg.pool.acquire() as conn:
row = await conn.fetchrow(
"SELECT MAX(version) AS v FROM schema_migrations"
)
row = await conn.fetchrow("SELECT MAX(version) AS v FROM schema_migrations")
assert row is not None
assert row["v"] == SCHEMA_VERSION, (
f"Expected schema version {SCHEMA_VERSION}, "
f"got {row['v']}"
f"Expected schema version {SCHEMA_VERSION}, " f"got {row['v']}"
)
@@ -280,7 +343,8 @@ async def test_create_and_query_row(pg: PgStore) -> None:
# ConfigSections round-trip
await conn.execute(
"INSERT INTO config_sections (name, description) VALUES ($1, $2)",
"test_section", "A test section for integration test",
"test_section",
"A test section for integration test",
)
row = await conn.fetchrow(
"SELECT name, description FROM config_sections WHERE name = $1",
@@ -357,4 +421,4 @@ async def test_audit_list_recent(pg: PgStore) -> None:
# Verify payload serialization worked
first = recent[0]
if first.payload:
assert "index" in first.payload
assert "index" in first.payload