Passed
Pull Request — master (#26)
by Paolo
02:00
created

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