8000 Add unit tests for PullFile#download · waynr/github3.py@d4185c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4185c2

Browse files
committed
Add unit tests for PullFile#download
1 parent 3344ea2 commit d4185c2

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

github3/pulls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def contents(self):
106106
:param stream: When true, the resulting object can be iterated over via
107107
``iter_contents``.
108108
"""
109-
headers = {'Accept': 'application/octect-stream'}
109+
headers = {'Accept': 'application/octet-stream'}
110110
resp = self._get(self.raw_url, headers=headers)
111111
if self._boolean(resp, 200, 404):
112112
return resp.content

tests/unit/test_pulls.py

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import pytest
33

44
from .helper import (UnitHelper, UnitIteratorHelper, create_url_helper,
5-
create_example_data_helper)
5+
create_example_data_helper, mock)
66

77
from github3 import GitHubError
8-
from github3.pulls import PullRequest, ReviewComment
8+
from github3 import pulls
99

1010
get_pr_example_data = create_example_data_helper('pull_request_example')
1111

@@ -16,10 +16,9 @@
1616

1717

1818
class TestPullRequest(UnitHelper):
19-
2019
"""PullRequest unit tests."""
2120

22-
described_class = PullRequest
21+
described_class = pulls.PullRequest
2322
example_data = get_pr_example_data()
2423

2524
def test_close(self):
@@ -109,10 +108,9 @@ def test_update(self):
109108

110109

111110
class TestPullRequestRequiresAuthentication(UnitHelper):
112-
113111
"""PullRequest unit tests that demonstrate which methods require auth."""
114112

115-
described_class = PullRequest
113+
described_class = pulls.PullRequest
116114
example_data = get_pr_example_data()
117115

118116
def after_setup(self):
@@ -146,10 +144,9 @@ def test_update(self):
146144

147145

148146
class TestPullRequestIterator(UnitIteratorHelper):
149-
150147
"""Test PullRequest methods that return Iterators."""
151148

152-
described_class = PullRequest
149+
described_class = pulls.PullRequest
153150
example_data = get_pr_example_data()
154151

155152
def test_commits(self):
@@ -198,10 +195,9 @@ def test_review_comments(self):
198195

199196

200197
class TestReviewComment(UnitHelper):
201-
202198
"""Unit tests for the ReviewComment class."""
203199

204-
described_class = ReviewComment
200+
described_class = pulls.ReviewComment
205201
example_data = {
206202
"url": ("https://api.github.com/repos/octocat/Hello-World/pulls/"
207203
"comments/1"),
@@ -276,3 +272,54 @@ def test_reply_requires_authentication(self):
276272

277273
with pytest.raises(GitHubError):
278274
self.instance.reply('')
275+
276+
277+
class TestPullFile(UnitHelper):
278+
"""Unit tests for the PullFile class."""
279+
280+
described_class = pulls.PullFile
281+
example_data = {
282+
"sha": "bbcd538c8e72b8c175046e27cc8f907076331401",
283+
"filename": "file1.txt",
284+
"status": "added",
285+
"additions": 103,
286+
"deletions": 21,
287+
"changes": 124,
288+
"blob_url": ("https://github.com/octocat/Hello-World/blob/"
289+
"6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt"),
290+
"raw_url": ("https://github.com/octocat/Hello-World/raw/"
291+
"6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt"),
292+
"contents_url": ("https://api.github.com/repos/octocat/Hello-World/"
293+
"contents/file1.txt?ref=6dcb09b5b57875f334f61aebed"
294+
"695e2e4193db5e"),
295+
"patch": ("@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@"
296+
" module Test")
297+
}
298+
299+
@mock.patch('github3.utils.stream_response_to_file')
300+
def test_download(self, stream_response_to_file):
301+
"""Verify the proper request is made to download file contents."""
302+
response_mock = mock.Mock()
303+
response_mock.status_code = 200
304+
self.session.get.return_value = response_mock
305+
306+
self.instance.download()
307+
308+
self.session.get.assert_called_once_with(
309+
self.example_data['raw_url'], stream=True,
310+
headers={'Accept': 'application/octet-stream'}
311+
)
312+
stream_response_to_file.assert_called_once_with(mock.ANY, 'file1.txt')
313+
314+
@mock.patch('github3.utils.stream_response_to_file')
315+
def test_download_does_not_stream(self, stream_response_to_file):
316+
"""Verify the proper request is made to download file contents."""
317+
# Since the default return value for self.session.get is None we do
318+
# not need to mock out the response object in this test.
319+
self.instance.download()
320+
321+
self.session.get.assert_called_once_with(
322+
self.example_data['raw_url'], stream=True,
323+
headers={'Accept': 'application/octet-stream'}
324+
)
325+
assert stream_response_to_file.called is False

0 commit comments

Comments
 (0)
0