Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-270946 Progress percentage computation to handle the case when f… #628

Merged
merged 1 commit into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
sfc-gh-cshi marked this conversation as resolved.
Show resolved Hide resolved
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)