ci: fix lint baseline and stabilize failing main tests
This commit is contained in:
@@ -4,8 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
@@ -48,7 +47,9 @@ def temp_db(tmp_path: Path) -> Path:
|
||||
|
||||
cursor.executemany(
|
||||
"""
|
||||
INSERT INTO trades (timestamp, stock_code, action, quantity, price, confidence, rationale, pnl)
|
||||
INSERT INTO trades (
|
||||
timestamp, stock_code, action, quantity, price, confidence, rationale, pnl
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
test_trades,
|
||||
@@ -73,9 +74,7 @@ class TestBackupExporter:
|
||||
exporter = BackupExporter(str(temp_db))
|
||||
output_dir = tmp_path / "exports"
|
||||
|
||||
results = exporter.export_all(
|
||||
output_dir, formats=[ExportFormat.JSON], compress=False
|
||||
)
|
||||
results = exporter.export_all(output_dir, formats=[ExportFormat.JSON], compress=False)
|
||||
|
||||
assert ExportFormat.JSON in results
|
||||
assert results[ExportFormat.JSON].exists()
|
||||
@@ -86,9 +85,7 @@ class TestBackupExporter:
|
||||
exporter = BackupExporter(str(temp_db))
|
||||
output_dir = tmp_path / "exports"
|
||||
|
||||
results = exporter.export_all(
|
||||
output_dir, formats=[ExportFormat.JSON], compress=True
|
||||
)
|
||||
results = exporter.export_all(output_dir, formats=[ExportFormat.JSON], compress=True)
|
||||
|
||||
assert ExportFormat.JSON in results
|
||||
assert results[ExportFormat.JSON].suffix == ".gz"
|
||||
@@ -98,15 +95,13 @@ class TestBackupExporter:
|
||||
exporter = BackupExporter(str(temp_db))
|
||||
output_dir = tmp_path / "exports"
|
||||
|
||||
results = exporter.export_all(
|
||||
output_dir, formats=[ExportFormat.CSV], compress=False
|
||||
)
|
||||
results = exporter.export_all(output_dir, formats=[ExportFormat.CSV], compress=False)
|
||||
|
||||
assert ExportFormat.CSV in results
|
||||
assert results[ExportFormat.CSV].exists()
|
||||
|
||||
# Verify CSV content
|
||||
with open(results[ExportFormat.CSV], "r") as f:
|
||||
with open(results[ExportFormat.CSV]) as f:
|
||||
lines = f.readlines()
|
||||
assert len(lines) == 4 # Header + 3 rows
|
||||
|
||||
@@ -146,7 +141,7 @@ class TestBackupExporter:
|
||||
# Should only have 1 trade (AAPL on Jan 2)
|
||||
import json
|
||||
|
||||
with open(results[ExportFormat.JSON], "r") as f:
|
||||
with open(results[ExportFormat.JSON]) as f:
|
||||
data = json.load(f)
|
||||
assert data["record_count"] == 1
|
||||
assert data["trades"][0]["stock_code"] == "AAPL"
|
||||
@@ -407,9 +402,7 @@ class TestBackupExporterAdditional:
|
||||
assert ExportFormat.JSON in results
|
||||
assert ExportFormat.CSV in results
|
||||
|
||||
def test_export_all_logs_error_on_failure(
|
||||
self, temp_db: Path, tmp_path: Path
|
||||
) -> None:
|
||||
def test_export_all_logs_error_on_failure(self, temp_db: Path, tmp_path: Path) -> None:
|
||||
"""export_all must log an error and continue when one format fails."""
|
||||
exporter = BackupExporter(str(temp_db))
|
||||
# Patch _export_format to raise on JSON, succeed on CSV
|
||||
@@ -430,9 +423,7 @@ class TestBackupExporterAdditional:
|
||||
assert ExportFormat.JSON not in results
|
||||
assert ExportFormat.CSV in results
|
||||
|
||||
def test_export_csv_empty_trades_no_compress(
|
||||
self, empty_db: Path, tmp_path: Path
|
||||
) -> None:
|
||||
def test_export_csv_empty_trades_no_compress(self, empty_db: Path, tmp_path: Path) -> None:
|
||||
"""CSV export with no trades and compress=False must write header row only."""
|
||||
exporter = BackupExporter(str(empty_db))
|
||||
results = exporter.export_all(
|
||||
@@ -446,9 +437,7 @@ class TestBackupExporterAdditional:
|
||||
content = out.read_text()
|
||||
assert "timestamp" in content
|
||||
|
||||
def test_export_csv_empty_trades_compressed(
|
||||
self, empty_db: Path, tmp_path: Path
|
||||
) -> None:
|
||||
def test_export_csv_empty_trades_compressed(self, empty_db: Path, tmp_path: Path) -> None:
|
||||
"""CSV export with no trades and compress=True must write gzipped header."""
|
||||
import gzip
|
||||
|
||||
@@ -465,9 +454,7 @@ class TestBackupExporterAdditional:
|
||||
content = f.read()
|
||||
assert "timestamp" in content
|
||||
|
||||
def test_export_csv_with_data_compressed(
|
||||
self, temp_db: Path, tmp_path: Path
|
||||
) -> None:
|
||||
def test_export_csv_with_data_compressed(self, temp_db: Path, tmp_path: Path) -> None:
|
||||
"""CSV export with data and compress=True must write gzipped rows."""
|
||||
import gzip
|
||||
|
||||
@@ -492,6 +479,7 @@ class TestBackupExporterAdditional:
|
||||
with patch.dict(sys.modules, {"pyarrow": None, "pyarrow.parquet": None}):
|
||||
try:
|
||||
import pyarrow # noqa: F401
|
||||
|
||||
pytest.skip("pyarrow is installed; cannot test ImportError path")
|
||||
except ImportError:
|
||||
pass
|
||||
@@ -557,9 +545,7 @@ class TestCloudStorage:
|
||||
importlib.reload(m)
|
||||
m.CloudStorage(s3_config)
|
||||
|
||||
def test_upload_file_success(
|
||||
self, mock_boto3_module, s3_config, tmp_path: Path
|
||||
) -> None:
|
||||
def test_upload_file_success(self, mock_boto3_module, s3_config, tmp_path: Path) -> None:
|
||||
"""upload_file must call client.upload_file and return the object key."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -572,9 +558,7 @@ class TestCloudStorage:
|
||||
assert key == "backups/backup.json.gz"
|
||||
storage.client.upload_file.assert_called_once()
|
||||
|
||||
def test_upload_file_default_key(
|
||||
self, mock_boto3_module, s3_config, tmp_path: Path
|
||||
) -> None:
|
||||
def test_upload_file_default_key(self, mock_boto3_module, s3_config, tmp_path: Path) -> None:
|
||||
"""upload_file without object_key must use the filename as key."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -586,9 +570,7 @@ class TestCloudStorage:
|
||||
|
||||
assert key == "myfile.gz"
|
||||
|
||||
def test_upload_file_not_found(
|
||||
self, mock_boto3_module, s3_config, tmp_path: Path
|
||||
) -> None:
|
||||
def test_upload_file_not_found(self, mock_boto3_module, s3_config, tmp_path: Path) -> None:
|
||||
"""upload_file must raise FileNotFoundError for missing files."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -611,9 +593,7 @@ class TestCloudStorage:
|
||||
with pytest.raises(RuntimeError, match="network error"):
|
||||
storage.upload_file(test_file)
|
||||
|
||||
def test_download_file_success(
|
||||
self, mock_boto3_module, s3_config, tmp_path: Path
|
||||
) -> None:
|
||||
def test_download_file_success(self, mock_boto3_module, s3_config, tmp_path: Path) -> None:
|
||||
"""download_file must call client.download_file and return local path."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -637,11 +617,8 @@ class TestCloudStorage:
|
||||
with pytest.raises(RuntimeError, match="timeout"):
|
||||
storage.download_file("key", tmp_path / "dest.gz")
|
||||
|
||||
def test_list_files_returns_objects(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_list_files_returns_objects(self, mock_boto3_module, s3_config) -> None:
|
||||
"""list_files must return parsed file metadata from S3 response."""
|
||||
from datetime import timezone
|
||||
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -651,7 +628,7 @@ class TestCloudStorage:
|
||||
{
|
||||
"Key": "backups/a.gz",
|
||||
"Size": 1024,
|
||||
"LastModified": datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
"LastModified": datetime(2026, 1, 1, tzinfo=UTC),
|
||||
"ETag": '"abc123"',
|
||||
}
|
||||
]
|
||||
@@ -662,9 +639,7 @@ class TestCloudStorage:
|
||||
assert files[0]["key"] == "backups/a.gz"
|
||||
assert files[0]["size_bytes"] == 1024
|
||||
|
||||
def test_list_files_empty_bucket(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_list_files_empty_bucket(self, mock_boto3_module, s3_config) -> None:
|
||||
"""list_files must return empty list when bucket has no objects."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -674,9 +649,7 @@ class TestCloudStorage:
|
||||
files = storage.list_files()
|
||||
assert files == []
|
||||
|
||||
def test_list_files_propagates_error(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_list_files_propagates_error(self, mock_boto3_module, s3_config) -> None:
|
||||
"""list_files must re-raise exceptions from the boto3 client."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -686,9 +659,7 @@ class TestCloudStorage:
|
||||
with pytest.raises(RuntimeError):
|
||||
storage.list_files()
|
||||
|
||||
def test_delete_file_success(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_delete_file_success(self, mock_boto3_module, s3_config) -> None:
|
||||
"""delete_file must call client.delete_object with the correct key."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -698,9 +669,7 @@ class TestCloudStorage:
|
||||
Bucket="test-bucket", Key="backups/old.gz"
|
||||
)
|
||||
|
||||
def test_delete_file_propagates_error(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_delete_file_propagates_error(self, mock_boto3_module, s3_config) -> None:
|
||||
"""delete_file must re-raise exceptions from the boto3 client."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -710,11 +679,8 @@ class TestCloudStorage:
|
||||
with pytest.raises(RuntimeError):
|
||||
storage.delete_file("backups/old.gz")
|
||||
|
||||
def test_get_storage_stats_success(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_get_storage_stats_success(self, mock_boto3_module, s3_config) -> None:
|
||||
"""get_storage_stats must aggregate file sizes correctly."""
|
||||
from datetime import timezone
|
||||
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -724,13 +690,13 @@ class TestCloudStorage:
|
||||
{
|
||||
"Key": "a.gz",
|
||||
"Size": 1024 * 1024,
|
||||
"LastModified": datetime(2026, 1, 1, tzinfo=timezone.utc),
|
||||
"LastModified": datetime(2026, 1, 1, tzinfo=UTC),
|
||||
"ETag": '"x"',
|
||||
},
|
||||
{
|
||||
"Key": "b.gz",
|
||||
"Size": 1024 * 1024,
|
||||
"LastModified": datetime(2026, 1, 2, tzinfo=timezone.utc),
|
||||
"LastModified": datetime(2026, 1, 2, tzinfo=UTC),
|
||||
"ETag": '"y"',
|
||||
},
|
||||
]
|
||||
@@ -741,9 +707,7 @@ class TestCloudStorage:
|
||||
assert stats["total_size_bytes"] == 2 * 1024 * 1024
|
||||
assert stats["total_size_mb"] == pytest.approx(2.0)
|
||||
|
||||
def test_get_storage_stats_on_error(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_get_storage_stats_on_error(self, mock_boto3_module, s3_config) -> None:
|
||||
"""get_storage_stats must return error dict without raising on failure."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -754,9 +718,7 @@ class TestCloudStorage:
|
||||
assert "error" in stats
|
||||
assert stats["total_files"] == 0
|
||||
|
||||
def test_verify_connection_success(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_verify_connection_success(self, mock_boto3_module, s3_config) -> None:
|
||||
"""verify_connection must return True when head_bucket succeeds."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -764,9 +726,7 @@ class TestCloudStorage:
|
||||
result = storage.verify_connection()
|
||||
assert result is True
|
||||
|
||||
def test_verify_connection_failure(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_verify_connection_failure(self, mock_boto3_module, s3_config) -> None:
|
||||
"""verify_connection must return False when head_bucket raises."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -776,9 +736,7 @@ class TestCloudStorage:
|
||||
result = storage.verify_connection()
|
||||
assert result is False
|
||||
|
||||
def test_enable_versioning(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_enable_versioning(self, mock_boto3_module, s3_config) -> None:
|
||||
"""enable_versioning must call put_bucket_versioning."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
@@ -786,9 +744,7 @@ class TestCloudStorage:
|
||||
storage.enable_versioning()
|
||||
storage.client.put_bucket_versioning.assert_called_once()
|
||||
|
||||
def test_enable_versioning_propagates_error(
|
||||
self, mock_boto3_module, s3_config
|
||||
) -> None:
|
||||
def test_enable_versioning_propagates_error(self, mock_boto3_module, s3_config) -> None:
|
||||
"""enable_versioning must re-raise exceptions from the boto3 client."""
|
||||
from src.backup.cloud_storage import CloudStorage
|
||||
|
||||
|
||||
@@ -323,7 +323,8 @@ class TestPromptOverride:
|
||||
# Verify the custom prompt was sent, not a built prompt
|
||||
mock_generate.assert_called_once()
|
||||
actual_prompt = mock_generate.call_args[1].get(
|
||||
"contents", mock_generate.call_args[0][1] if len(mock_generate.call_args[0]) > 1 else None
|
||||
"contents",
|
||||
mock_generate.call_args[0][1] if len(mock_generate.call_args[0]) > 1 else None,
|
||||
)
|
||||
assert actual_prompt == custom_prompt
|
||||
# Raw response preserved in rationale without parse_response (#247)
|
||||
@@ -385,7 +386,8 @@ class TestPromptOverride:
|
||||
await client.decide(market_data)
|
||||
|
||||
actual_prompt = mock_generate.call_args[1].get(
|
||||
"contents", mock_generate.call_args[0][1] if len(mock_generate.call_args[0]) > 1 else None
|
||||
"contents",
|
||||
mock_generate.call_args[0][1] if len(mock_generate.call_args[0]) > 1 else None,
|
||||
)
|
||||
# The custom prompt must be used, not the compressed prompt
|
||||
assert actual_prompt == custom_prompt
|
||||
@@ -411,7 +413,8 @@ class TestPromptOverride:
|
||||
await client.decide(market_data)
|
||||
|
||||
actual_prompt = mock_generate.call_args[1].get(
|
||||
"contents", mock_generate.call_args[0][1] if len(mock_generate.call_args[0]) > 1 else None
|
||||
"contents",
|
||||
mock_generate.call_args[0][1] if len(mock_generate.call_args[0]) > 1 else None,
|
||||
)
|
||||
# Should contain stock code from build_prompt, not be a custom override
|
||||
assert "005930" in actual_prompt
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -99,7 +99,10 @@ class TestTokenManagement:
|
||||
mock_resp_403 = AsyncMock()
|
||||
mock_resp_403.status = 403
|
||||
mock_resp_403.text = AsyncMock(
|
||||
return_value='{"error_code":"EGW00133","error_description":"접근토큰 발급 잠시 후 다시 시도하세요(1분당 1회)"}'
|
||||
return_value=(
|
||||
'{"error_code":"EGW00133","error_description":'
|
||||
'"접근토큰 발급 잠시 후 다시 시도하세요(1분당 1회)"}'
|
||||
)
|
||||
)
|
||||
mock_resp_403.__aenter__ = AsyncMock(return_value=mock_resp_403)
|
||||
mock_resp_403.__aexit__ = AsyncMock(return_value=False)
|
||||
@@ -232,9 +235,7 @@ class TestRateLimiter:
|
||||
mock_order_resp.__aenter__ = AsyncMock(return_value=mock_order_resp)
|
||||
mock_order_resp.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash_resp, mock_order_resp]
|
||||
):
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash_resp, mock_order_resp]):
|
||||
with patch.object(
|
||||
broker._rate_limiter, "acquire", new_callable=AsyncMock
|
||||
) as mock_acquire:
|
||||
@@ -405,7 +406,7 @@ class TestFetchMarketRankings:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
from src.broker.kis_api import kr_tick_unit, kr_round_down # noqa: E402
|
||||
from src.broker.kis_api import kr_round_down, kr_tick_unit # noqa: E402
|
||||
|
||||
|
||||
class TestKrTickUnit:
|
||||
@@ -435,13 +436,13 @@ class TestKrTickUnit:
|
||||
@pytest.mark.parametrize(
|
||||
"price, expected_rounded",
|
||||
[
|
||||
(188150, 188100), # 100원 단위, 50원 잔여 → 내림
|
||||
(188100, 188100), # 이미 정렬됨
|
||||
(75050, 75000), # 100원 단위, 50원 잔여 → 내림
|
||||
(49950, 49950), # 50원 단위 정렬됨
|
||||
(49960, 49950), # 50원 단위, 10원 잔여 → 내림
|
||||
(1999, 1999), # 1원 단위 → 그대로
|
||||
(5003, 5000), # 10원 단위, 3원 잔여 → 내림
|
||||
(188150, 188100), # 100원 단위, 50원 잔여 → 내림
|
||||
(188100, 188100), # 이미 정렬됨
|
||||
(75050, 75000), # 100원 단위, 50원 잔여 → 내림
|
||||
(49950, 49950), # 50원 단위 정렬됨
|
||||
(49960, 49950), # 50원 단위, 10원 잔여 → 내림
|
||||
(1999, 1999), # 1원 단위 → 그대로
|
||||
(5003, 5000), # 10원 단위, 3원 잔여 → 내림
|
||||
],
|
||||
)
|
||||
def test_round_down_to_tick(self, price: int, expected_rounded: int) -> None:
|
||||
@@ -538,15 +539,13 @@ class TestSendOrderTickRounding:
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.send_order("005930", "BUY", 1, price=188150)
|
||||
|
||||
order_call = mock_post.call_args_list[1]
|
||||
body = order_call[1].get("json", {})
|
||||
assert body["ORD_UNPR"] == "188100" # rounded down
|
||||
assert body["ORD_DVSN"] == "00" # 지정가
|
||||
assert body["ORD_DVSN"] == "00" # 지정가
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_limit_order_ord_dvsn_is_00(self, broker: KISBroker) -> None:
|
||||
@@ -563,9 +562,7 @@ class TestSendOrderTickRounding:
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.send_order("005930", "BUY", 1, price=50000)
|
||||
|
||||
order_call = mock_post.call_args_list[1]
|
||||
@@ -587,9 +584,7 @@ class TestSendOrderTickRounding:
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.send_order("005930", "SELL", 1, price=0)
|
||||
|
||||
order_call = mock_post.call_args_list[1]
|
||||
@@ -628,9 +623,7 @@ class TestTRIDBranchingDomestic:
|
||||
broker = self._make_broker(settings, "paper")
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
mock_resp.json = AsyncMock(
|
||||
return_value={"output1": [], "output2": {}}
|
||||
)
|
||||
mock_resp.json = AsyncMock(return_value={"output1": [], "output2": {}})
|
||||
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
@@ -645,9 +638,7 @@ class TestTRIDBranchingDomestic:
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
mock_resp.json = AsyncMock(
|
||||
return_value={"output1": [], "output2": {}}
|
||||
)
|
||||
mock_resp.json = AsyncMock(return_value={"output1": [], "output2": {}})
|
||||
mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
|
||||
mock_resp.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
@@ -672,9 +663,7 @@ class TestTRIDBranchingDomestic:
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.send_order("005930", "BUY", 1)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
@@ -695,9 +684,7 @@ class TestTRIDBranchingDomestic:
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.send_order("005930", "BUY", 1)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
@@ -718,9 +705,7 @@ class TestTRIDBranchingDomestic:
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.send_order("005930", "SELL", 1)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
@@ -741,9 +726,7 @@ class TestTRIDBranchingDomestic:
|
||||
mock_order.__aenter__ = AsyncMock(return_value=mock_order)
|
||||
mock_order.__aexit__ = AsyncMock(return_value=False)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.send_order("005930", "SELL", 1)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
@@ -788,9 +771,7 @@ class TestGetDomesticPendingOrders:
|
||||
mock_get.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_live_mode_calls_tttc0084r_with_correct_params(
|
||||
self, settings
|
||||
) -> None:
|
||||
async def test_live_mode_calls_tttc0084r_with_correct_params(self, settings) -> None:
|
||||
"""Live mode must call TTTC0084R with INQR_DVSN_1/2 and paging params."""
|
||||
broker = self._make_broker(settings, "live")
|
||||
pending = [{"odno": "001", "pdno": "005930", "psbl_qty": "10"}]
|
||||
@@ -872,9 +853,7 @@ class TestCancelDomesticOrder:
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 5)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
@@ -886,9 +865,7 @@ class TestCancelDomesticOrder:
|
||||
broker = self._make_broker(settings, "paper")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 5)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
@@ -900,9 +877,7 @@ class TestCancelDomesticOrder:
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 5)
|
||||
|
||||
body = mock_post.call_args_list[1][1].get("json", {})
|
||||
@@ -916,9 +891,7 @@ class TestCancelDomesticOrder:
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD123", "BRN456", 3)
|
||||
|
||||
body = mock_post.call_args_list[1][1].get("json", {})
|
||||
@@ -932,9 +905,7 @@ class TestCancelDomesticOrder:
|
||||
broker = self._make_broker(settings, "live")
|
||||
mock_hash, mock_order = self._make_post_mocks({"rt_cd": "0"})
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]
|
||||
) as mock_post:
|
||||
with patch("aiohttp.ClientSession.post", side_effect=[mock_hash, mock_order]) as mock_post:
|
||||
await broker.cancel_domestic_order("005930", "ORD001", "BRNO01", 2)
|
||||
|
||||
order_headers = mock_post.call_args_list[1][1].get("headers", {})
|
||||
|
||||
@@ -77,9 +77,7 @@ class TestContextStore:
|
||||
# Latest by updated_at, which should be the last one set
|
||||
assert latest == "2026-02-02"
|
||||
|
||||
def test_delete_old_contexts(
|
||||
self, store: ContextStore, db_conn: sqlite3.Connection
|
||||
) -> None:
|
||||
def test_delete_old_contexts(self, store: ContextStore, db_conn: sqlite3.Connection) -> None:
|
||||
"""Test deleting contexts older than a cutoff date."""
|
||||
# Insert contexts with specific old timestamps
|
||||
# (bypassing set_context which uses current time)
|
||||
@@ -170,9 +168,7 @@ class TestContextAggregator:
|
||||
log_trade(db_conn, "035720", "HOLD", 75, "Wait", quantity=0, price=0, pnl=0)
|
||||
|
||||
# Manually set timestamps to the target date
|
||||
db_conn.execute(
|
||||
f"UPDATE trades SET timestamp = '{date}T10:00:00+00:00'"
|
||||
)
|
||||
db_conn.execute(f"UPDATE trades SET timestamp = '{date}T10:00:00+00:00'")
|
||||
db_conn.commit()
|
||||
|
||||
# Aggregate
|
||||
@@ -194,18 +190,10 @@ class TestContextAggregator:
|
||||
week = "2026-W06"
|
||||
|
||||
# Set daily contexts
|
||||
aggregator.store.set_context(
|
||||
ContextLayer.L6_DAILY, "2026-02-02", "total_pnl_KR", 100.0
|
||||
)
|
||||
aggregator.store.set_context(
|
||||
ContextLayer.L6_DAILY, "2026-02-03", "total_pnl_KR", 200.0
|
||||
)
|
||||
aggregator.store.set_context(
|
||||
ContextLayer.L6_DAILY, "2026-02-02", "avg_confidence_KR", 80.0
|
||||
)
|
||||
aggregator.store.set_context(
|
||||
ContextLayer.L6_DAILY, "2026-02-03", "avg_confidence_KR", 85.0
|
||||
)
|
||||
aggregator.store.set_context(ContextLayer.L6_DAILY, "2026-02-02", "total_pnl_KR", 100.0)
|
||||
aggregator.store.set_context(ContextLayer.L6_DAILY, "2026-02-03", "total_pnl_KR", 200.0)
|
||||
aggregator.store.set_context(ContextLayer.L6_DAILY, "2026-02-02", "avg_confidence_KR", 80.0)
|
||||
aggregator.store.set_context(ContextLayer.L6_DAILY, "2026-02-03", "avg_confidence_KR", 85.0)
|
||||
|
||||
# Aggregate
|
||||
aggregator.aggregate_weekly_from_daily(week)
|
||||
@@ -223,15 +211,9 @@ class TestContextAggregator:
|
||||
month = "2026-02"
|
||||
|
||||
# Set weekly contexts
|
||||
aggregator.store.set_context(
|
||||
ContextLayer.L5_WEEKLY, "2026-W05", "weekly_pnl_KR", 100.0
|
||||
)
|
||||
aggregator.store.set_context(
|
||||
ContextLayer.L5_WEEKLY, "2026-W06", "weekly_pnl_KR", 200.0
|
||||
)
|
||||
aggregator.store.set_context(
|
||||
ContextLayer.L5_WEEKLY, "2026-W07", "weekly_pnl_KR", 150.0
|
||||
)
|
||||
aggregator.store.set_context(ContextLayer.L5_WEEKLY, "2026-W05", "weekly_pnl_KR", 100.0)
|
||||
aggregator.store.set_context(ContextLayer.L5_WEEKLY, "2026-W06", "weekly_pnl_KR", 200.0)
|
||||
aggregator.store.set_context(ContextLayer.L5_WEEKLY, "2026-W07", "weekly_pnl_KR", 150.0)
|
||||
|
||||
# Aggregate
|
||||
aggregator.aggregate_monthly_from_weekly(month)
|
||||
@@ -316,6 +298,7 @@ class TestContextAggregator:
|
||||
store = aggregator.store
|
||||
assert store.get_context(ContextLayer.L6_DAILY, date, "total_pnl_KR") == 1000.0
|
||||
from datetime import date as date_cls
|
||||
|
||||
trade_date = date_cls.fromisoformat(date)
|
||||
iso_year, iso_week, _ = trade_date.isocalendar()
|
||||
trade_week = f"{iso_year}-W{iso_week:02d}"
|
||||
@@ -324,7 +307,9 @@ class TestContextAggregator:
|
||||
trade_quarter = f"{trade_date.year}-Q{(trade_date.month - 1) // 3 + 1}"
|
||||
trade_year = str(trade_date.year)
|
||||
assert store.get_context(ContextLayer.L4_MONTHLY, trade_month, "monthly_pnl") == 1000.0
|
||||
assert store.get_context(ContextLayer.L3_QUARTERLY, trade_quarter, "quarterly_pnl") == 1000.0
|
||||
assert (
|
||||
store.get_context(ContextLayer.L3_QUARTERLY, trade_quarter, "quarterly_pnl") == 1000.0
|
||||
)
|
||||
assert store.get_context(ContextLayer.L2_ANNUAL, trade_year, "annual_pnl") == 1000.0
|
||||
|
||||
|
||||
@@ -429,9 +414,7 @@ class TestContextSummarizer:
|
||||
# summarize_layer
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def test_summarize_layer_no_data(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_summarize_layer_no_data(self, summarizer: ContextSummarizer) -> None:
|
||||
"""summarize_layer with no data must return the 'No data' sentinel."""
|
||||
result = summarizer.summarize_layer(ContextLayer.L6_DAILY)
|
||||
assert result["count"] == 0
|
||||
@@ -448,15 +431,12 @@ class TestContextSummarizer:
|
||||
result = summarizer.summarize_layer(ContextLayer.L6_DAILY)
|
||||
assert "total_entries" in result
|
||||
|
||||
def test_summarize_layer_with_dict_values(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_summarize_layer_with_dict_values(self, summarizer: ContextSummarizer) -> None:
|
||||
"""summarize_layer must handle dict values by extracting numeric subkeys."""
|
||||
store = summarizer.store
|
||||
# set_context serialises the value as JSON, so passing a dict works
|
||||
store.set_context(
|
||||
ContextLayer.L6_DAILY, "2026-02-01", "metrics",
|
||||
{"win_rate": 65.0, "label": "good"}
|
||||
ContextLayer.L6_DAILY, "2026-02-01", "metrics", {"win_rate": 65.0, "label": "good"}
|
||||
)
|
||||
|
||||
result = summarizer.summarize_layer(ContextLayer.L6_DAILY)
|
||||
@@ -464,9 +444,7 @@ class TestContextSummarizer:
|
||||
# numeric subkey "win_rate" should appear as "metrics.win_rate"
|
||||
assert "metrics.win_rate" in result
|
||||
|
||||
def test_summarize_layer_with_string_values(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_summarize_layer_with_string_values(self, summarizer: ContextSummarizer) -> None:
|
||||
"""summarize_layer must count string values separately."""
|
||||
store = summarizer.store
|
||||
# set_context stores string values as JSON-encoded strings
|
||||
@@ -480,9 +458,7 @@ class TestContextSummarizer:
|
||||
# rolling_window_summary
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def test_rolling_window_summary_basic(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_rolling_window_summary_basic(self, summarizer: ContextSummarizer) -> None:
|
||||
"""rolling_window_summary must return the expected structure."""
|
||||
store = summarizer.store
|
||||
store.set_context(ContextLayer.L6_DAILY, "2026-02-01", "pnl", 500.0)
|
||||
@@ -492,22 +468,16 @@ class TestContextSummarizer:
|
||||
assert "recent_data" in result
|
||||
assert "historical_summary" in result
|
||||
|
||||
def test_rolling_window_summary_no_older_data(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_rolling_window_summary_no_older_data(self, summarizer: ContextSummarizer) -> None:
|
||||
"""rolling_window_summary with summarize_older=False skips history."""
|
||||
result = summarizer.rolling_window_summary(
|
||||
ContextLayer.L6_DAILY, summarize_older=False
|
||||
)
|
||||
result = summarizer.rolling_window_summary(ContextLayer.L6_DAILY, summarize_older=False)
|
||||
assert result["historical_summary"] == {}
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# aggregate_to_higher_layer
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def test_aggregate_to_higher_layer_mean(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_aggregate_to_higher_layer_mean(self, summarizer: ContextSummarizer) -> None:
|
||||
"""aggregate_to_higher_layer with 'mean' via dict subkeys returns average."""
|
||||
store = summarizer.store
|
||||
# Use different outer keys but same inner metric key so get_all_contexts
|
||||
@@ -520,9 +490,7 @@ class TestContextSummarizer:
|
||||
)
|
||||
assert result == pytest.approx(150.0)
|
||||
|
||||
def test_aggregate_to_higher_layer_sum(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_aggregate_to_higher_layer_sum(self, summarizer: ContextSummarizer) -> None:
|
||||
"""aggregate_to_higher_layer with 'sum' must return the total."""
|
||||
store = summarizer.store
|
||||
store.set_context(ContextLayer.L6_DAILY, "2026-02-01", "day1", {"pnl": 100.0})
|
||||
@@ -533,9 +501,7 @@ class TestContextSummarizer:
|
||||
)
|
||||
assert result == pytest.approx(300.0)
|
||||
|
||||
def test_aggregate_to_higher_layer_max(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_aggregate_to_higher_layer_max(self, summarizer: ContextSummarizer) -> None:
|
||||
"""aggregate_to_higher_layer with 'max' must return the maximum."""
|
||||
store = summarizer.store
|
||||
store.set_context(ContextLayer.L6_DAILY, "2026-02-01", "day1", {"pnl": 100.0})
|
||||
@@ -546,9 +512,7 @@ class TestContextSummarizer:
|
||||
)
|
||||
assert result == pytest.approx(200.0)
|
||||
|
||||
def test_aggregate_to_higher_layer_min(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_aggregate_to_higher_layer_min(self, summarizer: ContextSummarizer) -> None:
|
||||
"""aggregate_to_higher_layer with 'min' must return the minimum."""
|
||||
store = summarizer.store
|
||||
store.set_context(ContextLayer.L6_DAILY, "2026-02-01", "day1", {"pnl": 100.0})
|
||||
@@ -559,9 +523,7 @@ class TestContextSummarizer:
|
||||
)
|
||||
assert result == pytest.approx(100.0)
|
||||
|
||||
def test_aggregate_to_higher_layer_no_data(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_aggregate_to_higher_layer_no_data(self, summarizer: ContextSummarizer) -> None:
|
||||
"""aggregate_to_higher_layer with no matching key must return None."""
|
||||
result = summarizer.aggregate_to_higher_layer(
|
||||
ContextLayer.L6_DAILY, ContextLayer.L5_WEEKLY, "nonexistent", "mean"
|
||||
@@ -585,9 +547,7 @@ class TestContextSummarizer:
|
||||
# create_compact_summary + format_summary_for_prompt
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def test_create_compact_summary(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_create_compact_summary(self, summarizer: ContextSummarizer) -> None:
|
||||
"""create_compact_summary must produce a dict keyed by layer value."""
|
||||
store = summarizer.store
|
||||
store.set_context(ContextLayer.L6_DAILY, "2026-02-01", "pnl", 100.0)
|
||||
@@ -615,9 +575,7 @@ class TestContextSummarizer:
|
||||
text = summarizer.format_summary_for_prompt(summary)
|
||||
assert text == ""
|
||||
|
||||
def test_format_summary_non_dict_value(
|
||||
self, summarizer: ContextSummarizer
|
||||
) -> None:
|
||||
def test_format_summary_non_dict_value(self, summarizer: ContextSummarizer) -> None:
|
||||
"""format_summary_for_prompt must render non-dict values as plain text."""
|
||||
summary = {
|
||||
"daily": {
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
from datetime import UTC, datetime
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
@@ -16,8 +17,6 @@ from src.evolution.daily_review import DailyReviewer
|
||||
from src.evolution.scorecard import DailyScorecard
|
||||
from src.logging.decision_logger import DecisionLogger
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
TODAY = datetime.now(UTC).strftime("%Y-%m-%d")
|
||||
|
||||
|
||||
@@ -53,7 +52,8 @@ def _log_decision(
|
||||
|
||||
|
||||
def test_generate_scorecard_market_scoped(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
reviewer = DailyReviewer(db_conn, context_store)
|
||||
logger = DecisionLogger(db_conn)
|
||||
@@ -134,7 +134,8 @@ def test_generate_scorecard_market_scoped(
|
||||
|
||||
|
||||
def test_generate_scorecard_top_winners_and_losers(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
reviewer = DailyReviewer(db_conn, context_store)
|
||||
logger = DecisionLogger(db_conn)
|
||||
@@ -168,7 +169,8 @@ def test_generate_scorecard_top_winners_and_losers(
|
||||
|
||||
|
||||
def test_generate_scorecard_empty_day(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
reviewer = DailyReviewer(db_conn, context_store)
|
||||
scorecard = reviewer.generate_scorecard(TODAY, "KR")
|
||||
@@ -184,7 +186,8 @@ def test_generate_scorecard_empty_day(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_lessons_without_gemini_returns_empty(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
reviewer = DailyReviewer(db_conn, context_store, gemini_client=None)
|
||||
lessons = await reviewer.generate_lessons(
|
||||
@@ -206,7 +209,8 @@ async def test_generate_lessons_without_gemini_returns_empty(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_lessons_parses_json_array(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
mock_gemini = MagicMock()
|
||||
mock_gemini.decide = AsyncMock(
|
||||
@@ -233,7 +237,8 @@ async def test_generate_lessons_parses_json_array(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_lessons_fallback_to_lines(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
mock_gemini = MagicMock()
|
||||
mock_gemini.decide = AsyncMock(
|
||||
@@ -260,7 +265,8 @@ async def test_generate_lessons_fallback_to_lines(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_lessons_handles_gemini_error(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
mock_gemini = MagicMock()
|
||||
mock_gemini.decide = AsyncMock(side_effect=RuntimeError("boom"))
|
||||
@@ -284,7 +290,8 @@ async def test_generate_lessons_handles_gemini_error(
|
||||
|
||||
|
||||
def test_store_scorecard_in_context(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
reviewer = DailyReviewer(db_conn, context_store)
|
||||
scorecard = DailyScorecard(
|
||||
@@ -316,7 +323,8 @@ def test_store_scorecard_in_context(
|
||||
|
||||
|
||||
def test_store_scorecard_key_is_market_scoped(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
reviewer = DailyReviewer(db_conn, context_store)
|
||||
kr = DailyScorecard(
|
||||
@@ -357,7 +365,8 @@ def test_store_scorecard_key_is_market_scoped(
|
||||
|
||||
|
||||
def test_generate_scorecard_handles_invalid_context_snapshot(
|
||||
db_conn: sqlite3.Connection, context_store: ContextStore,
|
||||
db_conn: sqlite3.Connection,
|
||||
context_store: ContextStore,
|
||||
) -> None:
|
||||
reviewer = DailyReviewer(db_conn, context_store)
|
||||
db_conn.execute(
|
||||
|
||||
@@ -355,6 +355,7 @@ def test_positions_empty_when_no_trades(tmp_path: Path) -> None:
|
||||
|
||||
def _seed_cb_context(conn: sqlite3.Connection, pnl_pct: float, market: str = "KR") -> None:
|
||||
import json as _json
|
||||
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO system_metrics (key, value, updated_at) VALUES (?, ?, ?)",
|
||||
(
|
||||
|
||||
@@ -79,7 +79,7 @@ class TestNewsAPI:
|
||||
# Mock the fetch to avoid real API call
|
||||
with patch.object(api, "_fetch_news", new_callable=AsyncMock) as mock_fetch:
|
||||
mock_fetch.return_value = None
|
||||
result = await api.get_news_sentiment("AAPL")
|
||||
await api.get_news_sentiment("AAPL")
|
||||
|
||||
# Should have attempted refetch since cache expired
|
||||
mock_fetch.assert_called_once_with("AAPL")
|
||||
@@ -111,9 +111,7 @@ class TestNewsAPI:
|
||||
"source": "Reuters",
|
||||
"time_published": "2026-02-04T10:00:00",
|
||||
"url": "https://example.com/1",
|
||||
"ticker_sentiment": [
|
||||
{"ticker": "AAPL", "ticker_sentiment_score": "0.85"}
|
||||
],
|
||||
"ticker_sentiment": [{"ticker": "AAPL", "ticker_sentiment_score": "0.85"}],
|
||||
"overall_sentiment_score": "0.75",
|
||||
},
|
||||
{
|
||||
@@ -122,9 +120,7 @@ class TestNewsAPI:
|
||||
"source": "Bloomberg",
|
||||
"time_published": "2026-02-04T09:00:00",
|
||||
"url": "https://example.com/2",
|
||||
"ticker_sentiment": [
|
||||
{"ticker": "AAPL", "ticker_sentiment_score": "-0.3"}
|
||||
],
|
||||
"ticker_sentiment": [{"ticker": "AAPL", "ticker_sentiment_score": "-0.3"}],
|
||||
"overall_sentiment_score": "-0.2",
|
||||
},
|
||||
]
|
||||
@@ -661,7 +657,9 @@ class TestGeminiClientWithExternalData:
|
||||
)
|
||||
|
||||
# Mock the Gemini API call
|
||||
with patch.object(client._client.aio.models, "generate_content", new_callable=AsyncMock) as mock_gen:
|
||||
with patch.object(
|
||||
client._client.aio.models, "generate_content", new_callable=AsyncMock
|
||||
) as mock_gen:
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = '{"action": "BUY", "confidence": 85, "rationale": "Good news"}'
|
||||
mock_gen.return_value = mock_response
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Tests for database helper functions."""
|
||||
|
||||
import tempfile
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from src.db import get_latest_buy_trade, get_open_position, init_db, log_trade
|
||||
|
||||
@@ -204,7 +204,8 @@ def test_mode_migration_adds_column_to_existing_db() -> None:
|
||||
assert "strategy_pnl" in columns
|
||||
assert "fx_pnl" in columns
|
||||
migrated = conn.execute(
|
||||
"SELECT pnl, strategy_pnl, fx_pnl, session_id FROM trades WHERE stock_code='AAPL' LIMIT 1"
|
||||
"SELECT pnl, strategy_pnl, fx_pnl, session_id "
|
||||
"FROM trades WHERE stock_code='AAPL' LIMIT 1"
|
||||
).fetchone()
|
||||
assert migrated is not None
|
||||
assert migrated[0] == 123.45
|
||||
@@ -407,9 +408,7 @@ def test_decision_logs_session_id_migration_backfills_unknown() -> None:
|
||||
conn = init_db(db_path)
|
||||
columns = {row[1] for row in conn.execute("PRAGMA table_info(decision_logs)").fetchall()}
|
||||
assert "session_id" in columns
|
||||
row = conn.execute(
|
||||
"SELECT session_id FROM decision_logs WHERE decision_id='d1'"
|
||||
).fetchone()
|
||||
row = conn.execute("SELECT session_id FROM decision_logs WHERE decision_id='d1'").fetchone()
|
||||
assert row is not None
|
||||
assert row[0] == "UNKNOWN"
|
||||
conn.close()
|
||||
|
||||
@@ -49,7 +49,10 @@ def test_log_decision_creates_record(logger: DecisionLogger, db_conn: sqlite3.Co
|
||||
|
||||
# Verify record exists in database
|
||||
cursor = db_conn.execute(
|
||||
"SELECT decision_id, action, confidence, session_id FROM decision_logs WHERE decision_id = ?",
|
||||
(
|
||||
"SELECT decision_id, action, confidence, session_id "
|
||||
"FROM decision_logs WHERE decision_id = ?"
|
||||
),
|
||||
(decision_id,),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
|
||||
@@ -208,7 +208,9 @@ def test_identify_failure_patterns_empty(optimizer: EvolutionOptimizer) -> None:
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_strategy_creates_file(optimizer: EvolutionOptimizer, tmp_path: Path) -> None:
|
||||
async def test_generate_strategy_creates_file(
|
||||
optimizer: EvolutionOptimizer, tmp_path: Path
|
||||
) -> None:
|
||||
"""Test that generate_strategy creates a strategy file."""
|
||||
failures = [
|
||||
{
|
||||
@@ -234,7 +236,9 @@ async def test_generate_strategy_creates_file(optimizer: EvolutionOptimizer, tmp
|
||||
return {"action": "HOLD", "confidence": 50, "rationale": "Waiting"}
|
||||
"""
|
||||
|
||||
with patch.object(optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)):
|
||||
with patch.object(
|
||||
optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)
|
||||
):
|
||||
with patch("src.evolution.optimizer.STRATEGIES_DIR", tmp_path):
|
||||
strategy_path = await optimizer.generate_strategy(failures)
|
||||
|
||||
@@ -247,7 +251,8 @@ async def test_generate_strategy_creates_file(optimizer: EvolutionOptimizer, tmp
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_strategy_saves_valid_python_code(
|
||||
optimizer: EvolutionOptimizer, tmp_path: Path,
|
||||
optimizer: EvolutionOptimizer,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""Test that syntactically valid generated code is saved."""
|
||||
failures = [{"decision_id": "1", "timestamp": "2024-01-15T09:30:00+00:00"}]
|
||||
@@ -255,12 +260,14 @@ async def test_generate_strategy_saves_valid_python_code(
|
||||
mock_response = Mock()
|
||||
mock_response.text = (
|
||||
'price = market_data.get("current_price", 0)\n'
|
||||
'if price > 0:\n'
|
||||
"if price > 0:\n"
|
||||
' return {"action": "BUY", "confidence": 80, "rationale": "Positive price"}\n'
|
||||
'return {"action": "HOLD", "confidence": 50, "rationale": "No signal"}\n'
|
||||
)
|
||||
|
||||
with patch.object(optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)):
|
||||
with patch.object(
|
||||
optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)
|
||||
):
|
||||
with patch("src.evolution.optimizer.STRATEGIES_DIR", tmp_path):
|
||||
strategy_path = await optimizer.generate_strategy(failures)
|
||||
|
||||
@@ -270,7 +277,9 @@ async def test_generate_strategy_saves_valid_python_code(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_generate_strategy_blocks_invalid_python_code(
|
||||
optimizer: EvolutionOptimizer, tmp_path: Path, caplog: pytest.LogCaptureFixture,
|
||||
optimizer: EvolutionOptimizer,
|
||||
tmp_path: Path,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test that syntactically invalid generated code is not saved."""
|
||||
failures = [{"decision_id": "1", "timestamp": "2024-01-15T09:30:00+00:00"}]
|
||||
@@ -281,7 +290,9 @@ async def test_generate_strategy_blocks_invalid_python_code(
|
||||
' return {"action": "BUY", "confidence": 80, "rationale": "broken"}\n'
|
||||
)
|
||||
|
||||
with patch.object(optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)):
|
||||
with patch.object(
|
||||
optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)
|
||||
):
|
||||
with patch("src.evolution.optimizer.STRATEGIES_DIR", tmp_path):
|
||||
with caplog.at_level("WARNING"):
|
||||
strategy_path = await optimizer.generate_strategy(failures)
|
||||
@@ -310,6 +321,7 @@ def test_get_performance_summary() -> None:
|
||||
"""Test getting performance summary from trades table."""
|
||||
# Create a temporary database with trades
|
||||
import tempfile
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as tmp:
|
||||
tmp_path = tmp.name
|
||||
|
||||
@@ -604,7 +616,9 @@ def test_calculate_improvement_trend_declining(performance_tracker: PerformanceT
|
||||
assert trend["pnl_change"] == -250.0
|
||||
|
||||
|
||||
def test_calculate_improvement_trend_insufficient_data(performance_tracker: PerformanceTracker) -> None:
|
||||
def test_calculate_improvement_trend_insufficient_data(
|
||||
performance_tracker: PerformanceTracker,
|
||||
) -> None:
|
||||
"""Test improvement trend with insufficient data."""
|
||||
metrics = [
|
||||
StrategyMetrics(
|
||||
@@ -718,7 +732,9 @@ async def test_full_evolution_pipeline(optimizer: EvolutionOptimizer, tmp_path:
|
||||
mock_response = Mock()
|
||||
mock_response.text = 'return {"action": "HOLD", "confidence": 50, "rationale": "Test"}'
|
||||
|
||||
with patch.object(optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)):
|
||||
with patch.object(
|
||||
optimizer._client.aio.models, "generate_content", new=AsyncMock(return_value=mock_response)
|
||||
):
|
||||
with patch("src.evolution.optimizer.STRATEGIES_DIR", tmp_path):
|
||||
with patch("subprocess.run") as mock_run:
|
||||
mock_run.return_value = Mock(returncode=0, stdout="", stderr="")
|
||||
|
||||
@@ -103,9 +103,7 @@ class TestSetupLogging:
|
||||
"""setup_logging must attach a JSON handler to the root logger."""
|
||||
setup_logging(level=logging.DEBUG)
|
||||
root = logging.getLogger()
|
||||
json_handlers = [
|
||||
h for h in root.handlers if isinstance(h.formatter, JSONFormatter)
|
||||
]
|
||||
json_handlers = [h for h in root.handlers if isinstance(h.formatter, JSONFormatter)]
|
||||
assert len(json_handlers) == 1
|
||||
assert root.level == logging.DEBUG
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -173,9 +173,7 @@ class TestGetNextMarketOpen:
|
||||
"""Should find next Monday opening when called on weekend."""
|
||||
# Saturday 2026-02-07 12:00 UTC
|
||||
test_time = datetime(2026, 2, 7, 12, 0, tzinfo=ZoneInfo("UTC"))
|
||||
market, open_time = get_next_market_open(
|
||||
enabled_markets=["KR"], now=test_time
|
||||
)
|
||||
market, open_time = get_next_market_open(enabled_markets=["KR"], now=test_time)
|
||||
assert market.code == "KR"
|
||||
# Monday 2026-02-09 09:00 KST
|
||||
expected = datetime(2026, 2, 9, 9, 0, tzinfo=ZoneInfo("Asia/Seoul"))
|
||||
@@ -185,9 +183,7 @@ class TestGetNextMarketOpen:
|
||||
"""Should find next day opening when called after market close."""
|
||||
# Monday 2026-02-02 16:00 KST (after close)
|
||||
test_time = datetime(2026, 2, 2, 16, 0, tzinfo=ZoneInfo("Asia/Seoul"))
|
||||
market, open_time = get_next_market_open(
|
||||
enabled_markets=["KR"], now=test_time
|
||||
)
|
||||
market, open_time = get_next_market_open(enabled_markets=["KR"], now=test_time)
|
||||
assert market.code == "KR"
|
||||
# Tuesday 2026-02-03 09:00 KST
|
||||
expected = datetime(2026, 2, 3, 9, 0, tzinfo=ZoneInfo("Asia/Seoul"))
|
||||
@@ -197,9 +193,7 @@ class TestGetNextMarketOpen:
|
||||
"""Should find earliest opening market among multiple."""
|
||||
# Saturday 2026-02-07 12:00 UTC
|
||||
test_time = datetime(2026, 2, 7, 12, 0, tzinfo=ZoneInfo("UTC"))
|
||||
market, open_time = get_next_market_open(
|
||||
enabled_markets=["KR", "US_NASDAQ"], now=test_time
|
||||
)
|
||||
market, open_time = get_next_market_open(enabled_markets=["KR", "US_NASDAQ"], now=test_time)
|
||||
# Monday 2026-02-09: KR opens at 09:00 KST = 00:00 UTC
|
||||
# Monday 2026-02-09: US opens at 09:30 EST = 14:30 UTC
|
||||
# KR opens first
|
||||
@@ -214,9 +208,7 @@ class TestGetNextMarketOpen:
|
||||
def test_get_next_market_open_invalid_market(self) -> None:
|
||||
"""Should skip invalid market codes."""
|
||||
test_time = datetime(2026, 2, 7, 12, 0, tzinfo=ZoneInfo("UTC"))
|
||||
market, _ = get_next_market_open(
|
||||
enabled_markets=["INVALID", "KR"], now=test_time
|
||||
)
|
||||
market, _ = get_next_market_open(enabled_markets=["INVALID", "KR"], now=test_time)
|
||||
assert market.code == "KR"
|
||||
|
||||
def test_get_next_market_open_prefers_extended_session(self) -> None:
|
||||
|
||||
@@ -8,7 +8,7 @@ import aiohttp
|
||||
import pytest
|
||||
|
||||
from src.broker.kis_api import KISBroker
|
||||
from src.broker.overseas import OverseasBroker, _PRICE_EXCHANGE_MAP, _RANKING_EXCHANGE_MAP
|
||||
from src.broker.overseas import _PRICE_EXCHANGE_MAP, _RANKING_EXCHANGE_MAP, OverseasBroker
|
||||
from src.config import Settings
|
||||
|
||||
|
||||
@@ -85,25 +85,27 @@ class TestConfigDefaults:
|
||||
assert mock_settings.OVERSEAS_RANKING_VOLUME_TR_ID == "HHDFS76270000"
|
||||
|
||||
def test_fluct_path(self, mock_settings: Settings) -> None:
|
||||
assert mock_settings.OVERSEAS_RANKING_FLUCT_PATH == "/uapi/overseas-stock/v1/ranking/updown-rate"
|
||||
assert (
|
||||
mock_settings.OVERSEAS_RANKING_FLUCT_PATH
|
||||
== "/uapi/overseas-stock/v1/ranking/updown-rate"
|
||||
)
|
||||
|
||||
def test_volume_path(self, mock_settings: Settings) -> None:
|
||||
assert mock_settings.OVERSEAS_RANKING_VOLUME_PATH == "/uapi/overseas-stock/v1/ranking/volume-surge"
|
||||
assert (
|
||||
mock_settings.OVERSEAS_RANKING_VOLUME_PATH
|
||||
== "/uapi/overseas-stock/v1/ranking/volume-surge"
|
||||
)
|
||||
|
||||
|
||||
class TestFetchOverseasRankings:
|
||||
"""Test fetch_overseas_rankings method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fluctuation_uses_correct_params(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_fluctuation_uses_correct_params(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Fluctuation ranking should use HHDFS76290000, updown-rate path, and correct params."""
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
mock_resp.json = AsyncMock(
|
||||
return_value={"output": [{"symb": "AAPL", "name": "Apple"}]}
|
||||
)
|
||||
mock_resp.json = AsyncMock(return_value={"output": [{"symb": "AAPL", "name": "Apple"}]})
|
||||
|
||||
mock_session = MagicMock()
|
||||
mock_session.get = MagicMock(return_value=_make_async_cm(mock_resp))
|
||||
@@ -132,15 +134,11 @@ class TestFetchOverseasRankings:
|
||||
overseas_broker._broker._auth_headers.assert_called_with("HHDFS76290000")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_volume_uses_correct_params(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_volume_uses_correct_params(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Volume ranking should use HHDFS76270000, volume-surge path, and correct params."""
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
mock_resp.json = AsyncMock(
|
||||
return_value={"output": [{"symb": "TSLA", "name": "Tesla"}]}
|
||||
)
|
||||
mock_resp.json = AsyncMock(return_value={"output": [{"symb": "TSLA", "name": "Tesla"}]})
|
||||
|
||||
mock_session = MagicMock()
|
||||
mock_session.get = MagicMock(return_value=_make_async_cm(mock_resp))
|
||||
@@ -169,9 +167,7 @@ class TestFetchOverseasRankings:
|
||||
overseas_broker._broker._auth_headers.assert_called_with("HHDFS76270000")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_404_returns_empty_list(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_404_returns_empty_list(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""HTTP 404 should return empty list (fallback) instead of raising."""
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 404
|
||||
@@ -186,9 +182,7 @@ class TestFetchOverseasRankings:
|
||||
assert result == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_404_error_raises(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_non_404_error_raises(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Non-404 HTTP errors should raise ConnectionError."""
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 500
|
||||
@@ -203,9 +197,7 @@ class TestFetchOverseasRankings:
|
||||
await overseas_broker.fetch_overseas_rankings("NASD")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_response_returns_empty(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_empty_response_returns_empty(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Empty output in response should return empty list."""
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -220,18 +212,14 @@ class TestFetchOverseasRankings:
|
||||
assert result == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ranking_disabled_returns_empty(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_ranking_disabled_returns_empty(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""When OVERSEAS_RANKING_ENABLED=False, should return empty immediately."""
|
||||
overseas_broker._broker._settings.OVERSEAS_RANKING_ENABLED = False
|
||||
result = await overseas_broker.fetch_overseas_rankings("NASD")
|
||||
assert result == []
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_limit_truncates_results(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_limit_truncates_results(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Results should be truncated to the specified limit."""
|
||||
rows = [{"symb": f"SYM{i}"} for i in range(20)]
|
||||
mock_resp = AsyncMock()
|
||||
@@ -247,9 +235,7 @@ class TestFetchOverseasRankings:
|
||||
assert len(result) == 5
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_network_error_raises(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_network_error_raises(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Network errors should raise ConnectionError."""
|
||||
cm = MagicMock()
|
||||
cm.__aenter__ = AsyncMock(side_effect=aiohttp.ClientError("timeout"))
|
||||
@@ -264,9 +250,7 @@ class TestFetchOverseasRankings:
|
||||
await overseas_broker.fetch_overseas_rankings("NASD")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exchange_code_mapping_applied(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_exchange_code_mapping_applied(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""All major exchanges should use mapped codes in API params."""
|
||||
for original, mapped in [("NASD", "NAS"), ("NYSE", "NYS"), ("AMEX", "AMS")]:
|
||||
mock_resp = AsyncMock()
|
||||
@@ -298,7 +282,9 @@ class TestGetOverseasPrice:
|
||||
mock_session.get = MagicMock(return_value=_make_async_cm(mock_resp))
|
||||
|
||||
_setup_broker_mocks(overseas_broker, mock_session)
|
||||
overseas_broker._broker._auth_headers = AsyncMock(return_value={"authorization": "Bearer t"})
|
||||
overseas_broker._broker._auth_headers = AsyncMock(
|
||||
return_value={"authorization": "Bearer t"}
|
||||
)
|
||||
|
||||
result = await overseas_broker.get_overseas_price("NASD", "AAPL")
|
||||
assert result["output"]["last"] == "150.00"
|
||||
@@ -530,11 +516,14 @@ class TestPriceExchangeMap:
|
||||
def test_price_map_equals_ranking_map(self) -> None:
|
||||
assert _PRICE_EXCHANGE_MAP is _RANKING_EXCHANGE_MAP
|
||||
|
||||
@pytest.mark.parametrize("original,expected", [
|
||||
("NASD", "NAS"),
|
||||
("NYSE", "NYS"),
|
||||
("AMEX", "AMS"),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"original,expected",
|
||||
[
|
||||
("NASD", "NAS"),
|
||||
("NYSE", "NYS"),
|
||||
("AMEX", "AMS"),
|
||||
],
|
||||
)
|
||||
def test_us_exchange_code_mapping(self, original: str, expected: str) -> None:
|
||||
assert _PRICE_EXCHANGE_MAP[original] == expected
|
||||
|
||||
@@ -574,9 +563,7 @@ class TestOrderRtCdCheck:
|
||||
return OverseasBroker(broker)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_success_rt_cd_returns_data(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_success_rt_cd_returns_data(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""rt_cd='0' → order accepted, data returned."""
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -590,9 +577,7 @@ class TestOrderRtCdCheck:
|
||||
assert result["rt_cd"] == "0"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_error_rt_cd_returns_data_with_msg(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_error_rt_cd_returns_data_with_msg(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""rt_cd != '0' → order rejected, data still returned (caller checks rt_cd)."""
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -623,6 +608,7 @@ class TestPaperOverseasCash:
|
||||
|
||||
def test_env_override(self) -> None:
|
||||
import os
|
||||
|
||||
os.environ["PAPER_OVERSEAS_CASH"] = "25000"
|
||||
settings = Settings(
|
||||
KIS_APP_KEY="k",
|
||||
@@ -635,6 +621,7 @@ class TestPaperOverseasCash:
|
||||
|
||||
def test_zero_disables_fallback(self) -> None:
|
||||
import os
|
||||
|
||||
os.environ["PAPER_OVERSEAS_CASH"] = "0"
|
||||
settings = Settings(
|
||||
KIS_APP_KEY="k",
|
||||
@@ -822,9 +809,7 @@ class TestGetOverseasPendingOrders:
|
||||
"""Tests for get_overseas_pending_orders method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_paper_mode_returns_empty(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_paper_mode_returns_empty(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Paper mode should immediately return [] without any API call."""
|
||||
# Default mock_settings has MODE="paper"
|
||||
overseas_broker._broker._settings = overseas_broker._broker._settings.model_copy(
|
||||
@@ -855,9 +840,7 @@ class TestGetOverseasPendingOrders:
|
||||
|
||||
overseas_broker._broker._auth_headers = mock_auth_headers # type: ignore[method-assign]
|
||||
|
||||
pending_orders = [
|
||||
{"odno": "001", "pdno": "AAPL", "sll_buy_dvsn_cd": "02", "nccs_qty": "5"}
|
||||
]
|
||||
pending_orders = [{"odno": "001", "pdno": "AAPL", "sll_buy_dvsn_cd": "02", "nccs_qty": "5"}]
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
mock_resp.json = AsyncMock(return_value={"output": pending_orders})
|
||||
@@ -879,9 +862,7 @@ class TestGetOverseasPendingOrders:
|
||||
assert captured_params[0]["OVRS_EXCG_CD"] == "NASD"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_live_mode_connection_error(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_live_mode_connection_error(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Network error in live mode should raise ConnectionError."""
|
||||
overseas_broker._broker._settings = overseas_broker._broker._settings.model_copy(
|
||||
update={"MODE": "live"}
|
||||
@@ -926,55 +907,41 @@ class TestCancelOverseasOrder:
|
||||
return captured_tr_ids, mock_session
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_us_live_uses_tttt1004u(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_us_live_uses_tttt1004u(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""US exchange in live mode should use TTTT1004U."""
|
||||
overseas_broker._broker._settings = overseas_broker._broker._settings.model_copy(
|
||||
update={"MODE": "live"}
|
||||
)
|
||||
captured, _ = self._setup_cancel_mocks(
|
||||
overseas_broker, {"rt_cd": "0", "msg1": "OK"}
|
||||
)
|
||||
captured, _ = self._setup_cancel_mocks(overseas_broker, {"rt_cd": "0", "msg1": "OK"})
|
||||
|
||||
await overseas_broker.cancel_overseas_order("NASD", "AAPL", "ORD001", 5)
|
||||
|
||||
assert "TTTT1004U" in captured
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_us_paper_uses_vttt1004u(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_us_paper_uses_vttt1004u(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""US exchange in paper mode should use VTTT1004U."""
|
||||
# Default mock_settings has MODE="paper"
|
||||
captured, _ = self._setup_cancel_mocks(
|
||||
overseas_broker, {"rt_cd": "0", "msg1": "OK"}
|
||||
)
|
||||
captured, _ = self._setup_cancel_mocks(overseas_broker, {"rt_cd": "0", "msg1": "OK"})
|
||||
|
||||
await overseas_broker.cancel_overseas_order("NASD", "AAPL", "ORD001", 5)
|
||||
|
||||
assert "VTTT1004U" in captured
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hk_live_uses_ttts1003u(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_hk_live_uses_ttts1003u(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""SEHK exchange in live mode should use TTTS1003U."""
|
||||
overseas_broker._broker._settings = overseas_broker._broker._settings.model_copy(
|
||||
update={"MODE": "live"}
|
||||
)
|
||||
captured, _ = self._setup_cancel_mocks(
|
||||
overseas_broker, {"rt_cd": "0", "msg1": "OK"}
|
||||
)
|
||||
captured, _ = self._setup_cancel_mocks(overseas_broker, {"rt_cd": "0", "msg1": "OK"})
|
||||
|
||||
await overseas_broker.cancel_overseas_order("SEHK", "0700", "ORD002", 10)
|
||||
|
||||
assert "TTTS1003U" in captured
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancel_sets_rvse_cncl_dvsn_cd_02(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_cancel_sets_rvse_cncl_dvsn_cd_02(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""Cancel body must include RVSE_CNCL_DVSN_CD='02' and OVRS_ORD_UNPR='0'."""
|
||||
captured_body: list[dict] = []
|
||||
|
||||
@@ -1005,9 +972,7 @@ class TestCancelOverseasOrder:
|
||||
assert captured_body[0]["ORGN_ODNO"] == "ORD003"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancel_sets_hashkey_header(
|
||||
self, overseas_broker: OverseasBroker
|
||||
) -> None:
|
||||
async def test_cancel_sets_hashkey_header(self, overseas_broker: OverseasBroker) -> None:
|
||||
"""hashkey must be set in the request headers."""
|
||||
captured_headers: list[dict] = []
|
||||
overseas_broker._broker._get_hash_key = AsyncMock(return_value="test_hash") # type: ignore[method-assign]
|
||||
|
||||
@@ -78,9 +78,7 @@ def _gemini_response_json(
|
||||
"rationale": "Near circuit breaker",
|
||||
}
|
||||
]
|
||||
return json.dumps(
|
||||
{"market_outlook": outlook, "global_rules": global_rules, "stocks": stocks}
|
||||
)
|
||||
return json.dumps({"market_outlook": outlook, "global_rules": global_rules, "stocks": stocks})
|
||||
|
||||
|
||||
def _make_planner(
|
||||
@@ -564,8 +562,12 @@ class TestBuildPrompt:
|
||||
def test_prompt_contains_cross_market(self) -> None:
|
||||
planner = _make_planner()
|
||||
cross = CrossMarketContext(
|
||||
market="US", date="2026-02-07", total_pnl=1.5,
|
||||
win_rate=60, index_change_pct=0.8, lessons=["Cut losses early"],
|
||||
market="US",
|
||||
date="2026-02-07",
|
||||
total_pnl=1.5,
|
||||
win_rate=60,
|
||||
index_change_pct=0.8,
|
||||
lessons=["Cut losses early"],
|
||||
)
|
||||
|
||||
prompt = planner._build_prompt("KR", [_candidate()], {}, None, cross)
|
||||
@@ -683,9 +685,7 @@ class TestSmartFallbackPlaybook:
|
||||
)
|
||||
|
||||
def test_momentum_candidate_gets_buy_on_volume(self) -> None:
|
||||
candidates = [
|
||||
_candidate(code="CHOW", signal="momentum", volume_ratio=13.64, rsi=100.0)
|
||||
]
|
||||
candidates = [_candidate(code="CHOW", signal="momentum", volume_ratio=13.64, rsi=100.0)]
|
||||
settings = self._make_settings()
|
||||
|
||||
pb = PreMarketPlanner._smart_fallback_playbook(
|
||||
@@ -707,9 +707,7 @@ class TestSmartFallbackPlaybook:
|
||||
assert sell_sc.condition.price_change_pct_below == -3.0
|
||||
|
||||
def test_oversold_candidate_gets_buy_on_rsi(self) -> None:
|
||||
candidates = [
|
||||
_candidate(code="005930", signal="oversold", rsi=22.0, volume_ratio=3.5)
|
||||
]
|
||||
candidates = [_candidate(code="005930", signal="oversold", rsi=22.0, volume_ratio=3.5)]
|
||||
settings = self._make_settings()
|
||||
|
||||
pb = PreMarketPlanner._smart_fallback_playbook(
|
||||
@@ -776,9 +774,7 @@ class TestSmartFallbackPlaybook:
|
||||
def test_empty_candidates_returns_empty_playbook(self) -> None:
|
||||
settings = self._make_settings()
|
||||
|
||||
pb = PreMarketPlanner._smart_fallback_playbook(
|
||||
date(2026, 2, 17), "US_AMEX", [], settings
|
||||
)
|
||||
pb = PreMarketPlanner._smart_fallback_playbook(date(2026, 2, 17), "US_AMEX", [], settings)
|
||||
|
||||
assert pb.stock_count == 0
|
||||
|
||||
@@ -814,19 +810,14 @@ class TestSmartFallbackPlaybook:
|
||||
planner = _make_planner()
|
||||
planner._gemini.decide = AsyncMock(side_effect=ConnectionError("429 quota exceeded"))
|
||||
# momentum candidate
|
||||
candidates = [
|
||||
_candidate(code="CHOW", signal="momentum", volume_ratio=13.64, rsi=100.0)
|
||||
]
|
||||
candidates = [_candidate(code="CHOW", signal="momentum", volume_ratio=13.64, rsi=100.0)]
|
||||
|
||||
pb = await planner.generate_playbook(
|
||||
"US_AMEX", candidates, today=date(2026, 2, 18)
|
||||
)
|
||||
pb = await planner.generate_playbook("US_AMEX", candidates, today=date(2026, 2, 18))
|
||||
|
||||
# Should NOT be all-SELL defensive; should have BUY for momentum
|
||||
assert pb.stock_count == 1
|
||||
buy_scenarios = [
|
||||
s for s in pb.stock_playbooks[0].scenarios
|
||||
if s.action == ScenarioAction.BUY
|
||||
s for s in pb.stock_playbooks[0].scenarios if s.action == ScenarioAction.BUY
|
||||
]
|
||||
assert len(buy_scenarios) == 1
|
||||
assert buy_scenarios[0].condition.volume_ratio_above == 2.0 # VOL_MULTIPLIER default
|
||||
|
||||
@@ -14,7 +14,7 @@ from src.strategy.models import (
|
||||
StockPlaybook,
|
||||
StockScenario,
|
||||
)
|
||||
from src.strategy.scenario_engine import ScenarioEngine, ScenarioMatch
|
||||
from src.strategy.scenario_engine import ScenarioEngine
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -162,13 +162,15 @@ class TestEvaluateCondition:
|
||||
def test_mixed_invalid_types_no_exception(self, engine: ScenarioEngine) -> None:
|
||||
"""Various invalid types should not raise exceptions."""
|
||||
cond = StockCondition(
|
||||
rsi_below=30.0, volume_ratio_above=2.0,
|
||||
price_above=100, price_change_pct_below=-1.0,
|
||||
rsi_below=30.0,
|
||||
volume_ratio_above=2.0,
|
||||
price_above=100,
|
||||
price_change_pct_below=-1.0,
|
||||
)
|
||||
data = {
|
||||
"rsi": [25], # list
|
||||
"rsi": [25], # list
|
||||
"volume_ratio": "bad", # non-numeric string
|
||||
"current_price": {}, # dict
|
||||
"current_price": {}, # dict
|
||||
"price_change_pct": object(), # arbitrary object
|
||||
}
|
||||
# Should return False (invalid types → None → False), never raise
|
||||
@@ -356,9 +358,7 @@ class TestEvaluate:
|
||||
|
||||
def test_match_details_populated(self, engine: ScenarioEngine) -> None:
|
||||
pb = _playbook(scenarios=[_scenario(rsi_below=30.0, volume_ratio_above=2.0)])
|
||||
result = engine.evaluate(
|
||||
pb, "005930", {"rsi": 25.0, "volume_ratio": 3.0}, {}
|
||||
)
|
||||
result = engine.evaluate(pb, "005930", {"rsi": 25.0, "volume_ratio": 3.0}, {})
|
||||
assert result.match_details.get("rsi") == 25.0
|
||||
assert result.match_details.get("volume_ratio") == 3.0
|
||||
|
||||
@@ -381,7 +381,9 @@ class TestEvaluate:
|
||||
),
|
||||
StockPlaybook(
|
||||
stock_code="MSFT",
|
||||
scenarios=[_scenario(rsi_above=75.0, action=ScenarioAction.SELL, confidence=80)],
|
||||
scenarios=[
|
||||
_scenario(rsi_above=75.0, action=ScenarioAction.SELL, confidence=80)
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
@@ -450,58 +452,42 @@ class TestEvaluate:
|
||||
class TestPositionAwareConditions:
|
||||
"""Tests for unrealized_pnl_pct and holding_days condition fields."""
|
||||
|
||||
def test_evaluate_condition_unrealized_pnl_above_matches(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_unrealized_pnl_above_matches(self, engine: ScenarioEngine) -> None:
|
||||
"""unrealized_pnl_pct_above should match when P&L exceeds threshold."""
|
||||
condition = StockCondition(unrealized_pnl_pct_above=3.0)
|
||||
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": 5.0}) is True
|
||||
|
||||
def test_evaluate_condition_unrealized_pnl_above_no_match(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_unrealized_pnl_above_no_match(self, engine: ScenarioEngine) -> None:
|
||||
"""unrealized_pnl_pct_above should NOT match when P&L is below threshold."""
|
||||
condition = StockCondition(unrealized_pnl_pct_above=3.0)
|
||||
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": 2.0}) is False
|
||||
|
||||
def test_evaluate_condition_unrealized_pnl_below_matches(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_unrealized_pnl_below_matches(self, engine: ScenarioEngine) -> None:
|
||||
"""unrealized_pnl_pct_below should match when P&L is under threshold."""
|
||||
condition = StockCondition(unrealized_pnl_pct_below=-2.0)
|
||||
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": -3.5}) is True
|
||||
|
||||
def test_evaluate_condition_unrealized_pnl_below_no_match(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_unrealized_pnl_below_no_match(self, engine: ScenarioEngine) -> None:
|
||||
"""unrealized_pnl_pct_below should NOT match when P&L is above threshold."""
|
||||
condition = StockCondition(unrealized_pnl_pct_below=-2.0)
|
||||
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": -1.0}) is False
|
||||
|
||||
def test_evaluate_condition_holding_days_above_matches(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_holding_days_above_matches(self, engine: ScenarioEngine) -> None:
|
||||
"""holding_days_above should match when position held longer than threshold."""
|
||||
condition = StockCondition(holding_days_above=5)
|
||||
assert engine.evaluate_condition(condition, {"holding_days": 7}) is True
|
||||
|
||||
def test_evaluate_condition_holding_days_above_no_match(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_holding_days_above_no_match(self, engine: ScenarioEngine) -> None:
|
||||
"""holding_days_above should NOT match when position held shorter."""
|
||||
condition = StockCondition(holding_days_above=5)
|
||||
assert engine.evaluate_condition(condition, {"holding_days": 3}) is False
|
||||
|
||||
def test_evaluate_condition_holding_days_below_matches(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_holding_days_below_matches(self, engine: ScenarioEngine) -> None:
|
||||
"""holding_days_below should match when position held fewer days."""
|
||||
condition = StockCondition(holding_days_below=3)
|
||||
assert engine.evaluate_condition(condition, {"holding_days": 1}) is True
|
||||
|
||||
def test_evaluate_condition_holding_days_below_no_match(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_evaluate_condition_holding_days_below_no_match(self, engine: ScenarioEngine) -> None:
|
||||
"""holding_days_below should NOT match when held more days."""
|
||||
condition = StockCondition(holding_days_below=3)
|
||||
assert engine.evaluate_condition(condition, {"holding_days": 5}) is False
|
||||
@@ -513,33 +499,33 @@ class TestPositionAwareConditions:
|
||||
holding_days_above=5,
|
||||
)
|
||||
# Both met → match
|
||||
assert engine.evaluate_condition(
|
||||
condition,
|
||||
{"unrealized_pnl_pct": 4.5, "holding_days": 7},
|
||||
) is True
|
||||
assert (
|
||||
engine.evaluate_condition(
|
||||
condition,
|
||||
{"unrealized_pnl_pct": 4.5, "holding_days": 7},
|
||||
)
|
||||
is True
|
||||
)
|
||||
# Only pnl met → no match
|
||||
assert engine.evaluate_condition(
|
||||
condition,
|
||||
{"unrealized_pnl_pct": 4.5, "holding_days": 3},
|
||||
) is False
|
||||
assert (
|
||||
engine.evaluate_condition(
|
||||
condition,
|
||||
{"unrealized_pnl_pct": 4.5, "holding_days": 3},
|
||||
)
|
||||
is False
|
||||
)
|
||||
|
||||
def test_missing_unrealized_pnl_does_not_match(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_missing_unrealized_pnl_does_not_match(self, engine: ScenarioEngine) -> None:
|
||||
"""Missing unrealized_pnl_pct key should not match the condition."""
|
||||
condition = StockCondition(unrealized_pnl_pct_above=3.0)
|
||||
assert engine.evaluate_condition(condition, {}) is False
|
||||
|
||||
def test_missing_holding_days_does_not_match(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_missing_holding_days_does_not_match(self, engine: ScenarioEngine) -> None:
|
||||
"""Missing holding_days key should not match the condition."""
|
||||
condition = StockCondition(holding_days_above=5)
|
||||
assert engine.evaluate_condition(condition, {}) is False
|
||||
|
||||
def test_match_details_includes_position_fields(
|
||||
self, engine: ScenarioEngine
|
||||
) -> None:
|
||||
def test_match_details_includes_position_fields(self, engine: ScenarioEngine) -> None:
|
||||
"""match_details should include position fields when condition specifies them."""
|
||||
pb = _playbook(
|
||||
scenarios=[
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from src.analysis.smart_scanner import ScanCandidate, SmartVolatilityScanner
|
||||
from src.analysis.volatility import VolatilityAnalyzer
|
||||
from src.broker.kis_api import KISBroker
|
||||
@@ -200,9 +201,7 @@ class TestSmartVolatilityScanner:
|
||||
assert len(candidates) <= scanner.top_n
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_stock_codes(
|
||||
self, scanner: SmartVolatilityScanner
|
||||
) -> None:
|
||||
async def test_get_stock_codes(self, scanner: SmartVolatilityScanner) -> None:
|
||||
"""Test extraction of stock codes from candidates."""
|
||||
candidates = [
|
||||
ScanCandidate(
|
||||
|
||||
@@ -19,7 +19,6 @@ from src.strategy.models import (
|
||||
StockScenario,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# StockCondition
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -5,7 +5,11 @@ from unittest.mock import AsyncMock, patch
|
||||
import aiohttp
|
||||
import pytest
|
||||
|
||||
from src.notifications.telegram_client import NotificationFilter, NotificationPriority, TelegramClient
|
||||
from src.notifications.telegram_client import (
|
||||
NotificationFilter,
|
||||
NotificationPriority,
|
||||
TelegramClient,
|
||||
)
|
||||
|
||||
|
||||
class TestTelegramClientInit:
|
||||
@@ -13,9 +17,7 @@ class TestTelegramClientInit:
|
||||
|
||||
def test_disabled_via_flag(self) -> None:
|
||||
"""Client disabled via enabled=False flag."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=False
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=False)
|
||||
assert client._enabled is False
|
||||
|
||||
def test_disabled_missing_token(self) -> None:
|
||||
@@ -30,9 +32,7 @@ class TestTelegramClientInit:
|
||||
|
||||
def test_enabled_with_credentials(self) -> None:
|
||||
"""Client enabled when credentials provided."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
assert client._enabled is True
|
||||
|
||||
|
||||
@@ -42,9 +42,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_success(self) -> None:
|
||||
"""send_message returns True on successful send."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -76,9 +74,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_api_error(self) -> None:
|
||||
"""send_message returns False on API error."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 400
|
||||
@@ -93,9 +89,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_with_markdown(self) -> None:
|
||||
"""send_message supports different parse modes."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -128,9 +122,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_trade_execution_format(self) -> None:
|
||||
"""Trade notification has correct format."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -163,9 +155,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_playbook_generated_format(self) -> None:
|
||||
"""Playbook generated notification has expected fields."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -190,9 +180,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_scenario_matched_format(self) -> None:
|
||||
"""Scenario matched notification has expected fields."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -217,9 +205,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_playbook_failed_format(self) -> None:
|
||||
"""Playbook failed notification has expected fields."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -240,9 +226,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_circuit_breaker_priority(self) -> None:
|
||||
"""Circuit breaker uses CRITICAL priority."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -260,9 +244,7 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_error_handling(self) -> None:
|
||||
"""API errors logged but don't crash."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 400
|
||||
@@ -277,25 +259,19 @@ class TestNotificationSending:
|
||||
@pytest.mark.asyncio
|
||||
async def test_timeout_handling(self) -> None:
|
||||
"""Timeouts logged but don't crash."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
with patch(
|
||||
"aiohttp.ClientSession.post",
|
||||
side_effect=aiohttp.ClientError("Connection timeout"),
|
||||
):
|
||||
# Should not raise exception
|
||||
await client.notify_error(
|
||||
error_type="Test Error", error_msg="Test", context="test"
|
||||
)
|
||||
await client.notify_error(error_type="Test Error", error_msg="Test", context="test")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_session_management(self) -> None:
|
||||
"""Session created and reused correctly."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
# Session should be None initially
|
||||
assert client._session is None
|
||||
@@ -324,9 +300,7 @@ class TestRateLimiting:
|
||||
"""Rate limiter delays rapid requests."""
|
||||
import time
|
||||
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True, rate_limit=2.0
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True, rate_limit=2.0)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -353,9 +327,7 @@ class TestMessagePriorities:
|
||||
@pytest.mark.asyncio
|
||||
async def test_low_priority_uses_info_emoji(self) -> None:
|
||||
"""LOW priority uses ℹ️ emoji."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -371,9 +343,7 @@ class TestMessagePriorities:
|
||||
@pytest.mark.asyncio
|
||||
async def test_critical_priority_uses_alarm_emoji(self) -> None:
|
||||
"""CRITICAL priority uses 🚨 emoji."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -389,9 +359,7 @@ class TestMessagePriorities:
|
||||
@pytest.mark.asyncio
|
||||
async def test_playbook_generated_priority(self) -> None:
|
||||
"""Playbook generated uses MEDIUM priority emoji."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -412,9 +380,7 @@ class TestMessagePriorities:
|
||||
@pytest.mark.asyncio
|
||||
async def test_playbook_failed_priority(self) -> None:
|
||||
"""Playbook failed uses HIGH priority emoji."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -433,9 +399,7 @@ class TestMessagePriorities:
|
||||
@pytest.mark.asyncio
|
||||
async def test_scenario_matched_priority(self) -> None:
|
||||
"""Scenario matched uses HIGH priority emoji."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_resp = AsyncMock()
|
||||
mock_resp.status = 200
|
||||
@@ -460,9 +424,7 @@ class TestClientCleanup:
|
||||
@pytest.mark.asyncio
|
||||
async def test_close_closes_session(self) -> None:
|
||||
"""close() closes the HTTP session."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
mock_session = AsyncMock()
|
||||
mock_session.closed = False
|
||||
@@ -475,9 +437,7 @@ class TestClientCleanup:
|
||||
@pytest.mark.asyncio
|
||||
async def test_close_handles_no_session(self) -> None:
|
||||
"""close() handles None session gracefully."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True
|
||||
)
|
||||
client = TelegramClient(bot_token="123:abc", chat_id="456", enabled=True)
|
||||
|
||||
# Should not raise exception
|
||||
await client.close()
|
||||
@@ -535,8 +495,12 @@ class TestNotificationFilter:
|
||||
)
|
||||
with patch("aiohttp.ClientSession.post") as mock_post:
|
||||
await client.notify_trade_execution(
|
||||
stock_code="005930", market="KR", action="BUY",
|
||||
quantity=10, price=70000.0, confidence=85.0
|
||||
stock_code="005930",
|
||||
market="KR",
|
||||
action="BUY",
|
||||
quantity=10,
|
||||
price=70000.0,
|
||||
confidence=85.0,
|
||||
)
|
||||
mock_post.assert_not_called()
|
||||
|
||||
@@ -556,8 +520,13 @@ class TestNotificationFilter:
|
||||
async def test_circuit_breaker_always_sends_regardless_of_filter(self) -> None:
|
||||
"""notify_circuit_breaker always sends (no filter flag)."""
|
||||
nf = NotificationFilter(
|
||||
trades=False, market_open_close=False, fat_finger=False,
|
||||
system_events=False, playbook=False, scenario_match=False, errors=False,
|
||||
trades=False,
|
||||
market_open_close=False,
|
||||
fat_finger=False,
|
||||
system_events=False,
|
||||
playbook=False,
|
||||
scenario_match=False,
|
||||
errors=False,
|
||||
)
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True, notification_filter=nf
|
||||
@@ -617,7 +586,7 @@ class TestNotificationFilter:
|
||||
nf = NotificationFilter()
|
||||
assert nf.set_flag("unknown_key", False) is False
|
||||
|
||||
def test_as_dict_keys_match_KEYS(self) -> None:
|
||||
def test_as_dict_keys_match_keys(self) -> None:
|
||||
"""as_dict() returns every key defined in KEYS."""
|
||||
nf = NotificationFilter()
|
||||
d = nf.as_dict()
|
||||
@@ -640,10 +609,17 @@ class TestNotificationFilter:
|
||||
def test_set_notification_all_on(self) -> None:
|
||||
"""set_notification('all', True) enables every filter flag."""
|
||||
client = TelegramClient(
|
||||
bot_token="123:abc", chat_id="456", enabled=True,
|
||||
bot_token="123:abc",
|
||||
chat_id="456",
|
||||
enabled=True,
|
||||
notification_filter=NotificationFilter(
|
||||
trades=False, market_open_close=False, scenario_match=False,
|
||||
fat_finger=False, system_events=False, playbook=False, errors=False,
|
||||
trades=False,
|
||||
market_open_close=False,
|
||||
scenario_match=False,
|
||||
fat_finger=False,
|
||||
system_events=False,
|
||||
playbook=False,
|
||||
errors=False,
|
||||
),
|
||||
)
|
||||
assert client.set_notification("all", True) is True
|
||||
|
||||
@@ -357,8 +357,7 @@ class TestTradingControlCommands:
|
||||
|
||||
pause_event.set()
|
||||
await client.send_message(
|
||||
"<b>▶️ Trading Resumed</b>\n\n"
|
||||
"Trading operations have been restarted."
|
||||
"<b>▶️ Trading Resumed</b>\n\nTrading operations have been restarted."
|
||||
)
|
||||
|
||||
handler.register_command("resume", mock_resume)
|
||||
@@ -526,9 +525,7 @@ class TestStatusCommands:
|
||||
|
||||
async def mock_status_error() -> None:
|
||||
"""Mock /status handler with error."""
|
||||
await client.send_message(
|
||||
"<b>⚠️ Error</b>\n\nFailed to retrieve trading status."
|
||||
)
|
||||
await client.send_message("<b>⚠️ Error</b>\n\nFailed to retrieve trading status.")
|
||||
|
||||
handler.register_command("status", mock_status_error)
|
||||
|
||||
@@ -603,10 +600,7 @@ class TestStatusCommands:
|
||||
|
||||
async def mock_positions_empty() -> None:
|
||||
"""Mock /positions handler with no positions."""
|
||||
message = (
|
||||
"<b>💼 Account Summary</b>\n\n"
|
||||
"No balance information available."
|
||||
)
|
||||
message = "<b>💼 Account Summary</b>\n\nNo balance information available."
|
||||
await client.send_message(message)
|
||||
|
||||
handler.register_command("positions", mock_positions_empty)
|
||||
@@ -639,9 +633,7 @@ class TestStatusCommands:
|
||||
|
||||
async def mock_positions_error() -> None:
|
||||
"""Mock /positions handler with error."""
|
||||
await client.send_message(
|
||||
"<b>⚠️ Error</b>\n\nFailed to retrieve positions."
|
||||
)
|
||||
await client.send_message("<b>⚠️ Error</b>\n\nFailed to retrieve positions.")
|
||||
|
||||
handler.register_command("positions", mock_positions_error)
|
||||
|
||||
|
||||
@@ -70,7 +70,9 @@ def test_load_changed_files_with_range_uses_git_diff(monkeypatch) -> None:
|
||||
assert check is True
|
||||
assert capture_output is True
|
||||
assert text is True
|
||||
return SimpleNamespace(stdout="docs/ouroboros/85_loss_recovery_action_plan.md\nsrc/main.py\n")
|
||||
return SimpleNamespace(
|
||||
stdout="docs/ouroboros/85_loss_recovery_action_plan.md\nsrc/main.py\n"
|
||||
)
|
||||
|
||||
monkeypatch.setattr(module.subprocess, "run", fake_run)
|
||||
changed = module.load_changed_files(["abc...def"], errors)
|
||||
|
||||
@@ -80,9 +80,7 @@ class TestVolatilityAnalyzer:
|
||||
# ATR should be roughly the average true range
|
||||
assert 3.0 <= atr <= 6.0
|
||||
|
||||
def test_calculate_atr_insufficient_data(
|
||||
self, volatility_analyzer: VolatilityAnalyzer
|
||||
) -> None:
|
||||
def test_calculate_atr_insufficient_data(self, volatility_analyzer: VolatilityAnalyzer) -> None:
|
||||
"""Test ATR with insufficient data returns 0."""
|
||||
high_prices = [110.0, 112.0]
|
||||
low_prices = [105.0, 107.0]
|
||||
@@ -120,17 +118,13 @@ class TestVolatilityAnalyzer:
|
||||
surge = volatility_analyzer.calculate_volume_surge(1000.0, 0.0)
|
||||
assert surge == 1.0
|
||||
|
||||
def test_calculate_pv_divergence_bullish(
|
||||
self, volatility_analyzer: VolatilityAnalyzer
|
||||
) -> None:
|
||||
def test_calculate_pv_divergence_bullish(self, volatility_analyzer: VolatilityAnalyzer) -> None:
|
||||
"""Test bullish price-volume divergence."""
|
||||
# Price up + Volume up = bullish
|
||||
divergence = volatility_analyzer.calculate_pv_divergence(5.0, 2.0)
|
||||
assert divergence > 0.0
|
||||
|
||||
def test_calculate_pv_divergence_bearish(
|
||||
self, volatility_analyzer: VolatilityAnalyzer
|
||||
) -> None:
|
||||
def test_calculate_pv_divergence_bearish(self, volatility_analyzer: VolatilityAnalyzer) -> None:
|
||||
"""Test bearish price-volume divergence."""
|
||||
# Price up + Volume down = bearish divergence
|
||||
divergence = volatility_analyzer.calculate_pv_divergence(5.0, 0.5)
|
||||
@@ -144,9 +138,7 @@ class TestVolatilityAnalyzer:
|
||||
divergence = volatility_analyzer.calculate_pv_divergence(-5.0, 2.0)
|
||||
assert divergence < 0.0
|
||||
|
||||
def test_calculate_momentum_score(
|
||||
self, volatility_analyzer: VolatilityAnalyzer
|
||||
) -> None:
|
||||
def test_calculate_momentum_score(self, volatility_analyzer: VolatilityAnalyzer) -> None:
|
||||
"""Test momentum score calculation."""
|
||||
score = volatility_analyzer.calculate_momentum_score(
|
||||
price_change_1m=5.0,
|
||||
@@ -500,9 +492,7 @@ class TestMarketScanner:
|
||||
# Should keep all current stocks since they're all in top movers
|
||||
assert set(updated) == set(current_watchlist)
|
||||
|
||||
def test_get_updated_watchlist_max_replacements(
|
||||
self, scanner: MarketScanner
|
||||
) -> None:
|
||||
def test_get_updated_watchlist_max_replacements(self, scanner: MarketScanner) -> None:
|
||||
"""Test that max_replacements limit is respected."""
|
||||
current_watchlist = ["000660", "035420", "005490"]
|
||||
|
||||
@@ -556,8 +546,6 @@ class TestMarketScanner:
|
||||
active_count = 0
|
||||
peak_count = 0
|
||||
|
||||
original_scan = scanner.scan_stock
|
||||
|
||||
async def tracking_scan(code: str, market: Any) -> VolatilityMetrics:
|
||||
nonlocal active_count, peak_count
|
||||
active_count += 1
|
||||
|
||||
Reference in New Issue
Block a user