Completed
Pull Request — devel (#90)
by Paolo
06:18
created

CommandsTestCase.tearDownClass()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
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 Mon Jan 21 14:30:04 2019
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
import io
10
import json
11
12
from pyUSIrest.auth import Auth
13
from pyUSIrest.usi import Sample
14
from pyUSIrest.exceptions import USIDataError
15
16
from unittest.mock import patch, PropertyMock
17
18
from django.core.management import call_command
19
from django.test import TestCase
20
21
from common.constants import SUBMITTED
22
23
from ..models import ManagedTeam, OrphanSubmission
24
25
from .common import BaseMixin, generate_token, BioSamplesMixin
26
27
28
class CommandsMixin():
29
    @classmethod
30
    def setUpClass(cls):
31
        # calling my base class setup
32
        super().setUpClass()
33
34
        # starting mocked objects
35
        cls.mock_auth_patcher = patch('biosample.helpers.get_manager_auth')
36
        cls.mock_auth = cls.mock_auth_patcher.start()
37
38
    @classmethod
39
    def tearDownClass(cls):
40
        cls.mock_auth_patcher.stop()
41
42
        # calling base method
43
        super().tearDownClass()
44
45
46
class CommandsTestCase(CommandsMixin, BaseMixin, TestCase):
47
    @patch('biosample.helpers.get_manager_auth')
48
    def test_fill_managed(self, my_auth):
49
        """test fill_managed command"""
50
51
        # remove a managed team
52
        managed = ManagedTeam.objects.get(pk=2)
53
        managed.delete()
54
55
        # patch get_manager_auth value
56
        my_auth.return_value = Auth(
57
            token=generate_token(
58
                domains=[
59
                    'subs.test-team-1',
60
                    'subs.test-team-2',
61
                    'subs.test-team-3']
62
            )
63
        )
64
65
        # calling commands
66
        call_command('fill_managed')
67
68
        # get all managedteams object
69
        self.assertEqual(ManagedTeam.objects.count(), 3)
70
        self.assertTrue(my_auth.called)
71
72
    def test_get_json_for_biosample(self):
73
74
        args = ["--submission", 1]
75
76
        # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
77
        with patch('sys.stdout', new_callable=io.StringIO) as handle:
78
            call_command('get_json_for_biosample', *args)
79
            handle.seek(0)
80
81
            data = json.load(handle)
82
            self.assertIsInstance(data, dict)
83
84
85
class BioSampleRemovalTestCase(CommandsMixin, BioSamplesMixin, TestCase):
86
    fixtures = [
87
        'biosample/managedteam',
88
        'biosample/orphansample',
89
        'uid/dictspecie',
90
    ]
91
92
    def setUp(self):
93
        # calling my base class setup
94
        super().setUp()
95
96
        # starting mocked objects
97
        self.mock_root_patcher = patch('pyUSIrest.usi.Root')
98
        self.mock_root = self.mock_root_patcher.start()
99
100
        # start root object
101
        self.my_root = self.mock_root.return_value
102
103
        # mocking chain
104
        self.my_team = self.my_root.get_team_by_name.return_value
105
        self.my_team.name = "subs.test-team-1"
106
107
        # mocking a new submission
108
        self.new_submission = self.my_team.create_submission.return_value
109
        self.new_submission.name = "new-submission"
110
111
        # set status. Because of the way mock attributes are stored you can’t
112
        # directly attach a PropertyMock to a mock object. Instead you can
113
        # attach it to the mock type object:
114
        self.new_submission.propertymock = PropertyMock(return_value='Draft')
115
        type(self.new_submission).status = self.new_submission.propertymock
116
117
    def tearDown(self):
118
        self.mock_root_patcher.stop()
119
120
        super().tearDown()
121
122
    @patch('biosample.helpers.get_manager_auth')
123
    def test_patch_orphan_biosamples(self, my_auth):
124
        """test patch_orphan_biosamples command"""
125
126
        # patch get_manager_auth value
127
        my_auth.return_value = Auth(
128
            token=generate_token()
129
        )
130
131
        # mocking sample creation: two objects: one submitted, one not
132
        self.new_submission.create_sample.side_effect = [
133
            Sample(auth=generate_token()),
134
            USIDataError("test")
135
        ]
136
137
        # calling commands
138
        call_command('patch_orphan_biosamples')
139
140
        # assert a submission created
141
        self.assertTrue(my_auth.called)
142
        self.assertTrue(self.my_team.create_submission.called)
143
        self.assertTrue(self.new_submission.create_sample.called)
144
145
        # check for an object into database
146
        submissions = OrphanSubmission.objects.all()
147
        self.assertEqual(len(submissions), 1)
148
149
        # test submission attributes
150
        submission = submissions[0]
151
        self.assertEqual(submission.usi_submission_name, "new-submission")
152
        self.assertEqual(submission.samples_count, 1)
153
        self.assertEqual(submission.status, SUBMITTED)
154