Files
cron-parser-cli/tests/test_scheduler.py
7000pctAUTO e07b052143
Some checks failed
CI / test (push) Has been cancelled
Add test files
2026-02-01 15:10:14 +00:00

92 lines
3.3 KiB
Python

"""Tests for cron execution scheduling."""
import pytest
from datetime import datetime, timedelta
from cronparse.scheduler import get_next_executions, format_timeline, format_relative_time
class TestGetNextExecutions:
"""Tests for get_next_executions function."""
def test_get_next_every_minute(self):
"""Test getting next executions for every minute."""
executions = get_next_executions("* * * * *", 3)
assert len(executions) == 3
for exec_time in executions:
assert isinstance(exec_time, datetime)
def test_get_next_daily_at_nine_am(self):
"""Test getting next executions for daily at 9 AM."""
executions = get_next_executions("0 9 * * *", 5)
assert len(executions) == 5
for exec_time in executions:
assert exec_time.hour == 9
assert exec_time.minute == 0
def test_get_next_every_five_minutes(self):
"""Test getting next executions for every 5 minutes."""
executions = get_next_executions("*/5 * * * *", 3)
assert len(executions) == 3
for exec_time in executions:
assert exec_time.minute % 5 == 0
def test_get_next_count_parameter(self):
"""Test that count parameter controls number of results."""
for count in [1, 5, 10]:
executions = get_next_executions("* * * * *", count)
assert len(executions) == count
class TestFormatTimeline:
"""Tests for format_timeline function."""
def test_empty_executions(self):
"""Test formatting empty executions list."""
result = format_timeline([])
assert "No executions" in result
def test_timeline_contains_executions(self):
"""Test that timeline contains execution info."""
base = datetime.now()
executions = [base + timedelta(hours=i) for i in range(3)]
result = format_timeline(executions)
assert "Timeline:" in result
assert "#1" in result
assert "#2" in result
assert "#3" in result
def test_timeline_contains_relative_time(self):
"""Test that timeline contains execution info."""
base = datetime.now()
executions = [base + timedelta(hours=1)]
result = format_timeline(executions)
assert "Timeline:" in result
class TestFormatRelativeTime:
"""Tests for format_relative_time function."""
def test_future_time_in_days(self):
"""Test formatting future time in days - verify 'days ago' output for past times."""
past = datetime.now() - timedelta(days=5)
result = format_relative_time(past)
assert "day" in result.lower()
def test_future_time_in_hours(self):
"""Test formatting future time in hours."""
future = datetime.now() + timedelta(hours=5)
result = format_relative_time(future)
assert "hour" in result.lower()
def test_future_time_in_minutes(self):
"""Test formatting future time in minutes."""
future = datetime.now() + timedelta(minutes=30)
result = format_relative_time(future)
assert "minute" in result.lower() or "soon" in result.lower()
def test_past_time(self):
"""Test formatting past time."""
past = datetime.now() - timedelta(days=2)
result = format_relative_time(past)
assert "2 day" in result.lower() or "days" in result.lower()