Skip to content

Commit

Permalink
fix(converter): Update unit-type converted to support '%'
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-p-may committed Oct 26, 2023
1 parent cb16f43 commit 2c872a6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ph_units/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ def _standardize_unit_name(_input, _unit_type_alias_dict):
try:
input_unit = _unit_type_alias_dict[_input_string]
except KeyError:
raise UnitTypeNameNotFound(
"\n\tI do not know how to understand the input unit: '{}'?\nValid formats include: {}".format(
_input_string, sorted(_unit_type_alias_dict.keys())
if _input_string == "%":
input_unit = "%"
else:
raise UnitTypeNameNotFound(
"\n\tI do not know how to understand the input unit: '{}'?\nValid formats include: {}".format(
_input_string, sorted(_unit_type_alias_dict.keys())
)
)
)

return input_unit

Expand Down
20 changes: 20 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# -*- Python Version: 2.7 -*-
import pytest
from ph_units.converter import _standardize_unit_name, unit_type_dict, unit_type_alias_dict, UnitTypeNameNotFound


def test_standardize_unit_name_simple() -> None:
assert _standardize_unit_name("FT", unit_type_alias_dict) == "FT"
assert _standardize_unit_name("FT", unit_type_alias_dict) == "FT"

def test_standardize_unit_name_percent() -> None:
assert _standardize_unit_name("%", unit_type_alias_dict) == "%"

def test_standardize_unit_name_alias() -> None:
assert _standardize_unit_name("FT3/M", unit_type_alias_dict) == "CFM"
assert _standardize_unit_name("CFM", unit_type_alias_dict) == "CFM"

def test_standardize_gives_error() -> None:
with pytest.raises(UnitTypeNameNotFound):
_standardize_unit_name("Not/A-Unit", unit_type_alias_dict)
12 changes: 12 additions & 0 deletions tests/test_unit_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ def test_default_Unit():
assert u.value == 0.0
assert u.unit == "-"

def test_percent_Unit():
u = Unit(1.0, "%")
assert u.value == 1.0
assert u.unit == "%"


def test_add_percent_unit():
u1 = Unit(1.0, "%")
u2 = Unit(1.0, "%")
u3 = u1 + u2
assert u3.value == 2.0
assert u3.unit == "%"

def test_Unit_is_frozen():
u = Unit()
Expand Down

0 comments on commit 2c872a6

Please sign in to comment.