import pytest import time_machine import io from datetime import datetime, timedelta from slic.utils.run_later import run_at, run_in, run_later, today, tomorrow, yesterday import pytest from slic.utils.run_later import MatMulTrick from datetime import datetime # Test MatMulTrick def original_func(*args, **kwargs): return ("called", args, kwargs) def test_init_and_repr(): wrapped = MatMulTrick(original_func) assert wrapped.func is original_func assert repr(wrapped) == repr(original_func) def test_call(): wrapped = MatMulTrick(original_func) result = wrapped(1, 2, a=3) assert result == ("called", (1, 2), {'a': 3}) def test_matmul_with_string(): wrapped = MatMulTrick(original_func) result = wrapped @ "6:30" assert result == ("called", (6, 30), {}) def test_matmul_with_list(): wrapped = MatMulTrick(original_func) result = wrapped @ [6, 30] assert result == ("called", (6, 30), {}) def test_matmul_with_single_int(): wrapped = MatMulTrick(original_func) result = wrapped @ 7 assert result == ("called", (7,), {}) # Test run functions def fexample(label): print(f"Triggered: {label}") def test_run_at_future_triggered_and_logs(capsys): with time_machine.travel(datetime(2025, 1, 1, 12, 0, 0), tick=True): run_at(datetime(2025, 1, 1, 12, 0, 0) + timedelta(seconds=0.2), fexample, "A") captured = capsys.readouterr().out assert "will run at" in captured assert "this will be in" in captured assert "Triggered: A" in captured def test_run_at_already_past(capsys): with time_machine.travel(datetime(2025, 1, 1, 12, 0, 0), tick=True): run_at(datetime(2025, 1, 1, 11, 59, 59), fexample, "B") captured = capsys.readouterr().out assert "it is already past" in captured assert "ago" in captured assert "Triggered: B" not in captured def test_run_in_future_triggered_and_logs(capsys): with time_machine.travel(datetime(2025, 1, 1, 12, 0, 0), tick=True): run_in(timedelta(seconds=0.2), fexample, "IN") captured = capsys.readouterr().out assert "will run at" in captured assert "this will be in" in captured assert "Triggered: IN" in captured def test_run_in_past(capsys): with time_machine.travel(datetime(2025, 1, 1, 12, 0, 0), tick=True): run_in(-timedelta(seconds=2), fexample, "IN PAST") captured = capsys.readouterr().out assert "it is already past" in captured assert "Triggered: IN PAST" not in captured from slic.utils.run_later import run_later def test_run_later_with_seconds(capsys): with time_machine.travel(datetime(2025, 1, 1, 14, 0, 0), tick=True): run_later(0.2, fexample, "LATER") captured = capsys.readouterr().out assert "will run at" in captured assert "Triggered: LATER" in captured def test_run_later_with_past_datetime(capsys): with time_machine.travel(datetime(2025, 1, 1, 14, 0, 0), tick=True): past_time = datetime(2025, 1, 1, 13, 59, 59) run_later(past_time, fexample, "TOO LATE") captured = capsys.readouterr().out assert "it is already past" in captured assert "Triggered: TOO LATE" not in captured def test_run_at_tqdm_multiple_updates(monkeypatch, capsys): def fexample(): print("Function done!") when = datetime.now() + timedelta(seconds=0.2) run_at(when, fexample) captured = capsys.readouterr() assert "0%| |" in captured.err assert "50%|█████ |" in captured.err assert "100%|██████████|" in captured.err assert "Function done!" in captured.out # Test today/yesterday and tomorrow functions def test_today_basic(): with time_machine.travel("2025-01-01 10:00:00", tick=False): val = today(12, 30) assert val == datetime(2025, 1, 1, 12, 30) def test_tomorrow_basic(): with time_machine.travel("2025-01-01 10:00:00", tick=False): val = tomorrow(12, 30) assert val == datetime(2025, 1, 2, 12, 30) def test_yesterday_basic(): with time_machine.travel("2025-01-01 10:00:00", tick=False): val = yesterday(12, 30) assert val == datetime(2024, 12, 31, 12, 30) def test_today_matmul_string(): with time_machine.travel("2025-01-01 00:00:00", tick=False): val = today @ "15:45" assert val == datetime(2025, 1, 1, 15, 45) def test_tomorrow_matmul_list(): with time_machine.travel("2025-01-01 00:00:00", tick=False): val = tomorrow @ [6, 0] assert val == datetime(2025, 1, 2, 6, 0) def test_yesterday_matmul_single(): with time_machine.travel("2025-01-01 00:00:00", tick=False): val = yesterday @ 7 assert val == datetime(2024, 12, 31, 7, 0)