34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def test_db(tmp_path, monkeypatch):
|
|
"""把数据库指向临时目录,避免污染真实 data/monitor.db。"""
|
|
from app import db
|
|
|
|
monkeypatch.setattr(db, "DB_PATH", tmp_path / "test.db")
|
|
monkeypatch.setattr(db, "DATA_DIR", tmp_path)
|
|
db.init_db()
|
|
return db
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_platform(test_db):
|
|
"""插入一个示例平台,返回 dict。"""
|
|
from app import db
|
|
|
|
with db.get_conn() as conn:
|
|
cur = conn.execute(
|
|
"""INSERT INTO platforms (name, currency, icon, method, url, headers, body,
|
|
balance_path, interval_seconds, retry_count, timeout_seconds, enabled, note)
|
|
VALUES ('OpenAI', 'USD', 'OpenAI', 'GET', 'https://x.test/api?key={{apiKey}}',
|
|
'{"Authorization": "Bearer {{apiKey}}"}', '', 'data.balance', 60, 2, 10, 1, '')"""
|
|
)
|
|
pid = cur.lastrowid
|
|
return dict((test_db.get_conn().execute("SELECT * FROM platforms WHERE id=?", (pid,)).fetchone()))
|