Completed
Push — master ( 4f7ee6...646424 )
by Paolo
08:30 queued 06:53
created

uid.tests.test_helpers   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 82
dl 0
loc 121
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A CreateOrGetTestCase.test_get_or_create_obj() 0 6 1
A GetModelObjectTestCase.setUp() 0 3 1
A ParseImageAliasTestCase.test_wrong_alias() 0 8 1
A GetModelObjectTestCase.test_get_model_object() 0 17 1
A CreateOrGetTestCase.test_update_or_create_obj() 0 6 1
A ParseImageAliasTestCase.setUp() 0 3 1
A ParseImageAliasTestCase.test_parse_aliases() 0 15 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Wed Feb 14 10:34:44 2018
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from django.test import TestCase
10
11
from uid.models import Animal, Sample, DictSex
12
13
from ..helpers import (
14
    get_model_object, parse_image_alias, get_or_create_obj,
15
    update_or_create_obj)
16
17
18
class GetModelObjectTestCase(TestCase):
19
    fixtures = [
20
        'uid/animal',
21
        'uid/dictbreed',
22
        'uid/dictcountry',
23
        'uid/dictrole',
24
        'uid/dictsex',
25
        'uid/dictspecie',
26
        'uid/dictstage',
27
        'uid/dictuberon',
28
        'uid/organization',
29
        'uid/publication',
30
        'uid/sample',
31
        'uid/submission',
32
        'uid/user',
33
    ]
34
35
    def setUp(self):
36
        self.animal_id = 1
37
        self.sample_id = 1
38
39
    def test_get_model_object(self):
40
41
        test = get_model_object("Animal", 1)
42
        self.assertEqual(test.id, self.animal_id)
43
        self.assertIsInstance(test, Animal)
44
45
        test = get_model_object("Sample", 1)
46
        self.assertEqual(test.id, self.sample_id)
47
        self.assertIsInstance(test, Sample)
48
49
        # assert errors
50
        self.assertRaisesMessage(
51
            Exception,
52
            "Unknown table",
53
            get_model_object,
54
            "Name",
55
            1)
56
57
58
class ParseImageAliasTestCase(TestCase):
59
    fixtures = [
60
        'uid/animal',
61
        'uid/dictbreed',
62
        'uid/dictcountry',
63
        'uid/dictrole',
64
        'uid/dictsex',
65
        'uid/dictspecie',
66
        'uid/dictstage',
67
        'uid/dictuberon',
68
        'uid/organization',
69
        'uid/publication',
70
        'uid/sample',
71
        'uid/submission',
72
        'uid/user',
73
    ]
74
75
    def setUp(self):
76
        self.animal = Animal.objects.get(pk=1)
77
        self.sample = Sample.objects.get(pk=1)
78
79
    def test_parse_aliases(self):
80
        animal_alias = self.animal.biosample_alias
81
        sample_alias = self.sample.biosample_alias
82
83
        # test animal
84
        table, pk = parse_image_alias(animal_alias)
85
86
        self.assertEqual(table, "Animal")
87
        self.assertEqual(pk, self.animal.id)
88
89
        # test sample
90
        table, pk = parse_image_alias(sample_alias)
91
92
        self.assertEqual(table, "Sample")
93
        self.assertEqual(pk, self.sample.id)
94
95
    def test_wrong_alias(self):
96
        """Test a wrong alias format"""
97
98
        self.assertRaisesMessage(
99
            Exception,
100
            "Cannot deal with 'FAKEA0000123456'",
101
            parse_image_alias,
102
            "FAKEA0000123456")
103
104
105
class CreateOrGetTestCase(TestCase):
106
    fixtures = ['uid/dictsex']
107
108
    def test_get_or_create_obj(self):
109
        sex = get_or_create_obj(DictSex, label="male")
110
        self.assertIsInstance(sex, DictSex)
111
112
        sex = get_or_create_obj(DictSex, label="foo")
113
        self.assertIsInstance(sex, DictSex)
114
115
    def test_update_or_create_obj(self):
116
        sex = update_or_create_obj(DictSex, label="male", term="PATO_0000384")
117
        self.assertIsInstance(sex, DictSex)
118
119
        sex = update_or_create_obj(DictSex, label="foo", term="bar")
120
        self.assertIsInstance(sex, DictSex)
121