add unit test to timegen weekdays

This commit is contained in:
boyska 2016-12-20 01:32:04 +01:00
parent a154350b1b
commit d0cd97aea0
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
2 changed files with 35 additions and 3 deletions

View file

@ -30,12 +30,23 @@ def onehour(now, request):
'interval': intervals[request.param],
'end': now + days(1)})
@pytest.fixture(params=[1, '1'])
def onehour_monday(request):
monday = request.param
weekday = request.param
yield FrequencyAlarm({
'interval': 3600*12,
'weekdays': [monday], # mondays only
'weekdays': [weekday],
'start': 0
})
@pytest.fixture(params=[7, '7'])
def onehour_sunday(request):
weekday = request.param
yield FrequencyAlarm({
'interval': 3600*12,
'weekdays': [weekday],
'start': 0
})
@ -124,10 +135,26 @@ def test_weekday_skip(onehour_monday):
t = datetime.fromtimestamp(0)
for _ in range(20): # 20 is an arbitrary number
t = onehour_monday.next_ring(t)
print(_, t, t.isoweekday())
assert t.isoweekday() == 1 # monday; don't get confused by .weekday()
def test_weekday_skip_2(onehour_sunday):
t = datetime.fromtimestamp(0)
for _ in range(20): # 20 is an arbitrary number
t = onehour_sunday.next_ring(t)
assert t.isoweekday() == 7 # monday; don't get confused by .weekday()
def test_sunday_is_not_0():
with pytest.raises(ValueError) as excinfo:
FrequencyAlarm({
'interval': 3600*12,
'weekdays': [0],
'start': 0
})
assert 'Not a valid weekday:' in excinfo.value.args[0]
def test_single_registered():
timegenerate({
'kind': 'single',

View file

@ -77,6 +77,11 @@ class FrequencyAlarm(Alarm):
self.end = getdate(obj['end']) if 'end' in obj else None
self.weekdays = [int(x) for x in obj['weekdays']] if \
'weekdays' in obj else None
if self.weekdays is not None:
for weekday in self.weekdays:
if not 1 <= weekday <= 7:
raise ValueError('Not a valid weekday: {}'
.format(weekday))
def next_ring(self, current_time=None):
'''if current_time is None, it is now()'''