Skip to content

Commit 791e4d7

Browse files
gh-7: Cover JSON and JSON Lines list output (#444)
1 parent 4db54c0 commit 791e4d7

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎tests/test_list.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import json
12
import pytest
23
import re
34

5+
from io import StringIO
46
from itertools import zip_longest
57

68
from manage import list_command
@@ -315,6 +317,39 @@ def test_format_csv_empty(assert_log):
315317
assert_log(assert_log.end_of_log())
316318

317319

320+
@pytest.fixture(params=["populated", "empty"])
321+
def json_format_installs(request):
322+
if request.param == "empty":
323+
return [], []
324+
prefix = r"C:\Python"
325+
installs = [
326+
{"id": "first", "prefix": Path(prefix)},
327+
{"id": "second", "display-name": "Python\nPreview"},
328+
]
329+
expected = [{**installs[0], "prefix": prefix}, installs[1]]
330+
return installs, expected
331+
332+
333+
def test_format_json(monkeypatch, json_format_installs):
334+
output = StringIO()
335+
monkeypatch.setattr(list_command.LOGGER, "print_console", output)
336+
installs, expected = json_format_installs
337+
338+
list_command.format_json(None, installs)
339+
340+
assert json.loads(output.getvalue()) == {"versions": expected}
341+
342+
343+
def test_format_json_lines(monkeypatch, json_format_installs):
344+
output = StringIO()
345+
monkeypatch.setattr(list_command.LOGGER, "print_console", output)
346+
installs, expected = json_format_installs
347+
348+
list_command.format_json_lines(None, installs)
349+
350+
assert [json.loads(line) for line in output.getvalue().splitlines()] == expected
351+
352+
318353
def test_csv_exclude():
319354
result = list(list_command._csv_filter_and_expand([
320355
dict(a=1, b=2),

0 commit comments

Comments
 (0)