Skip to content

Commit

Permalink
warn if object to convert is not a number, test it
Browse files Browse the repository at this point in the history
  • Loading branch information
stolarczyk committed Jul 24, 2019
1 parent ff3c422 commit 2fe307a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@ def test_nonexistent_path(tmpdir):
@pytest.mark.parametrize("size_num", list(range(0, 10)) + [i/3 for i in range(0, 10)])
def test_filesize_to_str_int(size_num):
""" Works with int and returns str """
print(size_num)
assert isinstance(filesize_to_str(size_num), str)


@pytest.mark.parametrize("obj", ["test", [], tuple()])
def test_filesize_to_str_other(obj):
""" Returns the original object if it's not an int or float and warns """
with pytest.warns(UserWarning):
assert filesize_to_str(obj) == obj
2 changes: 2 additions & 0 deletions ubiquerg/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from hashlib import md5
import os
from warnings import warn

__all__ = ["checksum", "size", "filesize_to_str"]
FILE_SIZE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
Expand Down Expand Up @@ -66,4 +67,5 @@ def filesize_to_str(size):
if size < 1024:
return "{}{}".format(round(size, 1), unit)
size /= 1024
warn("size argument was neither an int nor a float, returning the original object")
return size

0 comments on commit 2fe307a

Please sign in to comment.