Skip to content

Commit

Permalink
Fixed bug with github-to-sqlite get and single items, refs #50
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 17, 2020
1 parent e44ebee commit efbe77b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 9 additions & 1 deletion github_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,17 @@ def get(url, auth, paginate, nl):
"Save repos owened by the specified (or authenticated) username or organization"
token = load_token(auth)
first = True
should_output_closing_brace = not nl
while url:
response = utils.get(url, token)
items = response.json()
if isinstance(items, dict):
if nl:
click.echo(json.dumps(items))
else:
click.echo(json.dumps(items, indent=4))
should_output_closing_brace = False
break
if first and not nl:
click.echo("[")
for item in items:
Expand All @@ -484,7 +492,7 @@ def get(url, auth, paginate, nl):
url = response.links.get("next", {}).get("url")
else:
url = None
if not nl:
if should_output_closing_brace:
click.echo("\n]")


Expand Down
34 changes: 34 additions & 0 deletions tests/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def mocked_paginated(requests_mock):
json=[{"id": 3, "title": "Item 3"}, {"id": 4, "title": "Item 4"}],
headers={"link": '<https://api.github.com/paginated>; rel="prev"'},
)
requests_mock.get(
"https://api.github.com/single", json={"id": 1, "title": "Item 1"}
)


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


@pytest.mark.parametrize(
"nl,expected",
[
(True, '{"id": 1, "title": "Item 1"}'),
(
False,
textwrap.dedent(
"""
{
"id": 1,
"title": "Item 1"
}
"""
),
),
],
)
@pytest.mark.parametrize("paginate", [True, False])
def test_get_single(mocked_paginated, nl, expected, paginate):
runner = CliRunner()
with runner.isolated_filesystem():
args = ["get", "/single"]
if nl:
args.append("--nl")
if paginate:
args.append("--paginate")
result = runner.invoke(cli.cli, args)
assert 0 == result.exit_code
assert result.output.strip() == expected.strip()


@pytest.mark.parametrize(
"nl,expected",
(
Expand Down

0 comments on commit efbe77b

Please sign in to comment.