Skip to content

Commit 1dc4e5f

Browse files
committed
Fix tests
1 parent 76947fd commit 1dc4e5f

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

reportportal_client/client.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,6 @@ def start_test_item(
636636
:param uuid: Test Item UUID to use on start (overrides server one, should be globally unique).
637637
:return: Test Item UUID if successfully started or None.
638638
"""
639-
if not parent_item_id:
640-
logger.warning("Attempt to start item for non-existent parent item.")
641-
return None
642639
if parent_item_id:
643640
url = uri_join(self.base_url_v2, "item", parent_item_id)
644641
else:

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
from reportportal_client.client import RPClient
2323

2424

25+
class DummyResponse:
26+
# noinspection PyMethodMayBeStatic
27+
def json(self):
28+
return {
29+
"id": "321-321-4321-321",
30+
"message": "test",
31+
}
32+
33+
def ok(self):
34+
return True
35+
36+
2537
@fixture
2638
def response():
2739
"""Cook up a mock for the Response with specific arguments."""
@@ -46,6 +58,8 @@ def rp_client():
4658
"""Prepare instance of the RPClient for testing."""
4759
client = RPClient("http://endpoint", "project", "api_key")
4860
client.session = mock.Mock()
61+
client.session.post.return_value = DummyResponse()
62+
client.session.put.return_value = DummyResponse()
4963
client._skip_analytics = True
5064
return client
5165

tests/steps/conftest.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,10 @@
1616
# noinspection PyPackageRequirements
1717
from pytest import fixture
1818

19+
from tests.conftest import DummyResponse
1920
from reportportal_client.client import RPClient
2021

2122

22-
class DummyResponse:
23-
# noinspection PyMethodMayBeStatic
24-
def json(self):
25-
return {
26-
"id": "321-321-4321-321",
27-
"message": "test",
28-
}
29-
30-
3123
@fixture
3224
def rp_client():
3325
client = RPClient("http://endpoint", "project", "api_key")

tests/test_client.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from requests import Response
2121
from requests.exceptions import ReadTimeout
2222

23+
from conftest import DummyResponse
2324
from reportportal_client import RPClient
2425
from reportportal_client.core.rp_requests import RPRequestLog
2526
from reportportal_client.helpers import timestamp
@@ -38,7 +39,7 @@ def response_error(*args, **kwargs):
3839

3940
def invalid_response(*args, **kwargs):
4041
result = Response()
41-
result._content = "<html><head><title>Hello World!</title></head></html>"
42+
result._content = "<html><head><title>Hello World!</title></head></html>".encode("ASCII")
4243
result.status_code = 200
4344
return result
4445

@@ -60,6 +61,7 @@ def invalid_response(*args, **kwargs):
6061
)
6162
def test_connection_errors(rp_client, requests_method, client_method, client_params):
6263
rp_client._RPClient__launch_uuid = "test_launch_id"
64+
rp_client.session.get.return_value = DummyResponse()
6365
getattr(rp_client.session, requests_method).side_effect = connection_error
6466
result = getattr(rp_client, client_method)(*client_params)
6567
assert result is None
@@ -105,20 +107,20 @@ def get_call(*args, **kwargs):
105107

106108
@mock.patch("reportportal_client.client.getenv")
107109
@mock.patch("reportportal_client.client.send_event")
108-
def test_skip_statistics(send_event, getenv):
110+
def test_skip_statistics(send_event, getenv, rp_client):
109111
getenv.return_value = "1"
110112
client = RPClient("http://endpoint", "project", "api_key")
111-
client.session = mock.Mock()
113+
client.session = rp_client.session
112114
client.start_launch("Test Launch", timestamp())
113115
assert mock.call("start_launch", None, None) not in send_event.mock_calls
114116

115117

116118
@mock.patch("reportportal_client.client.getenv")
117119
@mock.patch("reportportal_client.client.send_event")
118-
def test_statistics(send_event, getenv):
120+
def test_statistics(send_event, getenv, rp_client):
119121
getenv.return_value = ""
120122
client = RPClient("http://endpoint", "project", "api_key")
121-
client.session = mock.Mock()
123+
client.session = rp_client.session
122124
client.start_launch("Test Launch", timestamp())
123125
assert mock.call("start_launch", None, None) in send_event.mock_calls
124126

@@ -186,20 +188,20 @@ def test_empty_api_key_argument(warn):
186188
assert client.api_key == api_key
187189

188190

189-
def test_launch_uuid_print():
191+
def test_launch_uuid_print(rp_client):
190192
str_io = StringIO()
191193
output_mock = mock.Mock()
192194
output_mock.get_output.side_effect = lambda: str_io
193195
client = RPClient(
194196
endpoint="http://endpoint", project="project", api_key="test", launch_uuid_print=True, print_output=output_mock
195197
)
196-
client.session = mock.Mock()
197-
client._skip_analytics = True
198+
client.session = rp_client.session
199+
client._skip_analytics = "True"
198200
client.start_launch("Test Launch", timestamp())
199201
assert "ReportPortal Launch UUID: " in str_io.getvalue()
200202

201203

202-
def test_no_launch_uuid_print():
204+
def test_no_launch_uuid_print(rp_client):
203205
str_io = StringIO()
204206
output_mock = mock.Mock()
205207
output_mock.get_output.side_effect = lambda: str_io
@@ -210,27 +212,27 @@ def test_no_launch_uuid_print():
210212
launch_uuid_print=False,
211213
print_output=output_mock,
212214
)
213-
client.session = mock.Mock()
214-
client._skip_analytics = True
215+
client.session = rp_client.session
216+
client._skip_analytics = "True"
215217
client.start_launch("Test Launch", timestamp())
216218
assert "ReportPortal Launch UUID: " not in str_io.getvalue()
217219

218220

219221
@mock.patch("reportportal_client.client.sys.stdout", new_callable=StringIO)
220-
def test_launch_uuid_print_default_io(mock_stdout):
222+
def test_launch_uuid_print_default_io(mock_stdout, rp_client):
221223
client = RPClient(endpoint="http://endpoint", project="project", api_key="test", launch_uuid_print=True)
222-
client.session = mock.Mock()
223-
client._skip_analytics = True
224+
client.session = rp_client.session
225+
client._skip_analytics = "True"
224226
client.start_launch("Test Launch", timestamp())
225227

226228
assert "ReportPortal Launch UUID: " in mock_stdout.getvalue()
227229

228230

229231
@mock.patch("reportportal_client.client.sys.stdout", new_callable=StringIO)
230-
def test_launch_uuid_print_default_print(mock_stdout):
232+
def test_launch_uuid_print_default_print(mock_stdout, rp_client):
231233
client = RPClient(endpoint="http://endpoint", project="project", api_key="test")
232-
client.session = mock.Mock()
233-
client._skip_analytics = True
234+
client.session = rp_client.session
235+
client._skip_analytics = "True"
234236
client.start_launch("Test Launch", timestamp())
235237

236238
assert "ReportPortal Launch UUID: " not in mock_stdout.getvalue()
@@ -256,6 +258,7 @@ def test_client_pickling():
256258
def test_attribute_truncation(rp_client: RPClient, method, call_method, arguments):
257259
# noinspection PyTypeChecker
258260
session: mock.Mock = rp_client.session
261+
session.get.return_value = DummyResponse()
259262
if method != "start_launch":
260263
rp_client._RPClient__launch_uuid = "test_launch_id"
261264

@@ -285,8 +288,11 @@ def test_http_timeout_bypass(method, call_method, arguments):
285288
http_timeout = (35.1, 33.3)
286289
rp_client = RPClient("http://endpoint", "project", "api_key", http_timeout=http_timeout, log_batch_size=1)
287290
session: mock.Mock = mock.Mock()
291+
session.get.return_value = DummyResponse()
292+
session.post.return_value = DummyResponse()
293+
session.put.return_value = DummyResponse()
288294
rp_client.session = session
289-
rp_client._skip_analytics = True
295+
rp_client._skip_analytics = "True"
290296

291297
if method != "start_launch":
292298
rp_client._RPClient__launch_uuid = "test_launch_id"

0 commit comments

Comments
 (0)