1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Thu Sep 13 16:19:19 2018 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from unittest.mock import patch, Mock |
10
|
|
|
|
11
|
|
|
from django.test import TestCase |
12
|
|
|
|
13
|
|
|
from ..tasks import ImportCryowebTask |
14
|
|
|
from ..helpers import CryoWebImportError |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class ImportCryowebTest(TestCase): |
18
|
|
|
# import this file and populate database once |
19
|
|
|
fixtures = [ |
20
|
|
|
"image_app/user", |
21
|
|
|
"image_app/dictrole", |
22
|
|
|
"image_app/organization", |
23
|
|
|
"image_app/dictcountry", |
24
|
|
|
"image_app/submission", |
25
|
|
|
"image_app/dictsex", |
26
|
|
|
"language/dictspecie", |
27
|
|
|
"language/speciesynonym" |
28
|
|
|
] |
29
|
|
|
|
30
|
|
|
def setUp(self): |
31
|
|
|
# calling my base setup |
32
|
|
|
super().setUp() |
33
|
|
|
|
34
|
|
|
# define my task |
35
|
|
|
self.my_task = ImportCryowebTask() |
36
|
|
|
|
37
|
|
|
# change lock_id (useful when running test during cron) |
38
|
|
|
self.my_task.lock_id = "test-ImportCryowebTask" |
39
|
|
|
|
40
|
|
|
# patching upload_cryoweb and truncate database |
41
|
|
|
@patch("cryoweb.tasks.truncate_database") |
42
|
|
|
@patch("cryoweb.tasks.cryoweb_import", return_value=True) |
43
|
|
|
@patch("cryoweb.tasks.upload_cryoweb", return_value=True) |
44
|
|
|
def test_import_from_cryoweb( |
45
|
|
|
self, my_upload, my_import, my_truncate): |
46
|
|
|
"""Testing cryoweb import""" |
47
|
|
|
|
48
|
|
|
# NOTE that I'm calling the function directly, without delay |
49
|
|
|
# (AsyncResult). I've patched the time consuming task |
50
|
|
|
res = self.my_task.run(submission_id=1) |
51
|
|
|
|
52
|
|
|
# assert a success with data uploading |
53
|
|
|
self.assertEqual(res, "success") |
54
|
|
|
|
55
|
|
|
# assert that method were called |
56
|
|
|
self.assertTrue(my_upload.called) |
57
|
|
|
self.assertTrue(my_import.called) |
58
|
|
|
|
59
|
|
|
# ensure that database is truncated |
60
|
|
|
self.assertTrue(my_truncate.called) |
61
|
|
|
|
62
|
|
|
@patch('submissions.helpers.send_message_to_websocket') |
63
|
|
|
@patch('asyncio.get_event_loop') |
64
|
|
|
@patch("cryoweb.helpers.cryoweb_has_data", return_value=True) |
65
|
|
|
@patch("cryoweb.tasks.truncate_database") |
66
|
|
|
def test_import_has_data( |
67
|
|
|
self, |
68
|
|
|
my_truncate, |
69
|
|
|
my_has_data, |
70
|
|
|
asyncio_mock, |
71
|
|
|
send_message_to_websocket_mock): |
72
|
|
|
"""Test cryoweb import with data in cryoweb database""" |
73
|
|
|
|
74
|
|
|
# mocking asyncio |
75
|
|
|
tmp = asyncio_mock.return_value |
76
|
|
|
tmp.run_until_complete = Mock() |
77
|
|
|
|
78
|
|
|
# importing data in staged area with data raises an exception |
79
|
|
|
self.assertRaises( |
80
|
|
|
CryoWebImportError, |
81
|
|
|
self.my_task.run, |
82
|
|
|
submission_id=1) |
83
|
|
|
|
84
|
|
|
self.assertTrue(my_has_data.called) |
85
|
|
|
|
86
|
|
|
# When I got an exception, task escapes without cleaning database |
87
|
|
|
self.assertFalse(my_truncate.called) |
88
|
|
|
|
89
|
|
|
# asserting mocked asyncio |
90
|
|
|
self.assertEqual(asyncio_mock.call_count, 1) |
91
|
|
|
self.assertEqual(tmp.run_until_complete.call_count, 1) |
92
|
|
|
self.assertEqual(send_message_to_websocket_mock.call_count, 1) |
93
|
|
|
send_message_to_websocket_mock.assert_called_with( |
94
|
|
|
{'message': 'Error', |
95
|
|
|
'notification_message': 'Error in importing data: ' |
96
|
|
|
'Cryoweb has data'}, 1) |
97
|
|
|
|
98
|
|
|
@patch("cryoweb.tasks.truncate_database") |
99
|
|
|
@patch("cryoweb.tasks.cryoweb_import") |
100
|
|
|
@patch("cryoweb.tasks.upload_cryoweb", return_value=False) |
101
|
|
|
def test_error_in_uploading( |
102
|
|
|
self, my_upload, my_import, my_truncate): |
103
|
|
|
"""Testing error in importing data into cryoweb""" |
104
|
|
|
|
105
|
|
|
res = self.my_task.run(submission_id=1) |
106
|
|
|
|
107
|
|
|
# assert an error in data uploading |
108
|
|
|
self.assertEqual(res, "Error in cryoweb import") |
109
|
|
|
|
110
|
|
|
# assert that method were called |
111
|
|
|
self.assertTrue(my_upload.called) |
112
|
|
|
self.assertFalse(my_import.called) |
113
|
|
|
|
114
|
|
|
# ensure that database is truncated |
115
|
|
|
self.assertTrue(my_truncate.called) |
116
|
|
|
|
117
|
|
|
@patch("cryoweb.tasks.truncate_database") |
118
|
|
|
@patch("cryoweb.tasks.cryoweb_import", return_value=False) |
119
|
|
|
@patch("cryoweb.tasks.upload_cryoweb", return_value=True) |
120
|
|
|
def test_error_in_uploading2( |
121
|
|
|
self, my_upload, my_import, my_truncate): |
122
|
|
|
"""Testing error in importing data from cryoweb to UID""" |
123
|
|
|
|
124
|
|
|
res = self.my_task.run(submission_id=1) |
125
|
|
|
|
126
|
|
|
# assert an error in data uploading |
127
|
|
|
self.assertEqual(res, "Error in cryoweb import") |
128
|
|
|
|
129
|
|
|
# assert that method were called |
130
|
|
|
self.assertTrue(my_upload.called) |
131
|
|
|
self.assertTrue(my_import.called) |
132
|
|
|
|
133
|
|
|
# ensure that database is truncated |
134
|
|
|
self.assertTrue(my_truncate.called) |
135
|
|
|
|
136
|
|
|
# Test a non blocking instance |
137
|
|
|
@patch("redis.lock.Lock.acquire", return_value=False) |
138
|
|
|
@patch("cryoweb.helpers.cryoweb_import") |
139
|
|
|
@patch("cryoweb.helpers.upload_cryoweb", return_value=True) |
140
|
|
|
@patch("cryoweb.models.db_has_data", return_value=False) |
141
|
|
|
@patch("cryoweb.models.truncate_database") |
142
|
|
|
def test_import_from_cryoweb_nb( |
143
|
|
|
self, my_truncate, my_has_data, my_upload, my_import, my_lock): |
144
|
|
|
|
145
|
|
|
# setting task as non-blocking to test stuff |
146
|
|
|
self.my_task.blocking = False |
147
|
|
|
|
148
|
|
|
# NOTE that I'm calling the function directly, without delay |
149
|
|
|
# (AsyncResult). I've patched the time consuming task |
150
|
|
|
res = self.my_task.delay(submission_id=1) |
151
|
|
|
|
152
|
|
|
# assert database is locked |
153
|
|
|
self.assertEqual(res, "Import Cryoweb already running!") |
154
|
|
|
|
155
|
|
|
# assert that methods were not called |
156
|
|
|
self.assertFalse(my_truncate.called) |
157
|
|
|
self.assertFalse(my_upload.called) |
158
|
|
|
self.assertFalse(my_import.called) |
159
|
|
|
self.assertTrue(my_lock.called) |
160
|
|
|
|