def test_update_job_change_type(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login(AdminFactory()) response = self.put( url_for('api.job', id=task.id), { 'name': task.name, 'description': task.description, 'task': task.task, 'interval': { 'every': 5, 'period': 'minutes', } }) self.assert200(response) self.assertEqual(response.json['id'], str(task.id)) self.assertEqual(response.json['name'], task.name) self.assertEqual(response.json['task'], task.task) self.assertEqual(response.json['description'], task.description) self.assertEqual(response.json['interval']['every'], 5) self.assertEqual(response.json['interval']['period'], 'minutes') self.assertIsNone(response.json['crontab'])
def test_update_job(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login(AdminFactory()) response = self.put( url_for('api.job', id=task.id), { 'name': task.name, 'description': 'New description', 'task': task.task, 'crontab': task.crontab.to_json() }) self.assert200(response) self.assertEqual(response.json['id'], str(task.id)) self.assertEqual(response.json['name'], task.name) self.assertEqual(response.json['task'], task.task) self.assertEqual(response.json['description'], 'New description') self.assertIsNotNone(response.json['crontab']) self.assertIsNone(response.json['interval'])
def test_delete_job(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login(AdminFactory()) response = self.delete(url_for('api.job', id=task.id)) self.assert204(response) self.assertIsNone(PeriodicTask.objects(id=task.id).first())
def test_scheduled_jobs_list(self): @job('a-job') def test_job(): pass for i in range(6): params = { 'name': faker.name(), 'description': faker.sentence(), 'task': 'a-job' } if i % 2: params['crontab'] = PeriodicTask.Crontab(minute=str(i)) else: params['interval'] = PeriodicTask.Interval(every=i, period='minutes') PeriodicTask.objects.create(**params) response = self.get(url_for('api.jobs')) self.assert200(response)
def test_unschedule_job(self, cli): PeriodicTask.objects.create( task=JOB_NAME, name='job', description='I\'m a scheduled job', enabled=True, crontab=PeriodicTask.Crontab.parse('0 0 0 0 0'), ) cli('job unschedule {0}'.format(JOB_NAME)) assert len(PeriodicTask.objects(task=JOB_NAME)) == 0
def test_unschedule_job_different_parameters(self, cli): PeriodicTask.objects.create( task=JOB_NAME, name='job', description='I\'m a scheduled job', enabled=True, crontab=PeriodicTask.Crontab.parse('0 0 0 0 0'), ) result = cli('job unschedule {0} arg'.format(JOB_NAME), check=False) assert result.exit_code != 0 assert len(PeriodicTask.objects(task=JOB_NAME)) == 1
def test_unschedule_job_with_parameters(self, cli): PeriodicTask.objects.create( task=JOB_NAME, name='job', description='I\'m a scheduled job', enabled=True, args=['arg'], kwargs={'key': 'value'}, crontab=PeriodicTask.Crontab.parse('0 0 0 0 0'), ) cli('job unschedule {0} arg key=value'.format(JOB_NAME)) assert len(PeriodicTask.objects(task=JOB_NAME)) == 0
def test_delete_job_need_admin(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login() response = self.delete(url_for('api.job', id=task.id)) self.assert403(response)
def test_schedule_job(self, cli): cli('job schedule "0 1 2 3 sunday" fake-job') tasks = PeriodicTask.objects(task=JOB_NAME) assert len(tasks) == 1 task = tasks[0] assert task.args == [] assert task.kwargs == {} assert task.crontab.minute == '0' assert task.crontab.hour == '1' assert task.crontab.day_of_month == '2' assert task.crontab.month_of_year == '3' assert task.crontab.day_of_week == 'sunday' assert task.enabled assert task.name == 'Job {0}'.format(JOB_NAME)
def test_get_job(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) response = self.get(url_for('api.job', id=task.id)) self.assert200(response) self.assertEqual(response.json['id'], str(task.id)) self.assertEqual(response.json['name'], task.name) self.assertEqual(response.json['description'], task.description) self.assertEqual(response.json['task'], task.task)
def test_delete_job(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5') ) self.login(AdminFactory()) response = self.delete(url_for('api.job', id=task.id)) self.assert204(response) self.assertIsNone(PeriodicTask.objects(id=task.id).first())
def test_schedule_job_with_parameters(self, cli): cli('job schedule "0 1 2 3 sunday" fake-job ' 'arg0 arg1 key1=value1 key0=value0') tasks = PeriodicTask.objects(task=JOB_NAME) assert len(tasks) == 1 task = tasks[0] assert task.args == ['arg0', 'arg1'] # Arguments order is preserved assert task.kwargs == {'key0': 'value0', 'key1': 'value1'} assert task.crontab.minute == '0' assert task.crontab.hour == '1' assert task.crontab.day_of_month == '2' assert task.crontab.month_of_year == '3' assert task.crontab.day_of_week == 'sunday' assert task.enabled # kwargs are sorted expected = 'Job {0}(arg0, arg1, key0=value0, key1=value1)' assert task.name == expected.format(JOB_NAME)
def test_update_job_need_admin(self): @job('a-job') def test_job(): pass task = PeriodicTask.objects.create( name=faker.name(), description=faker.sentence(), task='a-job', crontab=PeriodicTask.Crontab(minute='5')) self.login() response = self.put( url_for('api.job', id=task.id), { 'name': task.name, 'description': 'New description', 'task': task.task, 'crontab': task.crontab.to_json() }) self.assert403(response)