Esempio n. 1
0
 def test_convert(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, -273.15)
     ])
     assert cup.convert(42, Unit('km'),
                        to_unit=Unit('m')) == 42000  # source to ref
     assert cup.convert(42, Unit('m'),
                        to_unit=Unit('m')) == 42  # ref to ref
     assert cup.convert(42, Unit('m'),
                        to_unit=Unit('km')) == 0.042  # ref to source
     assert cup.convert(
         42, Unit('km'),
         to_unit=Unit('mm')) == 42000000  # source to other source
     with raises(ValueError):
         cup.convert(42, Unit('km'),
                     to_unit=Unit('K'))  # Don't share same ref unit
     with raises(ValueError):
         cup.convert(42, Unit('m'),
                     to_unit=Unit('C'))  # Don't share same ref unit
     with raises(ValueError):
         cup.convert(42, Unit('m'),
                     to_unit=Unit('bogus'))  # to_unit doesn't exist
     with raises(ValueError):
         cup.convert(42, Unit('bogus'),
                     to_unit=Unit('C'))  # from_unit doesn't exist
     with raises(ValueError):
         cup.convert(42, Unit('fake'),
                     to_unit=Unit('bogus'))  # neither unit exists
Esempio n. 2
0
 def test_iter(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, 273.15)
     ])
     assert Unit('mm') in cup
     assert Unit('m') in cup
     assert set(cup) == {'km', 'mm', 'm', 'C', 'K'}
Esempio n. 3
0
 def test_convert_to_ref(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, 273.15)
     ])
     assert cup.convert_to_ref(42, Unit('km')) == 42000  # source to ref
     assert cup.convert_to_ref(42, Unit('m')) == 42  # ref to ref
     with raises(ValueError):
         cup.convert_to_ref(42, Unit('bogus'))  # unit doesn't exist
Esempio n. 4
0
 def test_can_convert(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, 273.15)
     ])
     assert cup.can_convert(from_unit=Unit('km'), to_unit=Unit('m'))
     assert cup.can_convert(from_unit=Unit('m'), to_unit=Unit('km'))
     assert cup.can_convert(from_unit=Unit('km'), to_unit=Unit('mm'))
     assert not cup.can_convert(
         from_unit=Unit('km'),
         to_unit=Unit('C'))  # Don't share same ref unit
     assert not cup.can_convert(
         from_unit=Unit('bogus'),
         to_unit=Unit('m'))  # from_unit doesn't exist
Esempio n. 5
0
 def test_reverse(self):
     assert AffineUnitConversion(
         Unit('degC'), Unit('K'), 1,
         273.15).reverse().to_ref(293.15) == approx(20)
Esempio n. 6
0
 def test_from_ref(self):
     assert AffineUnitConversion(Unit('degC'), Unit('K'), 1,
                                 273.15).from_ref(0) == -273.15
Esempio n. 7
0
 def test_to_ref(self):
     assert AffineUnitConversion(Unit('degC'), Unit('K'), 1,
                                 273.15).to_ref(-273.15) == 0
Esempio n. 8
0
 def test_eq(self):
     assert AffineUnitConversion(Unit('degC'), Unit('K'), 1, 273.15) == \
            AffineUnitConversion(Unit('degC'), Unit('K'), 1, 273.15)
     assert AffineUnitConversion(Unit('degC'), Unit('K'), 1, 273.15) != \
            AffineUnitConversion(Unit('degC'), Unit('K'), 1, 666)