def __init__(self, target, chunk_size=MiB, simultaneous_uploads=3, headers=None, test_chunks=True, max_chunk_retries=100, permanent_errors=(400, 404, 415, 500, 501)): self.config = Config(target=target, chunk_size=chunk_size, simultaneous_uploads=simultaneous_uploads, headers=headers, test_chunks=test_chunks, max_chunk_retries=max_chunk_retries, permanent_errors=permanent_errors) self.session = requests.Session() self.session.headers['User-Agent'] = user_agent() if headers: self.session.headers.update(headers) self.files = [] self.executor = ThreadPoolExecutor(simultaneous_uploads) self.futures = [] self.file_added = CallbackDispatcher() self.file_completed = CallbackDispatcher() self.chunk_completed = CallbackDispatcher()
def test_resolve_chunk_no_test(): session = mock_session() config = Config(target=TEST_TARGET, test_chunks=False, permanent_errors=[500]) file = mock_file() chunk = mock_chunk() resolve_chunk(session, config, file, chunk) session.get.assert_not_called() assert_post(session) file.mark_chunk_completed.assert_called_once_with(chunk)
def test_resolve_chunk_exists(): session = mock_session(test_status=200) config = Config(target=TEST_TARGET, test_chunks=True, permanent_errors=[500]) file = mock_file() chunk = mock_chunk() resolve_chunk(session, config, file, chunk) assert_get(session) session.post.assert_not_called() file.mark_chunk_completed.assert_called_once_with(chunk)
def test_resolve_chunk(path, file_type, test_status): session = mock_session() config = Config(target=TEST_TARGET, test_chunks=True, permanent_errors=[500]) file = mock_file(path) chunk = mock_chunk() resolve_chunk(session, config, file, chunk) assert_get(session, path=path, file_type=file_type) assert_post(session, path=path, file_type=file_type) file.mark_chunk_completed.assert_called_once_with(chunk)
def test_resolve_chunk_send_permanent_error(): session = mock_session(send_status=500) config = Config(target=TEST_TARGET, test_chunks=True, permanent_errors=[500]) file = mock_file() chunk = mock_chunk() with pytest.raises(ResumableError): resolve_chunk(session, config, file, chunk) assert_get(session) assert_post(session) file.mark_chunk_completed.assert_not_called()
def test_resumable(session_mock, executor_mock): mock_sim_uploads = 5 mock_chunk_size = 100 mock_headers = {'header': 'foo'} mock_max_chunk_retries = 100 mock_permanent_errors = [500] mock_test_chunks = True manager = Resumable( MOCK_TARGET, mock_chunk_size, mock_sim_uploads, mock_headers, mock_test_chunks, mock_max_chunk_retries, mock_permanent_errors ) assert manager.config == Config( target=MOCK_TARGET, simultaneous_uploads=mock_sim_uploads, chunk_size=mock_chunk_size, headers=mock_headers, max_chunk_retries=mock_max_chunk_retries, permanent_errors=mock_permanent_errors, test_chunks=mock_test_chunks ) assert manager.session == session_mock.return_value manager.session.headers.update.assert_called_once_with(mock_headers) assert manager.files == [] assert manager.executor == executor_mock.return_value executor_mock.assert_called_once_with(mock_sim_uploads)
def test_config(): config = Config(foo='bar') assert config.foo == 'bar'
def test_neq(other): assert Config(foo='bar') != other
def test_eq(): assert Config(foo='bar') == Config(foo='bar')
def test_str(): assert str(Config(foo='one', bar='two')) in [ "Config(foo='one', bar='two')", "Config(bar='two', foo='one')" ]
def test_setattr(): config = Config() config.foo = 'bar' assert config.foo == 'bar'
from resumable.util import Config def test_config(): config = Config(foo='bar') assert config.foo == 'bar' def test_setattr(): config = Config() config.foo = 'bar' assert config.foo == 'bar' def test_str(): assert str(Config(foo='one', bar='two')) in [ "Config(foo='one', bar='two')", "Config(bar='two', foo='one')" ] def test_eq(): assert Config(foo='bar') == Config(foo='bar') @pytest.mark.parametrize( 'other', [Config(), Config(foo='other'), Config(foo='bar', other='other')]) def test_neq(other): assert Config(foo='bar') != other