8000 github3.github at 100% coverage · WheresWardy/github3.py@be98b3d · GitHub
[go: up one dir, main page]

Skip to content

Commit be98b3d

Browse files
committed
github3.github at 100% coverage
1 parent aafa0d1 commit be98b3d

File tree

5 files changed

+170
-24
lines changed

5 files changed

+170
-24
lines changed

HISTORY.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ History/Changelog
77
- Add support for the announced_ meta_ endpoint.
88

99
- Add support for conditional refreshing, e.g.,
10-
10+
1111
::
1212

1313
import github3
@@ -50,6 +50,8 @@ History/Changelog
5050
object can now *actually* return None if it gets a 404 instead of just
5151
raising an exception. (Inspired by #49)
5252

53+
- GitHubStatus API now works.
54+
5355
.. _announced: https://github.com/blog/1402-upcoming-changes-to-github-services
5456
.. _meta: http://developer.github.com/v3/meta/
5557

github3/github.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def iter_repo_issues(self, owner, repository, milestone=None,
554554
repo = self.repository(owner, repository)
555555
return repo.iter_issues(milestone, state, assignee, mentioned,
556556
labels, sort, direction, since, number)
557-
return self._iter(0, '', type)
557+
return iter([])
558558

559559
@requires_auth
560560
def iter_keys(self, number=-1):
@@ -690,6 +690,7 @@ def markdown(self, text, mode='', context='', raw=False):
690690
:returns: str -- HTML formatted text
691691
"""
692692
data = None
693+
json = False
693694
headers = {}
694695
if raw:
695696
url = self._build_url('markdown', 'raw')
@@ -707,11 +708,10 @@ def markdown(self, text, mode='', context='', raw=False):
707708

708709
if context:
709710
data['context'] = context
710-
711-
data = data
711+
json = True
712712

713713
if data:
714-
req = self._post(url, data=data, headers=headers)
714+
req = self._post(url, data=data, json=json, headers=headers)
715715
if req.ok:
716716
return req.content
717717
return '' # (No coverage)
@@ -1003,6 +1003,9 @@ def __init__(self, url, login='', password='', token=''):
10031003
super(GitHubEnterprise, self).__init__(login, password, token)
10041004
self._github_url = url.rstrip('/') + '/api/v3'
10051005

1006+
def __repr__(self):
1007+
return '<GitHub Enterprise [0.url]>'.format(self)
1008+
10061009
@requires_auth
10071010
def admin_stats(self, option):
10081011
"""This is a simple way to get statistics about your system.
@@ -1027,29 +1030,28 @@ class GitHubStatus(GitHubCore):
10271030
"""
10281031
def __init__(self):
10291032
super(GitHubStatus, self).__init__({})
1030-
self._github_url = 'https://status.github.com/'
1033+
self._github_url = 'https://status.github.com'
1034+
1035+
def __repr__(self):
1036+
return '<GitHub Status>'
10311037

10321038
def _recipe(self, *args):
10331039
url = self._build_url(*args)
10341040
resp = self._get(url)
1035-
return resp.json if self._boolean(resp, 200, 404) else {}
1041+
return resp.json() if self._boolean(resp, 200, 404) else {}
10361042

1037-
@classmethod
10381043
def api(self):
10391044
"""GET /api.json"""
10401045
return self._recipe('api.json')
10411046

1042-
@classmethod
10431047
def status(self):
10441048
"""GET /api/status.json"""
10451049
return self._recipe('api', 'status.json')
10461050

1047-
@classmethod
10481051
def last_message(self):
10491052
"""GET /api/last-message.json"""
10501053
return self._recipe('api', 'last-message.json')
10511054

1052-
@classmethod
10531055
def messages(self):
10541056
"""GET /api/messages.json"""
10551057
return self._recipe('api', 'messages.json')

github3/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from json import dumps
1010
from requests import session
11-
from requests.compat import urlparse
11+
from requests.compat import urlparse, basestring
1212
from github3.decorators import requires_auth
1313
from github3.packages.PySO8601 import parse
1414
from github3 import __version__
@@ -113,10 +113,10 @@ def _post(self, url, data=None, json=True, **kwargs):
113113
data = dumps(data) if data is not None else data
114114
return self._session.post(url, data=data, **kwargs)
115115
else:
116+
if 'headers' in kwargs:
117+
kwargs['headers'].update({'Content-Type': None})
116118
# Override the Content-Type header
117-
return self._session.post(
118-
url, data, headers={'Content-Type': None}, **kwargs
119-
)
119+
return self._session.post(url, data, **kwargs)
120120

121121
def _put(self, url, **kwargs):
122122
return self._session.put(url, **kwargs)

tests/test_github.py

Lines changed: 144 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55

66
class TestGitHub(BaseCase):
7+
def test_init(self):
8+
g = github3.GitHub('foo', 'bar')
9+
expect(repr(g).endswith('[foo]>'))
10+
11+
g = github3.GitHub(token='foo')
12+
expect(repr(g).endswith('{0:x}>'.format(id(g))))
13+
714
def test_authorization(self):
815
self.response('authorization')
916
self.get('https://api.github.com/authorizations/10')
@@ -21,12 +28,17 @@ def test_authorize(self):
2128
scopes = ['scope1', 'scope2']
2229

2330
self.g.authorize(None, None, scopes)
24-
assert self.request.called is False
31+
self.not_called()
2532

2633
a = self.g.authorize('user', 'password', scopes)
2734
expect(a).isinstance(github3.auths.Authorization)
2835
assert self.request.called is True
2936

37+
self.request.reset_mock()
38+
39+
self.login()
40+
a = self.g.authorize(None, None, scopes=scopes)
41+
3042
def test_create_gist(self):
3143
self.response('gist', 201)
3244

@@ -81,6 +93,8 @@ def test_delete_key(self):
8193
with patch.object(github3.github.GitHub, 'key') as key:
8294
key.return_value = github3.users.Key(load('key'), self.g)
8395
assert self.g.delete_key(10) is True
96+
key.return_value = None
97+
assert self.g.delete_key(10) is False
8498

8599
assert self.request.called is True
86100

@@ -302,6 +316,28 @@ def test_iter_gists(self):
302316
self.get('https://api.github.com/gists')
303317
self.mock_assertions()
304318

319+
def test_iter_notifications(self):
320+
self.response('notification', _iter=True)
321+
self.get('https://api.github.com/notifications')
322+
self.conf.update(params=None)
323+
324+
with expect.githuberror():
325+
self.g.iter_notifications()
326+
327+
self.not_called()
328+
self.login()
329+
thread = next(self.g.iter_notifications())
330+
expect(thread).isinstance(github3.notifications.Thread)
331+
self.mock_assertions()
332+
333+
self.conf.update(params={'all': True})
334+
next(self.g.iter_notifications(True))
335+
self.mock_assertions()
336+
337+
self.conf.update(params={'participating': True})
338+
next(self.g.iter_notifications(participating=True))
339+
self.mock_assertions()
340+
305341
def test_iter_org_issues(self):
306342
self.response('issue', _iter=True)
307343
self.get('https://api.github.com/orgs/github3py/issues')
@@ -377,6 +413,9 @@ def test_iter_repo_issues(self):
377413
expect(i).isinstance(github3.issues.Issue)
378414
self.mock_assertions()
379415

416+
with expect.raises(StopIteration):
417+
next(self.g.iter_repo_issues(None, None))
418+
380419
def test_iter_keys(self):
381420
self.response('key', _iter=True)
382421
self.get('https://api.github.com/user/keys')
@@ -416,6 +455,10 @@ def test_iter_repos(self):
416455
github3.repos.Repository)
417456
self.mock_assertions()
418457

458+
self.conf.update(params={'type': 'all', 'direction': 'desc'})
459+
next(self.g.iter_repos('sigmavirus24', 'all', direction='desc'))
460+
self.mock_assertions()
461+
419462
def test_iter_repos_sort(self):
420463
self.response('repo', _iter=True)
421464
self.conf.update(params={"sort": "created"})
@@ -476,7 +519,26 @@ def test_login(self):
476519

477520
# Unwritten test, not entirely sure how to mock this
478521
def test_markdown(self):
479-
pass
522+
self.response('archive')
523+
self.post('https://api.github.com/markdown')
524+
self.conf = dict(
525+
data={
526+
'text': 'Foo', 'mode': 'gfm', 'context': 'sigmavirus24/cfg'
527+
}
528+
)
529+
530+
expect(self.g.markdown('Foo', 'gfm', 'sigmavirus24/cfg')) == (
531+
b'archive_data'
532+
)
533+
self.mock_assertions()
534+
535+
self.post('https://api.github.com/markdown/raw')
536+
self.conf['data'] = 'Foo'
537+
self.g.markdown('Foo', raw=True)
538+
self.mock_assertions()
539+
540+
expect(self.g.markdown(None)) == ''
541+
self.not_called()
480542

481543
def test_meta(self):
482544
self.response('meta')
@@ -485,6 +547,12 @@ def test_meta(self):
485547
expect(meta).isinstance(dict)
486548
self.mock_assertions()
487549

550+
def test_octocat(self):
551+
self.response('archive')
552+
self.get('https://api.github.com/octocat')
553+
expect(self.g.octocat().startswith(b'archive_data'))
554+
self.mock_assertions()
555+
488556
def test_organization(self):
489557
self.response('org')
490558 10000
self.get('https://api.github.com/orgs/github3py')
@@ -518,6 +586,14 @@ def test_pubsubhubbub(self):
518586
expect(body) == kwargs['data']
519587
self.mock_assertions()
520588

589+
d['secret'] = 'secret'
590+
body.append(('hub.secret', 'secret'))
591+
expect(self.g.pubsubhubbub(**d)).is_True()
592+
_, kwargs = self.request.call_args
593+
expect('data').is_in(kwargs)
594+
expect(body) == kwargs['data']
595+
self.mock_assertions()
596+
521597
def test_pull_request(self):
522598
self.response('pull')
523599
self.get('https://api.github.com/repos/sigmavirus24/'
@@ -572,8 +648,8 @@ def test_search_repos(self):
572648
expect(repos[0].is_private()) == repos[0].private
573649
self.mock_assertions()
574650

575-
self.conf.update(params={'language': 'python'})
576-
repos = self.g.search_repos('github3.py', language='python')
651+
self.conf.update(params={'language': 'python', 'start_page': 10})
652+
repos = self.g.search_repos('github3.py', 'python', 10)
577653
self.mock_assertions()
578654

579655
def test_search_users(self):
@@ -609,6 +685,9 @@ def test_set_user_agent(self):
609685
self.g.set_user_agent(ua)
610686
expect(self.g._session.headers['User-Agent']) == ua
611687

688+
self.g.set_user_agent(None)
689+
expect(self.g._session.headers['User-Agent']) == ua
690+
612691
def test_star(self):
613692
self.response('', 204)
614693
self.put('https://api.github.com/user/starred/sigmavirus24/github3.py')
@@ -716,4 +795,64 @@ def test_utf8_user(self):
716795
self.fail('Regression caught. See PR #52. Names must be utf-8'
717796
' encoded')
718797

719-
# no test_zen
798+
def test_zen(self):
799+
self.response('archive')
800+
self.get('https://api.github.com/zen')
801+
802+
expect(self.g.zen()) == b'archive_data'
803+
self.mock_assertions()
804+
805+
806+
class TestGitHubEnterprise(BaseCase):
807+
def setUp(self):
808+
super(TestGitHubEnterprise, self).setUp()
809+
self.g = github3.GitHubEnterprise('https://github.example.com/')
810+
811+
def test_admin_stats(self):
812+
self.response('user')
813+
self.get('https://github.example.com/api/v3/enterprise/stats/all')
814+
815+
with expect.githuberror():
816+
self.g.admin_stats(None)
817+
818+
self.not_called()
819+
self.login()
820+
expect(self.g.admin_stats('all')).isinstance(dict)
821+
self.mock_assertions()
822+
823+
def test_repr(self):
824+
expect(repr(self.g).startswith('<GitHub Enterprise')).is_True()
825+
826+
827+
class TestGitHubStatus(BaseCase):
828+
def setUp(self):
829+
super(TestGitHubStatus, self).setUp()
830+
self.g = github3.GitHubStatus()
831+
self.api = 'https://status.github.com/'
832+
833+
def test_repr(self):
834+
expect(repr(self.g)) == '<GitHub Status>'
835+
836+
def test_api(self):
837+
self.response('user')
838+
self.get(self.api + 'api.json')
839+
expect(self.g.api()).isinstance(dict)
840+
self.mock_assertions()
841+
842+
def test_status(self):
843+
self.response('user')
844+
self.get(self.api + 'api/status.json')
845+
expect(self.g.status()).isinstance(dict)
846+
self.mock_assertions()
847+
848+
def test_last_message(self):
849+
self.response('user')
850+
self.get(self.api + 'api/last-message.json')
851+
expect(self.g.last_message()).isinstance(dict)
852+
self.mock_assertions()
853+
854+
def test_messages(self):
855+
self.response('user')
856+
self.get(self.api + 'api/messages.json')
857+
expect(self.g.messages()).isinstance(dict)
858+
self.mock_assertions()

tests/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ def mock_assertions(self):
7979

8080
expect(self.args) == args
8181

82-
if 'data' in self.conf and isinstance(self.conf['data'], dict):
83-
for k, v in list(self.conf['data'].items()):
84-
s = json.dumps({k: v})[1:-1]
85-
expect(s).is_in(kwargs['data'])
82+
if 'data' in self.conf:
83+
if isinstance(self.conf['data'], dict):
84+
for k, v in list(self.conf['data'].items()):
85+
s = json.dumps({k: v})[1:-1]
86+
expect(s).is_in(kwargs['data'])
87+
else:
88+
expect(self.conf['data']) == kwargs['data']
8689

8790
del self.conf['data']
8891

0 commit comments

Comments
 (0)
0