Completed
Push — master ( 2c16e2...28a4a1 )
by Paolo
17s queued 14s
created

ImportCryowebTest.tearDown()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 1
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
        # mocking annotate with zooma function
41
        self.mock_annotateall_patcher = patch('submissions.tasks.AnnotateAll')
42
        self.mock_annotateall = self.mock_annotateall_patcher.start()
43
44
    def tearDown(self):
45
        # stopping mock objects
46
        self.mock_annotateall_patcher.stop()
47
48
        # calling base methods
49
        super().tearDown()
50
51
    # patching upload_cryoweb and truncate database
52
    @patch("cryoweb.tasks.truncate_database")
53
    @patch("cryoweb.tasks.cryoweb_import", return_value=True)
54
    @patch("cryoweb.tasks.upload_cryoweb", return_value=True)
55
    def test_import_from_cryoweb(
56
            self, my_upload, my_import, my_truncate):
57
        """Testing cryoweb import"""
58
59
        # NOTE that I'm calling the function directly, without delay
60
        # (AsyncResult). I've patched the time consuming task
61
        res = self.my_task.run(submission_id=1)
62
63
        # assert a success with data uploading
64
        self.assertEqual(res, "success")
65
66
        # assert that method were called
67
        self.assertTrue(my_upload.called)
68
        self.assertTrue(my_import.called)
69
70
        # ensure that database is truncated
71
        self.assertTrue(my_truncate.called)
72
73
        # assering zooma called
74
        self.assertTrue(self.mock_annotateall.called)
75
76
    @patch('submissions.helpers.send_message_to_websocket')
77
    @patch('asyncio.get_event_loop')
78
    @patch("cryoweb.helpers.cryoweb_has_data", return_value=True)
79
    @patch("cryoweb.tasks.truncate_database")
80
    def test_import_has_data(
81
            self,
82
            my_truncate,
83
            my_has_data,
84
            asyncio_mock,
85
            send_message_to_websocket_mock):
86
        """Test cryoweb import with data in cryoweb database"""
87
88
        # mocking asyncio
89
        tmp = asyncio_mock.return_value
90
        tmp.run_until_complete = Mock()
91
92
        # importing data in staged area with data raises an exception
93
        self.assertRaises(
94
            CryoWebImportError,
95
            self.my_task.run,
96
            submission_id=1)
97
98
        self.assertTrue(my_has_data.called)
99
100
        # When I got an exception, task escapes without cleaning database
101
        self.assertFalse(my_truncate.called)
102
103
        # asserting mocked asyncio
104
        self.assertEqual(asyncio_mock.call_count, 1)
105
        self.assertEqual(tmp.run_until_complete.call_count, 1)
106
        self.assertEqual(send_message_to_websocket_mock.call_count, 1)
107
        send_message_to_websocket_mock.assert_called_with(
108
            {'message': 'Error',
109
             'notification_message': 'Error in importing data: '
110
                                     'Cryoweb has data'}, 1)
111
112
        # assering zooma not called
113
        self.assertFalse(self.mock_annotateall.called)
114
115
    @patch("cryoweb.tasks.truncate_database")
116
    @patch("cryoweb.tasks.cryoweb_import")
117
    @patch("cryoweb.tasks.upload_cryoweb", return_value=False)
118
    def test_error_in_uploading(
119
            self, my_upload, my_import, my_truncate):
120
        """Testing error in importing data into cryoweb"""
121
122
        res = self.my_task.run(submission_id=1)
123
124
        # assert an error in data uploading
125
        self.assertEqual(res, "Error in cryoweb import")
126
127
        # assert that method were called
128
        self.assertTrue(my_upload.called)
129
        self.assertFalse(my_import.called)
130
131
        # ensure that database is truncated
132
        self.assertTrue(my_truncate.called)
133
134
        # assering zooma not called
135
        self.assertFalse(self.mock_annotateall.called)
136
137
    @patch("cryoweb.tasks.truncate_database")
138
    @patch("cryoweb.tasks.cryoweb_import", return_value=False)
139
    @patch("cryoweb.tasks.upload_cryoweb", return_value=True)
140
    def test_error_in_uploading2(
141
            self, my_upload, my_import, my_truncate):
142
        """Testing error in importing data from cryoweb to UID"""
143
144
        res = self.my_task.run(submission_id=1)
145
146
        # assert an error in data uploading
147
        self.assertEqual(res, "Error in cryoweb import")
148
149
        # assert that method were called
150
        self.assertTrue(my_upload.called)
151
        self.assertTrue(my_import.called)
152
153
        # ensure that database is truncated
154
        self.assertTrue(my_truncate.called)
155
156
        # assering zooma not called
157
        self.assertFalse(self.mock_annotateall.called)
158
159
    # Test a non blocking instance
160
    @patch("redis.lock.Lock.acquire", return_value=False)
161
    @patch("cryoweb.helpers.cryoweb_import")
162
    @patch("cryoweb.helpers.upload_cryoweb", return_value=True)
163
    @patch("cryoweb.models.db_has_data", return_value=False)
164
    @patch("cryoweb.models.truncate_database")
165
    def test_import_from_cryoweb_nb(
166
            self, my_truncate, my_has_data, my_upload, my_import, my_lock):
167
168
        # NOTE that I'm calling the function directly, without delay
169
        # (AsyncResult). I've patched the time consuming task
170
        res = self.my_task.run(submission_id=1)
171
172
        # assert database is locked
173
        self.assertEqual(res, "Import Cryoweb already running!")
174
175
        # assert that methods were not called
176
        self.assertFalse(my_truncate.called)
177
        self.assertFalse(my_upload.called)
178
        self.assertFalse(my_import.called)
179
        self.assertTrue(my_lock.called)
180
181
        # assering zooma not called
182
        self.assertFalse(self.mock_annotateall.called)
183