-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_task_monitor_bgjobs.py
66 lines (52 loc) · 1.95 KB
/
test_task_monitor_bgjobs.py
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
62
63
64
65
66
from unittest.mock import patch
from doajtest.helpers import DoajTestCase
from portality import models
from portality.background import BackgroundApi
from portality.core import app
from portality.tasks import monitor_bgjobs
class TestMonitorBgjobsBackgroundTask(DoajTestCase):
store_impl = None
@staticmethod
def execute_task_a():
username = app.config.get("SYSTEM_USERNAME")
job = monitor_bgjobs.MonitorBgjobsBackgroundTask.prepare(
username, to_address_list=['to@alskdj.com'],
from_address='from@akdjaksj.com', )
task = monitor_bgjobs.MonitorBgjobsBackgroundTask(job)
BackgroundApi.execute(task)
@patch.object(monitor_bgjobs.app_email, 'send_mail')
def test_run__mail_sent(self, mock_send_mail):
assert models.BackgroundJob.count() == 0
# create target background_job records
target_jobs = []
for _ in range(2):
bg = models.BackgroundJob()
bg.fail()
bg.user = 'system'
bg.action = 'harvest'
bg.save()
target_jobs.append(bg)
# create not related background_job
bg = models.BackgroundJob()
bg.save(blocking=True)
# execute
self.execute_task_a()
# check result
self.assertTrue(mock_send_mail.called)
self.assertTrue(mock_send_mail.call_args)
self.assertIn(
target_jobs[0].id,
mock_send_mail.call_args.kwargs.get('msg_body', '')
)
@patch.object(monitor_bgjobs.app_email, 'send_mail')
def test_run__no_mail(self, mock_send_mail):
assert models.BackgroundJob.count() == 0
# create target background_job records
models.BackgroundJob.count()
# create not related background_job
bg = models.BackgroundJob()
bg.save(blocking=True)
# execute
self.execute_task_a()
# check result
self.assertFalse(mock_send_mail.called)