|
2 | 2 | import pytest
|
3 | 3 |
|
4 | 4 | from .helper import (UnitHelper, UnitIteratorHelper, create_url_helper,
|
5 |
| - create_example_data_helper) |
| 5 | + create_example_data_helper, mock) |
6 | 6 |
|
7 | 7 | from github3 import GitHubError
|
8 |
| -from github3.pulls import PullRequest, ReviewComment |
| 8 | +from github3 import pulls |
9 | 9 |
|
10 | 10 | get_pr_example_data = create_example_data_helper('pull_request_example')
|
11 | 11 |
|
|
16 | 16 |
|
17 | 17 |
|
18 | 18 | class TestPullRequest(UnitHelper):
|
19 |
| - |
20 | 19 | """PullRequest unit tests."""
|
21 | 20 |
|
22 |
| - described_class = PullRequest |
| 21 | + described_class = pulls.PullRequest |
23 | 22 | example_data = get_pr_example_data()
|
24 | 23 |
|
25 | 24 | def test_close(self):
|
@@ -109,10 +108,9 @@ def test_update(self):
|
109 | 108 |
|
110 | 109 |
|
111 | 110 | class TestPullRequestRequiresAuthentication(UnitHelper):
|
112 |
| - |
113 | 111 | """PullRequest unit tests that demonstrate which methods require auth."""
|
114 | 112 |
|
115 |
| - described_class = PullRequest |
| 113 | + described_class = pulls.PullRequest |
116 | 114 | example_data = get_pr_example_data()
|
117 | 115 |
|
118 | 116 | def after_setup(self):
|
@@ -146,10 +144,9 @@ def test_update(self):
|
146 | 144 |
|
147 | 145 |
|
148 | 146 | class TestPullRequestIterator(UnitIteratorHelper):
|
149 |
| - |
150 | 147 | """Test PullRequest methods that return Iterators."""
|
151 | 148 |
|
152 |
| - described_class = PullRequest |
| 149 | + described_class = pulls.PullRequest |
153 | 150 | example_data = get_pr_example_data()
|
154 | 151 |
|
155 | 152 | def test_commits(self):
|
@@ -198,10 +195,9 @@ def test_review_comments(self):
|
198 | 195 |
|
199 | 196 |
|
200 | 197 | class TestReviewComment(UnitHelper):
|
201 |
| - |
202 | 198 | """Unit tests for the ReviewComment class."""
|
203 | 199 |
|
204 |
| - described_class = ReviewComment |
| 200 | + described_class = pulls.ReviewComment |
205 | 201 | example_data = {
|
206 | 202 | "url": ("https://api.github.com/repos/octocat/Hello-World/pulls/"
|
207 | 203 | "comments/1"),
|
@@ -276,3 +272,54 @@ def test_reply_requires_authentication(self):
|
276 | 272 |
|
277 | 273 | with pytest.raises(GitHubError):
|
278 | 274 | 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