Files
octokit.py/tests/test_octokit.py
T

75 lines
2.5 KiB
Python
Raw Normal View History

import pytest
2018-08-29 10:46:08 -05:00
class MockHeaders(object):
def __init__(self, requested_page, **kwargs):
link = 'Links <https://api.github.com/installation/repositories?page={}another=%2Cwith+a+space>; rel="next", <https://api.github.com/installation/repositories?page=4>; rel="last"'.format( # noqa E501
2018-09-04 08:44:39 -05:00
min(requested_page + 1, 4)
)
2020-01-17 11:49:05 -06:00
self.headers = {"Link": kwargs.get("link", link)}
2018-08-29 10:46:08 -05:00
class MockObject(object):
def __init__(self, page, kwargs):
self._response = MockHeaders(page, **kwargs)
2020-01-17 11:49:05 -06:00
self.json = {"page": page, "kwargs": kwargs}
2018-08-29 10:46:08 -05:00
def MockResponse(page=None, **kwargs):
return MockObject(page, kwargs)
def MockResponseWithoutLinks(page=None, **kwargs):
2020-01-17 11:49:05 -06:00
kwargs["link"] = ""
2018-08-29 10:46:08 -05:00
return MockObject(page, kwargs)
2018-02-05 16:36:27 -06:00
class TestOctokit(object):
def test_can_instantiate_class(self):
import octokit
2020-01-17 11:49:05 -06:00
2018-02-05 16:36:27 -06:00
assert isinstance(octokit.Octokit, object)
octo = octokit.Octokit()
assert isinstance(octo, object)
2018-02-05 14:43:08 -06:00
2018-02-05 16:36:27 -06:00
def test_can_import_class(self):
from octokit import Octokit
2020-01-17 11:49:05 -06:00
2018-02-05 16:36:27 -06:00
assert isinstance(Octokit, object)
octokit = Octokit()
assert isinstance(octokit, object)
2018-02-05 14:43:08 -06:00
2018-02-05 16:36:27 -06:00
def test_clients_are_lower_case(self):
from octokit import Octokit
2020-01-17 11:49:05 -06:00
2018-02-05 16:36:27 -06:00
assert all(client.islower() for client in Octokit.__dict__)
2018-08-29 10:46:08 -05:00
def test_pagination(self):
from octokit import Octokit
2020-01-17 11:49:05 -06:00
2018-08-29 10:46:08 -05:00
sut_obj = MockResponse
2020-01-17 11:49:05 -06:00
p = Octokit().paginate(sut_obj, param="value")
assert next(p) == {"page": 1, "kwargs": {"param": "value"}}
assert next(p) == {"page": 2, "kwargs": {"param": "value"}}
assert next(p) == {"page": 3, "kwargs": {"param": "value"}}
assert next(p) == {"page": 4, "kwargs": {"param": "value"}}
2019-01-02 14:39:28 -06:00
def test_pagination_does_break_when_iterating_over_a_single_page(self):
from octokit import Octokit
2020-01-17 11:49:05 -06:00
sut_obj = MockResponseWithoutLinks
2020-01-17 11:49:05 -06:00
p = Octokit().paginate(sut_obj, param="value")
assert next(p) == {"page": 1, "kwargs": {"param": "value", "link": ""}}
with pytest.raises(StopIteration):
assert next(p)
2019-01-02 14:39:28 -06:00
def test_can_speficy_the_route_specifications_used(self):
from octokit import Octokit
2020-01-17 11:49:05 -06:00
octokit = Octokit(routes="ghe-2.18")
assert "/enterprise/2.18" in octokit.issues.create.__doc__
2019-01-02 14:39:28 -06:00
octokit = Octokit()
2020-01-17 11:49:05 -06:00
assert "/developer.github.com" in octokit.issues.create.__doc__
octokit = Octokit(routes="api.github.com")
assert "/developer.github.com" in octokit.issues.create.__doc__