forked from getpatchwork/git-pw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_series.py
More file actions
406 lines (330 loc) · 12.5 KB
/
test_series.py
File metadata and controls
406 lines (330 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import unittest
from unittest import mock
from click.testing import CliRunner as CLIRunner
from git_pw import series
@mock.patch('git_pw.api.get')
@mock.patch('git_pw.api.detail')
@mock.patch('git_pw.api.download')
@mock.patch('git_pw.utils.git_am')
class ApplyTestCase(unittest.TestCase):
def test_apply_without_args(
self, mock_git_am, mock_download, mock_detail, mock_api_get
):
"""Validate calling with no arguments."""
rsp = {'mbox': 'http://example.com/api/patches/123/mbox/'}
mock_detail.return_value = rsp
mock_download.return_value = 'test.patch'
runner = CLIRunner()
result = runner.invoke(series.apply_cmd, ['123'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
mock_download.assert_called_once_with(rsp['mbox'])
mock_git_am.assert_called_once_with(mock_download.return_value, ())
def test_apply_with_args(
self, mock_git_am, mock_download, mock_detail, mock_api_get
):
"""Validate passthrough of arbitrary arguments to git-am."""
rsp = {'mbox': 'http://example.com/api/patches/123/mbox/'}
mock_detail.return_value = rsp
mock_download.return_value = 'test.patch'
runner = CLIRunner()
result = runner.invoke(series.apply_cmd, ['123', '-3'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
mock_download.assert_called_once_with(rsp['mbox'])
mock_git_am.assert_called_once_with(
mock_download.return_value, ('-3',)
)
def test_apply_with_deps_unsupported(
self, mock_git_am, mock_download, mock_detail, mock_api_get
):
"""
Validate that a series is applied when dependencies are
requested and dependencies do not appear in the API.
"""
rsp = {'mbox': 'http://example.com/api/series/123/mbox/'}
mock_detail.return_value = rsp
mock_download.return_value = 'test.patch'
runner = CLIRunner()
result = runner.invoke(series.apply_cmd, ['123', '--deps'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
mock_download.assert_called_once_with(rsp['mbox'])
mock_git_am.assert_called_once_with(mock_download.return_value, ())
def test_apply_with_deps(
self, mock_git_am, mock_download, mock_detail, mock_api_get
):
"""Validate that dependencies are applied when flag is given."""
dep_ids = [
'120',
'121',
'122',
]
dependencies = list(
map(lambda x: f"http://example.com/api/series/{x}/", dep_ids)
)
dep_details = list(map(lambda x: {"mbox": f"{x}mbox/"}, dependencies))
mboxes = list(map(lambda x: x["mbox"], dep_details))
mboxes.append('http://example.com/api/series/123/mbox/')
files = list(map(lambda x: f"series_{x}.mbox", [*dep_ids, '123']))
rsp_base = {
'mbox': 'http://example.com/api/series/123/mbox/',
'dependencies': dependencies,
}
mock_detail.return_value = rsp_base
mock_api_get.side_effect = dep_details
mock_download.side_effect = files
runner = CLIRunner()
result = runner.invoke(series.apply_cmd, ['123', '--deps'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
mock_api_get.assert_has_calls(
map(lambda x: mock.call(x), dependencies)
)
mock_download.assert_has_calls(map(lambda x: mock.call(x), mboxes))
mock_git_am.assert_has_calls(map(lambda x: mock.call(x, ()), files))
@mock.patch('git_pw.api.detail')
@mock.patch('git_pw.api.download')
class DownloadTestCase(unittest.TestCase):
def test_download(self, mock_download, mock_detail):
"""Validate standard behavior."""
rsp = {'mbox': 'http://example.com/api/patches/123/mbox/'}
mock_detail.return_value = rsp
runner = CLIRunner()
result = runner.invoke(series.download_cmd, ['123'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
mock_download.assert_called_once_with(rsp['mbox'], output=None)
def test_download_to_file(self, mock_download, mock_detail):
"""Validate downloading to a file."""
rsp = {'mbox': 'http://example.com/api/patches/123/mbox/'}
mock_detail.return_value = rsp
runner = CLIRunner()
result = runner.invoke(series.download_cmd, ['123', 'test.patch'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
mock_download.assert_called_once_with(rsp['mbox'], output=mock.ANY)
assert isinstance(
mock_download.call_args[1]['output'],
str,
)
def test_download_separate_to_dir(self, mock_download, mock_detail):
"""Validate downloading seperate to a directory."""
rsp = {
'mbox': 'http://example.com/api/patches/123/mbox/',
'patches': [
{
'id': 10539359,
'mbox': 'https://example.com/project/foo/patch/123/mbox/',
}
],
}
mock_detail.return_value = rsp
runner = CLIRunner()
result = runner.invoke(series.download_cmd, ['123', '--separate', '.'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
mock_download.assert_called_once_with(
rsp['patches'][0]['mbox'],
output=mock.ANY,
)
assert isinstance(
mock_download.call_args[1]['output'],
str,
)
class ShowTestCase(unittest.TestCase):
@staticmethod
def _get_series(**kwargs):
rsp = {
'id': 123,
'date': '2017-01-01 00:00:00',
'name': 'Sample series',
'submitter': {
'name': 'foo',
'email': 'foo@bar.com',
},
'project': {
'name': 'bar',
},
'version': '1',
'total': 2,
'received_total': 2,
'received_all': True,
'cover_letter': None,
'patches': [],
'dependencies': [],
'dependents': [],
}
rsp.update(**kwargs)
return rsp
@mock.patch('git_pw.api.detail')
def test_show(self, mock_detail):
"""Validate standard behavior."""
rsp = self._get_series()
mock_detail.return_value = rsp
runner = CLIRunner()
result = runner.invoke(series.show_cmd, ['123'])
assert result.exit_code == 0, result
mock_detail.assert_called_once_with('series', 123)
@mock.patch('git_pw.api.version', return_value=(1, 0))
@mock.patch('git_pw.api.index')
@mock.patch('git_pw.utils.echo_via_pager')
class ListTestCase(unittest.TestCase):
@staticmethod
def _get_series(**kwargs):
return ShowTestCase._get_series(**kwargs)
@staticmethod
def _get_people(**kwargs):
rsp = {
'id': 1,
'name': 'John Doe',
'email': 'john@example.com',
}
rsp.update(**kwargs)
return rsp
def test_list(self, mock_echo, mock_index, mock_version):
"""Validate standard behavior."""
rsp = [self._get_series()]
mock_index.return_value = rsp
runner = CLIRunner()
result = runner.invoke(series.list_cmd, [])
assert result.exit_code == 0, result
mock_index.assert_called_once_with(
'series',
[
('q', None),
('page', None),
('per_page', None),
('order', '-date'),
],
)
def test_list_with_formatting(self, mock_echo, mock_index, mock_version):
"""Validate behavior with formatting applied."""
rsp = [self._get_series()]
mock_index.return_value = rsp
runner = CLIRunner()
result = runner.invoke(
series.list_cmd,
['--format', 'simple', '--column', 'ID', '--column', 'Name'],
)
assert result.exit_code == 0, result
mock_echo.assert_called_once_with(
mock.ANY, ('ID', 'Name'), fmt='simple'
)
def test_list_with_filters(self, mock_echo, mock_index, mock_version):
"""Validate behavior with filters applied.
Apply all filters, including those for pagination.
"""
people_rsp = [self._get_people()]
series_rsp = [self._get_series()]
mock_index.side_effect = [people_rsp, series_rsp]
runner = CLIRunner()
result = runner.invoke(
series.list_cmd,
[
'--submitter',
'john@example.com',
'--submitter',
'2',
'--limit',
1,
'--page',
1,
'--sort',
'-name',
'test',
'--since',
'2022-01-01',
'--before',
'2022-12-31',
],
)
assert result.exit_code == 0, result
calls = [
mock.call('people', [('q', 'john@example.com')]),
mock.call(
'series',
[
('submitter', 1),
('submitter', '2'),
('q', 'test'),
('page', 1),
('per_page', 1),
('order', '-name'),
('since', '2022-01-01T00:00:00'),
('before', '2022-12-31T00:00:00'),
],
),
]
mock_index.assert_has_calls(calls)
@mock.patch('git_pw.api.LOG')
def test_list_with_wildcard_filters(
self, mock_log, mock_echo, mock_index, mock_version
):
"""Validate behavior with a "wildcard" filter.
Patchwork API v1.0 did not support multiple filters correctly. Ensure
the user is warned as necessary if a filter has multiple matches.
"""
people_rsp = [self._get_people(), self._get_people()]
series_rsp = [self._get_series()]
mock_index.side_effect = [people_rsp, series_rsp]
runner = CLIRunner()
runner.invoke(series.list_cmd, ['--submitter', 'john@example.com'])
assert mock_log.warning.called
@mock.patch('git_pw.api.LOG')
def test_list_with_multiple_filters(
self, mock_log, mock_echo, mock_index, mock_version
):
"""Validate behavior with use of multiple filters.
Patchwork API v1.0 did not support multiple filters correctly. Ensure
the user is warned as necessary if they specify multiple filters.
"""
people_rsp = [self._get_people()]
series_rsp = [self._get_series()]
mock_index.side_effect = [people_rsp, people_rsp, series_rsp]
runner = CLIRunner()
result = runner.invoke(
series.list_cmd,
[
'--submitter',
'john@example.com',
'--submitter',
'jimmy@example.com',
],
)
assert result.exit_code == 0, result
assert mock_log.warning.called
@mock.patch('git_pw.api.LOG')
def test_list_api_v1_1(
self, mock_log, mock_echo, mock_index, mock_version
):
"""Validate behavior with API v1.1."""
mock_version.return_value = (1, 1)
people_rsp = [self._get_people()]
series_rsp = [self._get_series()]
mock_index.side_effect = [people_rsp, series_rsp]
runner = CLIRunner()
result = runner.invoke(
series.list_cmd,
['--submitter', 'jimmy@example.com', '--submitter', 'John Doe'],
)
assert result.exit_code == 0, result
# We should have only made a single call to '/people' since API v1.1
# supports filtering with emails natively
calls = [
mock.call('people', [('q', 'John Doe')]),
mock.call(
'series',
[
('submitter', 'jimmy@example.com'),
('submitter', 1),
('q', None),
('page', None),
('per_page', None),
('order', '-date'),
],
),
]
mock_index.assert_has_calls(calls)
# We shouldn't see a warning about multiple versions either
assert not mock_log.warning.called