Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75b51f3

Browse files
committed
Add tests around stream_response_to_file
1 parent882fa52 commit75b51f3

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

‎tests/unit/test_utils.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
fromdatetimeimportdatetime
2-
fromgithub3.utilsimporttimestamp_parameter
2+
fromgithub3.utilsimportstream_response_to_file,timestamp_parameter
33

4+
importio
5+
importmock
46
importpytest
7+
importrequests
58

69

710
classTestTimestampConverter:
@@ -38,3 +41,46 @@ def test_none_handling(self):
3841

3942
deftest_invalid_type_handling(self):
4043
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')

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp