80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
import sqlite3
|
|
from pathlib import Path
|
|
|
|
from llm_trade_bot.database import Database
|
|
|
|
|
|
async def test_notes_and_admin_deduplication(tmp_path: Path):
|
|
database = Database(tmp_path / "state.db")
|
|
await database.open()
|
|
try:
|
|
await database.add_note("first")
|
|
await database.add_note("second")
|
|
assert await database.recent_notes(10, 100) == ["first", "second"]
|
|
first_id, first_queued = await database.add_admin_request("Need a new data feed")
|
|
second_id, second_queued = await database.add_admin_request("Need a new data feed")
|
|
assert first_queued is True
|
|
assert second_queued is False
|
|
assert second_id == first_id
|
|
outbox = await database.pending_outbox()
|
|
assert len(outbox) == 1
|
|
assert outbox[0].kind == "ask_admin"
|
|
finally:
|
|
await database.close()
|
|
|
|
|
|
async def test_run_lifecycle(tmp_path: Path):
|
|
database = Database(tmp_path / "state.db")
|
|
await database.open()
|
|
try:
|
|
run_id = await database.start_run("test")
|
|
await database.add_event("tool_started", {"name": "note"}, run_id)
|
|
await database.finish_run(run_id, status="completed", summary="idle")
|
|
last = await database.last_run()
|
|
assert last is not None
|
|
assert last["status"] == "completed"
|
|
assert last["summary"] == "idle"
|
|
finally:
|
|
await database.close()
|
|
|
|
|
|
async def test_managed_orders_migrate_legacy_rows_to_bybit(tmp_path: Path):
|
|
path = tmp_path / "legacy.db"
|
|
connection = sqlite3.connect(path)
|
|
connection.execute(
|
|
"""
|
|
CREATE TABLE managed_orders (
|
|
order_link_id TEXT PRIMARY KEY, order_id TEXT, category TEXT NOT NULL,
|
|
symbol TEXT NOT NULL, side TEXT NOT NULL, status TEXT NOT NULL,
|
|
expires_at TEXT, trailing_distance TEXT, raw_json TEXT NOT NULL, updated_at TEXT NOT NULL
|
|
)
|
|
"""
|
|
)
|
|
connection.execute(
|
|
"""
|
|
INSERT INTO managed_orders VALUES (
|
|
'ltb-old', 'old-1', 'linear', 'BTCUSDT', 'Buy', 'Created',
|
|
'2000-01-01T00:00:00+00:00', NULL, '{}', '2000-01-01T00:00:00+00:00'
|
|
)
|
|
"""
|
|
)
|
|
connection.commit()
|
|
connection.close()
|
|
|
|
database = Database(path)
|
|
await database.open()
|
|
try:
|
|
rows = await database.expired_managed_orders(provider="bybit")
|
|
assert rows == [
|
|
{
|
|
"order_link_id": "ltb-old",
|
|
"order_id": "old-1",
|
|
"provider": "bybit",
|
|
"category": "linear",
|
|
"symbol": "BTCUSDT",
|
|
}
|
|
]
|
|
assert await database.expired_managed_orders(provider="binance") == []
|
|
finally:
|
|
await database.close()
|