From fd6f6436a5cc439a1e08502fef08334aea6668fa Mon Sep 17 00:00:00 2001 From: "Italo A. de Abreu" Date: Wed, 13 Nov 2024 13:14:07 -0300 Subject: [PATCH] Adjust list index out of range error cron(00 4 ? * 3-1 ) --- src/pyawscron/awscron.py | 10 ++++++++-- tests/next_tests.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/pyawscron/awscron.py b/src/pyawscron/awscron.py index 6ff49ad..07ea663 100644 --- a/src/pyawscron/awscron.py +++ b/src/pyawscron/awscron.py @@ -112,8 +112,14 @@ def __parse_one_rule(self, rule, min, max): parts = s.split('-') start = int(parts[0]) end = int(parts[1]) - for i in range(start, end + 1, 1): - allows.append(i) + if start <= end: + for i in range(start, end + 1): + allows.append(i) + else: + for i in range(start, 8): + allows.append(i) + for i in range(1, end + 1): + allows.append(i) else: allows.append(int(s)) allows.sort() diff --git a/tests/next_tests.py b/tests/next_tests.py index ca90d49..63d64e5 100644 --- a/tests/next_tests.py +++ b/tests/next_tests.py @@ -385,6 +385,35 @@ def test_generate_multiple_next_14(self): print(f"Result: {dt}\tExpected: {expected}\n") self.assertEqual(expected, str(dt)) + def test_generate_multiple_next_occurences15(self): + """ + Tests the generation of multiple next occurrences + from a specific CRON expression. Ensures that + days_of_week are within the expected range. + :return: None + """ + cron = '00 4 ? * 3-1 *' + expected_list= ['2024-11-14 04:00:00+00:00', + '2024-11-15 04:00:00+00:00', + '2024-11-16 04:00:00+00:00', + '2024-11-17 04:00:00+00:00', + + '2024-11-19 04:00:00+00:00', + '2024-11-20 04:00:00+00:00', + '2024-11-21 04:00:00+00:00', + '2024-11-22 04:00:00+00:00', + '2024-11-23 04:00:00+00:00', + '2024-11-24 04:00:00+00:00'] + cron = AWSCron(cron) + dt = datetime.datetime(2024, 11, 13, 12, 30, 57, tzinfo=datetime.timezone.utc) + results = [] + for expected in expected_list: + print(f"Input {cron}, occurrence: {dt}") + dt = cron.occurrence(dt).next() + results.append(str(dt)) + print(f"Result: {dt}\tExpected: {expected}\n") + self.assertEqual(expected, str(dt)) + if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()