1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Tue Apr 3 12:16:55 2018 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import os |
10
|
|
|
import json |
11
|
|
|
|
12
|
|
|
from django.test import TestCase |
13
|
|
|
from django.utils import timezone |
14
|
|
|
|
15
|
|
|
from common.constants import OBO_URL |
16
|
|
|
from common.helpers import get_deleted_objects |
17
|
|
|
from common.tests import PersonMixinTestCase |
18
|
|
|
from common.constants import ( |
19
|
|
|
WAITING, LOADED, ERROR, READY, NEED_REVISION, SUBMITTED, COMPLETED) |
20
|
|
|
|
21
|
|
|
from uid.models import ( |
22
|
|
|
Animal, Submission, DictBreed, DictCountry, |
23
|
|
|
DictSex, DictSpecie, Sample, uid_report, Person, User, db_has_data) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class DictSexTestCase(TestCase): |
27
|
|
|
"""Testing DictSex class""" |
28
|
|
|
|
29
|
|
|
fixtures = [ |
30
|
|
|
"uid/dictsex", |
31
|
|
|
"uid/ontology" |
32
|
|
|
] |
33
|
|
|
|
34
|
|
|
def setUp(self): |
35
|
|
|
# my attributes |
36
|
|
|
self.label = 'male' |
37
|
|
|
self.term = 'PATO_0000384' |
38
|
|
|
|
39
|
|
|
def test_str(self): |
40
|
|
|
"""Testing str representation""" |
41
|
|
|
|
42
|
|
|
male = DictSex.objects.get(label=self.label) |
43
|
|
|
self.assertEqual( |
44
|
|
|
str(male), |
45
|
|
|
"{label} ({term})".format( |
46
|
|
|
label=self.label, |
47
|
|
|
term=self.term)) |
48
|
|
|
|
49
|
|
|
def test_format_attribute(self): |
50
|
|
|
"""Testing format attribute""" |
51
|
|
|
|
52
|
|
|
reference = [{ |
53
|
|
|
"value": "male", |
54
|
|
|
"terms": [{ |
55
|
|
|
"url": "%s/PATO_0000384" % (OBO_URL) |
56
|
|
|
}] |
57
|
|
|
}] |
58
|
|
|
|
59
|
|
|
male = DictSex.objects.get(label=self.label) |
60
|
|
|
test = male.format_attribute() |
61
|
|
|
|
62
|
|
|
self.assertEqual(reference, test) |
63
|
|
|
|
64
|
|
|
def test_format_attribute_default(self): |
65
|
|
|
"""Testing format attribute with no library_uri""" |
66
|
|
|
|
67
|
|
|
reference = [{ |
68
|
|
|
"value": "male", |
69
|
|
|
"terms": [{ |
70
|
|
|
"url": "%s/PATO_0000384" % (OBO_URL) |
71
|
|
|
}] |
72
|
|
|
}] |
73
|
|
|
|
74
|
|
|
male = DictSex.objects.get(label=self.label) |
75
|
|
|
male.library_name = None |
76
|
|
|
test = male.format_attribute() |
77
|
|
|
|
78
|
|
|
self.assertEqual(reference, test) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
class DictSpecieTestCase(TestCase): |
82
|
|
|
"""Testing DictSpecie class""" |
83
|
|
|
|
84
|
|
|
fixtures = [ |
85
|
|
|
'uid/dictcountry', |
86
|
|
|
'uid/dictspecie', |
87
|
|
|
'uid/ontology', |
88
|
|
|
'uid/speciesynonym' |
89
|
|
|
] |
90
|
|
|
|
91
|
|
|
def setUp(self): |
92
|
|
|
# my attributes |
93
|
|
|
self.label = 'Sus scrofa' |
94
|
|
|
self.term = 'NCBITaxon_9823' |
95
|
|
|
|
96
|
|
|
def test_str(self): |
97
|
|
|
"""Testing str representation""" |
98
|
|
|
|
99
|
|
|
sus = DictSpecie.objects.get(label=self.label) |
100
|
|
|
self.assertEqual( |
101
|
|
|
str(sus), |
102
|
|
|
"{label} ({term})".format( |
103
|
|
|
label=self.label, |
104
|
|
|
term=self.term)) |
105
|
|
|
|
106
|
|
|
def test_missing_taxon(self): |
107
|
|
|
"""test no term returns no taxon""" |
108
|
|
|
|
109
|
|
|
sus = DictSpecie.objects.get(label=self.label) |
110
|
|
|
sus.term = None |
111
|
|
|
self.assertIsNone(sus.taxon_id) |
112
|
|
|
|
113
|
|
|
def test_get_specie_by_synonym(self): |
114
|
|
|
"""Getting specie using synonym""" |
115
|
|
|
|
116
|
|
|
sus = DictSpecie.get_by_synonym('Pig', 'United Kingdom') |
117
|
|
|
|
118
|
|
|
self.assertEqual(sus.label, self.label) |
119
|
|
|
self.assertEqual(sus.term, self.term) |
120
|
|
|
|
121
|
|
|
# assert a specie in a different language (synonym not defined) |
122
|
|
|
sus = DictSpecie.get_by_synonym('Pig', 'Italy') |
123
|
|
|
|
124
|
|
|
self.assertEqual(sus.label, self.label) |
125
|
|
|
self.assertEqual(sus.term, self.term) |
126
|
|
|
|
127
|
|
|
# using a word not registered returns no data |
128
|
|
|
with self.assertRaises(DictSpecie.DoesNotExist): |
129
|
|
|
DictSpecie.get_by_synonym('Foo', 'United Kingdom') |
130
|
|
|
|
131
|
|
|
def test_get_specie_by_synonym_nospaces(self): |
132
|
|
|
"""Getting specie using synonym, no matters spaces""" |
133
|
|
|
|
134
|
|
|
# assert with spaces |
135
|
|
|
sus = DictSpecie.get_by_synonym('Pig (domestic)', 'United Kingdom') |
136
|
|
|
|
137
|
|
|
self.assertEqual(sus.label, self.label) |
138
|
|
|
self.assertEqual(sus.term, self.term) |
139
|
|
|
|
140
|
|
|
# assert without spaces |
141
|
|
|
sus = DictSpecie.get_by_synonym('Pig(domestic)', 'United Kingdom') |
142
|
|
|
|
143
|
|
|
self.assertEqual(sus.label, self.label) |
144
|
|
|
self.assertEqual(sus.term, self.term) |
145
|
|
|
|
146
|
|
|
def test_get_specie_check_synonyms(self): |
147
|
|
|
"""Get the same specie from latin or dictionary table""" |
148
|
|
|
|
149
|
|
|
# get a country object |
150
|
|
|
language = DictCountry.objects.get(label="Italy") |
151
|
|
|
|
152
|
|
|
sus = DictSpecie.get_specie_check_synonyms( |
153
|
|
|
species_label=self.label, |
154
|
|
|
language=language) |
155
|
|
|
|
156
|
|
|
self.assertEqual(sus.label, self.label) |
157
|
|
|
self.assertEqual(sus.term, self.term) |
158
|
|
|
|
159
|
|
|
sus = DictSpecie.get_specie_check_synonyms( |
160
|
|
|
species_label="Pig", |
161
|
|
|
language=language) |
162
|
|
|
|
163
|
|
|
self.assertEqual(sus.label, self.label) |
164
|
|
|
self.assertEqual(sus.term, self.term) |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
class DictCountryTestCase(TestCase): |
168
|
|
|
"""Testing DictCountry class""" |
169
|
|
|
|
170
|
|
|
fixtures = [ |
171
|
|
|
"uid/dictcountry", |
172
|
|
|
"uid/ontology" |
173
|
|
|
] |
174
|
|
|
|
175
|
|
|
def setUp(self): |
176
|
|
|
# my attributes |
177
|
|
|
self.label = 'United Kingdom' |
178
|
|
|
self.term = 'NCIT_C17233' |
179
|
|
|
|
180
|
|
|
def test_str(self): |
181
|
|
|
"""Testing str representation""" |
182
|
|
|
|
183
|
|
|
uk = DictCountry.objects.get(label=self.label) |
184
|
|
|
self.assertEqual( |
185
|
|
|
str(uk), |
186
|
|
|
"{label} ({term})".format( |
187
|
|
|
label=self.label, |
188
|
|
|
term=self.term)) |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
class DictBreedTestCase(TestCase): |
192
|
|
|
"""Testing DictBreed class""" |
193
|
|
|
|
194
|
|
|
fixtures = [ |
195
|
|
|
"uid/dictbreed", |
196
|
|
|
"uid/dictcountry", |
197
|
|
|
"uid/dictspecie", |
198
|
|
|
"uid/ontology" |
199
|
|
|
] |
200
|
|
|
|
201
|
|
|
def test_str(self): |
202
|
|
|
"""Testing str representation (as mapped_breed, if present)""" |
203
|
|
|
|
204
|
|
|
breed = DictBreed.objects.get(pk=1) |
205
|
|
|
self.assertEqual( |
206
|
|
|
str(breed), |
207
|
|
|
("Bunte Bentheimer - United Kingdom (Bentheim Black Pied, " |
208
|
|
|
"Sus scrofa)")) |
209
|
|
|
|
210
|
|
|
# unset mapped_breed |
211
|
|
|
breed.mapped_breed = None |
212
|
|
|
breed.mapped_breed_term = None |
213
|
|
|
self.assertEqual( |
214
|
|
|
str(breed), |
215
|
|
|
"Bunte Bentheimer - United Kingdom (pig breed, Sus scrofa)") |
216
|
|
|
|
217
|
|
|
def test_default_mapped_breed(self): |
218
|
|
|
"""Test mapped breed returns specie.label if no mapping occours""" |
219
|
|
|
|
220
|
|
|
breed = DictBreed.objects.get(pk=1) |
221
|
|
|
self.assertEqual(breed.mapped_breed, "Bentheim Black Pied") |
222
|
|
|
self.assertEqual(breed.mapped_breed_term, "LBO_0000347") |
223
|
|
|
|
224
|
|
|
# unset mapped_breed |
225
|
|
|
breed.mapped_breed = None |
226
|
|
|
breed.mapped_breed_term = None |
227
|
|
|
self.assertEqual(breed.mapped_breed, "pig breed") |
228
|
|
|
self.assertEqual(breed.mapped_breed_term, "LBO_0000003") |
229
|
|
|
|
230
|
|
|
def test_get_no_mapped_breed(self): |
231
|
|
|
"""Test with a specie not defined in get_general_breed_by_species""" |
232
|
|
|
|
233
|
|
|
specie = DictSpecie(label='Anser anser', term="NCBITaxon_8843") |
234
|
|
|
breed = DictBreed(specie=specie, supplied_breed='Anser anser') |
235
|
|
|
|
236
|
|
|
self.assertIsNone(breed.mapped_breed) |
237
|
|
|
self.assertIsNone(breed.mapped_breed_term) |
238
|
|
|
self.assertIsNone(breed.format_attribute()) |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
class SubmissionTestCase(TestCase): |
242
|
|
|
"""Testing Submission class""" |
243
|
|
|
|
244
|
|
|
fixtures = [ |
245
|
|
|
'uid/dictcountry', |
246
|
|
|
'uid/dictrole', |
247
|
|
|
'uid/ontology', |
248
|
|
|
'uid/organization', |
249
|
|
|
'uid/submission', |
250
|
|
|
'uid/user' |
251
|
|
|
] |
252
|
|
|
|
253
|
|
|
def setUp(self): |
254
|
|
|
self.submission = Submission.objects.get(pk=1) |
255
|
|
|
|
256
|
|
|
def test_str(self): |
257
|
|
|
test = str(self.submission) |
258
|
|
|
reference = "Cryoweb (United Kingdom, test)" |
259
|
|
|
|
260
|
|
|
self.assertEqual(reference, test) |
261
|
|
|
|
262
|
|
|
def test_waiting(self): |
263
|
|
|
"""Test waiting status""" |
264
|
|
|
|
265
|
|
|
# force submission status |
266
|
|
|
self.submission.status = WAITING |
267
|
|
|
|
268
|
|
|
# test my helper methods |
269
|
|
|
self.assertFalse(self.submission.can_edit()) |
270
|
|
|
self.assertFalse(self.submission.can_validate()) |
271
|
|
|
self.assertFalse(self.submission.can_submit()) |
272
|
|
|
|
273
|
|
|
def test_loaded(self): |
274
|
|
|
"""Test loaded status""" |
275
|
|
|
|
276
|
|
|
# force submission status |
277
|
|
|
self.submission.status = LOADED |
278
|
|
|
|
279
|
|
|
# test my helper methods |
280
|
|
|
self.assertTrue(self.submission.can_edit()) |
281
|
|
|
self.assertTrue(self.submission.can_validate()) |
282
|
|
|
self.assertFalse(self.submission.can_submit()) |
283
|
|
|
|
284
|
|
|
def test_submitted(self): |
285
|
|
|
"""Test submitted status""" |
286
|
|
|
|
287
|
|
|
# force submission status |
288
|
|
|
self.submission.status = SUBMITTED |
289
|
|
|
|
290
|
|
|
# test my helper methods |
291
|
|
|
self.assertFalse(self.submission.can_edit()) |
292
|
|
|
self.assertFalse(self.submission.can_validate()) |
293
|
|
|
self.assertFalse(self.submission.can_submit()) |
294
|
|
|
|
295
|
|
|
def test_error(self): |
296
|
|
|
"""Test error status""" |
297
|
|
|
|
298
|
|
|
# force submission status |
299
|
|
|
self.submission.status = ERROR |
300
|
|
|
|
301
|
|
|
# test my helper methods |
302
|
|
|
self.assertTrue(self.submission.can_edit()) |
303
|
|
|
self.assertFalse(self.submission.can_validate()) |
304
|
|
|
self.assertFalse(self.submission.can_submit()) |
305
|
|
|
|
306
|
|
|
def test_need_revision(self): |
307
|
|
|
"""Test need_revision status""" |
308
|
|
|
|
309
|
|
|
# force submission status |
310
|
|
|
self.submission.status = NEED_REVISION |
311
|
|
|
|
312
|
|
|
# test my helper methods |
313
|
|
|
self.assertTrue(self.submission.can_edit()) |
314
|
|
|
self.assertTrue(self.submission.can_validate()) |
315
|
|
|
self.assertFalse(self.submission.can_submit()) |
316
|
|
|
|
317
|
|
|
def test_ready(self): |
318
|
|
|
"""Test ready status""" |
319
|
|
|
|
320
|
|
|
# force submission status |
321
|
|
|
self.submission.status = READY |
322
|
|
|
|
323
|
|
|
# test my helper methods |
324
|
|
|
self.assertTrue(self.submission.can_edit()) |
325
|
|
|
self.assertTrue(self.submission.can_validate()) |
326
|
|
|
self.assertTrue(self.submission.can_submit()) |
327
|
|
|
|
328
|
|
|
def test_completed(self): |
329
|
|
|
"""Test completed status""" |
330
|
|
|
|
331
|
|
|
# force submission status |
332
|
|
|
self.submission.status = COMPLETED |
333
|
|
|
|
334
|
|
|
# test my helper methods |
335
|
|
|
self.assertTrue(self.submission.can_edit()) |
336
|
|
|
self.assertFalse(self.submission.can_validate()) |
337
|
|
|
self.assertFalse(self.submission.can_submit()) |
338
|
|
|
|
339
|
|
|
|
340
|
|
|
class AnimalTestCase(PersonMixinTestCase, TestCase): |
341
|
|
|
"""Testing Animal Class""" |
342
|
|
|
|
343
|
|
|
# an attribute for PersonMixinTestCase |
344
|
|
|
person = Person |
345
|
|
|
|
346
|
|
|
fixtures = [ |
347
|
|
|
'uid/animal', |
348
|
|
|
'uid/dictbreed', |
349
|
|
|
'uid/dictcountry', |
350
|
|
|
'uid/dictrole', |
351
|
|
|
'uid/dictsex', |
352
|
|
|
'uid/dictspecie', |
353
|
|
|
'uid/ontology', |
354
|
|
|
'uid/organization', |
355
|
|
|
'uid/publication', |
356
|
|
|
'uid/submission', |
357
|
|
|
'uid/user' |
358
|
|
|
] |
359
|
|
|
|
360
|
|
|
def setUp(self): |
361
|
|
|
# create animal |
362
|
|
|
self.animal = Animal.objects.get(pk=1) |
363
|
|
|
self.submission = self.animal.submission |
364
|
|
|
|
365
|
|
View Code Duplication |
def test_to_biosample(self): |
|
|
|
|
366
|
|
|
"""Testing JSON conversion for biosample submission""" |
367
|
|
|
|
368
|
|
|
base_dir = os.path.dirname(os.path.abspath(__file__)) |
369
|
|
|
file_path = os.path.join(base_dir, "biosample_animal.json") |
370
|
|
|
handle = open(file_path) |
371
|
|
|
reference = json.load(handle) |
372
|
|
|
|
373
|
|
|
# fix release date to today |
374
|
|
|
now = timezone.now() |
375
|
|
|
reference['releaseDate'] = str(now.date()) |
376
|
|
|
|
377
|
|
|
test = self.animal.to_biosample() |
378
|
|
|
|
379
|
|
|
self.maxDiff = None |
380
|
|
|
self.assertEqual(reference, test) |
381
|
|
|
|
382
|
|
|
def test_to_biosample2(self): |
383
|
|
|
"""testing json conversion with a biosample_id""" |
384
|
|
|
|
385
|
|
|
# assign a different biosample id |
386
|
|
|
reference = "SAMEA4450079" |
387
|
|
|
self.animal.biosample_id = reference |
388
|
|
|
self.animal.save() |
389
|
|
|
|
390
|
|
|
test = self.animal.to_biosample() |
391
|
|
|
|
392
|
|
|
# asserting biosample_.id in response |
393
|
|
|
self.assertEqual(test["accession"], reference) |
394
|
|
|
|
395
|
|
|
def test_relationship(self): |
396
|
|
|
"""Testing an animal who has mother and father""" |
397
|
|
|
|
398
|
|
|
animal = Animal.objects.get(pk=3) |
399
|
|
|
|
400
|
|
|
test = animal.to_biosample() |
401
|
|
|
|
402
|
|
|
reference = [ |
403
|
|
|
{'alias': 'IMAGEA000000001', |
404
|
|
|
'relationshipNature': 'child of'}, |
405
|
|
|
{'alias': 'IMAGEA000000002', |
406
|
|
|
'relationshipNature': 'child of'} |
407
|
|
|
] |
408
|
|
|
|
409
|
|
|
# asserting relationship |
410
|
|
|
self.assertEqual(test['sampleRelationships'], reference) |
411
|
|
|
|
412
|
|
|
# cleaning up relationship (by erasing fk - as crbanim data could be) |
413
|
|
|
# erase father relationship |
414
|
|
|
animal.father = None |
415
|
|
|
animal.save() |
416
|
|
|
|
417
|
|
|
test = animal.to_biosample() |
418
|
|
|
|
419
|
|
|
reference = [ |
420
|
|
|
{'alias': 'IMAGEA000000002', |
421
|
|
|
'relationshipNature': 'child of'}, |
422
|
|
|
] |
423
|
|
|
|
424
|
|
|
# asserting relationship |
425
|
|
|
self.assertEqual(test['sampleRelationships'], reference) |
426
|
|
|
|
427
|
|
|
# cleaning up last relationship |
428
|
|
|
animal.mother = None |
429
|
|
|
animal.save() |
430
|
|
|
|
431
|
|
|
test = animal.to_biosample() |
432
|
|
|
|
433
|
|
|
reference = [] |
434
|
|
|
|
435
|
|
|
# asserting relationship |
436
|
|
|
self.assertEqual(test['sampleRelationships'], reference) |
437
|
|
|
|
438
|
|
|
# TODO: test None rendering |
439
|
|
|
|
440
|
|
|
def test_waiting(self): |
441
|
|
|
"""Test waiting status""" |
442
|
|
|
|
443
|
|
|
# force submission status |
444
|
|
|
self.submission.status = WAITING |
445
|
|
|
self.submission.save() |
446
|
|
|
|
447
|
|
|
# test my helper methods |
448
|
|
|
self.assertFalse(self.animal.can_delete()) |
449
|
|
|
self.assertFalse(self.animal.can_edit()) |
450
|
|
|
|
451
|
|
|
def test_loaded(self): |
452
|
|
|
"""Test loaded status""" |
453
|
|
|
|
454
|
|
|
# force submission status |
455
|
|
|
self.submission.status = LOADED |
456
|
|
|
self.submission.save() |
457
|
|
|
|
458
|
|
|
# test my helper methods |
459
|
|
|
self.assertTrue(self.animal.can_delete()) |
460
|
|
|
self.assertTrue(self.animal.can_edit()) |
461
|
|
|
|
462
|
|
|
def test_submitted(self): |
463
|
|
|
"""Test submitted status""" |
464
|
|
|
|
465
|
|
|
# force submission status |
466
|
|
|
self.submission.status = SUBMITTED |
467
|
|
|
self.submission.save() |
468
|
|
|
|
469
|
|
|
# test my helper methods |
470
|
|
|
self.assertFalse(self.animal.can_delete()) |
471
|
|
|
self.assertFalse(self.animal.can_edit()) |
472
|
|
|
|
473
|
|
|
def test_error(self): |
474
|
|
|
"""Test error status""" |
475
|
|
|
|
476
|
|
|
# force submission status |
477
|
|
|
self.submission.status = ERROR |
478
|
|
|
self.submission.save() |
479
|
|
|
|
480
|
|
|
# test my helper methods |
481
|
|
|
self.assertTrue(self.animal.can_delete()) |
482
|
|
|
self.assertTrue(self.animal.can_edit()) |
483
|
|
|
|
484
|
|
|
def test_need_revision(self): |
485
|
|
|
"""Test need_revision status""" |
486
|
|
|
|
487
|
|
|
# force submission status |
488
|
|
|
self.submission.status = NEED_REVISION |
489
|
|
|
self.submission.save() |
490
|
|
|
|
491
|
|
|
# test my helper methods |
492
|
|
|
self.assertTrue(self.animal.can_delete()) |
493
|
|
|
self.assertTrue(self.animal.can_edit()) |
494
|
|
|
|
495
|
|
|
def test_ready(self): |
496
|
|
|
"""Test ready status""" |
497
|
|
|
|
498
|
|
|
# force submission status |
499
|
|
|
self.submission.status = READY |
500
|
|
|
self.submission.save() |
501
|
|
|
|
502
|
|
|
# test my helper methods |
503
|
|
|
self.assertTrue(self.animal.can_delete()) |
504
|
|
|
self.assertTrue(self.animal.can_edit()) |
505
|
|
|
|
506
|
|
|
def test_completed(self): |
507
|
|
|
"""Test completed status""" |
508
|
|
|
|
509
|
|
|
# force submission status |
510
|
|
|
self.submission.status = COMPLETED |
511
|
|
|
self.submission.save() |
512
|
|
|
|
513
|
|
|
# test my helper methods |
514
|
|
|
self.assertTrue(self.animal.can_delete()) |
515
|
|
|
self.assertTrue(self.animal.can_edit()) |
516
|
|
|
|
517
|
|
|
|
518
|
|
|
class SampleTestCase(PersonMixinTestCase, TestCase): |
519
|
|
|
"""testing sample class""" |
520
|
|
|
|
521
|
|
|
# an attribute for PersonMixinTestCase |
522
|
|
|
person = Person |
523
|
|
|
|
524
|
|
|
fixtures = [ |
525
|
|
|
'uid/animal', |
526
|
|
|
'uid/dictbreed', |
527
|
|
|
'uid/dictcountry', |
528
|
|
|
'uid/dictrole', |
529
|
|
|
'uid/dictsex', |
530
|
|
|
'uid/dictspecie', |
531
|
|
|
'uid/dictstage', |
532
|
|
|
'uid/dictuberon', |
533
|
|
|
'uid/ontology', |
534
|
|
|
'uid/organization', |
535
|
|
|
'uid/publication', |
536
|
|
|
'uid/sample', |
537
|
|
|
'uid/submission', |
538
|
|
|
'uid/user' |
539
|
|
|
] |
540
|
|
|
|
541
|
|
|
def setUp(self): |
542
|
|
|
# create animal |
543
|
|
|
self.animal = Animal.objects.get(pk=1) |
544
|
|
|
|
545
|
|
|
# now get a sample object |
546
|
|
|
self.sample = Sample.objects.get(pk=1) |
547
|
|
|
|
548
|
|
|
# set submission |
549
|
|
|
self.submission = self.sample.submission |
550
|
|
|
|
551
|
|
View Code Duplication |
def test_to_biosample(self): |
|
|
|
|
552
|
|
|
"""Testing JSON conversion for biosample submission""" |
553
|
|
|
|
554
|
|
|
base_dir = os.path.dirname(os.path.abspath(__file__)) |
555
|
|
|
file_path = os.path.join(base_dir, "biosample_sample.json") |
556
|
|
|
handle = open(file_path) |
557
|
|
|
reference = json.load(handle) |
558
|
|
|
|
559
|
|
|
# fix release date to today |
560
|
|
|
now = timezone.now() |
561
|
|
|
reference['releaseDate'] = str(now.date()) |
562
|
|
|
|
563
|
|
|
test = self.sample.to_biosample() |
564
|
|
|
|
565
|
|
|
self.maxDiff = None |
566
|
|
|
self.assertEqual(reference, test) |
567
|
|
|
|
568
|
|
|
def test_to_biosample2(self): |
569
|
|
|
"""testing json conversion with a biosample_id""" |
570
|
|
|
|
571
|
|
|
# assign a different biosample id |
572
|
|
|
animal_reference = "SAMEA4450079" |
573
|
|
|
self.animal.biosample_id = animal_reference |
574
|
|
|
self.animal.save() |
575
|
|
|
|
576
|
|
|
sample_reference = "SAMEA4450075" |
577
|
|
|
self.sample.biosample_id = sample_reference |
578
|
|
|
self.sample.save() |
579
|
|
|
|
580
|
|
|
test = self.sample.to_biosample() |
581
|
|
|
|
582
|
|
|
# asserting biosample_.id in response |
583
|
|
|
self.assertEqual(test["accession"], sample_reference) |
584
|
|
|
|
585
|
|
|
# asserting animal biosample_id in relatioship |
586
|
|
|
reference = [{ |
587
|
|
|
"accession": animal_reference, |
588
|
|
|
"relationshipNature": "derived from", |
589
|
|
|
}] |
590
|
|
|
self.assertEqual(test['sampleRelationships'], reference) |
591
|
|
|
|
592
|
|
|
def test_to_biosample_no_foreign(self): |
593
|
|
|
"""Testing JSON conversion for biosample submission without |
594
|
|
|
foreign keys""" |
595
|
|
|
|
596
|
|
|
base_dir = os.path.dirname(os.path.abspath(__file__)) |
597
|
|
|
file_path = os.path.join(base_dir, "biosample_sample.json") |
598
|
|
|
handle = open(file_path) |
599
|
|
|
reference = json.load(handle) |
600
|
|
|
|
601
|
|
|
# fix release date to today |
602
|
|
|
now = timezone.now() |
603
|
|
|
reference['releaseDate'] = str(now.date()) |
604
|
|
|
|
605
|
|
|
# remove foreign keys from reference |
606
|
|
|
for key in ["Organism part", "Developmental stage"]: |
607
|
|
|
if key in reference["attributes"]: |
608
|
|
|
del(reference["attributes"][key]) |
609
|
|
|
|
610
|
|
|
# remove foreing keys from sample object |
611
|
|
|
self.sample.organism_part = None |
612
|
|
|
self.sample.developmental_stage = None |
613
|
|
|
self.sample.save() |
614
|
|
|
|
615
|
|
|
test = self.sample.to_biosample() |
616
|
|
|
|
617
|
|
|
self.maxDiff = None |
618
|
|
|
self.assertEqual(reference, test) |
619
|
|
|
|
620
|
|
|
# TODO: test biosample with None rendering |
621
|
|
|
|
622
|
|
|
def test_related_object(self): |
623
|
|
|
"""Testing relationship using get_deleted_objects""" |
624
|
|
|
|
625
|
|
|
deletable_objects, model_count, protected = get_deleted_objects( |
626
|
|
|
[self.animal]) |
627
|
|
|
|
628
|
|
|
self.assertEqual(model_count, {'animals': 2, 'samples': 1}) |
629
|
|
|
|
630
|
|
|
def test_uid_report(self): |
631
|
|
|
"""testing uid report after a Sample and Animal insert""" |
632
|
|
|
|
633
|
|
|
reference = { |
634
|
|
|
'n_of_animals': 3, |
635
|
|
|
'n_of_samples': 1, |
636
|
|
|
'breeds_total': 1, |
637
|
|
|
'breeds_without_ontology': 0, |
638
|
|
|
'countries_total': 3, |
639
|
|
|
'countries_without_ontology': 0, |
640
|
|
|
'species_total': 1, |
641
|
|
|
'species_without_ontology': 0, |
642
|
|
|
'developmental_stages_total': 1, |
643
|
|
|
'developmental_stages_without_ontology': 0, |
644
|
|
|
'organism_parts_total': 1, |
645
|
|
|
'organism_parts_without_ontology': 0, |
646
|
|
|
'physiological_stages_total': 1, |
647
|
|
|
'physiological_stages_without_ontology': 0, |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
user = User.objects.get(pk=1) |
651
|
|
|
|
652
|
|
|
test = uid_report(user) |
653
|
|
|
|
654
|
|
|
self.assertEqual(reference, test) |
655
|
|
|
|
656
|
|
|
def test_uid_has_data(self): |
657
|
|
|
"""testing db_has_data for uid""" |
658
|
|
|
|
659
|
|
|
self.assertTrue(db_has_data()) |
660
|
|
|
|
661
|
|
|
def test_waiting(self): |
662
|
|
|
"""Test waiting status""" |
663
|
|
|
|
664
|
|
|
# force submission status |
665
|
|
|
self.submission.status = WAITING |
666
|
|
|
self.submission.save() |
667
|
|
|
|
668
|
|
|
# test my helper methods |
669
|
|
|
self.assertFalse(self.sample.can_delete()) |
670
|
|
|
self.assertFalse(self.sample.can_edit()) |
671
|
|
|
|
672
|
|
|
def test_loaded(self): |
673
|
|
|
"""Test loaded status""" |
674
|
|
|
|
675
|
|
|
# force submission status |
676
|
|
|
self.submission.status = LOADED |
677
|
|
|
self.submission.save() |
678
|
|
|
|
679
|
|
|
# test my helper methods |
680
|
|
|
self.assertTrue(self.sample.can_delete()) |
681
|
|
|
self.assertTrue(self.sample.can_edit()) |
682
|
|
|
|
683
|
|
|
def test_submitted(self): |
684
|
|
|
"""Test submitted status""" |
685
|
|
|
|
686
|
|
|
# force submission status |
687
|
|
|
self.submission.status = SUBMITTED |
688
|
|
|
self.submission.save() |
689
|
|
|
|
690
|
|
|
# test my helper methods |
691
|
|
|
self.assertFalse(self.sample.can_delete()) |
692
|
|
|
self.assertFalse(self.sample.can_edit()) |
693
|
|
|
|
694
|
|
|
def test_error(self): |
695
|
|
|
"""Test error status""" |
696
|
|
|
|
697
|
|
|
# force submission status |
698
|
|
|
self.submission.status = ERROR |
699
|
|
|
self.submission.save() |
700
|
|
|
|
701
|
|
|
# test my helper methods |
702
|
|
|
self.assertTrue(self.sample.can_delete()) |
703
|
|
|
self.assertTrue(self.sample.can_edit()) |
704
|
|
|
|
705
|
|
|
def test_need_revision(self): |
706
|
|
|
"""Test need_revision status""" |
707
|
|
|
|
708
|
|
|
# force submission status |
709
|
|
|
self.submission.status = NEED_REVISION |
710
|
|
|
self.submission.save() |
711
|
|
|
|
712
|
|
|
# test my helper methods |
713
|
|
|
self.assertTrue(self.sample.can_delete()) |
714
|
|
|
self.assertTrue(self.sample.can_edit()) |
715
|
|
|
|
716
|
|
|
def test_ready(self): |
717
|
|
|
"""Test ready status""" |
718
|
|
|
|
719
|
|
|
# force submission status |
720
|
|
|
self.submission.status = READY |
721
|
|
|
self.submission.save() |
722
|
|
|
|
723
|
|
|
# test my helper methods |
724
|
|
|
self.assertTrue(self.sample.can_delete()) |
725
|
|
|
self.assertTrue(self.sample.can_edit()) |
726
|
|
|
|
727
|
|
|
def test_completed(self): |
728
|
|
|
"""Test completed status""" |
729
|
|
|
|
730
|
|
|
# force submission status |
731
|
|
|
self.submission.status = COMPLETED |
732
|
|
|
self.submission.save() |
733
|
|
|
|
734
|
|
|
# test my helper methods |
735
|
|
|
self.assertTrue(self.sample.can_delete()) |
736
|
|
|
self.assertTrue(self.sample.can_edit()) |
737
|
|
|
|
738
|
|
|
|
739
|
|
|
class PersonTestCase(PersonMixinTestCase, TestCase): |
740
|
|
|
"""Testing Person Class""" |
741
|
|
|
|
742
|
|
|
# an attribute for PersonMixinTestCase |
743
|
|
|
person = Person |
744
|
|
|
|
745
|
|
|
fixtures = [ |
746
|
|
|
'uid/dictcountry', |
747
|
|
|
'uid/dictrole', |
748
|
|
|
'uid/organization', |
749
|
|
|
'uid/user' |
750
|
|
|
] |
751
|
|
|
|
752
|
|
|
def setUp(self): |
753
|
|
|
# get a person |
754
|
|
|
self.person = Person.objects.get(user__id=1) |
755
|
|
|
|
756
|
|
|
def test_str(self): |
757
|
|
|
"""test a repr method""" |
758
|
|
|
|
759
|
|
|
test = str(self.person) |
760
|
|
|
self.assertEqual( |
761
|
|
|
test, "Foo Bar (Test organization (United Kingdom))") |
762
|
|
|
|