otko/tests/unit/test_formatting.py
smillmorel f1d58273b7 feat: shared unit-aware formatting helpers
Pure presentation helpers (labels, number parse/format) shared by docks
and dialogs; no Qt imports.
2026-09-16 12:03:07 -04:00

51 lines
1.3 KiB
Python

"""Unit tests for :mod:`otko.views.formatting` (no Qt required)."""
from __future__ import annotations
import pytest
from otko.core import UnitSystem
from otko.views.formatting import (
DOF_LABELS,
MASS_LABELS,
RESTRAINT_LABELS,
format_number,
parse_float,
parse_int,
unit_label,
)
def test_label_tuples() -> None:
assert DOF_LABELS == ("Ux", "Uy", "Uz", "Rx", "Ry", "Rz")
assert MASS_LABELS == ("Mx", "My", "Mz", "Ixx", "Iyy", "Izz")
assert RESTRAINT_LABELS == ("Ux", "Uy", "Uz", "Rx", "Ry", "Rz")
def test_format_number() -> None:
assert format_number(50.0) == "50"
assert format_number(1.5) == "1.5"
def test_parse_float_required() -> None:
with pytest.raises(ValueError):
parse_float("", "x")
def test_parse_float_allow_empty() -> None:
assert parse_float("", "x", allow_empty=True) == 0.0
def test_parse_int_rejects_fractional() -> None:
with pytest.raises(ValueError):
parse_int("1.5", "x")
def test_unit_label() -> None:
assert unit_label("force", UnitSystem.SI_M_N) == "N"
assert unit_label("moment", UnitSystem.US_FT_KIP) == "kip·ft"
def test_unit_label_unknown_quantity() -> None:
with pytest.raises(ValueError):
unit_label("temperature", UnitSystem.SI_M_N)