|
| 1 | +from click.testing import CliRunner |
| 2 | +from github_to_sqlite import cli |
| 3 | +import pytest |
| 4 | +import textwrap |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def mocked_paginated(requests_mock): |
| 9 | + requests_mock.get( |
| 10 | + "https://api.github.com/paginated", |
| 11 | + json=[{"id": 1, "title": "Item 1"}, {"id": 2, "title": "Item 2"}], |
| 12 | + headers={"link": '<https://api.github.com/paginated?page=2>; rel="next"'}, |
| 13 | + ) |
| 14 | + requests_mock.get( |
| 15 | + "https://api.github.com/paginated?page=2", |
| 16 | + json=[{"id": 3, "title": "Item 3"}, {"id": 4, "title": "Item 4"}], |
| 17 | + headers={"link": '<https://api.github.com/paginated>; rel="prev"'}, |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("url", ["https://api.github.com/paginated", "/paginated"]) |
| 22 | +def test_get(mocked_paginated, url): |
| 23 | + runner = CliRunner() |
| 24 | + with runner.isolated_filesystem(): |
| 25 | + result = runner.invoke(cli.cli, ["get", url]) |
| 26 | + assert 0 == result.exit_code |
| 27 | + expected = textwrap.dedent( |
| 28 | + """ |
| 29 | + [ |
| 30 | + { |
| 31 | + "id": 1, |
| 32 | + "title": "Item 1" |
| 33 | + }, |
| 34 | + { |
| 35 | + "id": 2, |
| 36 | + "title": "Item 2" |
| 37 | + } |
| 38 | + ] |
| 39 | + """ |
| 40 | + ).strip() |
| 41 | + assert result.output.strip() == expected |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize( |
| 45 | + "nl,expected", |
| 46 | + ( |
| 47 | + ( |
| 48 | + False, |
| 49 | + textwrap.dedent( |
| 50 | + """ |
| 51 | + [ |
| 52 | + { |
| 53 | + "id": 1, |
| 54 | + "title": "Item 1" |
| 55 | + }, |
| 56 | + { |
| 57 | + "id": 2, |
| 58 | + "title": "Item 2" |
| 59 | + }, |
| 60 | + { |
| 61 | + "id": 3, |
| 62 | + "title": "Item 3" |
| 63 | + }, |
| 64 | + { |
| 65 | + "id": 4, |
| 66 | + "title": "Item 4" |
| 67 | + } |
| 68 | + ]""" |
| 69 | + ).strip(), |
| 70 | + ), |
| 71 | + ( |
| 72 | + True, |
| 73 | + textwrap.dedent( |
| 74 | + """ |
| 75 | + {"id": 1, "title": "Item 1"} |
| 76 | + {"id": 2, "title": "Item 2"} |
| 77 | + {"id": 3, "title": "Item 3"} |
| 78 | + {"id": 4, "title": "Item 4"} |
| 79 | + """ |
| 80 | + ).strip(), |
| 81 | + ), |
| 82 | + ), |
| 83 | +) |
| 84 | +def test_get_paginate(mocked_paginated, nl, expected): |
| 85 | + runner = CliRunner() |
| 86 | + with runner.isolated_filesystem(): |
| 87 | + result = runner.invoke( |
| 88 | + cli.cli, |
| 89 | + ["get", "https://api.github.com/paginated", "--paginate"] |
| 90 | + + (["--nl"] if nl else []), |
| 91 | + ) |
| 92 | + assert 0 == result.exit_code |
| 93 | + assert result.output.strip() == expected |
0 commit comments