from datetime import datetime import pytest from sqlalchemy import create_engine, text from app.collect.store import replace_batch, table_name def test_table_name_replaces_special_characters(): assert table_name("appdbv3.a_xxx") == "appdbv3_a_xxx" assert table_name("a b/c") == "a_b_c" with pytest.raises(ValueError): table_name("app_setting") def test_same_task_and_window_replaces_rows(): engine = create_engine("sqlite://") columns = [{"columnname": "starttime"}, {"columnname": "city"}] start = datetime(2026, 10, 1) end = datetime(2026, 10, 31, 23, 59, 59) schedule = {"mode": "monthly", "monthDay": 1, "time": "08:00"} with engine.begin() as connection: replace_batch( connection, "appdbv3.a_demo", columns, [{"starttime": "2026-10-01", "city": "江门"}], task_id=7, task_name="第一次", time_mode="prev_month", time_count=1, window_start=start, window_end=end, city="江门", schedule=schedule, ) replace_batch( connection, "appdbv3.a_demo", columns, [{"starttime": "2026-10-02", "city": "广州"}], task_id=7, task_name="改名后", time_mode="prev_month", time_count=1, window_start=start, window_end=end, city="江门", schedule=schedule, ) replace_batch( connection, "appdbv3.a_demo", columns, [{"starttime": "2026-10-01", "city": "江门"}], task_id=8, task_name="另一任务", time_mode="prev_month", time_count=1, window_start=start, window_end=end, city="", schedule={"mode": "weekly", "weekday": 1, "time": "09:00"}, ) with engine.connect() as connection: rows = connection.execute(text('SELECT collect_task_id, collect_task_name, city FROM "appdbv3_a_demo" ORDER BY collect_task_id')).fetchall() assert [(row[0], row[1], row[2]) for row in rows] == [(7, "改名后", "广州"), (8, "另一任务", "江门")]