Passed
Pull Request — devel (#90)
by Paolo
06:10
created

biosample.tests.test_async_biosample   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 72
dl 0
loc 117
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A AsyncBioSamplesTestCase.setUp() 0 19 1
A AsyncBioSamplesTestCase.test_request() 0 24 3
A AsyncBioSamplesTestCase.setUpClass() 0 7 1
A AsyncBioSamplesTestCase.tearDownClass() 0 6 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Tue Jan 21 10:54:09 2020
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
import os
10
import asynctest
11
12
from aioresponses import aioresponses
13
from django.test import TestCase
14
from unittest.mock import patch, Mock
15
16
from common.constants import BIOSAMPLE_URL
17
from uid.models import Animal as UIDAnimal, Sample as UIDSample
18
19
from ..tasks.cleanup import check_samples
20
from ..models import OrphanSample
21
22
from .common import generate_token
23
24
# get my path
25
dir_path = os.path.dirname(os.path.realpath(__file__))
26
27
# define data path
28
DATA_PATH = os.path.join(dir_path, "data")
29
30
31
with open(os.path.join(DATA_PATH, "page_0.json")) as handle:
32
    page0 = handle.read()
33
34
with open(os.path.join(DATA_PATH, "page_1.json")) as handle:
35
    page1 = handle.read()
36
37
38
# TODO: need mocking the get_manager_auth function
39
class AsyncBioSamplesTestCase(asynctest.TestCase, TestCase):
40
41
    fixtures = [
42
        'uid/animal',
43
        'uid/dictbreed',
44
        'uid/dictcountry',
45
        'uid/dictrole',
46
        'uid/dictsex',
47
        'uid/dictspecie',
48
        'uid/dictstage',
49
        'uid/dictuberon',
50
        'uid/ontology',
51
        'uid/organization',
52
        'uid/publication',
53
        'uid/sample',
54
        'uid/submission',
55
        'uid/user'
56
    ]
57
58
    @classmethod
59
    def setUpClass(cls):
60
        # calling my base class setup
61
        super().setUpClass()
62
63
        cls.mock_auth_patcher = patch('pyUSIrest.auth.requests.get')
64
        cls.mock_auth = cls.mock_auth_patcher.start()
65
66
    @classmethod
67
    def tearDownClass(cls):
68
        cls.mock_auth_patcher.stop()
69
70
        # calling base method
71
        super().tearDownClass()
72
73
    def setUp(self):
74
        # calling my base setup
75
        super().setUp()
76
77
        # well, updating data and set two biosample ids. Those are not
78
        # orphans
79
        animal = UIDAnimal.objects.get(pk=1)
80
        animal.biosample_id = "SAMEA6376980"
81
        animal.save()
82
83
        sample = UIDSample.objects.get(pk=1)
84
        sample.biosample_id = "SAMEA6376982"
85
        sample.save()
86
87
        # generate tocken
88
        self.mock_auth.return_value = Mock()
89
        self.mock_auth.return_value.text = generate_token(
90
            domains=['subs.test-team-19'])
91
        self.mock_auth.return_value.status_code = 200
92
93
    async def test_request(self) -> None:
94
        with aioresponses() as mocked:
95
            mocked.get(
96
                '{url}?filter=attr:project:IMAGE&size=20'.format(
97
                    url=BIOSAMPLE_URL),
98
                status=200,
99
                body=page0)
100
            mocked.get(
101
                '{url}?filter=attr:project:IMAGE&page=1&size=20'.format(
102
                    url=BIOSAMPLE_URL),
103
                status=200,
104
                body=page1)
105
106
            await check_samples()
107
108
            # get accessions
109
            reference = ['SAMEA6376991', 'SAMEA6376992']
110
111
            self.assertEqual(OrphanSample.objects.count(), 2)
112
113
            # check objects into UID
114
            for accession in reference:
115
                qs = OrphanSample.objects.filter(biosample_id=accession)
116
                self.assertTrue(qs.exists())
117