def test_init_WhenCalledWithIDAndRemoteHostAddress_SetsExpectedPropertiesOnCreatedClass( self): cfg = self.testConfigRemoteSensor1 ts = sensors.temperaturesensor('test1', cfg) self.assertEqual('test1', ts.id) self.assertEqual(cfg['name'], ts.name) self.assertEqual(cfg['host'], ts.host) #self.assertEqual('http://' + cfg['host'] + ':5000/temp/' + cfg['id'], ts.address) self.assertEqual(False, ts.islocal)
def test_get_WhenCalledWithMockedRedisForRemoteSensorValue_AttemptsToGetValueFromRedisAndReturns20( self): with mock.patch('storage.redisclient.getasstring', return_value=20) as mocked_redis: cfg = self.testConfigRemoteSensor1 ts = sensors.temperaturesensor('test1', cfg) temp = ts.get() mocked_redis.assert_called_with('test1') self.assertEqual(20, temp)
def reload(self): cfg = config().get() # print(cfg['tempsensors']) for ts in cfg['tempsensors']: self.sensors[ts] = sensors.temperaturesensor( ts, cfg['tempsensors'][ts]) for ct in cfg['controls']: self.controls[ct] = control(ct, cfg['controls'][ct])
def test_update_WhenCalledWithMockedRedis_AttemptsToReadSensorFileAndToSetValueInRedisAndReturnsSetValue( self): with mock.patch( 'builtins.open', new_callable=mock_open, read_data= '00 01 4b 46 7f ff 0c 10 f5 : crc=f5 YES\n00 01 4b 46 7f ff 0c 10 f5 t=20000' ) as mocked_file, mock.patch( 'storage.redisclient.set') as mocked_redis: cfg = self.testConfigLocalSensor1 ts = sensors.temperaturesensor('test1', cfg) temp = ts.update() mocked_redis.assert_called_with('test1', 20) self.assertEqual(20, temp)
def test_update_WhenCalledAgainstLocalSensorWithMockedIO_AttemptsToReadSensorFileAndToSetValueOf120InRedisAndReturnsSetValue( self): with mock.patch( 'builtins.open', new_callable=mock_open, read_data= '00 01 4b 46 7f ff 0c 10 f5 : crc=f5 YES\n00 01 4b 46 7f ff 0c 10 f5 t=120000' ) as mocked_file, mock.patch( 'storage.redisclient.set') as mocked_redis: cfg = self.testConfigLocalSensor1 ts = sensors.temperaturesensor('test1', cfg) temp = ts.update() mocked_redis.assert_called_with('test1', 120) mocked_file.assert_called_with('/sys/bus/w1/devices/' + cfg['hardwareid'] + '/w1_slave') self.assertEqual(120, temp)
def test_update_WhenCalledAgainstNonExistentFile_ThrowsFileNotFoundError( self): with mock.patch('storage.redisclient.set') as mocked_redis: cfg = self.testConfigLocalSensor1 ts = sensors.temperaturesensor('test1', cfg) try: ts.update() self.assertFalse( 'No exception thrown when FileNotFound error was expected') except FileNotFoundError: self.assertTrue('OK') except Exception as ex: self.assertFalse( 'Unexpected \'' + type(ex).__name__ + '\' exception thrown when FileNotFound error was expected')