Skip to content

Commit 7d28825

Browse files
committed
Add tests for API methods
Prove pagination works as expected and test everything else for good measure. Signed-off-by: Stephen Finucane <[email protected]>
1 parent e1e716a commit 7d28825

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ ignore = [
6161
# we make extensive use of printf style formatting and rewriting is a chore
6262
"UP031",
6363
]
64+
65+
[tool.ruff.lint.per-file-ignores]
66+
"tests/*" = ["S"]

tests/test_api.py

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest import mock
2+
13
import pytest
24

35
from pwclient import api
@@ -14,7 +16,7 @@ def test_xmlrpc_init__missing_username():
1416
def test_xmlrpc_init__invalid_auth():
1517
"""The XML-RPC API doesn't support tokens."""
1618
with pytest.raises(exceptions.ConfigError) as exc:
17-
api.XMLRPC('https://example.com/xmlrpc', token='foo') # noqa: S106
19+
api.XMLRPC('https://example.com/xmlrpc', token='foo')
1820

1921
assert 'The XML-RPC API does not support API tokens' in str(exc.value)
2022

@@ -35,3 +37,116 @@ def test_rest_init__transform_legacy_url(capsys):
3537
assert (
3638
'Automatically converted XML-RPC URL to REST API URL.' in captured.err
3739
)
40+
41+
42+
def test_rest_client__create():
43+
"""Validate the _create helper."""
44+
client = api.REST(
45+
'https://patchwork.kernel.org/api',
46+
username='user',
47+
password='pass',
48+
)
49+
50+
fake_response = mock.MagicMock()
51+
fake_response.read.return_value = (
52+
b'{"id": 1, "state": "success", "context": "foo"}'
53+
)
54+
fake_response.getheaders.return_value = []
55+
56+
with mock.patch('urllib.request.urlopen') as mock_open:
57+
fake_response.__enter__.return_value = fake_response
58+
mock_open.return_value = fake_response
59+
60+
expected = {'id': 1, 'state': 'success', 'context': 'foo'}
61+
actual = client._create(
62+
'patches',
63+
resource_id=1,
64+
subresource_type='checks',
65+
data={'context': 'foo', 'state': 'success'},
66+
)
67+
68+
assert expected == actual
69+
70+
71+
def test_rest_client__update():
72+
"""Validate the _update helper."""
73+
client = api.REST(
74+
'https://patchwork.kernel.org/api',
75+
username='user',
76+
password='pass',
77+
)
78+
79+
fake_response = mock.MagicMock()
80+
fake_response.read.return_value = b'{"id": 1, "archived": true}'
81+
fake_response.getheaders.return_value = []
82+
83+
with mock.patch('urllib.request.urlopen') as mock_open:
84+
fake_response.__enter__.return_value = fake_response
85+
mock_open.return_value = fake_response
86+
87+
expected = {'id': 1, 'archived': True}
88+
actual = client._update(
89+
'patches',
90+
1,
91+
{'archived': True},
92+
)
93+
94+
assert expected == actual
95+
96+
97+
def test_rest_client__detail():
98+
"""Validate the _detail helper."""
99+
client = api.REST(
100+
'https://patchwork.kernel.org/api',
101+
username='user',
102+
password='pass',
103+
)
104+
105+
fake_response = mock.MagicMock()
106+
fake_response.read.return_value = b'{"id": 1, "name": "foo"}'
107+
fake_response.getheaders.return_value = []
108+
109+
with mock.patch('urllib.request.urlopen') as mock_open:
110+
fake_response.__enter__.return_value = fake_response
111+
mock_open.return_value = fake_response
112+
113+
expected = {'id': 1, 'name': 'foo'}
114+
actual = client._detail(
115+
'patches',
116+
1,
117+
)
118+
119+
assert expected == actual
120+
121+
122+
def test_rest_client__list():
123+
"""Validate the _list helper."""
124+
client = api.REST(
125+
'https://patchwork.kernel.org/api',
126+
username='user',
127+
password='pass',
128+
)
129+
130+
fake_response = mock.MagicMock()
131+
fake_response.read.side_effect = [
132+
b'[{"id": 1}]',
133+
b'[{"id": 2}]',
134+
]
135+
fake_response.getheaders.side_effect = [
136+
[
137+
(
138+
'Link',
139+
'<https://patchwork.kernel.org/api/patches/?page=2&project=patchwork>; rel="next"',
140+
)
141+
],
142+
[],
143+
]
144+
145+
with mock.patch('urllib.request.urlopen') as mock_open:
146+
fake_response.__enter__.return_value = fake_response
147+
mock_open.return_value = fake_response
148+
149+
expected = [{'id': 1}, {'id': 2}]
150+
actual = list(client._list('patches'))
151+
152+
assert expected == actual

tests/test_patches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def test_action_list__submitter_filter(mock_list_patches, capsys):
137137
assert (
138138
'Patches submitted by Joe Bloggs <[email protected]>:'
139139
in captured.out
140-
) # noqa: E501
140+
)
141141

142142
api.patch_list.assert_called_once_with(
143143
project='defaultproject',

0 commit comments

Comments
 (0)