import pytest from datetime import datetime from cpl.core.time.cron import Cron def test_next_returns_datetime(): cron = Cron("* * * * *") result = cron.next() assert isinstance(result, datetime) def test_next_is_in_the_future(): cron = Cron("* * * * *") result = cron.next() assert result > datetime.now() def test_next_called_multiple_times_is_monotonic(): cron = Cron("* * * * *") results = [cron.next() for _ in range(5)] for i in range(1, len(results)): assert results[i] > results[i - 1] def test_every_minute_interval(): start = datetime(2024, 1, 1, 12, 0, 0) cron = Cron("* * * * *", start_time=start) first = cron.next() second = cron.next() diff = (second - first).total_seconds() assert diff == 60 def test_hourly_interval(): start = datetime(2024, 1, 1, 12, 0, 0) cron = Cron("0 * * * *", start_time=start) first = cron.next() second = cron.next() diff = (second - first).total_seconds() assert diff == 3600 def test_daily_midnight(): start = datetime(2024, 1, 1, 0, 0, 0) cron = Cron("0 0 * * *", start_time=start) first = cron.next() assert first.hour == 0 assert first.minute == 0 def test_custom_start_time(): start = datetime(2024, 6, 15, 10, 30, 0) cron = Cron("*/5 * * * *", start_time=start) result = cron.next() assert result > start def test_invalid_cron_expression(): with pytest.raises(Exception): Cron("invalid expression")