def test_conversion_errors(self): r""" Sends invalid unit strings to various functions """ # root_unit, prefix = SI.check_abbreviation('badunit') assert(root_unit == 'badunit') assert(prefix == '') # with pytest.raises(ValueError): SI.convert_to_si('badunit') # with pytest.raises(Exception): UnitDecomposition.parse_unit_string('celsius') # with pytest.raises(KeyError): UnitDecomposition.parse_unit_string('badunit') # with pytest.raises(AttributeError): UnitDecomposition.build_unit_list('[pascal_1]') # with pytest.raises(ValueError): UnitDecomposition.build_unit_list('[millipascal^1]') # with pytest.raises(KeyError): UnitDecomposition.build_unit_list('[badunit^1]') # with pytest.raises(ValueError): UnitConversion.convert_temperature(0.0, 'badunit', 'SI') # with pytest.raises(ValueError): UnitConversion.convert_temperature(0.0, 'SI', 'badunit') # with pytest.raises(ValueError): Distance.convert_to_si('badunit')
def test_get_conversion_factor(self): r""" Testing the get_conversion_factor function """ for unit_in, unit_out, test_fact in self._convertion_factor_test: fact = UnitConversion.get_conversion_factor(unit_in, unit_out) fact = round(fact * 1.0E9) test_fact = round(test_fact * 1.0E9) msg = 'Get conversion factor for {} to {} failed.' msg = msg.format(unit_in, unit_out) # assert fact == test_fact, msg
def test_convert_temperature(self): r""" Testing values returned by temperature conversion """ for val_in, unit_in, unit_out, val_out in self.temperature_units: val = UnitConversion.convert_value(val_in, unit_in, unit_out) # # applying a tolerance since were working with floating point values val = round(val * 1.0E9) val_out = round(val_out * 1.0E9) msg = 'Temperature conversion from '+unit_in+' to '+unit_out+' failed.' # assert val == val_out, msg
def test_convert_abbrev_units(self): r""" Testing values returned when using common unit abbrevations """ for val_in, unit_in, unit_out, val_out in self.abbrev_units: val = UnitConversion.convert_value(val_in, unit_in, unit_out) # # applying a tolerance since were working with floating point values val = round(val * 1.0E9) val_out = round(val_out * 1.0E9) msg = 'Abbreviated unit conversion from {} to {} failed.' msg = msg.format(unit_in, unit_out) # assert val == val_out, msg
def test_convert_all_units(self): r""" Testing values returned by a general test of many conversions """ for val_in, unit_in, unit_out, val_out in self.all_unit_test: val = UnitConversion.convert_value(val_in, unit_in, unit_out) # # applying a tolerance since were working with floating point values val = round(val * 1.0E9) val_out = round(val_out * 1.0E9) msg = 'Mutlitple unit conversion from {} to {} failed.' msg = msg.format(unit_in, unit_out) # assert val == val_out, msg
def test_convert_formatted_units(self): r""" Testing values returned by formatted unit strings """ for val_in, unit_in, unit_out, val_out in self.formatted_units: val = UnitConversion.convert_value(val_in, unit_in, unit_out) # # applying a tolerance since were working with floating point values val = round(val * 1.0E9) val_out = round(val_out * 1.0E9) msg = 'Formatted unit conversion from {} to {} failed.' msg = msg.format(unit_in, unit_out) # assert val == val_out, msg # # manually sending a formatted unit list with a prefix unit_list = UnitDecomposition.build_unit_list('[millimeter^1]') factor = process_unit_list(unit_list) assert factor == 0.001