import os, tempfile def _fresh_db(monkeypatch): tmp = tempfile.mkdtemp() path = os.path.join(tmp, "lotto.db") from app import db monkeypatch.setattr(db, "DB_PATH", path) db.init_db() return db def test_backtest_tables_exist(monkeypatch): db = _fresh_db(monkeypatch) with db._conn() as conn: tables = {r["name"] for r in conn.execute( "SELECT name FROM sqlite_master WHERE type='table'").fetchall()} assert "backtest_runs" in tables assert "winner_calibration" in tables def test_backtest_runs_unique(monkeypatch): db = _fresh_db(monkeypatch) db.save_backtest_run(draw_no=100, strategy="random_null", weight_label="-", weight_json=None, trial_id=None, n_tickets=10, hist={"m3":1,"m4":0,"m5":0,"m6":0,"bonus_hits":0}, best_match=3, avg_meta_score=0.5) db.save_backtest_run(draw_no=100, strategy="random_null", weight_label="-", weight_json=None, trial_id=None, n_tickets=10, hist={"m3":2,"m4":0,"m5":0,"m6":0,"bonus_hits":0}, best_match=3, avg_meta_score=0.6) # 멱등 upsert rows = db.get_backtest_runs(draw_no=100) assert len(rows) == 1 assert rows[0]["m3"] == 2 # 마지막 값으로 갱신