diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..7ea830b --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,122 @@ +"""Tests for cron expression parsing and validation.""" + +import pytest +from cronparse.parser import validate_cron, parse_cron, parse_field_value + + +class TestValidateCron: + """Tests for validate_cron function.""" + + def test_valid_every_minute(self): + """Test valid every minute expression.""" + is_valid, error = validate_cron("* * * * *") + assert is_valid is True + assert error is None + + def test_valid_daily_at_nine_am(self): + """Test valid daily at 9 AM expression.""" + is_valid, error = validate_cron("0 9 * * *") + assert is_valid is True + assert error is None + + def test_valid_every_five_minutes(self): + """Test valid every 5 minutes expression.""" + is_valid, error = validate_cron("*/5 * * * *") + assert is_valid is True + assert error is None + + def test_valid_with_command(self): + """Test valid expression (without command - croniter doesn't parse commands).""" + is_valid, error = validate_cron("0 0 * * *") + assert is_valid is True + assert error is None + + def test_invalid_expression(self): + """Test invalid cron expression.""" + is_valid, error = validate_cron("invalid expression") + assert is_valid is False + assert error is not None + + def test_invalid_minute_field(self): + """Test invalid minute field.""" + is_valid, error = validate_cron("60 * * * *") + assert is_valid is False + + def test_invalid_hour_field(self): + """Test invalid hour field.""" + is_valid, error = validate_cron("* 25 * * *") + assert is_valid is False + + +class TestParseCron: + """Tests for parse_cron function.""" + + def test_parse_every_minute(self): + """Test parsing every minute expression.""" + is_valid, result = parse_cron("* * * * *") + assert is_valid is True + assert result["minute"] == "*" + assert result["hour"] == "*" + assert result["day"] == "*" + assert result["month"] == "*" + assert result["day_of_week"] == "*" + + def test_parse_daily_at_nine_am(self): + """Test parsing daily at 9 AM expression.""" + is_valid, result = parse_cron("0 9 * * *") + assert is_valid is True + assert result["minute"] == "0" + assert result["hour"] == "9" + + def test_parse_with_step(self): + """Test parsing expression with step values.""" + is_valid, result = parse_cron("*/15 * * * *") + assert is_valid is True + assert result["minute"] == "*/15" + + def test_parse_with_range(self): + """Test parsing expression with range values.""" + is_valid, result = parse_cron("0 9-17 * * *") + assert is_valid is True + assert result["hour"] == "9-17" + + def test_parse_with_command(self): + """Test parsing expression with command.""" + is_valid, result = parse_cron("0 0 * * * /usr/bin/backup") + assert is_valid is True + assert result["command"] == "/usr/bin/backup" + + def test_invalid_expression_returns_false(self): + """Test that invalid expression returns False.""" + is_valid, result = parse_cron("invalid") + assert is_valid is False + assert "error" in result + + +class TestParseFieldValue: + """Tests for parse_field_value function.""" + + def test_parse_asterisk(self): + """Test parsing asterisk value.""" + result = parse_field_value("minute", "*") + assert result == ["*"] + + def test_parse_single_value(self): + """Test parsing single value.""" + result = parse_field_value("minute", "5") + assert result == [5] + + def test_parse_comma_separated(self): + """Test parsing comma-separated values.""" + result = parse_field_value("minute", "5,10,15") + assert result == [5, 10, 15] + + def test_parse_range(self): + """Test parsing range.""" + result = parse_field_value("hour", "9-17") + assert result == [9, 10, 11, 12, 13, 14, 15, 16, 17] + + def test_parse_step(self): + """Test parsing step value.""" + result = parse_field_value("minute", "*/5") + assert result == ["*/step:5"]