util: add helper to parse size strings
Code is based on `common.DataSizeToUint64` in Composer, with a
modification to allow `unlimited` so that the result is compatible
with `fscache.MaximumSizeType`.
[1] f4aed3e6e2/internal/common/helpers.go (L46)
This commit is contained in:
parent
e2c687e363
commit
1e0e1fa2c2
2 changed files with 72 additions and 0 deletions
39
test/mod/test_util_parsing.py
Normal file
39
test/mod/test_util_parsing.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
"""Unit tests for osbuild.util.parsing"""
|
||||
|
||||
import pytest
|
||||
|
||||
from osbuild.util import parsing
|
||||
|
||||
|
||||
def test_parse_size():
|
||||
cases = [
|
||||
("123", True, 123),
|
||||
("123 kB", True, 123000),
|
||||
("123 KiB", True, 123 * 1024),
|
||||
("123 MB", True, 123 * 1000 * 1000),
|
||||
("123 MiB", True, 123 * 1024 * 1024),
|
||||
("123 GB", True, 123 * 1000 * 1000 * 1000),
|
||||
("123 GiB", True, 123 * 1024 * 1024 * 1024),
|
||||
("123 TB", True, 123 * 1000 * 1000 * 1000 * 1000),
|
||||
("123 TiB", True, 123 * 1024 * 1024 * 1024 * 1024),
|
||||
("123kB", True, 123000),
|
||||
("123KiB", True, 123 * 1024),
|
||||
(" 123", True, 123),
|
||||
(" 123kB", True, 123000),
|
||||
(" 123KiB", True, 123 * 1024),
|
||||
("unlimited", True, "unlimited"),
|
||||
("string", False, 0),
|
||||
("123 KB", False, 0),
|
||||
("123 mb", False, 0),
|
||||
("123 PB", False, 0),
|
||||
("123 PiB", False, 0),
|
||||
|
||||
]
|
||||
|
||||
for s, success, num in cases:
|
||||
if not success:
|
||||
with pytest.raises(TypeError):
|
||||
parsing.parse_size(s)
|
||||
else:
|
||||
res = parsing.parse_size(s)
|
||||
assert res == num, f"{s} parsed as {res} (wanted {num})"
|
||||
Loading…
Add table
Add a link
Reference in a new issue