File: test_schedule.py

package info (click to toggle)
python-irc 8.5.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 424 kB
  • ctags: 466
  • sloc: python: 2,403; makefile: 6
file content (61 lines) | stat: -rw-r--r-- 1,674 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import time
import random
import datetime

import pytest

from irc import schedule


def test_delayed_command_order():
	"""
	delayed commands should be sorted by delay time
	"""
	null = lambda: None
	delays = [random.randint(0, 99) for x in range(5)]
	cmds = sorted([
		schedule.DelayedCommand.after(delay, null)
		for delay in delays
	])
	assert [c.delay.seconds for c in cmds] == sorted(delays)

def test_periodic_command_delay():
	"A PeriodicCommand must have a positive, non-zero delay."
	with pytest.raises(ValueError) as exc_info:
		schedule.PeriodicCommand.after(0, None)
	assert str(exc_info.value) == test_periodic_command_delay.__doc__

def test_periodic_command_fixed_delay():
	"""
	Test that we can construct a periodic command with a fixed initial
	delay.
	"""
	fd = schedule.PeriodicCommandFixedDelay.at_time(
		at = datetime.datetime.now(),
		delay = datetime.timedelta(seconds=2),
		function = lambda: None,
		)
	assert fd.due() is True
	assert fd.next().due() is False

class TestCommands(object):
	def test_delayed_command_from_timestamp(self):
		"""
		Ensure a delayed command can be constructed from a timestamp.
		"""
		t = time.time()
		do_nothing = lambda: None
		schedule.DelayedCommand.at_time(t, do_nothing)

	def test_command_at_noon(self):
		"""
		Create a periodic command that's run at noon every day.
		"""
		when = datetime.time(12,0)
		cmd = schedule.PeriodicCommandFixedDelay.daily_at(when, function=None)
		assert cmd.due() is False
		next_cmd = cmd.next()
		daily = datetime.timedelta(days=1)
		day_from_now = datetime.datetime.now() + daily
		two_days_from_now = day_from_now + daily
		assert day_from_now < next_cmd < two_days_from_now