|
1 |
| -"""Tests for service.py .""" |
| 1 | +"""This modules includes unit tests for the service.py module.""" |
2 | 2 |
|
3 |
| -try: |
4 |
| - from unittest.mock import create_autospec, Mock, patch, MagicMock |
5 |
| -except ImportError: |
6 |
| - from mock import create_autospec, Mock, patch, MagicMock |
7 | 3 |
|
8 |
| -from reportportal_client.service import _convert_string, _list_to_payload, \ |
9 |
| - uri_join, _get_id, _get_msg, _get_data, _get_json, _get_messages, \ |
10 |
| - ReportPortalService |
11 |
| - |
12 |
| -import unittest |
13 | 4 | from datetime import datetime
|
| 5 | +from six.moves import mock |
| 6 | + |
| 7 | +from delayed_assert import expect, assert_expectations |
| 8 | + |
| 9 | +from reportportal_client.service import ( |
| 10 | + _convert_string, |
| 11 | + _get_data, |
| 12 | + _get_id, |
| 13 | + _get_json, |
| 14 | + _get_messages, |
| 15 | + _get_msg, |
| 16 | + _list_to_payload |
| 17 | +) |
14 | 18 |
|
15 | 19 |
|
16 |
| -class TestServiceFunctions(unittest.TestCase): |
17 |
| - """Test for additional functions.""" |
| 20 | +class TestServiceFunctions: |
| 21 | + """This class contains test methods for helper functions.""" |
18 | 22 |
|
19 | 23 | def test_check_convert_to_string(self):
|
20 | 24 | """Test for support and convert strings to utf-8."""
|
21 |
| - self.assertEqual(_convert_string("Hello world"), 'Hello world') |
22 |
| - self.assertEqual(type(_convert_string("Hello world")), str) |
| 25 | + expect(_convert_string("Hello world") == 'Hello world') |
| 26 | + expect(lambda: isinstance(_convert_string("Hello world"), str)) |
| 27 | + assert_expectations() |
23 | 28 |
|
24 | 29 | def test_list_to_payload(self):
|
25 | 30 | """Test convert dict to list of dicts."""
|
26 | 31 | initial_dict = {'key': "value", 'key1': 'value1'}
|
27 | 32 | expected_list = [{'key': 'key', 'value': 'value'},
|
28 | 33 | {'key': 'key1', 'value': 'value1'}]
|
29 |
| - self.assertEqual(_list_to_payload(initial_dict), expected_list) |
30 |
| - |
31 |
| - def test_get_id(self): |
32 |
| - """Test for get id from Response obj.""" |
33 |
| - fake_json = {"id": 123} |
| 34 | + assert _list_to_payload(initial_dict) == expected_list |
34 | 35 |
|
35 |
| - with patch('requests.Response', new_callable=MagicMock()) as mock_get: |
36 |
| - mock_get.status_code = 200 |
37 |
| - mock_get.json.return_value = fake_json |
| 36 | + def test_get_id(self, response): |
| 37 | + """Test for the get_id function.""" |
| 38 | + assert _get_id(response(200, {"id": 123})) == 123 |
38 | 39 |
|
39 |
| - obj = _get_id(mock_get) |
40 |
| - |
41 |
| - self.assertEqual(obj, 123) |
42 |
| - |
43 |
| - def test_get_msg(self): |
44 |
| - """Test get_msg recieved from Response.""" |
| 40 | + def test_get_msg(self, response): |
| 41 | + """Test for the get_msg function.""" |
45 | 42 | fake_json = {"id": 123}
|
| 43 | + assert _get_msg(response(200, fake_json)) == fake_json |
46 | 44 |
|
47 |
| - with patch('requests.Response', new_callable=MagicMock()) as mock_get: |
48 |
| - mock_get.status_code = 200 |
49 |
| - mock_get.json.return_value = fake_json |
50 |
| - |
51 |
| - obj = _get_msg(mock_get) |
52 |
| - |
53 |
| - self.assertEqual(obj, fake_json) |
54 |
| - |
55 |
| - def test_get_data(self): |
56 |
| - """Test get data from Response.""" |
| 45 | + def test_get_data(self, response): |
| 46 | + """Test for the get_data function.""" |
57 | 47 | fake_json = {"id": 123}
|
| 48 | + assert _get_data(response(200, fake_json)) == fake_json |
58 | 49 |
|
59 |
| - with patch('requests.Response', new_callable=MagicMock()) as mock_get: |
60 |
| - mock_get.status_code = 200 |
61 |
| - mock_get.json.return_value = fake_json |
62 |
| - |
63 |
| - obj = _get_data(mock_get) |
64 |
| - |
65 |
| - self.assertEqual(obj, fake_json) |
66 |
| - |
67 |
| - def test_get_json(self): |
68 |
| - """Test get json from Response.""" |
| 50 | + def test_get_json(self, response): |
| 51 | + """Test for the get_json function.""" |
69 | 52 | fake_json = {"id": 123}
|
70 |
| - |
71 |
| - with patch('requests.Response', new_callable=MagicMock()) as mock_get: |
72 |
| - mock_get.status_code = 200 |
73 |
| - mock_get.json.return_value = fake_json |
74 |
| - |
75 |
| - obj = _get_json(mock_get) |
76 |
| - |
77 |
| - self.assertEqual(obj, fake_json) |
| 53 | + assert _get_json(response(200, fake_json)) == fake_json |
78 | 54 |
|
79 | 55 | def test_get_messages(self):
|
80 |
| - """Test get errors from response.""" |
| 56 | + """Test for the get_messages function.""" |
81 | 57 | data = {"responses": [{"errorCode": 422, "message": "error"}]}
|
82 |
| - |
83 |
| - obj = _get_messages(data) |
84 |
| - |
85 |
| - self.assertEqual(obj, ['422: error']) |
| 58 | + assert _get_messages(data) == ['422: error'] |
86 | 59 |
|
87 | 60 |
|
88 |
| -class ReportPortalServiceTest(unittest.TestCase): |
| 61 | +class TestReportPortalService: |
89 | 62 | """This class stores methods which test ReportPortalService."""
|
90 | 63 |
|
91 |
| - def setUp(self): |
92 |
| - """Instantiate the ReportPortalService class and mock its session.""" |
93 |
| - self.rp = ReportPortalService('http://endpoint', 'project', 'token') |
94 |
| - self.rp.session = MagicMock() |
95 |
| - |
96 |
| - def test_start_launch(self): |
97 |
| - """Test start launch and sending request.""" |
98 |
| - with patch('reportportal_client.service._get_data', |
99 |
| - new_callable=Mock()) as mock_get: |
100 |
| - mock_get.return_value = {"id": 111} |
101 |
| - launch_id = self.rp.start_launch('name', |
102 |
| - datetime.now().isoformat()) |
103 |
| - self.assertEqual(launch_id, 111) |
104 |
| - |
105 |
| - def test_finish_launch(self): |
106 |
| - """Test finish launch and sending request.""" |
107 |
| - with patch('reportportal_client.service._get_msg', |
108 |
| - new_callable=Mock()) as mock_get: |
109 |
| - mock_get.return_value = {"id": 111} |
110 |
| - _get_msg = self.rp.finish_launch('name', |
111 |
| - datetime.now().isoformat()) |
112 |
| - self.assertEqual(_get_msg, {"id": 111}) |
| 64 | + @mock.patch('reportportal_client.service._get_data') |
| 65 | + def test_start_launch(self, mock_get, rp_service): |
| 66 | + """Test start launch and sending request. |
| 67 | +
|
| 68 | + :param mock_get: Mocked _get_data() function |
| 69 | + :param rp_service: Pytest fixture |
| 70 | + """ |
| 71 | + mock_get.return_value = {"id": 111} |
| 72 | + launch_id = rp_service.start_launch('name', datetime.now().isoformat()) |
| 73 | + assert launch_id == 111 |
| 74 | + |
| 75 | + @mock.patch('reportportal_client.service._get_msg') |
| 76 | + def test_finish_launch(self, mock_get, rp_service): |
| 77 | + """Test finish launch and sending request. |
| 78 | +
|
| 79 | + :param mock_get: Mocked _get_msg() function |
| 80 | + :param rp_service: Pytest fixture |
| 81 | + """ |
| 82 | + mock_get.return_value = {"id": 111} |
| 83 | + _get_msg = rp_service.finish_launch('name', datetime.now().isoformat()) |
| 84 | + assert _get_msg == {"id": 111} |
0 commit comments