68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
"""Tests for the unit conversion module."""
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from disk_checker.core.converter import to_unit, UNIT_FACTORS, UNIT_LABELS
|
|
|
|
|
|
class TestToUnit:
|
|
"""Tests for to_unit function."""
|
|
|
|
def test_nanoseconds_to_microseconds(self):
|
|
"""Test conversion from ns to us."""
|
|
values = np.array([1000, 2000, 3000])
|
|
result = to_unit(values, "us")
|
|
expected = np.array([1.0, 2.0, 3.0])
|
|
np.testing.assert_array_equal(result, expected)
|
|
|
|
def test_nanoseconds_to_milliseconds(self):
|
|
"""Test conversion from ns to ms."""
|
|
values = np.array([1000000, 2000000])
|
|
result = to_unit(values, "ms")
|
|
expected = np.array([1.0, 2.0])
|
|
np.testing.assert_array_equal(result, expected)
|
|
|
|
def test_nanoseconds_to_seconds(self):
|
|
"""Test conversion from ns to s."""
|
|
values = np.array([1000000000, 2000000000])
|
|
result = to_unit(values, "s")
|
|
expected = np.array([1.0, 2.0])
|
|
np.testing.assert_array_equal(result, expected)
|
|
|
|
def test_same_unit_returns_same(self):
|
|
"""Test conversion to same unit."""
|
|
values = np.array([100, 200, 300])
|
|
result = to_unit(values, "ns")
|
|
np.testing.assert_array_equal(result, values)
|
|
|
|
def test_empty_array(self):
|
|
"""Test conversion with empty array."""
|
|
values = np.array([])
|
|
result = to_unit(values, "us")
|
|
assert len(result) == 0
|
|
|
|
def test_single_value(self):
|
|
"""Test conversion with single value."""
|
|
result = to_unit(1000, "us")
|
|
assert result == 1.0
|
|
|
|
|
|
class TestConstants:
|
|
"""Tests for module constants."""
|
|
|
|
def test_unit_factors(self):
|
|
"""Test UNIT_FACTORS contains all expected units."""
|
|
assert set(UNIT_FACTORS.keys()) == {"ns", "us", "ms", "s"}
|
|
assert UNIT_FACTORS["ns"] == 1.0
|
|
assert UNIT_FACTORS["us"] == 1e3
|
|
assert UNIT_FACTORS["ms"] == 1e6
|
|
assert UNIT_FACTORS["s"] == 1e9
|
|
|
|
def test_unit_labels(self):
|
|
"""Test UNIT_LABELS contains all expected units."""
|
|
assert set(UNIT_LABELS.keys()) == {"ns", "us", "ms", "s"}
|
|
assert UNIT_LABELS["ns"] == "ns"
|
|
assert UNIT_LABELS["us"] == "µs"
|
|
assert UNIT_LABELS["ms"] == "ms"
|
|
assert UNIT_LABELS["s"] == "s" |