Skip to content

Commit efbe77b

Browse files
committed
Fixed bug with github-to-sqlite get and single items, refs #50
1 parent e44ebee commit efbe77b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

github_to_sqlite/cli.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,17 @@ def get(url, auth, paginate, nl):
466466
"Save repos owened by the specified (or authenticated) username or organization"
467467
token = load_token(auth)
468468
first = True
469+
should_output_closing_brace = not nl
469470
while url:
470471
response = utils.get(url, token)
471472
items = response.json()
473+
if isinstance(items, dict):
474+
if nl:
475+
click.echo(json.dumps(items))
476+
else:
477+
click.echo(json.dumps(items, indent=4))
478+
should_output_closing_brace = False
479+
break
472480
if first and not nl:
473481
click.echo("[")
474482
for item in items:
@@ -484,7 +492,7 @@ def get(url, auth, paginate, nl):
484492
url = response.links.get("next", {}).get("url")
485493
else:
486494
url = None
487-
if not nl:
495+
if should_output_closing_brace:
488496
click.echo("\n]")
489497

490498

tests/test_get.py

+34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def mocked_paginated(requests_mock):
1616
json=[{"id": 3, "title": "Item 3"}, {"id": 4, "title": "Item 4"}],
1717
headers={"link": '<https://api.github.com/paginated>; rel="prev"'},
1818
)
19+
requests_mock.get(
20+
"https://api.github.com/single", json={"id": 1, "title": "Item 1"}
21+
)
1922

2023

2124
@pytest.mark.parametrize("url", ["https://api.github.com/paginated", "/paginated"])
@@ -41,6 +44,37 @@ def test_get(mocked_paginated, url):
4144
assert result.output.strip() == expected
4245

4346

47+
@pytest.mark.parametrize(
48+
"nl,expected",
49+
[
50+
(True, '{"id": 1, "title": "Item 1"}'),
51+
(
52+
False,
53+
textwrap.dedent(
54+
"""
55+
{
56+
"id": 1,
57+
"title": "Item 1"
58+
}
59+
"""
60+
),
61+
),
62+
],
63+
)
64+
@pytest.mark.parametrize("paginate", [True, False])
65+
def test_get_single(mocked_paginated, nl, expected, paginate):
66+
runner = CliRunner()
67+
with runner.isolated_filesystem():
68+
args = ["get", "/single"]
69+
if nl:
70+
args.append("--nl")
71+
if paginate:
72+
args.append("--paginate")
73+
result = runner.invoke(cli.cli, args)
74+
assert 0 == result.exit_code
75+
assert result.output.strip() == expected.strip()
76+
77+
4478
@pytest.mark.parametrize(
4579
"nl,expected",
4680
(

0 commit comments

Comments
 (0)