From 308d5a0a61af9c2a180ad948fe1baa0197141835 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 1 Aug 2024 17:55:17 +0200 Subject: [PATCH] add tests for invalid schedules --- tubearchivist/task/tests/__init__.py | 0 tubearchivist/task/tests/test_src/__init__.py | 0 .../tests/test_src/test_config_schedule.py | 68 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 tubearchivist/task/tests/__init__.py create mode 100644 tubearchivist/task/tests/test_src/__init__.py create mode 100644 tubearchivist/task/tests/test_src/test_config_schedule.py diff --git a/tubearchivist/task/tests/__init__.py b/tubearchivist/task/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tubearchivist/task/tests/test_src/__init__.py b/tubearchivist/task/tests/test_src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tubearchivist/task/tests/test_src/test_config_schedule.py b/tubearchivist/task/tests/test_src/test_config_schedule.py new file mode 100644 index 00000000..637e8884 --- /dev/null +++ b/tubearchivist/task/tests/test_src/test_config_schedule.py @@ -0,0 +1,68 @@ +"""test schedule parsing""" + +# flake8: noqa: E402 + +import os + +import django + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") +django.setup() + +import pytest +from task.src.config_schedule import CrontabValidator + +INCORRECT_CRONTAB = [ + "0 0 * * *", + "0 0", + "0", +] + + +@pytest.mark.parametrize("invalid_value", INCORRECT_CRONTAB) +def test_invalid_len(invalid_value): + """raise error on invalid crontab""" + validator = CrontabValidator() + with pytest.raises(ValueError, match="three cron schedule fields"): + validator.validate_cron(invalid_value) + + +NONE_INT_MINUTE = [ + "* * *", + "0,30 * *", + "0,1,2 * *", + "-1 * *", +] + + +@pytest.mark.parametrize("invalid_value", NONE_INT_MINUTE) +def test_none_int_crontabs(invalid_value): + """raise error on invalid crontab""" + validator = CrontabValidator() + with pytest.raises(ValueError, match="Must be an integer."): + validator.validate_cron(invalid_value) + + +INVALID_MINUTE = ["60 * *", "61 * *"] + + +@pytest.mark.parametrize("invalid_value", INVALID_MINUTE) +def test_invalid_minute(invalid_value): + """raise error on invalid crontab""" + validator = CrontabValidator() + with pytest.raises(ValueError, match="Must be between 0 and 59."): + validator.validate_cron(invalid_value) + + +INVALID_CRONTAB = [ + "0 /1 *", + "0 0/1 *", +] + + +@pytest.mark.parametrize("invalid_value", INVALID_CRONTAB) +def test_invalid_crontab(invalid_value): + """raise error on invalid crontab""" + validator = CrontabValidator() + with pytest.raises(ValueError, match="invalid crontab"): + validator.validate_cron(invalid_value)