def test_crontab_hourly(): job = CronJob() job.set_crontab('@hourly') dt = datetime(2000, 1, 1, 1, 0, 1) assert job.is_runnable(dt) is True dt = datetime(2000, 1, 1, 0, 1, 1) assert job.is_runnable(dt) is False
def test_runnig_in_target_time_with_crontab(): job = CronJob() job.set_crontab('25 * * * *') dt = datetime(2000, 1, 1, 1, 25, 1) assert job.is_runnable(dt) is True dt = datetime(2000, 1, 1, 0, 26, 1) assert job.is_runnable(dt) is False
def test_not_run_in_target_time(): job = CronJob() job.trigger_format = '%H' job.trigger_time = '01' dt = datetime(2000, 1, 1, 1, 1, 1) assert job.is_runnable(dt) is True dt = datetime(2000, 1, 1, 0, 1, 1) assert job.is_runnable(dt) is False
def test_for_display(): job = CronJob() job.set_triggers('%H', '01') assert str(job) == 'CronJob(trigger=[%H->01])' job = CronJob() assert str(job) == 'CronJob(trigger=[])' job = CronJob() job.set_crontab('25 * * * *') assert str(job) == 'CronJob(crontab=[25 * * * *])'
def test_polled_once(capsys): job1 = CronJob() job1.set_action('stub.print_datetime') job1.set_triggers('%H', '00') job2 = CronJob() job2.set_action('stub.print_datetime') job2.set_triggers('%H', '01') plugin = MockedImpl() plugin._crontab = [job1, job2, ] with freeze_time('2016-01-01 00:00:01'): plugin.poll_crontab() out, err = capsys.readouterr() assert out == '2016-01-01'
def test_set_triggers(): job = CronJob() job.set_triggers('%H', '01') dt = datetime(2000, 1, 1, 1, 1, 1) assert job.is_runnable(dt) is True dt = datetime(2000, 1, 1, 0, 1, 1) assert job.is_runnable(dt) is False for keys in ( (None, '1'), ('1', None), ): with pytest.raises(ValueError): job.set_triggers(*keys)
def test_set_action_not_func(): with pytest.raises(AttributeError): job = CronJob() job.set_action('stub.echo_hello_not')
def test_crontab(): job = CronJob() job.set_crontab('25 * * * *') assert isinstance(job._crontab, CronTab) assert job._crontab.test(datetime(2001, 1, 1, 0, 25)) is True
def test_polled_once(capsys): job1 = CronJob() job1.set_action('stub.print_datetime') job1.set_triggers('%H', '00') job2 = CronJob() job2.set_action('stub.print_datetime') job2.set_triggers('%H', '01') plugin = MockedImpl() plugin._crontab = [ job1, job2, ] with freeze_time('2016-01-01 00:00:01'): plugin.poll_crontab() out, err = capsys.readouterr() assert out == '2016-01-01'
def test_do_action(): job = CronJob() job.set_action('stub.echo_datetime') dt = datetime(2000, 1, 1, 1, 1, 1) assert job.do_action(None, dt) == '2000-01-01'
def test_set_action(): job = CronJob() job.set_action('stub.echo_hello') assert isinstance(job.action, types.FunctionType)
def test_do_action_with_arg(): job = CronJob() job.set_action('stub.echo_datetime_with_head', 'sample') dt = datetime(2000, 1, 1, 1, 1, 1) assert job.do_action(None, dt) == 'sample2000-01-01'