diff --git a/bia/test_units.py b/bia/test_units.py index be83207..b671378 100644 --- a/bia/test_units.py +++ b/bia/test_units.py @@ -1,4 +1,5 @@ -from .units import Unit +import pytest +from .units import Unit, ConversionError from .units import UnitValue as V @@ -36,6 +37,15 @@ def test_complex_conversions(): assert g_ft.unit == Unit(['ft'], ['s', 's']) +def test_invalid_conversion_basic(): + with pytest.raises(ConversionError): + V(1, 'm').as_unit('s') + + +def test_invalid_conversion_dimension(): + with pytest.raises(ConversionError): + V(1, ['m']).as_unit(['m', 'm']) + def test_basic_cmp(): assert V(1, 'kg') < V(2, 'kg') diff --git a/bia/units.py b/bia/units.py index 9beded3..d6ebd86 100644 --- a/bia/units.py +++ b/bia/units.py @@ -3,15 +3,17 @@ from collections import defaultdict _UNITS = { # mass - 'kg': {'base': 'kg', 'scale': 1.0}, - 'lb': {'base': 'kg', 'scale': 2.20462}, - 'g': {'base': 'kg', 'scale': 1000.0}, + 'kg': {'type': 'mass', 'scale': 1.0}, + 'lb': {'type': 'mass', 'scale': 2.20462}, + 'g': {'type': 'mass', 'scale': 1000.0}, - 'm': {'base': 'm', 'scale': 1.0}, - 'cm': {'base': 'm', 'scale': 100.0}, - 'ft': {'base': 'm', 'scale': 3.28084}, + # length + 'm': {'type': 'length', 'scale': 1.0}, + 'cm': {'type': 'length', 'scale': 100.0}, + 'ft': {'type': 'length', 'scale': 3.28084}, - 's': {'base': 's', 'scale': 1.0}, + # time + 's': {'type': 'time', 'scale': 1.0}, } @@ -58,6 +60,14 @@ class Unit(object): self.denominator.remove(u) def conversion_factor(self, other): + if (sorted([_UNITS[u]['type'] for u in self.numerator]) != + sorted([_UNITS[u]['type'] for u in other.numerator])): + raise ConversionError('cannot convert {} to {}') + if (sorted([_UNITS[u]['type'] for u in self.denominator]) != + sorted([_UNITS[u]['type'] for u in other.denominator])): + raise ConversionError('cannot convert {} to {}') + + # compute factor factor = 1 for u in self.numerator: factor /= _UNITS[u]['scale']