55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import pytest
|
|
import time_machine
|
|
from datetime import datetime, timedelta
|
|
from slic.utils.run_later import run_at, run_in, run_later, today, tomorrow, yesterday
|
|
|
|
# Dummy progress bar replacement
|
|
class DummyBar:
|
|
def __enter__(self): return self
|
|
def __exit__(self, *a): pass
|
|
def set(self, val): pass
|
|
|
|
@pytest.fixture
|
|
def patch_time(monkeypatch):
|
|
monkeypatch.setattr("slic.utils.run_later.sleep", lambda x: None)
|
|
monkeypatch.setattr("slic.utils.run_later.tqdm_mod", lambda *a, **k: DummyBar())
|
|
|
|
def test_run_at(patch_time):
|
|
triggered = []
|
|
def fexample(label): triggered.append(label)
|
|
|
|
with time_machine.travel("2025-01-01 06:00:00", tick=False) as traveller:
|
|
# Schedule for 6:30
|
|
run_at(today(6, 30), fexample, "now OK")
|
|
assert not triggered # Shouldn't have run yet
|
|
|
|
# Move to 6:30:01
|
|
traveller.move_to("2025-01-01 06:30:01")
|
|
run_at(today(6, 30), fexample, "now OK") # Should trigger
|
|
assert "now OK" in triggered
|
|
|
|
def test_run_in(patch_time):
|
|
triggered = []
|
|
def fexample(label): triggered.append(label)
|
|
|
|
with time_machine.travel("2025-01-01 12:00:00", tick=False):
|
|
run_in(timedelta(seconds=60), fexample, "now OK")
|
|
assert not triggered # Not triggered yet
|
|
|
|
# Fast-forward time
|
|
with time_machine.travel("2025-01-01 12:01:01", tick=False):
|
|
run_in(timedelta(seconds=60), fexample, "now OK") # Should trigger
|
|
assert "now OK" in triggered
|
|
|
|
def test_run_later(patch_time):
|
|
triggered = []
|
|
def fexample(label): triggered.append(label)
|
|
|
|
with time_machine.travel("2025-01-01 14:00:00", tick=False):
|
|
run_later(45, fexample, "now OK")
|
|
assert not triggered # Not triggered yet
|
|
|
|
# Fast-forward
|
|
with time_machine.travel("2025-01-01 14:01:00", tick=False):
|
|
run_later(45, fexample, "now OK") # Should trigger
|
|
assert "now OK" in triggered |