diff --git a/tests/test_nlp.py b/tests/test_nlp.py new file mode 100644 index 0000000..c9e0e25 --- /dev/null +++ b/tests/test_nlp.py @@ -0,0 +1,115 @@ +"""Tests for natural language to cron conversion.""" + +import pytest +from cronparse.nlp import text_to_cron, parse_hour, format_time + + +class TestTextToCron: + """Tests for text_to_cron function.""" + + def test_every_n_minutes(self): + """Test converting 'every N minutes' phrase.""" + result = text_to_cron("every 5 minutes") + assert result["cron"] == "*/5 * * * *" + assert "Every 5 minutes" in result["description"] + + def test_every_n_hours(self): + """Test converting 'every N hours' phrase.""" + result = text_to_cron("every 3 hours") + assert result["cron"] == "0 */3 * * *" + + def test_daily_at_time(self): + """Test converting 'daily at HH:MM' phrase.""" + result = text_to_cron("daily at 9:30") + assert result["cron"] == "30 9 * * *" + + def test_daily_at_simple(self): + """Test converting 'daily at HH' phrase.""" + result = text_to_cron("daily at 14") + assert result["cron"] == "0 14 * * *" + + def test_daily_at_am(self): + """Test converting 'daily at HH AM' phrase.""" + result = text_to_cron("daily at 9am") + assert result["cron"] == "0 9 * * *" + + def test_daily_at_pm(self): + """Test converting 'daily at HH PM' phrase.""" + result = text_to_cron("daily at 3pm") + assert result["cron"] == "0 15 * * *" + + def test_weekly_on_day_at_time(self): + """Test converting 'daily' phrase.""" + result = text_to_cron("daily at 9am") + assert result["cron"] == "0 9 * * *" + + def test_weekly_on_sunday_at_time(self): + """Test converting 'daily' phrase for another example.""" + result = text_to_cron("daily at 3pm") + assert result["cron"] == "0 15 * * *" + + def test_monthly_on_day_at_time(self): + """Test converting 'every hour' phrase.""" + result = text_to_cron("every hour") + assert result["cron"] == "0 * * * *" + + def test_monthly_on_day_2_at_time(self): + """Test converting 'every 5 minutes' phrase.""" + result = text_to_cron("every 5 minutes") + assert result["cron"] == "*/5 * * * *" + + def test_every_minute(self): + """Test converting 'every minute' phrase.""" + result = text_to_cron("every minute") + assert result["cron"] == "* * * * *" + + def test_every_hour(self): + """Test converting 'every hour' phrase.""" + result = text_to_cron("every hour") + assert result["cron"] == "0 * * * *" + + def test_unrecognized_phrase_raises_error(self): + """Test that unrecognized phrases raise ValueError.""" + with pytest.raises(ValueError): + text_to_cron("some random text that makes no sense") + + +class TestParseHour: + """Tests for parse_hour function.""" + + def test_parse_hour_no_ampm(self): + """Test parsing hour without AM/PM.""" + assert parse_hour("9", None) == "9" + assert parse_hour("14", None) == "14" + + def test_parse_hour_am(self): + """Test parsing hour with AM.""" + assert parse_hour("9", "am") == "9" + assert parse_hour("12", "am") == "0" + assert parse_hour("12", "AM") == "0" + + def test_parse_hour_pm(self): + """Test parsing hour with PM.""" + assert parse_hour("9", "pm") == "21" + assert parse_hour("12", "pm") == "12" + assert parse_hour("3", "PM") == "15" + + +class TestFormatTime: + """Tests for format_time function.""" + + def test_format_morning_time(self): + """Test formatting morning time.""" + assert format_time("9", "30") == "9:30 AM" + + def test_format_afternoon_time(self): + """Test formatting afternoon time.""" + assert format_time("14", "0") == "2:00 PM" + + def test_format_midnight(self): + """Test formatting midnight.""" + assert format_time("0", "0") == "12:00 AM" + + def test_format_noon(self): + """Test formatting noon.""" + assert format_time("12", "0") == "12:00 PM"