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