Passed
Push — devel ( f2610c...8651b9 )
by Paolo
06:38
created

TestAnnotateBreed.test_issue_in_annotate_breed()   A

Complexity

Conditions 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
rs 9.95
c 0
b 0
f 0
cc 1
nop 2
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Mon May  7 16:23:17 2018
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from unittest.mock import patch
10
11
from django.test import TestCase
12
13
from uid.models import (
14
    DictBreed, DictSpecie, DictCountry, DictUberon, DictDevelStage,
15
    DictPhysioStage)
16
from common.constants import OBO_URL, GOOD, HIGH
17
18
from ..helpers import (
19
    annotate_breed, annotate_specie, annotate_country, annotate_organismpart,
20
    annotate_develstage, annotate_physiostage)
21
22
23
class TestAnnotateBreed(TestCase):
24
    """A class to test annotate breeds"""
25
26
    fixtures = [
27
        "uid/dictbreed",
28
        "uid/dictcountry",
29
        "uid/dictspecie"
30
    ]
31
32
    def setUp(self):
33
        # get a breed object
34
        self.breed = DictBreed.objects.get(pk=1)
35
36
        # erase attributes
37
        self.breed.mapped_breed = None
38
        self.breed.mapped_breed_term = None
39
        self.breed.confidence = None
40
        self.breed.save()
41
42
    @patch("zooma.helpers.use_zooma")
43
    def test_annotate_breed(self, my_zooma):
44
        """Testing annotate breed"""
45
46
        my_zooma.return_value = {
47
            'type': 'breed',
48
            'confidence': 'Good',
49
            'text': 'Bentheim Black Pied',
50
            'ontologyTerms': '%s/LBO_0000347' % (OBO_URL)
51
        }
52
53
        # call my method
54
        annotate_breed(self.breed)
55
56
        self.assertTrue(my_zooma.called)
57
58
        # ensure annotation
59
        self.breed.refresh_from_db()
60
61
        self.assertEqual(self.breed.mapped_breed, "Bentheim Black Pied")
62
        self.assertEqual(self.breed.mapped_breed_term, "LBO_0000347")
63
        self.assertEqual(self.breed.confidence, GOOD)
64
65
    @patch("zooma.helpers.use_zooma")
66
    def test_issue_in_annotate_breed(self, my_zooma):
67
        """Testing annotate breed with a issue in zooma"""
68
69
        my_zooma.side_effect = Exception("Issue with zooma")
70
71
        # deal with exception in zooma
72
        annotate_breed(self.breed)
73
74
        self.assertTrue(my_zooma.called)
75
76
        # non annotation
77
        self.breed.refresh_from_db()
78
79
        # a non annotated pig has at least the general ontology annotation
80
        self.assertEqual(self.breed.mapped_breed, "pig breed")
81
        self.assertEqual(self.breed.mapped_breed_term, "LBO_0000003")
82
        self.assertIsNone(self.breed.confidence)
83
84
85
class TestAnnotateCountry(TestCase):
86
    """A class to test annotate countries"""
87
88
    fixtures = [
89
        "uid/dictcountry",
90
    ]
91
92
    def setUp(self):
93
        # get a country object
94
        self.country = DictCountry.objects.get(pk=1)
95
96
        # erase attributes
97
        self.country.term = None
98
        self.country.confidence = None
99
        self.country.save()
100
101
    @patch("zooma.helpers.use_zooma")
102
    def test_annotate_country(self, my_zooma):
103
        """Testing annotate country"""
104
105
        my_zooma.return_value = {
106
            'type': 'country',
107
            'confidence': 'Good',
108
            'text': 'United Kingdom',
109
            'ontologyTerms': '%s/NCIT_C17233' % (OBO_URL)}
110
111
        # call my method
112
        annotate_country(self.country)
113
114
        self.assertTrue(my_zooma.called)
115
116
        # ensure annotation
117
        self.country.refresh_from_db()
118
119
        self.assertEqual(self.country.label, "United Kingdom")
120
        self.assertEqual(self.country.term, "NCIT_C17233")
121
        self.assertEqual(self.country.confidence, GOOD)
122
123
124
class TestAnnotateSpecie(TestCase):
125
    """A class to test annotate species"""
126
127
    fixtures = [
128
        "uid/dictspecie",
129
    ]
130
131
    def setUp(self):
132
        # get a specie object
133
        self.specie = DictSpecie.objects.get(pk=1)
134
135
        # erase attributes
136
        self.specie.term = None
137
        self.specie.confidence = None
138
        self.specie.save()
139
140
    @patch("zooma.helpers.use_zooma")
141
    def test_annotate_specie(self, my_zooma):
142
        """Testing annotate specie"""
143
144
        my_zooma.return_value = {
145
            'type': 'specie',
146
            'confidence': 'Good',
147
            'text': 'Sus scrofa',
148
            'ontologyTerms': '%s/NCBITaxon_9823' % (OBO_URL)
149
        }
150
151
        # call my method
152
        annotate_specie(self.specie)
153
154
        self.assertTrue(my_zooma.called)
155
156
        # ensure annotation
157
        self.specie.refresh_from_db()
158
159
        self.assertEqual(self.specie.label, "Sus scrofa")
160
        self.assertEqual(self.specie.term, "NCBITaxon_9823")
161
        self.assertEqual(self.specie.confidence, GOOD)
162
163
164
class TestAnnotateUberon(TestCase):
165
    """A class to test annotate uberon"""
166
167
    fixtures = [
168
        "uid/dictuberon",
169
    ]
170
171
    def setUp(self):
172
        # get a specie object
173
        self.part = DictUberon.objects.get(pk=1)
174
175
        # erase attributes
176
        self.part.term = None
177
        self.part.confidence = None
178
        self.part.save()
179
180
    @patch("zooma.helpers.use_zooma")
181
    def test_annotate_uberon(self, my_zooma):
182
        """Testing annotate uberon"""
183
184
        my_zooma.return_value = {
185
            'type': 'organism part',
186
            'confidence': 'Good',
187
            'text': 'semen',
188
            'ontologyTerms': '%s/UBERON_0001968' % (OBO_URL)
189
        }
190
191
        # call my method
192
        annotate_organismpart(self.part)
193
194
        self.assertTrue(my_zooma.called)
195
196
        # ensure annotation
197
        self.part.refresh_from_db()
198
199
        self.assertEqual(self.part.label, "semen")
200
        self.assertEqual(self.part.term, "UBERON_0001968")
201
        self.assertEqual(self.part.confidence, GOOD)
202
203
204
class TestAnnotateDevelStage(TestCase):
205
    """A class to test developmental stage"""
206
207
    fixtures = [
208
        "uid/dictstage",
209
    ]
210
211
    def setUp(self):
212
        # get a specie object
213
        self.stage = DictDevelStage.objects.get(pk=1)
214
215
        # erase attributes
216
        self.stage.term = None
217
        self.stage.confidence = None
218
        self.stage.save()
219
220
    @patch("zooma.helpers.use_zooma")
221
    def test_annotate_dictdevelstage(self, my_zooma):
222
        """Testing annotate dictdevelstage"""
223
224
        my_zooma.return_value = {
225
            'confidence': 'High',
226
            'ontologyTerms': 'http://www.ebi.ac.uk/efo/EFO_0001272',
227
            'text': 'adult',
228
            'type': 'developmental stage'
229
        }
230
231
        # call my method
232
        annotate_develstage(self.stage)
233
234
        self.assertTrue(my_zooma.called)
235
236
        # ensure annotation
237
        self.stage.refresh_from_db()
238
239
        self.assertEqual(self.stage.label, "adult")
240
        self.assertEqual(self.stage.term, "EFO_0001272")
241
        self.assertEqual(self.stage.confidence, HIGH)
242
243
244
class TestAnnotatePhysioStage(TestCase):
245
    """A class to test developmental stage"""
246
247
    fixtures = [
248
        "uid/dictstage",
249
    ]
250
251
    def setUp(self):
252
        # get a specie object
253
        self.stage = DictPhysioStage.objects.get(pk=1)
254
255
        # erase attributes
256
        self.stage.term = None
257
        self.stage.confidence = None
258
        self.stage.save()
259
260
    @patch("zooma.helpers.use_zooma")
261
    def test_annotate_dictphysiostage(self, my_zooma):
262
        """Testing annotate dictphysiostage"""
263
264
        my_zooma.return_value = {
265
            'confidence': 'High',
266
            'ontologyTerms': 'http://purl.obolibrary.org/obo/PATO_0001701',
267
            'text': 'mature',
268
            'type': 'developmental stage'
269
        }
270
271
        # call my method
272
        annotate_physiostage(self.stage)
273
274
        self.assertTrue(my_zooma.called)
275
276
        # ensure annotation
277
        self.stage.refresh_from_db()
278
279
        self.assertEqual(self.stage.label, "mature")
280
        self.assertEqual(self.stage.term, "PATO_0001701")
281
        self.assertEqual(self.stage.confidence, HIGH)
282