def test_no_sensor_found(sensors): """Test exception when no sensor was found""" with pytest.raises( NoSensorFoundError, message="No Unknown temperature sensor with id '' found" ): W1ThermSensor() with pytest.raises( NoSensorFoundError, message="No DS1822 temperature sensor with id '1' found" ): W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, "1")
def test_no_sensor_found(sensors): """Test exception when no sensor was found""" with pytest.raises(NoSensorFoundError) as exc: W1ThermSensor() assert str(exc.value) == "No Unknown temperature sensor with id '' found" with pytest.raises(NoSensorFoundError) as exc: W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, '1') assert str(exc.value) == "No DS1822 temperature sensor with id '1' found"
def test_no_sensor_found(sensors, monkeypatch): """Test exception when no sensor was found""" monkeypatch.setattr(time, "sleep", lambda x: True) with pytest.raises(NoSensorFoundError, match="Could not find any sensor"): W1ThermSensor() with pytest.raises( NoSensorFoundError, match="Could not find sensor of type DS1822 with id 1" ): W1ThermSensor(Sensor.DS1822, "1")
def test_init_sensor_by_type_and_id(sensors, sensor_specs): """Test initialize sensor by given type and id""" # when sensor = W1ThermSensor(**sensor_specs) # then assert sensor.id == sensor_specs['sensor_id'] assert sensor.type == sensor_specs['sensor_type']
def test_init_first_sensor_of_type_if_not_existent(sensors): # then with pytest.raises( NoSensorFoundError, match="Could not find any sensor of type DS18B20" ): # when W1ThermSensor(Sensor.DS18B20)
def test_unsupported_unit_error(self): unsupported_unit = 0xFF sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS1822) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, sensor_id) sensor.get_temperature.when.called_with(unsupported_unit).should.throw( UnsupportedUnitError, "Only Degress C, F and Kelvin are currently supported")
def test_sensor_not_ready_error(): sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS1822, w1_file=W1_FILE_NOT_READY) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, sensor_id) sensor.get_temperature.when.called_with( W1ThermSensor.DEGREES_C).should.throw( SensorNotReadyError, "Sensor is not yet ready to read temperature")
def test_repr(self): sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) s1 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) s2 = eval(repr(s1)) s1.id.should.be.equal(s2.id) s1.type.should.be.equal(s2.type)
def test_str(self): sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) s1 = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) str(s1).should.be.equal( "W1ThermSensor(name='DS18B20', type=40(0x28), id='%s')" % sensor_id)
def test_get_temperature_in_multiple_units(sensors, units, expected_temperatures): """Test getting a sensor temperature in multiple units""" # given sensor = W1ThermSensor() # when temperatures = sensor.get_temperatures(units) # then assert temperatures == pytest.approx(expected_temperatures)
def test_init_specific_sensor(self): # create DS18B20 sensor sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.should.be.a(W1ThermSensor) sensor.type.should.be.equal(W1ThermSensor.THERM_SENSOR_DS18B20) sensor.id.should.be.equal(sensor_id)
def test_sensor_temperature_in_C(self): # create DS18B20 sensor with 20 C degrees sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20, temperature=20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.id.should.be.equal(sensor_id) sensor.get_temperature(W1ThermSensor.DEGREES_C).should.be.equal(20.0) # create DS18B20 sensor with 26.55 C degrees sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20, temperature=26.55) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.id.should.be.equal(sensor_id) sensor.get_temperature(W1ThermSensor.DEGREES_C).should.be.equal( 26.55, epsilon=FLOAT_EPSILON)
def test_get_precision(sensors, expected_precision): """Test getting the sensor precison""" # given sensor = W1ThermSensor() # when precision = sensor.get_precision() # then assert precision == pytest.approx(expected_precision)
def test_sensor_type_name(sensors, expected_sensor_name): """Test getting the sensor type name""" # given sensor = W1ThermSensor() # when sensor_name = sensor.type_name # then assert sensor_name == expected_sensor_name
def test_get_temperature_for_different_units(sensors, unit, expected_temperature): """Test getting a sensor temperature for different units""" # given sensor = W1ThermSensor() # when temperature = sensor.get_temperature(unit) # then assert temperature == pytest.approx(expected_temperature)
def test_str_protocol(sensors): """Test __str__ protocol of a sensor instance""" # given sensor = W1ThermSensor() # when stringyfied = str(sensor) # then assert stringyfied == "W1ThermSensor(name='DS18B20', type=40(0x28), id='1')"
def test_unsupported_unit_error(sensors): """Test exception when requested to read temperature in unsupported unit""" # given sensor = W1ThermSensor() expected_error_msg = "Only Degrees C, F and Kelvin are currently supported" # when & then with pytest.raises(UnsupportedUnitError, message=expected_error_msg): sensor.get_temperature(unit=0xFF) # 0xFF is no valid unit id
def test_init_first_sensor_of_type(sensors, sensor_type): """Test that first found sensor of specific type is initialized if not sensor specs given""" # given sensor_id = sensors[0]['id'] # when sensor = W1ThermSensor(sensor_type) # then assert sensor.id == sensor_id assert sensor.type == sensor_type
def test_init_first_sensor_by_id(sensors, sensor_id): """Test that sensor can be initialized by id""" # given sensor_type = sensors[0]['type'] # when sensor = W1ThermSensor(sensor_id=sensor_id) # then assert sensor.id == sensor_id assert sensor.type == sensor_type
def test_repr_protocol(sensors): """Test the __repr__ protocol of a sensor instance""" # given sensor = W1ThermSensor() # when sensor_copy = eval(repr(sensor)) # then assert sensor.id == sensor_copy.id assert sensor.type == sensor_copy.type
def available(self): try: wts = W1ThermSensor(sensor_type=self.w1_type, sensor_id=self.w1_id) except NoSensorFoundError: return False except KernelModuleLoadError: return False else: return True
def test_setting_invalid_resolution(sensors, resolution): """Test setting invalid resolution for sensor""" # given sensor = W1ThermSensor() expected_error_msg = "The given sensor resolution '{}' is out of range (9-12)".format( resolution) # when & then with pytest.raises(ValueError, message=expected_error_msg): sensor.set_resolution(resolution)
def test_sensor_partially_disconnected(sensors): """Test handling the partially disconnected sensor with wrong value""" # given sensor = W1ThermSensor() expected_error_msg = "Sensor {} is not yet ready to read temperature".format( sensor.id) # when & then with pytest.raises(SensorNotReadyError, message=expected_error_msg): sensor.get_temperature()
def test_sensor_all_temperature_units(): # create DS18B20 sensor with 20 C degrees sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20, temperature=20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.id.should.be.equal(sensor_id) sensor.get_temperatures([ W1ThermSensor.DEGREES_C, W1ThermSensor.DEGREES_F, W1ThermSensor.KELVIN ]).should.be.equal([20, 68, 293.15])
def test_sensor_type_name(): # create sensors of all types ds18s20_sensor_id = create_w1_therm_sensor( W1ThermSensor.THERM_SENSOR_DS18S20) ds1822_sensor_id = create_w1_therm_sensor( W1ThermSensor.THERM_SENSOR_DS1822) ds18b20_sensor_id = create_w1_therm_sensor( W1ThermSensor.THERM_SENSOR_DS18B20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18S20, ds18s20_sensor_id) sensor.type_name.should.be.equal("DS18S20") sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS1822, ds1822_sensor_id) sensor.type_name.should.be.equal("DS1822") sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, ds18b20_sensor_id) sensor.type_name.should.be.equal("DS18B20")
def test_init_first_sensor(sensors): """Test that first found sensor is initialized if no sensor specs given""" # given sensor_id = sensors[0]['id'] sensor_type = sensors[0]['type'] # when sensor = W1ThermSensor() # then assert sensor.id == sensor_id assert sensor.type == sensor_type
def test_sensor_not_ready(sensors): """Test exception when sensor is not ready yet""" # given sensor = W1ThermSensor() expected_error_msg = "Sensor {} is not yet ready to read temperature".format( sensor.id) # when & then with pytest.raises(SensorNotReadyError, message=expected_error_msg): sensor.get_temperature()
def test_sensor_not_ready(sensors): """Test exception when sensor is not ready yet""" # given sensor = W1ThermSensor() # when with pytest.raises(SensorNotReadyError) as exc: sensor.get_temperature() # then assert str(exc.value) == 'Sensor is not yet ready to read temperature'
def test_sensor_temperature_in_K(self): # 20 C = 293.15 K # 26.55 C = 299.7 K # create DS18B20 sensor with 20 C degrees sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20, temperature=20) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.id.should.be.equal(sensor_id) sensor.get_temperature(W1ThermSensor.KELVIN).should.be.equal(293.15) # create DS18B20 sensor with 26.55 C degrees sensor_id = create_w1_therm_sensor(W1ThermSensor.THERM_SENSOR_DS18B20, temperature=26.55) sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, sensor_id) sensor.id.should.be.equal(sensor_id) sensor.get_temperature(W1ThermSensor.KELVIN).should.be.equal( 299.7, epsilon=FLOAT_EPSILON)
def test_unsupported_unit_error(sensors): """Test exception when requested to read temperature in unsupported unit""" # given sensor = W1ThermSensor() # when with pytest.raises(UnsupportedUnitError) as exc: sensor.get_temperature(unit=0xFF) # 0xFF is no valid unit id # then assert str( exc.value) == 'Only Degrees C, F and Kelvin are currently supported'