51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
"""Offline smoke for scheduler logic."""
|
|
import os
|
|
|
|
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://test:test@localhost:5432/test")
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from app.services.scheduler import ZOMBIE_THRESHOLD_HOURS, compute_next_run_at
|
|
|
|
|
|
def test_compute_next_run_in_normal_window() -> None:
|
|
now = datetime(2026, 5, 23, 12, 0, tzinfo=timezone.utc)
|
|
next_at = compute_next_run_at(2, 5, now=now)
|
|
assert next_at.date() == datetime(2026, 5, 24).date()
|
|
assert 2 <= next_at.hour <= 4 # window [2, 5) UTC
|
|
|
|
|
|
def test_compute_next_run_cross_midnight() -> None:
|
|
now = datetime(2026, 5, 23, 12, 0, tzinfo=timezone.utc)
|
|
# Window 22..3 = 22:00-23:59 + 00:00-02:59
|
|
next_at = compute_next_run_at(22, 3, now=now)
|
|
# Должен попасть либо в [22,23] либо в [0,3) range
|
|
hour = next_at.hour
|
|
assert hour >= 22 or hour < 3
|
|
|
|
|
|
def test_compute_next_run_window_1_hour() -> None:
|
|
now = datetime(2026, 5, 23, 12, 0, tzinfo=timezone.utc)
|
|
# Минимальное окно [3, 4) → ровно 3-й час
|
|
next_at = compute_next_run_at(3, 4, now=now)
|
|
assert next_at.hour == 3
|
|
|
|
|
|
def test_zombie_threshold_constant() -> None:
|
|
assert ZOMBIE_THRESHOLD_HOURS > 0
|
|
|
|
|
|
def test_compute_next_run_is_future() -> None:
|
|
"""next_run_at всегда в будущем (завтра) — не в прошлом."""
|
|
now = datetime(2026, 5, 23, 3, 30, tzinfo=timezone.utc)
|
|
next_at = compute_next_run_at(2, 5, now=now)
|
|
assert next_at > now
|
|
|
|
|
|
def test_compute_next_run_timezone_aware() -> None:
|
|
"""Результат всегда timezone-aware (UTC)."""
|
|
now = datetime(2026, 5, 23, 12, 0, tzinfo=timezone.utc)
|
|
next_at = compute_next_run_at(2, 5, now=now)
|
|
assert next_at.tzinfo is not None
|