|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
""" |
|
4
|
|
|
Created on Tue Jul 9 11:51:09 2019 |
|
5
|
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
from unittest.mock import patch |
|
10
|
|
|
from billiard.einfo import ExceptionInfo |
|
11
|
|
|
|
|
12
|
|
|
from django.core import mail |
|
13
|
|
|
from django.test import TestCase |
|
14
|
|
|
|
|
15
|
|
|
from common.constants import ERROR |
|
16
|
|
|
from common.tests import WebSocketMixin |
|
17
|
|
|
from image_app.models import Submission |
|
18
|
|
|
|
|
19
|
|
|
from .common import BaseExcelMixin |
|
20
|
|
|
from ..tasks import ImportTemplateTask |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
View Code Duplication |
class ImportTemplateTaskTest(WebSocketMixin, BaseExcelMixin, TestCase): |
|
|
|
|
|
|
24
|
|
|
def setUp(self): |
|
25
|
|
|
# calling my base class setup |
|
26
|
|
|
super().setUp() |
|
27
|
|
|
|
|
28
|
|
|
# setting task |
|
29
|
|
|
self.my_task = ImportTemplateTask() |
|
30
|
|
|
|
|
31
|
|
|
def test_on_failure(self): |
|
32
|
|
|
"""Testing on failure methods""" |
|
33
|
|
|
|
|
34
|
|
|
exc = Exception("Test") |
|
35
|
|
|
task_id = "test_task_id" |
|
36
|
|
|
args = [self.submission_id] |
|
37
|
|
|
kwargs = {} |
|
38
|
|
|
einfo = ExceptionInfo |
|
39
|
|
|
|
|
40
|
|
|
# call on_failure method |
|
41
|
|
|
self.my_task.on_failure(exc, task_id, args, kwargs, einfo) |
|
42
|
|
|
|
|
43
|
|
|
# check submission status and message |
|
44
|
|
|
submission = Submission.objects.get(pk=self.submission_id) |
|
45
|
|
|
|
|
46
|
|
|
# check submission.state changed |
|
47
|
|
|
self.assertEqual(submission.status, ERROR) |
|
48
|
|
|
self.assertEqual( |
|
49
|
|
|
submission.message, |
|
50
|
|
|
"Error in Template loading: Test") |
|
51
|
|
|
|
|
52
|
|
|
# test email sent |
|
53
|
|
|
self.assertEqual(len(mail.outbox), 1) |
|
54
|
|
|
|
|
55
|
|
|
# read email |
|
56
|
|
|
email = mail.outbox[0] |
|
57
|
|
|
|
|
58
|
|
|
self.assertEqual( |
|
59
|
|
|
"Error in Template loading: %s" % self.submission_id, |
|
60
|
|
|
email.subject) |
|
61
|
|
|
|
|
62
|
|
|
message = 'Error' |
|
63
|
|
|
notification_message = 'Error in Template loading: Test' |
|
64
|
|
|
|
|
65
|
|
|
self.check_message(message, notification_message) |
|
66
|
|
|
|
|
67
|
|
|
@patch("excel.tasks.upload_template", return_value=True) |
|
68
|
|
|
def test_import_from_template(self, my_upload): |
|
69
|
|
|
"""Testing template import""" |
|
70
|
|
|
|
|
71
|
|
|
# NOTE that I'm calling the function directly, without delay |
|
72
|
|
|
# (AsyncResult). I've patched the time consuming task |
|
73
|
|
|
res = self.my_task.run(submission_id=1) |
|
74
|
|
|
|
|
75
|
|
|
# assert a success with data uploading |
|
76
|
|
|
self.assertEqual(res, "success") |
|
77
|
|
|
|
|
78
|
|
|
# assert that method were called |
|
79
|
|
|
self.assertTrue(my_upload.called) |
|
80
|
|
|
|
|
81
|
|
|
@patch("excel.tasks.upload_template", return_value=False) |
|
82
|
|
|
def test_import_from_template_errors(self, my_upload): |
|
83
|
|
|
"""Testing template import with errors""" |
|
84
|
|
|
|
|
85
|
|
|
# NOTE that I'm calling the function directly, without delay |
|
86
|
|
|
# (AsyncResult). I've patched the time consuming task |
|
87
|
|
|
res = self.my_task.run(submission_id=1) |
|
88
|
|
|
|
|
89
|
|
|
# assert a success with data uploading |
|
90
|
|
|
self.assertEqual(res, "Error in uploading Template data") |
|
91
|
|
|
|
|
92
|
|
|
# assert that method were called |
|
93
|
|
|
self.assertTrue(my_upload.called) |
|
94
|
|
|
|