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 test_get_returns_single_model_object(self, sqlalchemy): service = SensorReadingPersistenceService() reading = AnalogSensorReading() service.create(reading) reading2 = AnalogSensorReading() service.create(reading2) assert service.get(reading.id).id == reading.id
def test_all_returns_all_model_objects(self, sqlalchemy): service = SensorReadingPersistenceService() reading = AnalogSensorReading() service.create(reading) reading2 = AnalogSensorReading() service.create(reading2) assert len(service.all()) == 2
def test_analog_sensor_reading(self, sqlalchemy): service = SensorReadingPersistenceService() reading = AnalogSensorReading() service.create(reading) assert service.get(reading.id).id == reading.id
def test_delete_deletes_single_model_object(self, sqlalchemy): service = SensorReadingPersistenceService() reading = AnalogSensorReading() service.create(reading) service.delete(reading) assert len(service.all()) == 0
def test_all_reversed_bad_paging(self, sqlalchemy): service = SensorReadingPersistenceService() older = AnalogSensorReading() older.timestamp = datetime.now() newer = DigitalSensorReading() newer.timestamp = datetime.now() service.create(older) service.create(newer) paged = service.all_reversed(1000, 1) assert paged.elements == [] paged = service.all_reversed(-1, 1) assert paged.elements == []
def test_polymorphic(self, sqlalchemy): service = SensorReadingPersistenceService() reading = SensorReading() analog = AnalogSensorReading() digital = DigitalSensorReading() service.create(reading) service.create(analog) service.create(digital) assert len(service.all()) == 3
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_all_reversed(self, sqlalchemy): service = SensorReadingPersistenceService() older = AnalogSensorReading() older.timestamp = datetime.now() newer = DigitalSensorReading() newer.timestamp = datetime.now() service.create(older) service.create(newer) paged = service.all_reversed(0, 100) assert paged.page_number == 0 assert paged.page_size == 100 assert paged.total_pages == 1 readings = paged.elements assert readings[0].timestamp == newer.timestamp assert readings[1].timestamp == older.timestamp assert len(readings) == 2
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