def test_get_newest_notification_returns_newest_record(self, sqlalchemy): sensor_service = SensorReadingPersistenceService() reading = AnalogSensorReading() reading.name = 'Test Sensor' sensor_service.create(reading) old_notification = NotificationEvent() old_notification.sensor_id = reading.id old_notification.timestamp = datetime(2015, 1, 1) new_notification = NotificationEvent() new_notification.sensor_id = reading.id new_notification.timestamp = datetime(2016, 1, 1) service = NotificationPersistenceService() # Insert in reverse order to confirm order by service.create(new_notification) service.create(old_notification) newest_timestamp = new_notification.timestamp db_timestamp = service.get_newest_notification(reading.name).timestamp assert db_timestamp == newest_timestamp
def _read_analog_sensor(self, analog_sensor): value = analog_sensor.read_analog_sensor() analog_reading = AnalogSensorReading() analog_reading.name = analog_sensor.sensor_name analog_reading.value = value analog_reading.unit = analog_sensor.unit analog_reading.timestamp = datetime.now() self.sensor_persistence.create(analog_reading) if value != analog_sensor.error_sentinel: if value < analog_sensor.min_normal or \ value > analog_sensor.max_normal: logger.info(analog_reading.name + ' Analog Sensor Value: ' + str(analog_reading.value) + analog_reading.unit + ' - Out of Normal Range') self._send_notification(analog_sensor, analog_reading) else: logger.info(analog_reading.name + ' Analog Sensor Value: ' + str(analog_reading.value) + analog_reading.unit + ' - Within Normal Range') else: logger.info(analog_reading.name + ' Analog Sensor Value: ' + str(analog_reading.value) + analog_reading.unit + ' - Error Sentinel - Ignoring')
def test_update_updates_single_model_object(self, sqlalchemy): service = SensorReadingPersistenceService() original_reading = AnalogSensorReading() original_reading.name = 'original' original_reading.value = 1 original_reading.unit = 'original' service.create(original_reading) persisted_reading = service.get(original_reading.id) persisted_reading.name = 'new' persisted_reading.value = 2 persisted_reading.unit = 'new' service.update(persisted_reading) updated_reading = service.get(original_reading.id) assert updated_reading.name == 'new' assert updated_reading.value == 2 assert updated_reading.unit == 'new'
def test_index_analog_sensor_reading(self, sqlalchemy): service = SensorReadingPersistenceService() timestamp = datetime.now() reading = AnalogSensorReading() reading.name = "testsensor" reading.unit = "testunit" reading.value = 0 reading.timestamp = timestamp service.create(reading) client = web.app.test_client() rv = client.get("/") assert b"testsensor" in rv.data assert b"Analog" in rv.data assert b"0 (testunit)" in rv.data assert str.encode(str(timestamp)) in rv.data assert b"newer" not in rv.data assert b"older" not in rv.data
def test_index_analog_sensor_reading(self, sqlalchemy): service = SensorReadingPersistenceService() timestamp = datetime.now() reading = AnalogSensorReading() reading.name = 'testsensor' reading.unit = 'testunit' reading.value = 0 reading.timestamp = timestamp service.create(reading) client = web.app.test_client() rv = client.get('/') assert b'testsensor' in rv.data assert b'Analog' in rv.data assert b'0 (testunit)' in rv.data assert str.encode(str(timestamp)) in rv.data assert b'newer' not in rv.data assert b'older' not in rv.data