initial commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""Unittests for the check_env function."""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from pytohn_utils import check_env
|
||||
|
||||
|
||||
class TestCheckEnv(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Set up test environment variables."""
|
||||
cls.set_vars = ["A", "B", "C"]
|
||||
for var in cls.set_vars:
|
||||
os.environ[var] = f"value_{var.lower()}"
|
||||
cls.unset_var = "D"
|
||||
if os.getenv(cls.unset_var) is not None:
|
||||
raise unittest.SkipTest(
|
||||
f"Environment variable '{cls.unset_var}' should not be set for this test."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
for var in cls.set_vars:
|
||||
if var in os.environ:
|
||||
del os.environ[var]
|
||||
os.environ.pop(var, None)
|
||||
|
||||
def test_check_env_accepts_single_string(self):
|
||||
"""Test check_env function with a valid single string."""
|
||||
self.assertIsNone(check_env(self.set_vars[0]))
|
||||
|
||||
def test_check_env_accepts_list_of_strings(self):
|
||||
"""Test check_env function with a valid list of strings."""
|
||||
self.assertIsNone(check_env(self.set_vars))
|
||||
|
||||
def test_check_env_accepts_set_of_strings(self):
|
||||
"""Test check_env function with a valid set of strings."""
|
||||
self.assertIsNone(check_env(set(self.set_vars)))
|
||||
|
||||
def test_check_env_accepts_tuple_of_strings(self):
|
||||
"""Test check_env function with a valid tuple of strings."""
|
||||
self.assertIsNone(check_env(tuple(self.set_vars)))
|
||||
|
||||
def test_check_env_rejects_empty_list(self):
|
||||
"""Test check_env function with an empty list."""
|
||||
with self.assertRaises(ValueError):
|
||||
check_env([])
|
||||
|
||||
def test_check_env_raises_error_on_missing_vars(self):
|
||||
"""Test check_env function with missing environment variables."""
|
||||
with self.assertRaises(OSError):
|
||||
check_env(self.unset_var)
|
||||
|
||||
def test_check_env_does_not_raise_error_when_missing_ok_flag_set(self):
|
||||
"""Test that check_env does not raise an error when the environment variable is missing and missing_ok=True.
|
||||
|
||||
The 'missing_ok' flag allows the function to skip raising an error if the specified environment variable(s) are not set.
|
||||
"""
|
||||
self.assertIsNone(check_env(self.unset_var, missing_ok=True))
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Unittests for the readable function."""
|
||||
|
||||
import pytest
|
||||
|
||||
from python_utils import readable_unit
|
||||
|
||||
|
||||
TEST_CASES = [
|
||||
(-1, "B", ValueError),
|
||||
("abc", "B", ValueError),
|
||||
(100, "", ValueError),
|
||||
(100, 123, ValueError),
|
||||
(100, " ", ValueError),
|
||||
(0.5, "B", "0.50 B"),
|
||||
(0, "B", "0.00 B"),
|
||||
(1, "B", "1.00 B"),
|
||||
(1024, "B", "1.00 KB"),
|
||||
(500000, "bps", "488.28 Kbps"),
|
||||
(1048576, "bps", "1.00 Mbps"),
|
||||
(50000000, "bps", "47.68 Mbps"),
|
||||
(1073741824, "Hz", "1.00 GHz"),
|
||||
(5000000000, "Hz", "4.66 GHz"),
|
||||
(1099511627776, "foo", "1.00 Tfoo"),
|
||||
(5000000000000, "bar", "4.55 Tbar"),
|
||||
(2**50, "foo", "1.00 Pfoo"),
|
||||
(2**60, "B", "1.00 EB"),
|
||||
(2**70, "B", "1.00 ZB"),
|
||||
(2**80, "B", "1.00 YB"),
|
||||
(2**90, "B", "1024.00 YB"),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value, unit, expected", TEST_CASES)
|
||||
def test_readable_unit(value, unit, expected):
|
||||
"""Test readable_unit function with valid inputs."""
|
||||
if isinstance(expected, type) and issubclass(expected, Exception):
|
||||
with pytest.raises(expected):
|
||||
readable_unit(value, unit)
|
||||
else:
|
||||
result = readable_unit(value, unit)
|
||||
assert result == expected
|
||||
Reference in New Issue
Block a user