def test_get_and_send_measurement(self): timestamp_before = get_current_timestamp() self.updater.get_and_send_measurement() timestamp_after = get_current_timestamp() self.assertEqual(self.sender.value, MEASUREMENT_VALUE) self.assertGreaterEqual(self.sender.timestamp, timestamp_before) self.assertLessEqual(self.sender.timestamp, timestamp_after)
def test_get_measurements_between(self): current_timestamp = get_current_timestamp() self.db_cursor.execute('INSERT INTO Pressure VALUES (?, ?);', (current_timestamp, 735,)) measurements_list = self.pressure_storage.get_measurements_between(get_current_timestamp() - 100, get_current_timestamp()) self.assertEqual(len(measurements_list), 1) self.assertEqual(measurements_list[0].timestamp, current_timestamp) measurements_list = self.pressure_storage.get_measurements_between(self._old_time_stamp - 100, self._old_time_stamp) self.assertEqual(len(measurements_list), 1) self.assertEqual(measurements_list[0].timestamp, self._old_time_stamp)
def test_put_measurement(self): count_query = 'SELECT Count(*) FROM Pressure;' measurements_num = self.db_cursor.execute(count_query).fetchone()[0] timestamp = get_current_timestamp() new_measurement = PressureMeasurement(735, timestamp) self.pressure_storage.put_measurement(new_measurement) measurements_num_after = self.db_cursor.execute(count_query).fetchone()[0] self.assertEqual(measurements_num + 1, measurements_num_after) created_measurements_num = self.db_cursor.execute('SELECT Count(*) FROM Pressure WHERE timestamp=?;', (timestamp,)).fetchone()[0] self.assertEqual(created_measurements_num, 1)
def test_create_measurement(self): count_query = 'SELECT Count(*) FROM Pressure;' measurements_num = self.db_cursor.execute(count_query).fetchone()[0] timestamp = get_current_timestamp() self.pressure_service.create_measurement(600, timestamp) measurements_num_after = self.db_cursor.execute( count_query).fetchone()[0] self.assertEqual(measurements_num + 1, measurements_num_after) created_measurements_num = self.db_cursor.execute( 'SELECT Count(*) FROM Pressure WHERE timestamp=?;', (timestamp, )).fetchone()[0] self.assertEqual(created_measurements_num, 1)
def test_get_last_n_measurements(self): count_query = 'SELECT Count(*) FROM Pressure;' measurements_num = self.db_cursor.execute(count_query).fetchone()[0] measurements_list = self.pressure_storage.get_last_n_measurements(1) self.assertEqual(len(measurements_list), measurements_num) current_timestamp = get_current_timestamp() self.db_cursor.execute('INSERT INTO Pressure VALUES (?, ?);', (current_timestamp, 735,)) measurements_list = self.pressure_storage.get_last_n_measurements(1) self.assertEqual(len(measurements_list), 1) measurements_list = self.pressure_storage.get_last_n_measurements(2) self.assertEqual(len(measurements_list), 2) measurements_list = self.pressure_storage.get_last_n_measurements(10) self.assertEqual(len(measurements_list), measurements_num + 1) self.assertEqual(measurements_list[0].timestamp, self._old_time_stamp)
def get_and_send_measurement(self): measurement = self._getter() timestamp = get_current_timestamp() self._sender(measurement, timestamp)
def create_measurement(self, value: Union[int, float], timestamp: int = None) -> Measurement: new_measurement = self._measurement_class(value, timestamp) self._storage.put_measurement(new_measurement) return new_measurement def get_last_n_measurements(self, n: int) -> List[Measurement]: return self._storage.get_last_n_measurements(n) def get_measurements_between(self, from_date: datetime, to_date: datetime) -> List[Measurement]: return self._storage.get_measurements_between(from_date, to_date) if __name__ == "__main__": from measurement_service.storages.sqlite_cursor import get_sqlite_cursor db_cursor = get_sqlite_cursor(':memory:') db_cursor.execute('CREATE TABLE Pressure (timestamp INT, value INT);') from measurement_service.storages.SQLMeasurementStorage import SQLMeasurementStorage from measurement_service.models import Measurement, PressureMeasurement, Measurement, Measurement, Measurement pressure_storage = SQLMeasurementStorage(db_cursor, 'Pressure', PressureMeasurement) pressure_service = MeasurementService(pressure_storage, PressureMeasurement) pressure_service.create_measurement(735, get_current_timestamp()) measurements = pressure_service.get_last_n_measurements(10) print(measurements)