|
1 | 1 | fromdatetimeimportdatetime
|
2 |
| -fromgithub3.utilsimporttimestamp_parameter |
| 2 | +fromgithub3.utilsimportstream_response_to_file,timestamp_parameter |
3 | 3 |
|
| 4 | +importio |
| 5 | +importmock |
4 | 6 | importpytest
|
| 7 | +importrequests |
5 | 8 |
|
6 | 9 |
|
7 | 10 | classTestTimestampConverter:
|
@@ -38,3 +41,46 @@ def test_none_handling(self):
|
38 | 41 |
|
39 | 42 | deftest_invalid_type_handling(self):
|
40 | 43 | pytest.raises(ValueError,timestamp_parameter,1)
|
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +defmocked_open(): |
| 48 | +returnmock.mock_open() |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture |
| 52 | +defresponse(): |
| 53 | +r=requests.Response() |
| 54 | +r.raw=io.BytesIO(b'fake data') |
| 55 | +r.headers.update({'content-disposition':'filename=a_file_name'}) |
| 56 | +returnr |
| 57 | + |
| 58 | + |
| 59 | +classOpenFile: |
| 60 | +def__init__(self): |
| 61 | +self.data=b'' |
| 62 | +self.written_to=False |
| 63 | + |
| 64 | +defwrite(self,data): |
| 65 | +self.written_to=True |
| 66 | +self.data+=data |
| 67 | + |
| 68 | + |
| 69 | +classTestStreamingDownloads: |
| 70 | +deftest_opens_a_new_file(self,mocked_open,response): |
| 71 | +withmock.patch('github3.utils.open',mocked_open,create=True): |
| 72 | +stream_response_to_file(response,'some_file') |
| 73 | + |
| 74 | +mocked_open.assert_called_once_with('some_file','wb') |
| 75 | + |
| 76 | +deftest_uses_existing_file(self,response): |
| 77 | +fd=OpenFile() |
| 78 | +stream_response_to_file(response,fd) |
| 79 | +assertfd.written_toisTrue |
| 80 | +assertfd.data==b'fake data' |
| 81 | + |
| 82 | +deftest_finds_filename_in_headers(self,mocked_open,response): |
| 83 | +withmock.patch('github3.utils.open',mocked_open,create=True): |
| 84 | +stream_response_to_file(response) |
| 85 | + |
| 86 | +mocked_open.assert_called_once_with('a_file_name','wb') |