1
1
"""This modules includes unit tests for the service.py module."""
2
2
3
3
from datetime import datetime
4
- from pkg_resources import DistributionNotFound
5
4
6
- from delayed_assert import assert_expectations , expect
7
5
import pytest
6
+ from delayed_assert import assert_expectations , expect
7
+ from pkg_resources import DistributionNotFound
8
8
from six .moves import mock
9
9
10
10
from reportportal_client .service import (
@@ -24,40 +24,40 @@ class TestServiceFunctions:
24
24
25
25
def test_check_convert_to_string (self ):
26
26
"""Test for support and convert strings to utf-8."""
27
- expect (_convert_string (" Hello world" ) == 'Hello world' )
28
- expect (lambda : isinstance (_convert_string (" Hello world" ), str ))
27
+ expect (_convert_string (' Hello world' ) == 'Hello world' )
28
+ expect (lambda : isinstance (_convert_string (' Hello world' ), str ))
29
29
assert_expectations ()
30
30
31
31
@pytest .mark .parametrize ('system' , [True , False ])
32
32
def test_dict_to_payload_with_system_key (self , system ):
33
33
"""Test convert dict to list of dicts with key system."""
34
- initial_dict = {"aa" : 1 , "b" : 2 , " system" : system }
34
+ initial_dict = {'aa' : 1 , 'b' : 2 , ' system' : system }
35
35
expected_list = [{'key' : 'aa' , 'value' : '1' , 'system' : system },
36
36
{'key' : 'b' , 'value' : '2' , 'system' : system }]
37
37
assert _dict_to_payload (initial_dict ) == expected_list
38
38
39
39
def test_get_id (self , response ):
40
40
"""Test for the get_id function."""
41
- assert _get_id (response (200 , {"id" : 123 })) == 123
41
+ assert _get_id (response (200 , {'id' : 123 })) == 123
42
42
43
43
def test_get_msg (self , response ):
44
44
"""Test for the get_msg function."""
45
- fake_json = {"id" : 123 }
45
+ fake_json = {'id' : 123 }
46
46
assert _get_msg (response (200 , fake_json )) == fake_json
47
47
48
48
def test_get_data (self , response ):
49
49
"""Test for the get_data function."""
50
- fake_json = {"id" : 123 }
50
+ fake_json = {'id' : 123 }
51
51
assert _get_data (response (200 , fake_json )) == fake_json
52
52
53
53
def test_get_json (self , response ):
54
54
"""Test for the get_json function."""
55
- fake_json = {"id" : 123 }
55
+ fake_json = {'id' : 123 }
56
56
assert _get_json (response (200 , fake_json )) == fake_json
57
57
58
58
def test_get_messages (self ):
59
59
"""Test for the get_messages function."""
60
- data = {" responses" : [{" errorCode" : 422 , " message" : " error" }]}
60
+ data = {' responses' : [{' errorCode' : 422 , ' message' : ' error' }]}
61
61
assert _get_messages (data ) == ['422: error' ]
62
62
63
63
@@ -71,7 +71,7 @@ def test_start_launch(self, mock_get, rp_service):
71
71
:param mock_get: Mocked _get_data() function
72
72
:param rp_service: Pytest fixture
73
73
"""
74
- mock_get .return_value = {"id" : 111 }
74
+ mock_get .return_value = {'id' : 111 }
75
75
launch_id = rp_service .start_launch ('name' , datetime .now ().isoformat ())
76
76
assert launch_id == 111
77
77
@@ -82,10 +82,10 @@ def test_finish_launch(self, mock_get, rp_service):
82
82
:param mock_get: Mocked _get_msg() function
83
83
:param rp_service: Pytest fixture
84
84
"""
85
- mock_get .return_value = {"id" : 111 }
85
+ mock_get .return_value = {'id' : 111 }
86
86
_get_msg = rp_service .finish_launch (
87
87
'name' , datetime .now ().isoformat ())
88
- assert _get_msg == {"id" : 111 }
88
+ assert _get_msg == {'id' : 111 }
89
89
90
90
@mock .patch ('platform.system' , mock .Mock (return_value = 'linux' ))
91
91
@mock .patch ('platform.machine' , mock .Mock (return_value = 'Windows-PC' ))
@@ -131,3 +131,70 @@ def test_get_system_information_without_pkg(self):
131
131
cond = (ReportPortalService .get_system_information ('pytest' )
132
132
== expected_result )
133
133
assert cond
134
+
135
+ @mock .patch ('reportportal_client.service._get_data' ,
136
+ mock .Mock (return_value = {'id' : 123 }))
137
+ def test_start_item (self , rp_service ):
138
+ """Test for validate start_test_item.
139
+
140
+ :param: rp_service: fixture of ReportPortal
141
+ """
142
+ rp_start = rp_service .start_test_item (name = 'name' ,
143
+ start_time = 1591032041348 ,
144
+ item_type = 'STORY' )
145
+ expected_result = dict (json = {'name' : 'name' ,
146
+ 'description' : None ,
147
+ 'attributes' : None ,
148
+ 'startTime' : 1591032041348 ,
149
+ 'launchUuid' : 111 ,
150
+ 'type' : 'STORY' , 'parameters' : None ,
151
+ 'hasStats' : True ,
152
+ 'codeRef' : None },
153
+ url = 'http://endpoint/api/v2/project/item' ,
154
+ verify = True )
155
+
156
+ rp_service .session .post .assert_called_with (** expected_result )
157
+ assert rp_start == 123
158
+
159
+ start_item_optional = [
160
+ ('code_ref' , '/path/to/test - test_item' , 'codeRef' ,
161
+ '/path/to/test - test_item' ),
162
+ ('attributes' , {'attr1' : True }, 'attributes' ,
163
+ [{'key' : 'attr1' , 'value' : 'True' , 'system' : False }])
164
+ ]
165
+
166
+ @pytest .mark .parametrize (
167
+ 'field_name,field_value,expected_name,expected_value' ,
168
+ start_item_optional )
169
+ @mock .patch ('reportportal_client.service._get_data' ,
170
+ mock .Mock (return_value = {'id' : 123 }))
171
+ def test_start_item_code_optional_params (self , rp_service , field_name ,
172
+ field_value , expected_name ,
173
+ expected_value ):
174
+ """Test for validate different fields in start_test_item.
175
+
176
+ :param: rp_service: fixture of ReportPortal
177
+ :param: field_name: a name of a field bypassed to
178
+ rp_service.start_test_item method
179
+ :param: field_value: a value of a field bypassed to
180
+ rp_service.start_test_item method
181
+ :param: expected_name: a name of a field which should be in the result
182
+ JSON request
183
+ :param: expected_value: an exact value of a field which should be in
184
+ the result JSON request
185
+ """
186
+ rp_service .start_test_item (name = 'name' , start_time = 1591032041348 ,
187
+ item_type = 'STORY' ,
188
+ ** {field_name : field_value })
189
+ expected_result = dict (json = {'name' : 'name' ,
190
+ 'description' : None ,
191
+ 'attributes' : None ,
192
+ 'startTime' : 1591032041348 ,
193
+ 'launchUuid' : 111 ,
194
+ 'type' : 'STORY' , 'parameters' : None ,
195
+ 'hasStats' : True ,
196
+ 'codeRef' : None },
197
+ url = 'http://endpoint/api/v2/project/item' ,
198
+ verify = True )
199
+ expected_result ['json' ][expected_name ] = expected_value
200
+ rp_service .session .post .assert_called_with (** expected_result )
0 commit comments