Skip to content

Commit

Permalink
Fix hashed expressions omitting some entries (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: zhouyizhen <zhouyizhen@metrodata.tech>
  • Loading branch information
JabberWocky-22 and zhouyizhen authored Jul 18, 2024
1 parent 584661a commit 29274ae
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/croniter/croniter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,8 +1050,9 @@ def expand(self, efl, idx, expr, hash_id=None, match='', **kw):
idx,
hash_type=m['hash_type'],
hash_id=hash_id,
range_end=int(m['divisor']),
) + int(m['range_begin']),
range_begin=int(m['range_begin']),
range_end=int(m['divisor']) - 1 + int(m['range_begin']),
),
int(m['range_end']),
int(m['divisor']),
)
Expand All @@ -1076,7 +1077,8 @@ def expand(self, efl, idx, expr, hash_id=None, match='', **kw):
idx,
hash_type=m['hash_type'],
hash_id=hash_id,
range_end=int(m['divisor']),
range_begin=self.cron.RANGES[idx][0],
range_end=int(m['divisor']) - 1 + self.cron.RANGES[idx][0],
),
self.cron.RANGES[idx][1],
int(m['divisor']),
Expand Down
52 changes: 50 additions & 2 deletions src/croniter/tests/test_croniter_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_hash_word_midnight(self):
@midnight is actually up to 3 hours after midnight, not exactly midnight
"""
self._test_iter('@midnight', datetime(2020, 1, 1, 1, 10, 32), timedelta(days=1))
self._test_iter('@midnight', datetime(2020, 1, 1, 2, 10, 32), timedelta(days=1))

def test_hash_word_hourly(self):
"""Test built-in @hourly"""
Expand Down Expand Up @@ -250,6 +250,16 @@ def test_expand_minutes_range_15_minutes(self):
assert min(minutes) == self.MIN_VALUE
assert max(minutes) == self.MAX_VALUE

def test_expand_minutes_with_full_range(self):
minutes = set()
expression = 'H(0-59) * * * *'
for hash_id in self.HASH_IDS:
expanded = croniter.expand(expression, hash_id=hash_id)
minutes.add(expanded[0][0][0])
assert len(minutes) == self.TOTAL
assert min(minutes) == self.MIN_VALUE
assert max(minutes) == self.MAX_VALUE


class CroniterHashExpanderExpandHoursTest(CroniterHashExpanderBase):
MIN_VALUE = 0
Expand Down Expand Up @@ -326,7 +336,15 @@ def test_expand_hours_range_12_hours(self):
assert min(hours) == self.MIN_VALUE
assert max(hours) == self.MAX_VALUE


def test_expand_hours_with_full_range(self):
minutes = set()
expression = '* H(0-23) * * *'
for hash_id in self.HASH_IDS:
expanded = croniter.expand(expression, hash_id=hash_id)
minutes.add(expanded[0][1][0])
assert len(minutes) == self.TOTAL
assert min(minutes) == self.MIN_VALUE
assert max(minutes) == self.MAX_VALUE


class CroniterHashExpanderExpandMonthDaysTest(CroniterHashExpanderBase):
Expand Down Expand Up @@ -380,6 +398,16 @@ def test_expand_month_days_range_12_days(self):
assert min(month_days) == self.MIN_VALUE
assert max(month_days) == self.MAX_VALUE

def test_expand_month_days_with_full_range(self):
month_days = set()
expression = '* * H(1-31) * *'
for hash_id in self.HASH_IDS:
expanded = croniter.expand(expression, hash_id=hash_id)
month_days.add(expanded[0][2][0])
assert len(month_days) == self.TOTAL
assert min(month_days) == self.MIN_VALUE
assert max(month_days) == self.MAX_VALUE


class CroniterHashExpanderExpandMonthTest(CroniterHashExpanderBase):
MIN_VALUE = 1
Expand Down Expand Up @@ -432,6 +460,16 @@ def test_expand_month_days_range_5_months(self):
assert min(months) == self.MIN_VALUE
assert max(months) == self.MAX_VALUE

def test_expand_months_with_full_range(self):
months = set()
expression = '* * * H(1-12) *'
for hash_id in self.HASH_IDS:
expanded = croniter.expand(expression, hash_id=hash_id)
months.add(expanded[0][3][0])
assert len(months) == self.TOTAL
assert min(months) == self.MIN_VALUE
assert max(months) == self.MAX_VALUE


class CroniterHashExpanderExpandWeekDays(CroniterHashExpanderBase):
MIN_VALUE = 0
Expand Down Expand Up @@ -471,3 +509,13 @@ def test_expand_week_days_range_4_days(self):
assert len(days) == self.TOTAL
assert min(days) == self.MIN_VALUE
assert max(days) == self.MAX_VALUE

def test_expand_week_days_with_full_range(self):
days = set()
expression = '* * * * H(0-6)'
for hash_id in self.HASH_IDS:
expanded = croniter.expand(expression, hash_id=hash_id)
days.add(expanded[0][4][0])
assert len(days) == self.TOTAL
assert min(days) == self.MIN_VALUE
assert max(days) == self.MAX_VALUE

0 comments on commit 29274ae

Please sign in to comment.