Skip to content

Commit

Permalink
SNOW-270946 Progress percentage computation to handle the case when f…
Browse files Browse the repository at this point in the history
…ile size is zero (#628)
  • Loading branch information
sfc-gh-cshi authored Feb 27, 2021
1 parent 23d8207 commit 74da1c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/snowflake/connector/file_transfer_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ def _update_progress(
return progress == 1.0


class SnowflakeProgressPercentage:
def percent(seen_so_far: int, size: float) -> float:
return 1.0 if seen_so_far >= size or size <= 0\
else float(seen_so_far / size)


class SnowflakeProgressPercentage():
"""Built-in Progress bar for PUT commands."""

def __init__(
Expand Down Expand Up @@ -208,7 +213,7 @@ def __call__(self, bytes_amount: int):
with self._lock:
if self._output_stream:
self._seen_so_far += bytes_amount
percentage = float(self._seen_so_far / self._size)
percentage = percent(self._seen_so_far, self._size)
if not self._done:
self._done = _update_progress(
self._filename, self._start_time,
Expand All @@ -234,7 +239,7 @@ def __call__(self, current: int):
with self._lock:
if self._output_stream:
self._seen_so_far = current
percentage = float(self._seen_so_far / self._size)
percentage = percent(self._seen_so_far, self._size)
if not self._done:
self._done = _update_progress(
self._filename, self._start_time,
Expand Down
22 changes: 21 additions & 1 deletion test/unit/test_put_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

from snowflake.connector.compat import IS_WINDOWS
from snowflake.connector.errors import Error
from snowflake.connector.file_transfer_agent import SnowflakeFileTransferAgent
from snowflake.connector.file_transfer_agent import (
SnowflakeAzureProgressPercentage,
SnowflakeFileTransferAgent,
SnowflakeS3ProgressPercentage,
percent,
)


@pytest.mark.skipif(IS_WINDOWS, reason='permission model is different')
Expand Down Expand Up @@ -74,3 +79,18 @@ def test_put_error(tmpdir):
sf_file_transfer_agent.result()

chmod(file1, 0o700)


def test_percentage(tmp_path):
"""Tests for ProgressPercentage classes."""
assert 1.0 == percent(0, 0)
assert 1.0 == percent(20, 0)
assert 1.0 == percent(40, 20)
assert 0.5 == percent(14, 28)

file_path = tmp_path / 'zero_file1'
file_path.touch()
func_callback = SnowflakeS3ProgressPercentage(str(file_path), 0)
func_callback(1)
func_callback = SnowflakeAzureProgressPercentage(str(file_path), 0)
func_callback(1)

0 comments on commit 74da1c5

Please sign in to comment.