def test_generate_execution_time_from_day_of_week_match(): from_str = "2019-04-22 00:00" to_str = "2019-04-23 23:59" print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "0 0 * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 2 print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "0 0 * * 0", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 0 print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "0 0 * * 1", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 1
def test_generate_execution_time_from_hour_match(): from_str = "2019-04-22 00:00" to_str = "2019-04-23 23:59" print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "0 * * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 48 print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "15 0,5,10,15,20 * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 10 print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "0 */2 * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 24 print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "0 1/2 * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 24 print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "0 7-9 * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 6
def test_match_day_of_week(): dt_str = "2019-04-23 1:00" # Is a Tuesday dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * * * *", dt) assert CronValidator.match_datetime("* * * * 2", dt) assert CronValidator.match_datetime("* * * * 5", dt) is False assert CronValidator.match_datetime("* * * * 2-5", dt) assert CronValidator.match_datetime("* * * * 2-3", dt) assert CronValidator.match_datetime("* * * * 2/5", dt) assert CronValidator.match_datetime("* * * * 5/1", dt) is False assert CronValidator.match_datetime("* * * * 1/1", dt) assert CronValidator.match_datetime("* * * * 3,4,5", dt) is False assert CronValidator.match_datetime("* * * * 2,3,1", dt)
def test_match_month(): dt_str = "2019-04-23 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * * * *", dt) assert CronValidator.match_datetime("* * * 4 *", dt) assert CronValidator.match_datetime("* * * 5 *", dt) is False assert CronValidator.match_datetime("* * * 1-5 *", dt) assert CronValidator.match_datetime("* * * 1-3 *", dt) is False assert CronValidator.match_datetime("* * * 1/5 *", dt) is False assert CronValidator.match_datetime("* * * 5/1 *", dt) is False assert CronValidator.match_datetime("* * * 1/1 *", dt) assert CronValidator.match_datetime("* * * 2,3,4 *", dt) assert CronValidator.match_datetime("* * * 2,3,5 *", dt) is False
def test_match_hour(): dt_str = "2019-04-23 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * * * *", dt) assert CronValidator.match_datetime("* 1 * * *", dt) assert CronValidator.match_datetime("* 0 * * *", dt) is False assert CronValidator.match_datetime("* 0-5 * * *", dt) assert CronValidator.match_datetime("* 0/5 * * *", dt) is False assert CronValidator.match_datetime("* 0/1 * * *", dt) assert CronValidator.match_datetime("* 1/1 * * *", dt) assert CronValidator.match_datetime("* 2/1 * * *", dt) is False assert CronValidator.match_datetime("* 1,2,3,4 * * *", dt) assert CronValidator.match_datetime("* 2,3,4 * * *", dt) is False
def test_match_minute(): dt_str = "2019-04-23 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * * * *", dt) assert CronValidator.match_datetime("0 * * * *", dt) assert CronValidator.match_datetime("0-30 * * * *", dt) assert CronValidator.match_datetime("0/30 * * * *", dt) assert CronValidator.match_datetime("0/1 * * * *", dt) assert CronValidator.match_datetime("1/1 * * * *", dt) is False assert CronValidator.match_datetime("1,2,3,4 * * * *", dt) is False assert CronValidator.match_datetime("30 * * * *", dt) is False
def test_generate_execution_time_from_minute_match(): from_str = "2019-04-23 12:00" to_str = "2019-04-23 12:59" print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "* * * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 60 print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "23 * * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 1 assert dts[0] == str_to_datetime("2019-04-23 12:23") print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "1,23,59 * * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert len(dts) == 3 assert dts[0] == str_to_datetime("2019-04-23 12:01") assert dts[1] == str_to_datetime("2019-04-23 12:23") assert dts[2] == str_to_datetime("2019-04-23 12:59")
def test_make_sure_dt_is_rounded(): from_str = "2019-04-23 12:00:01" to_str = "2019-04-23 12:59:02" print("--------------------------------------------------") dts = [] for dt in CronValidator.get_execution_time( "* * * * *", from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)): print(dt) dts.append(dt) assert dt.second == 0 assert dt.microsecond == 0 assert len(dts) == 60
def test_match_eb_syntax(): dt_str = "2023-04-28 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * * * 30W", dt, version=Version.EB) assert CronValidator.match_datetime("* * * * 5L", dt, version=Version.EB) dt_str = "2022-02-28 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * L * *", dt, version=Version.EB) dt_str = "2022-02-27 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * L * *", dt, version=Version.EB) is False dt_str = "2020-02-28 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * L * *", dt, version=Version.EB) is False dt_str = "2020-02-29 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * L * *", dt, version=Version.EB)
def assert_validate_successfully(expr): try: elements = CronValidator.parse(expr) print(f"{expr}: {elements}") except ValueError: pytest.fail(f"Invalid expression: {expr}")
def assert_validate_fail(expr): try: elements = CronValidator.parse(expr) pytest.fail(f"Wrong validate {expr}: {elements}") except ValueError: pass
def test_match_day_of_month(): dt_str = "2019-04-23 1:00" dt = str_to_datetime(dt_str) assert CronValidator.match_datetime("* * * * *", dt) assert CronValidator.match_datetime("* * 23 * *", dt) assert CronValidator.match_datetime("* * 1 * *", dt) is False assert CronValidator.match_datetime("* * 1-5 * *", dt) is False assert CronValidator.match_datetime("* * 1-23 * *", dt) assert CronValidator.match_datetime("* * 1/5 * *", dt) is False assert CronValidator.match_datetime("* * 1/1 * *", dt) assert CronValidator.match_datetime("* * 1/1 * *", dt) assert CronValidator.match_datetime("* * 24/1 * *", dt) is False assert CronValidator.match_datetime("* * 23,2,3,4 * *", dt) assert CronValidator.match_datetime("* * 2,3,24 * *", dt) is False