def test_db_space_get_empty(self): space_db = SpaceDB() # get all items result = space_db.get_all() assert list(result) == [] # try to get an inexistant item with pytest.raises(ItemNotFoundError): space_db.get_by_id('not_existing')
def init_spaces(): floor_ids, building_ids, site_ids = init_floors() space_occ = SpaceOccupancy(9, 20) spaces = [ Space('Space #{}'.format(idx+1), floor_ids[0], 'OpenSpace', space_occ, id=space_id) for idx, space_id in enumerate(_get_space_ids())] space_ids = [SpaceDB().create(space) for space in spaces] return space_ids, floor_ids, building_ids, site_ids
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 test_db_space_create(self, init_floors): floor_ids = init_floors space_db = SpaceDB() # check that database is empty result = space_db.get_all() assert list(result) == [] # create an item space_occ = SpaceOccupancy(9, 20) space = Space('Space #0', floor_ids[0], 'OpenSpace', space_occ) new_space_id = space_db.create(space) assert new_space_id is not None assert new_space_id == space.id # check that database is not empty now result = space_db.get_all() spaces = list(result) assert len(spaces) == 1 assert spaces[0].id == space.id assert spaces[0].name == space.name assert spaces[0].description == space.description assert spaces[0].kind == space.kind assert spaces[0].occupancy.nb_max == space_occ.nb_max assert spaces[0].occupancy.nb_permanents == space_occ.nb_permanents assert spaces[0].floor_id == space.floor_id == floor_ids[0] #ensure we can access the parent site sites = SiteDB().get_all() assert space_db.get_parent( space.id) in [str(site.id) for site in sites]
def test_db_space_update(self, init_spaces): space_ids = init_spaces space_db = SpaceDB() # get all items result = space_db.get_all() spaces = list(result) assert len(spaces) == 2 for cur_space in spaces: assert cur_space.id in space_ids # get an item by its ID space = space_db.get_by_id(spaces[0].id) # update item data new_description = 'updated by patator' new_occ_max = 12 space.description = new_description space.occupancy.nb_max = new_occ_max space_db.update(space.id, space) # check that item has really been updated in database updated_space = space_db.get_by_id(space.id) assert updated_space.id == space.id assert updated_space.name == space.name assert updated_space.description == new_description assert updated_space.occupancy.nb_max == new_occ_max assert updated_space.kind == space.kind assert updated_space.floor_id == space.floor_id # delete an item by its ID space_db.remove(space.id) # get an item by its ID with pytest.raises(ItemNotFoundError): # it has been removed... space_db.get_by_id(space.id)