Completed
Push — master ( 8a2447...e2760f )
by Paolo
06:42
created

SubmissionTestCase.test_empty_submission()   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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