Passed
Push — master ( f95e2c...db2fec )
by Paolo
01:45
created

SubmissionTestCase.test_completed()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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