77 lines
3.0 KiB
Python
77 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from app.timeutil import beijing_day_bounds, push_date_from_ts
|
|
|
|
|
|
def test_shorts_quota_counts_scheduled(temp_db):
|
|
ch = temp_db.create_shorts_channel(name="Q", daily_min=5, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
temp_db.create_shorts_job(
|
|
session_id="",
|
|
room_id="r1",
|
|
channel_pk=ch["id"],
|
|
push_date=day,
|
|
)
|
|
job = temp_db.create_shorts_job(
|
|
session_id="",
|
|
room_id="r2",
|
|
channel_pk=ch["id"],
|
|
push_date=day,
|
|
)
|
|
temp_db.update_shorts_job(job["id"], status="scheduled", upload_at=time.time() + 3600)
|
|
assert temp_db.shorts_channel_upload_count(ch["id"], day) == 2
|
|
assert temp_db.shorts_channel_uploaded_count(ch["id"], day) == 0
|
|
|
|
|
|
def test_pick_shorts_channel_prefers_under_min(temp_db):
|
|
ch1 = temp_db.create_shorts_channel(name="A", daily_min=5, daily_max=9, enabled=True)
|
|
ch2 = temp_db.create_shorts_channel(name="B", daily_min=5, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
for i in range(5):
|
|
job = temp_db.create_shorts_job(session_id="", room_id=f"a{i}", channel_pk=ch1["id"], push_date=day)
|
|
temp_db.update_shorts_job(job["id"], status="uploaded", finished_at=time.time())
|
|
picked = temp_db.pick_shorts_channel(day)
|
|
assert picked is not None
|
|
assert picked["id"] == ch2["id"]
|
|
|
|
|
|
def test_quota_gap_and_summary(temp_db):
|
|
ch = temp_db.create_shorts_channel(name="G", daily_min=5, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
for i in range(3):
|
|
job = temp_db.create_shorts_job(session_id="", room_id=f"g{i}", channel_pk=ch["id"], push_date=day)
|
|
temp_db.update_shorts_job(job["id"], status="uploaded", finished_at=time.time())
|
|
summary = temp_db.shorts_channels_quota_summary(day)
|
|
assert len(summary) == 1
|
|
assert summary[0]["used"] == 3
|
|
assert summary[0]["uploaded"] == 3
|
|
assert summary[0]["gap"] == 6
|
|
assert summary[0]["min_met"] is False
|
|
assert temp_db.shorts_total_quota_gap(day) == 6
|
|
|
|
|
|
def test_quota_progress_ignores_pending(temp_db):
|
|
ch = temp_db.create_shorts_channel(name="P", daily_min=9, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
for i in range(2):
|
|
job = temp_db.create_shorts_job(session_id="", room_id=f"up{i}", channel_pk=ch["id"], push_date=day)
|
|
temp_db.update_shorts_job(job["id"], status="uploaded", finished_at=time.time())
|
|
for i in range(7):
|
|
temp_db.create_shorts_job(session_id="", room_id=f"pe{i}", channel_pk=ch["id"], push_date=day)
|
|
summary = temp_db.shorts_channels_quota_summary(day)[0]
|
|
assert summary["uploaded"] == 2
|
|
assert summary["used"] == 2
|
|
assert summary["pipeline"] == 9
|
|
assert summary["gap"] == 7
|
|
assert summary["max_met"] is False
|
|
assert summary["pipeline_full"] is True
|
|
|
|
|
|
def test_beijing_day_bounds():
|
|
day = push_date_from_ts(time.time())
|
|
start, end = beijing_day_bounds(day)
|
|
assert end - start == 86400
|