1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Tue Jul 2 10:58:42 2019 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import types |
10
|
|
|
from collections import defaultdict |
11
|
|
|
|
12
|
|
|
from django.test import TestCase |
13
|
|
|
|
14
|
|
|
from common.constants import ERROR, LOADED |
15
|
|
|
from common.tests import WebSocketMixin |
16
|
|
|
from image_app.models import ( |
17
|
|
|
Submission, Animal, Sample, DictCountry, DictSpecie, DictSex, db_has_data) |
18
|
|
|
|
19
|
|
|
from ..helpers import ExcelTemplate, upload_template |
20
|
|
|
from .common import BaseExcelMixin |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class ExcelTemplateTestCase(BaseExcelMixin, TestCase): |
24
|
|
|
"""Test excel class upload""" |
25
|
|
|
|
26
|
|
|
def setUp(self): |
27
|
|
|
# calling my base class setup |
28
|
|
|
super().setUp() |
29
|
|
|
|
30
|
|
|
# crate a Excel Template object |
31
|
|
|
self.reader = ExcelTemplate() |
32
|
|
|
|
33
|
|
|
# get filenames for DataSourceMixinTestCase.dst_path |
34
|
|
|
self.reader.read_file(self.dst_path) |
35
|
|
|
|
36
|
|
|
def test_check_sheets(self): |
37
|
|
|
# test check sheets method |
38
|
|
|
status, not_found = self.reader.check_sheets() |
39
|
|
|
|
40
|
|
|
self.assertTrue(status) |
41
|
|
|
self.assertEqual(not_found, []) |
42
|
|
|
|
43
|
|
|
def test_check_columns(self): |
44
|
|
|
# test check sheets method |
45
|
|
|
status, not_found = self.reader.check_columns() |
46
|
|
|
|
47
|
|
|
self.assertTrue(status) |
48
|
|
|
self.assertIsInstance(not_found, defaultdict) |
49
|
|
|
self.assertEqual(len(not_found), 0) |
50
|
|
|
|
51
|
|
|
def check_generator(self, records, length): |
52
|
|
|
self.assertIsInstance(records, types.GeneratorType) |
53
|
|
|
self.assertEqual(len(list(records)), length) |
54
|
|
|
|
55
|
|
|
def test_get_breed_records(self): |
56
|
|
|
"""get_breed_records returns an iterator""" |
57
|
|
|
|
58
|
|
|
breeds = self.reader.get_breed_records() |
59
|
|
|
self.check_generator(breeds, 2) |
60
|
|
|
|
61
|
|
|
def test_get_animal_records(self): |
62
|
|
|
"""get_animal_records returns an iterator""" |
63
|
|
|
|
64
|
|
|
animals = self.reader.get_animal_records() |
65
|
|
|
self.check_generator(animals, 3) |
66
|
|
|
|
67
|
|
|
def test_get_sample_records(self): |
68
|
|
|
"""get_sample_records returns an iterator""" |
69
|
|
|
|
70
|
|
|
samples = self.reader.get_sample_records() |
71
|
|
|
self.check_generator(samples, 3) |
72
|
|
|
|
73
|
|
|
# TODO: pretty similar to CRBanim - move to a common mixin |
74
|
|
View Code Duplication |
def test_check_species(self): |
|
|
|
|
75
|
|
|
"""Test check species method""" |
76
|
|
|
|
77
|
|
|
# get a country |
78
|
|
|
country = DictCountry.objects.get(label="United Kingdom") |
79
|
|
|
|
80
|
|
|
check, not_found = self.reader.check_species(country) |
81
|
|
|
|
82
|
|
|
self.assertTrue(check) |
83
|
|
|
self.assertEqual(len(not_found), 0) |
84
|
|
|
|
85
|
|
|
# changing species set |
86
|
|
|
DictSpecie.objects.filter(label='Sus scrofa').delete() |
87
|
|
|
|
88
|
|
|
check, not_found = self.reader.check_species(country) |
89
|
|
|
|
90
|
|
|
# the read species are not included in fixtures |
91
|
|
|
self.assertFalse(check) |
92
|
|
|
self.assertGreater(len(not_found), 0) |
93
|
|
|
|
94
|
|
|
# TODO: identical to CRBanim - move to a common mixin |
95
|
|
|
def test_check_sex(self): |
96
|
|
|
"""Test check sex method""" |
97
|
|
|
|
98
|
|
|
check, not_found = self.reader.check_sex() |
99
|
|
|
|
100
|
|
|
self.assertTrue(check) |
101
|
|
|
self.assertEqual(len(not_found), 0) |
102
|
|
|
|
103
|
|
|
# changing sex set |
104
|
|
|
DictSex.objects.filter(label='female').delete() |
105
|
|
|
|
106
|
|
|
check, not_found = self.reader.check_sex() |
107
|
|
|
|
108
|
|
|
# the read species are not included in fixtures |
109
|
|
|
self.assertFalse(check) |
110
|
|
|
self.assertGreater(len(not_found), 0) |
111
|
|
|
|
112
|
|
|
def test_check_accuracies(self): |
113
|
|
|
"""Test check accuracies method""" |
114
|
|
|
|
115
|
|
|
check, not_found = self.reader.check_accuracies() |
116
|
|
|
|
117
|
|
|
self.assertTrue(check) |
118
|
|
|
self.assertEqual(len(not_found), 0) |
119
|
|
|
|
120
|
|
|
|
121
|
|
View Code Duplication |
class ExcelMixin(WebSocketMixin, BaseExcelMixin): |
|
|
|
|
122
|
|
|
"""Common tests for Excel classes""" |
123
|
|
|
|
124
|
|
|
def test_upload_template(self): |
125
|
|
|
"""Testing uploading and importing data from excel template to UID""" |
126
|
|
|
|
127
|
|
|
# assert upload |
128
|
|
|
self.assertTrue(upload_template(self.submission)) |
129
|
|
|
|
130
|
|
|
# reload submission |
131
|
|
|
self.submission.refresh_from_db() |
132
|
|
|
|
133
|
|
|
# assert submission messages |
134
|
|
|
self.assertEqual( |
135
|
|
|
self.submission.status, |
136
|
|
|
LOADED) |
137
|
|
|
|
138
|
|
|
self.assertIn( |
139
|
|
|
"Template import completed for submission", |
140
|
|
|
self.submission.message) |
141
|
|
|
|
142
|
|
|
# assert data into database |
143
|
|
|
self.assertTrue(db_has_data()) |
144
|
|
|
self.assertTrue(Animal.objects.exists()) |
145
|
|
|
self.assertTrue(Sample.objects.exists()) |
146
|
|
|
|
147
|
|
|
# check async message called |
148
|
|
|
message = 'Loaded' |
149
|
|
|
notification_message = ( |
150
|
|
|
'Template import completed for submission: 1') |
151
|
|
|
validation_message = { |
152
|
|
|
'animals': 3, 'samples': 3, |
153
|
|
|
'animal_unkn': 3, 'sample_unkn': 3, |
154
|
|
|
'animal_issues': 0, 'sample_issues': 0} |
155
|
|
|
|
156
|
|
|
self.check_message(message, notification_message, validation_message) |
157
|
|
|
|
158
|
|
|
def check_errors(self, my_check, message, notification_message): |
159
|
|
|
"""Common stuff for error in excel template loading""" |
160
|
|
|
|
161
|
|
|
self.assertFalse(upload_template(self.submission)) |
162
|
|
|
|
163
|
|
|
# reload submission |
164
|
|
|
self.submission.refresh_from_db() |
165
|
|
|
|
166
|
|
|
# test my mock method called |
167
|
|
|
self.assertTrue(my_check.called) |
168
|
|
|
|
169
|
|
|
# reload submission |
170
|
|
|
self.submission = Submission.objects.get(pk=1) |
171
|
|
|
|
172
|
|
|
self.assertEqual( |
173
|
|
|
self.submission.status, |
174
|
|
|
ERROR) |
175
|
|
|
|
176
|
|
|
# check for two distinct messages |
177
|
|
|
self.assertIn( |
178
|
|
|
message, |
179
|
|
|
self.submission.message) |
180
|
|
|
|
181
|
|
|
self.assertNotIn( |
182
|
|
|
"Template import completed for submission", |
183
|
|
|
self.submission.message) |
184
|
|
|
|
185
|
|
|
# assert data into database |
186
|
|
|
self.assertFalse(db_has_data()) |
187
|
|
|
self.assertFalse(Animal.objects.exists()) |
188
|
|
|
self.assertFalse(Sample.objects.exists()) |
189
|
|
|
|
190
|
|
|
# check async message called |
191
|
|
|
self.check_message('Error', notification_message) |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
class UploadTemplateTestCase(ExcelMixin, TestCase): |
195
|
|
|
"""Test uploading data for Template excel path""" |
196
|
|
|
|
197
|
|
|
pass |
198
|
|
|
|