Skip to content

Commit

Permalink
Added pickling tests for triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Apr 3, 2016
1 parent 1f324b8 commit ba5e8dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
11 changes: 11 additions & 0 deletions apscheduler/triggers/cron/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def get_next_value(self, date, field):
if next <= maxval:
return next

def __eq__(self, other):
return isinstance(other, self.__class__) and self.step == other.step

def __str__(self):
if self.step:
return '*/%d' % self.step
Expand Down Expand Up @@ -79,6 +82,10 @@ def get_next_value(self, date, field):
if next <= maxval:
return next

def __eq__(self, other):
return (isinstance(other, self.__class__) and self.first == other.first and
self.last == other.last)

def __str__(self):
if self.last != self.first and self.last is not None:
range = '%d-%d' % (self.first, self.last)
Expand Down Expand Up @@ -163,6 +170,10 @@ def get_next_value(self, date, field):
if target_day <= last_day and target_day >= date.day:
return target_day

def __eq__(self, other):
return (super(WeekdayPositionExpression, self).__eq__(other) and
self.option_num == other.option_num and self.weekday == other.weekday)

def __str__(self):
return '%s %s' % (self.options[self.option_num], WEEKDAYS[self.weekday])

Expand Down
3 changes: 3 additions & 0 deletions apscheduler/triggers/cron/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def compile_expression(self, expr):

raise ValueError('Unrecognized expression "%s" for field "%s"' % (expr, self.name))

def __eq__(self, other):
return isinstance(self, self.__class__) and self.expressions == other.expressions

def __str__(self):
expr_strings = (str(e) for e in self.expressions)
return ','.join(expr_strings)
Expand Down
33 changes: 32 additions & 1 deletion tests/test_triggers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
import pickle
from datetime import datetime, timedelta, date

import pytest
import pytz
Expand Down Expand Up @@ -213,6 +214,17 @@ def test_timezone_change(self, timezone):
correct_next_date = est.localize(datetime(2009, 9, 26, 11, 20))
assert str(trigger.get_next_fire_time(None, start_date)) == str(correct_next_date)

def test_pickle(self, timezone):
"""Test that the trigger is pickleable."""

trigger = CronTrigger(year=2016, month='5-6', day='20-28', hour=7, minute=25, second='*',
timezone=timezone)
data = pickle.dumps(trigger, 2)
trigger2 = pickle.loads(data)

for attr in CronTrigger.__slots__:
assert getattr(trigger2, attr) == getattr(trigger, attr)


class TestDateTrigger(object):
@pytest.mark.parametrize('run_date,alter_tz,previous,now,expected', [
Expand Down Expand Up @@ -254,6 +266,14 @@ def test_str(self, timezone):
trigger = DateTrigger(datetime(2009, 7, 6), timezone)
assert str(trigger) == "date[2009-07-06 00:00:00 CEST]"

def test_pickle(self, timezone):
"""Test that the trigger is pickleable."""

trigger = DateTrigger(date(2016, 4, 3), timezone=timezone)
data = pickle.dumps(trigger, 2)
trigger2 = pickle.loads(data)
assert trigger2.run_date == trigger.run_date


class TestIntervalTrigger(object):
@pytest.fixture()
Expand Down Expand Up @@ -324,3 +344,14 @@ def test_repr(self, trigger):

def test_str(self, trigger):
assert str(trigger) == "interval[0:00:01]"

def test_pickle(self, timezone):
"""Test that the trigger is pickleable."""

trigger = IntervalTrigger(weeks=2, days=6, minutes=13, seconds=2,
start_date=date(2016, 4, 3), timezone=timezone)
data = pickle.dumps(trigger, 2)
trigger2 = pickle.loads(data)

for attr in IntervalTrigger.__slots__:
assert getattr(trigger2, attr) == getattr(trigger, attr)

0 comments on commit ba5e8dd

Please sign in to comment.