def init_measures(): sensor_ids, space_ids, floor_ids, building_ids, site_ids = init_sensors() measures = [ Measure(str(sensor_ids[idx]), 'DegreeCelsius', 'Air', 'Temperature', description='A sample measure #{}'.format(idx+1), associated_locations=[space_ids[0]]) for idx, _ in enumerate(_get_measure_ids())] measure_ids = [MeasureDB().create(measure) for measure in measures] return ( measure_ids, sensor_ids, space_ids, floor_ids, building_ids, site_ids,)
def test_db_measure_create(self, init_sensors): sensor_ids, space_ids, _, _, _ = init_sensors sensor_id = sensor_ids[0] measure_db = MeasureDB() # check that database is empty result = measure_db.get_all() assert list(result) == [] # create an item measure = Measure(sensor_id, 'DegreeCelsius', 'Air', 'Temperature', description='A sample measure', outdoor=True, set_point=True, ambient=True, associated_locations=space_ids) new_measure_id = measure_db.create(measure) assert new_measure_id is not None assert new_measure_id == measure.id # check that database is not empty now result = measure_db.get_all() measures = list(result) assert len(measures) == 1 assert measures[0].id == measure.id assert measures[0].description == measure.description assert measures[0].medium == measure.medium assert measures[0].unit == measure.unit == 'DegreeCelsius' assert measures[0].observation_type == measure.observation_type assert measures[0].method == 'Frequency' assert not measures[0].on_index assert measures[0].set_point assert measures[0].outdoor assert measures[0].ambient assert measures[0].sensor_id == sensor_id assert {loc.type for loc in measures[0].associated_locations} ==\ {'space', 'floor', 'building', 'site'} assert set(space_ids) ==\ {loc.id for loc in measures[0].associated_locations if loc.type == 'space'} for loc_type in ['floor', 'building', 'site']: assert len([ loc.id for loc in measures[0].associated_locations if loc.type == loc_type ]) == 1 # check the link from sensor to measure is created sensor_db = SensorDB() sensor = sensor_db.get_by_id(sensor_id) assert new_measure_id in sensor.measures
def test_db_measure_filter(self, init_spaces, init_sensors, get_created_building_ids): space_ids = init_spaces sensor_ids = init_sensors building_id = get_created_building_ids[0] measure_db = MeasureDB() # check that database is empty result = measure_db.get_all(building_id=building_id) assert list(result) == [] result = measure_db.get_all(space_id=space_ids[0]) assert list(result) == [] # create an item measure_db.create( Measure(sensor_ids[0], 'DegreeCelsius', 'Air', 'Temperature', associated_locations=[str(space_ids[0])], description='New sample measure')) measure_db.create( Measure( sensor_ids[1], 'DegreeCelsius', 'Air', 'Temperature', associated_locations=[str(space_ids[1]), str(space_ids[0])], description='New sample measure')) measures = list(measure_db.get_all(location_id=space_ids[0])) assert len(measures) == 2 result = measure_db.get_all(location_id=space_ids[1]) assert len(list(result)) == 1 space_db = SpaceDB() # common site for the two spaces result = measure_db.get_all( location_id=space_db.get_parent(str(space_ids[0]))) assert len(list(result)) == 2
def _create_measures(device_ids, space_ids): measures_data = [ {'description': 'measure version A', 'unit': 'DegreeCelsius', 'observation_type': 'Temperature', 'medium': 'Air', 'sensor_id': device_ids[0]}, {'description': 'measure version B', 'unit': 'DegreeFahrenheit', 'observation_type': 'Temperature', 'medium': 'Water', 'sensor_id': device_ids[0], 'associated_locations': [space_ids[0]]}, {'description': 'measure version C', 'unit': 'DegreeCelsius', 'observation_type': 'Temperature', 'medium': 'Water', 'sensor_id': device_ids[2]}, {'description': 'measure version D', 'unit': 'DegreeFahrenheit', 'observation_type': 'Temperature', 'medium': 'Air', 'sensor_id': device_ids[3]}, ] measures = [Measure(**data) for data in measures_data] for idx, measure in enumerate(measures): measure.add_external_id('Test_{}'.format(idx)) return [db_accessor.create(m) for m in measures]