Skip to content

Commit 67ca14b

Browse files
committed
Add tests for PullFile#contents
- Re-order methods - Make existing PullFile#download unit test better
1 parent d9991fe commit 67ca14b

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

github3/pulls.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ def _update_attributes(self, pfile):
8282
def _repr(self):
8383
return '<Pull Request File [{0}]>'.format(self.filename)
8484

85+
def contents(self):
86+
"""Return the contents of the file as bytes.
87+
88+
:param stream: When true, the resulting object can be iterated over via
89+
``iter_contents``.
90+
"""
91+
headers = {'Accept': 'application/octet-stream'}
92+
resp = self._get(self.raw_url, headers=headers)
93+
if self._boolean(resp, 200, 404):
94+
return resp.content
95+
return b''
96+
8597
def download(self, path=None):
8698
"""Download the contents for this file to disk.
8799
@@ -100,18 +112,6 @@ def download(self, path=None):
100112
return utils.stream_response_to_file(resp, path)
101113
return None
102114

103-
def contents(self):
104-
"""Return the contents of the file as bytes.
105-
106-
:param stream: When true, the resulting object can be iterated over via
107-
``iter_contents``.
108-
"""
109-
headers = {'Accept': 'application/octet-stream'}
110-
resp = self._get(self.raw_url, headers=headers)
111-
if self._boolean(resp, 200, 404):
112-
return resp.content
113-
return b''
114-
115115

116116
class PullRequest(models.GitHubCore):
117117

tests/cassettes/PullFile_contents.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/integration/test_pulls.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,36 @@ def test_reply(self):
132132

133133
class TestPullFile(IntegrationHelper):
134134
"""Integration tests for the PullFile object."""
135+
def get_pull_request_file(self, owner, repo, pull_number, filename):
136+
p = self.gh.pull_request(owner, repo, pull_number)
137+
138+
for pull_file in p.files():
139+
if pull_file.filename == filename:
140+
break
141+
else:
142+
assert False, "Could not find '{0}'".format(filename)
143+
144+
return pull_file
145+
146+
def test_contents(self):
147+
"""Show that a user can retrieve the contents of a PR file."""
148+
cassette_name = self.cassette_name('contents')
149+
with self.recorder.use_cassette(cassette_name):
150+
pull_file = self.get_pull_request_file(
151+
owner='sigmavirus24', repo='github3.py', pull_number=286,
152+
filename='github3/pulls.py'
153+
)
154+
155+
assert isinstance(pull_file.contents(), bytes)
135156

136157
def test_download(self):
137158
"""Show that a user can download a file in a pull request."""
138159
cassette_name = self.cassette_name('download')
139160
with self.recorder.use_cassette(cassette_name):
140-
p = self.gh.pull_request('sigmavirus24', 'github3.py', 286)
141-
142-
for pull_file in p.files():
143-
if pull_file.filename == 'github3/pulls.py':
144-
break
145-
else:
146-
assert False, "Could not find 'github3/pulls.py'"
161+
pull_file = self.get_pull_request_file(
162+
owner='sigmavirus24', repo='github3.py', pull_number=286,
163+
filename='github3/pulls.py'
164+
)
147165

148166
with tempfile.NamedTemporaryFile() as fd:
149167
filename = pull_file.download(fd)

tests/unit/test_pulls.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ def test_download(self, stream_response_to_file):
309309
self.example_data['raw_url'], stream=True,
310310
headers={'Accept': 'application/octet-stream'}
311311
)
312-
stream_response_to_file.assert_called_once_with(mock.ANY, 'file1.txt')
312+
stream_response_to_file.assert_called_once_with(response_mock,
313+
'file1.txt')
313314

314315
@mock.patch('github3.utils.stream_response_to_file')
315316
def test_download_does_not_stream(self, stream_response_to_file):
@@ -323,3 +324,12 @@ def test_download_does_not_stream(self, stream_response_to_file):
323324
headers={'Accept': 'application/octet-stream'}
324325
)
325326
assert stream_response_to_file.called is False
327+
328+
def test_contents(self):
329+
"""Verify the request made to fetch a pull request file contents."""
330+
self.instance.contents()
331+
332+
self.session.get.assert_called_once_with(
333+
self.example_data['raw_url'],
334+
headers={'Accept': 'application/octet-stream'}
335+
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy