"""봇 감지 이력 기록 CRUD 테스트 (실 lps_db).""" import pytest_asyncio from sqlalchemy import text from crud.bot_detection import BotDetectionLog @pytest_asyncio.fixture async def bd(db_engine): async with db_engine.begin() as conn: await conn.execute(text("TRUNCATE bot_detection")) return BotDetectionLog() async def test_record_persists_event(bd, db_engine): await bd.record({ "source": "coupang", "query": "맥심 커피", "ip_request_no": 7, "proxy_port": 10003, "elapsed_sec": 42, "marker": "/akam/", "headless": False, "html_len": 2604, }) async with db_engine.begin() as conn: row = (await conn.execute(text( "SELECT source, query, ip_request_no, proxy_port, marker FROM bot_detection" ))).first() assert row.source == "coupang" and row.query == "맥심 커피" assert row.ip_request_no == 7 and row.proxy_port == 10003 and row.marker == "/akam/" async def test_record_tolerates_missing_fields(bd, db_engine): await bd.record({"source": "coupang"}) # 나머지는 None 허용 async with db_engine.begin() as conn: cnt = (await conn.execute(text("SELECT count(*) FROM bot_detection"))).scalar() assert cnt == 1