Skip to content

Commit

Permalink
- Parsing simple ints for memory limit
Browse files Browse the repository at this point in the history
  • Loading branch information
matveyvarg committed Nov 13, 2023
1 parent 1336ee5 commit e21451a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions deker/tools/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def convert_human_memory_to_bytes(memory_limit: Union[int, str]) -> int:
if isinstance(memory_limit, int):
return memory_limit

try:
return int(memory_limit)
except ValueError:
pass

limit, div = memory_limit[:-1], memory_limit.lower()[-1]
try:
int_limit: int = int(limit)
Expand Down
22 changes: 20 additions & 2 deletions tests/test_cases/test_tools/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from tests.parameters.collection_params import CollectionParams

from deker.collection import Collection
from deker.errors import DekerInstanceNotExistsError, DekerMemoryError
from deker.tools import check_memory
from deker.errors import DekerInstanceNotExistsError, DekerMemoryError, DekerValidationError
from deker.tools import check_memory, convert_human_memory_to_bytes
from deker.tools.time import convert_datetime_attrs_to_iso, convert_iso_attrs_to_datetime


Expand Down Expand Up @@ -227,5 +227,23 @@ def test_generate_id_raises(array_type_arg):
generate_uid(array_type_arg)


@pytest.mark.parametrize(
"params,result,error",
(
("1K", 1024, None),
("1M", 1024**2, None),
("1G", 1024**3, None),
("0", 0, None),
("Foo", None, DekerValidationError),
),
)
def test_convert_human_to_bytes(params, result, error):
if error:
with pytest.raises(error):
convert_human_memory_to_bytes(params)
else:
assert convert_human_memory_to_bytes(params) == result


if __name__ == "__main__":
pytest.main()

0 comments on commit e21451a

Please sign in to comment.