def test_5_conversions(self): """Test if setting various attributes works appropriately.""" for celsius, fahrenheit, kelvin in zip(DEGREES_celsius, DEGREES_FAHRENHEIT, KELVINS): temperature = Temperature() temperature.celsius = celsius self.assertTrue( equal_to_n_decimal_places(temperature.fahrenheit, fahrenheit, 4)) self.assertTrue( equal_to_n_decimal_places(temperature.kelvin, kelvin, 4)) temperature.fahrenheit = fahrenheit self.assertTrue( equal_to_n_decimal_places(temperature.celsius, celsius, 4)) self.assertTrue( equal_to_n_decimal_places(temperature.kelvin, kelvin, 4)) temperature.kelvin = kelvin self.assertTrue( equal_to_n_decimal_places(temperature.celsius, celsius, 4)) self.assertTrue( equal_to_n_decimal_places(temperature.fahrenheit, fahrenheit, 4))
# All should raise TemperatureErrors print('Trying to initialize with bad values') for value in INVALID_TEMPERATURES: try: Temperature(value) except TemperatureError: print('Correct error raised for', value) except Exception as e: print(e, 'raised instead of TemperatureError for', value) print() print('Testing the setters') print('Each of these values should be 0.0:') try: a = Temperature() a.kelvin = 273.15 print(a.celsius) a.kelvin = '273.15K' print(a.celsius) a.kelvin = '273.15' print(a.celsius) a.fahrenheit = 32.0 print(a.celsius) a.fahrenheit = '32F' print(a.celsius) a.fahrenheit = '32' print(a.celsius) a.celsius = '0C' print(a.celsius) a.celsius = 0 print(a.celsius)