def setUp(self): self._read_patcher = mock.patch('temp_serve.sensor.Sensor._read') self.mock_read = self._read_patcher.start() self._setup_patcher = mock.patch('temp_serve.sensor.Sensor._setup') self.mock_setup = self._setup_patcher.start() self.sensor = Sensor()
def setUp(self): self.MINIMUM_UPDATE_TIME_patcher = mock.patch( 'temp_serve.sensor.MINIMUM_UPDATE_TIME', 30) self.MINIMUM_UPDATE_TIME_patcher.start() self.open_patcher = mock.patch('__builtin__.open') self.mock_open = self.open_patcher.start() self.mock_open.return_value.__enter__.return_value.read.return_value = ( 'asdfasdasdf\n0 1 2 3 4 5 6 7 8 t=50000') self.listdir_patcher = mock.patch('temp_serve.sensor.os.listdir') self.mock_listdir = self.listdir_patcher.start() self.mock_listdir.return_value = ['test_bus'] self.sensor = Sensor() self.sensor._last_update = datetime.now() self.sensor._cached_temp = 'test_cached_temp'
def test_sensor_not_found(self): sensor = Sensor() self.assertRaises(SensorException, sensor._setup)
def test_bus_set(self): self.mock_listdir.return_value = ['w1_bus_master1', 'test_foo'] sensor = Sensor() self.assertEqual(sensor._bus, 'test_foo')
def test_already_initialized(self): sensor = Sensor() sensor._bus = 'valid' self.assertRaises(SensorException, sensor._setup) self.assertEqual(sensor._bus, 'valid')
def test_setup_raises(self): self.mock_setup.side_effect = SensorException('FAIL') Sensor()
def test_init(self): Sensor() self.mock_setup.assert_called_once_with()
import threading import time from lcd import LCD from flask import ( Flask, jsonify, ) from werkzeug.contrib.fixers import ProxyFix from temp_serve.sensor import Sensor app = Flask(__name__, static_url_path='') app.wsgi_app = ProxyFix(app.wsgi_app) sensor = Sensor() lcd = LCD() @app.route('/status/', methods=['GET']) @app.route('/status', methods=['GET']) def status(): res = {'status': sensor.is_initialized()} return jsonify(res) @app.route('/', methods=['GET']) def get_temp(): res = { 'celsius': sensor.get_celsius(), 'fahrenheit': sensor.get_fahrenheit(), }