def test_create_small_file_connection_error(): # turn a requests.ConnectionError into a RuntimeError with a more helpful # message that the file might exist new_file_url = ('https://files.osf.io/v1/resources/9zpcy/providers/' + 'osfstorage/foo123/') store = Storage({}) store._new_file_url = new_file_url store._put = MagicMock(side_effect=ConnectionError) try: exception = RuntimeError except NameError: exception = OSError fake_fp = MagicMock() fake_fp.mode = 'rb' # set file size of 1 MB minus 1 byte ("small" file) with patch('osfclient.models.storage.get_local_file_size', return_value=2**20-1): with pytest.raises(exception): store.create_file('foo.txt', fake_fp) store._put.assert_called_once_with(new_file_url, data=fake_fp, params={'name': 'foo.txt'}) assert fake_fp.call_count == 0
def test_create_big_file_connection_error(monkeypatch): # with a "big" file, we're more confident that a connection error means the # file alredy exists, so raise FileExistsError without hedging new_file_url = ('https://files.osf.io/v1/resources/9zpcy/providers/' + 'osfstorage/foo123/') store = Storage({}) store._new_file_url = new_file_url store._put = MagicMock(side_effect=ConnectionError) try: exception = FileExistsError except NameError: exception = OSError fake_fp = MagicMock() fake_fp.mode = 'rb' # set file size of 1 MB ("big" file) with patch('osfclient.models.storage.get_local_file_size', return_value=2**20): with pytest.raises(exception): store.create_file('foo.txt', fake_fp) store._put.assert_called_once_with(new_file_url, data=fake_fp, params={'name': 'foo.txt'}) assert fake_fp.call_count == 0
def test_create_new_zero_length_file(): # check zero length files are special cased new_file_url = ('https://files.osf.io/v1/resources/9zpcy/providers/' + 'osfstorage/foo123/') store = Storage({}) store._new_file_url = new_file_url store._put = MagicMock(return_value=FakeResponse(201, None)) fake_fp = MagicMock() fake_fp.mode = 'rb' if six.PY3: fake_fp.peek = lambda: '' if six.PY2: fake_fp.read = lambda: '' store.create_file('foo.txt', fake_fp) store._put.assert_called_once_with( new_file_url, # this is the important check in # this test data=b'', params={'name': 'foo.txt'}) assert fake_fp.call_count == 0
def test_create_new_file(): # create a new file at the top level new_file_url = ('https://files.osf.io/v1/resources/9zpcy/providers/' + 'osfstorage/foo123/') store = Storage({}) store._new_file_url = new_file_url store._put = MagicMock(return_value=FakeResponse(201, None)) fake_fp = MagicMock() fake_fp.mode = 'rb' store.create_file('foo.txt', fake_fp) store._put.assert_called_once_with(new_file_url, data=fake_fp, params={'name': 'foo.txt'}) assert fake_fp.call_count == 0
def test_create_existing_file(): # try to create file with a name that is already taken new_file_url = ('https://files.osf.io/v1/resources/9zpcy/providers/' + 'osfstorage/foo123/') store = Storage({}) store._new_file_url = new_file_url store._put = MagicMock(return_value=FakeResponse(409, None)) try: exception = FileExistsError except NameError: exception = OSError fake_fp = MagicMock() fake_fp.mode = 'rb' with pytest.raises(exception): store.create_file('foo.txt', fake_fp) store._put.assert_called_once_with(new_file_url, data=fake_fp, params={'name': 'foo.txt'}) assert fake_fp.call_count == 0