Skip to content

Commit

Permalink
WIP Implement converter types
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Feb 20, 2024
1 parent 35acbc7 commit cc7aa0e
Show file tree
Hide file tree
Showing 3 changed files with 488 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/property_utils/tests/units/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
GenericDimension,
GenericCompositeDimension,
)
from property_utils.units.converter_types import (
register_converter,
AbsoluteUnitConverter,
RelativeUnitConverter,
ExponentiatedPhysicalPropertyUnitConverter,
CompositePhysicalPropertyUnitConverter,
)


class Unit1(MeasurementUnit):
A = "A"
a = "a"
A2 = "A2"

@classmethod
def si(cls) -> "Unit1":
Expand All @@ -35,6 +43,30 @@ def si(cls) -> "Unit3":
return cls.c


class UnregisteredConverter(AbsoluteUnitConverter): ...


@register_converter(Unit1)
class Unit1Converter(AbsoluteUnitConverter):
reference_unit = Unit1.A
conversion_map = {Unit1.A: 1, Unit1.a: 10}


@register_converter(Unit2)
class Unit2Converter(RelativeUnitConverter):
reference_unit = Unit2.B
conversion_map = {Unit2.B: lambda u: u, Unit2.b: lambda u: (2 * u) + 3}
reference_conversion_map = {Unit2.B: lambda u: u, Unit2.b: lambda u: (u - 3) / 2}


@register_converter(Unit1**3.14)
class Unit1_314Converter(ExponentiatedPhysicalPropertyUnitConverter): ...


@register_converter(Unit1 * Unit2)
class Unit1Unit2Converter(CompositePhysicalPropertyUnitConverter): ...


def dimension_1(power: float = 1) -> Dimension:
"""
A^power
Expand Down
86 changes: 86 additions & 0 deletions src/property_utils/tests/units/test_converter_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from unittest import TestSuite, TextTestRunner

from unittest_extensions import TestCase, args

from property_utils.units.converter_types import (
get_converter,
register_converter,
AbsoluteUnitConverter,
RelativeUnitConverter,
CompositePhysicalPropertyUnitConverter,
)
from property_utils.exceptions.base import (
PropertyUtilsTypeError,
PropertyUtilsValueError,
)
from property_utils.exceptions.units.converter_types import UndefinedConverter
from property_utils.tests.utils import add_to
from property_utils.tests.units.data import (
Unit1,
Unit2,
Unit3,
Unit1Converter,
UnregisteredConverter,
Unit1_314Converter,
Unit2Converter,
Unit1Unit2Converter,
)

converter_types_test_suite = TestSuite()

converter_types_test_suite.addTests(
[
(TestGetConverter_test_suite := TestSuite()),
(TestRegisterConverter_test_suite := TestSuite()),
]
)


@add_to(TestGetConverter_test_suite)
class TestGetConverter(TestCase):
def subject(self, generic):
return get_converter(generic)

@args({"generic": Unit1.A})
def test_with_measurement_unit(self):
self.assertResultRaises(PropertyUtilsTypeError)

@args({"generic": Unit3})
def test_with_unregistered_generic(self):
self.assertResultRaises(UndefinedConverter)

@args({"generic": Unit2})
def test_with_measurement_unit_type(self):
self.assertResult(Unit2Converter)

@args({"generic": Unit1**3.14})
def test_with_generic_dimension(self):
self.assertResult(Unit1_314Converter)

@args({"generic": Unit1 * Unit2})
def test_with_generic_composite_dimension(self):
self.assertResult(Unit1Unit2Converter)


@add_to(TestRegisterConverter_test_suite)
class TestRegisterConverter(TestCase):
def subject(self, generic):
return register_converter(generic)(UnregisteredConverter)

@args({"generic": Unit1.A})
def test_with_measurement_unit(self):
self.assertResultRaises(PropertyUtilsTypeError)

@args({"generic": Unit1})
def test_with_registered_converter(self):
self.assertResultRaises(PropertyUtilsValueError)

@args({"generic": Unit3})
def test_with_unregistered_converter(self):
self.assertResult(UnregisteredConverter)
self.assertEqual(self.cachedResult().generic_unit_descriptor, Unit3)


if __name__ == "__main__":
runner = TextTestRunner()
runner.run(converter_types_test_suite)
Loading

0 comments on commit cc7aa0e

Please sign in to comment.