def test_filestorage_manager_list( self, tmpdir, ifc_file_data_stream, ifc_zip_file_data): """Tests on file storage manager list function.""" fs_mgr = FileStorageMgr(str(tmpdir)) file_id_1 = uuid_gen() file_name_1, data_stream_1 = ifc_file_data_stream file_id_2 = uuid_gen() file_name_2, data_stream_2 = ifc_zip_file_data # create entries fs_entry_1 = fs_mgr.add(file_id_1, file_name_1, data_stream_1) fs_entry_2 = fs_mgr.add(file_id_2, file_name_2, data_stream_2) fs_entry_2.extract() # list all files (even exrtacted from compressed archives) file_paths = fs_mgr.list_file_paths(include_extracted=True) assert len(file_paths) == 3 assert fs_entry_1.file_path in file_paths assert fs_entry_2.file_path in file_paths for extracted_file_path_2 in fs_entry_2.extracted_file_paths: assert extracted_file_path_2 in file_paths # list all 'root' files (not including the extracted from archives) file_paths = fs_mgr.list_file_paths() assert len(file_paths) == 2 assert fs_entry_1.file_path in file_paths assert fs_entry_2.file_path in file_paths
def test_views_buildings_post(self, init_db_data): """Test post api endpoint""" # retrieve database informations site_id = str(init_db_data['sites'][0]) # Get building list: no buildings response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post a new building response = self.post_item(name='Cartman\'s house', area=9999, kind='House', site_id=site_id) assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'Cartman\'s house' assert response.json['area'] == 9999 assert response.json['kind'] == 'House' assert response.json['site_id'] == site_id # Get building list: 1 building found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 1 # Errors: # area not a number (422) response = self.post_item(name='area_not_a_number', area='not_a_number', kind='Cinema', site_id=str(uuid_gen())) assert response.status_code == 422 # missing area (422) response = self.post_item(name='missing_area', site_id=str(uuid_gen())) assert response.status_code == 422 # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', area=0, kind='Cinema', site_id=site_id) #site_id=str(uuid_gen())) assert response.status_code == 201 assert response.json['id'] != new_id # Post new item with non-existing site id -> 422, ref not found response = self.post_item(name='test', area=69, kind='Cinema', site_id=str(uuid_gen())) assert response.status_code == 422 assert response.json['errors'] == {'site_id': ['Reference not found']}
def test_views_zones_post(self, init_db_data): """Test post api endpoint""" # retrieve database informations building_id = str(init_db_data['buildings'][0]) # Get list: no items response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post new item with non-existing space id -> 422, ref not found z_spaces = [str(uuid_gen())] response = self.post_item(name='Zone 51', spaces=z_spaces, building_id=building_id) assert response.status_code == 422 assert response.json['errors'] == { 'spaces': { '0': ['Reference not found'] } } # Post a new item - with an existing space ID z_spaces = [str(init_db_data['spaces'][0])] response = self.post_item(name='Zone 51', spaces=z_spaces, building_id=building_id) assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'Zone 51' assert response.json['zones'] == [] assert response.json['spaces'] == z_spaces assert 'description' not in response.json # Get list: 1 found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 1 # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', spaces=z_spaces, building_id=building_id) assert response.status_code == 201 assert response.json['id'] != new_id
def test_views_ifc_download(self, init_db_data, ifc_file_data): """Test download api endpoint""" # retrieve database informations ifc_file_id = next(iter(init_db_data['ifc_files'])) # retrieve ifc file informations _, file_content = ifc_file_data # Download file response = self.get_subitem_by_id( item_id=str(ifc_file_id), subitem_params={ 'download': { 'item_id': init_db_data['ifc_files'][ifc_file_id] } }) assert response.status_code == 200 assert response.data == bytes(file_content, 'utf-8') # Errors: # file id not found (404) response = self.get_subitem_by_id( item_id=str(uuid_gen()), subitem_params={'download': { 'item_id': 'not_found' }}) assert response.status_code == 404 # file name not corresponding: not found (404) response = self.get_subitem_by_id( item_id=str(ifc_file_id), subitem_params={'download': { 'item_id': 'not_expected_file_name' }}) assert response.status_code == 404
def test_views_windows_post(self, init_db_data): """Test post api endpoint""" # retrieve database informations facade_id = str(init_db_data['facades'][0]) # Get list: none yet response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post a new item response = self.post_item(name='Open window', covering='Curtain', facade_id=facade_id, surface_info={'area': 2.2}, orientation='South_West', u_value=2.12) assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'Open window' assert response.json['covering'] == 'Curtain' assert response.json['surface_info']['area'] == 2.2 assert response.json['orientation'] == 'South_West' assert response.json['facade_id'] == facade_id assert response.json['u_value'] == 2.12 # Get list: 1 found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 1 # Errors: # wrong covering (422) response = self.post_item(name='wrong_cov_choice', covering='wrong', facade_id=facade_id, surface_info={'area': 2.2}, orientation='South_West') assert response.status_code == 422 # wrong orientation response = self.post_item(name='wrong_orientation', covering='Curtain', facade_id=facade_id, surface_info={'area': 2.2}, orientation='Nowhere') assert response.status_code == 422 # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', covering='Curtain', surface_info={'area': 2.2}, orientation='South_West', facade_id=facade_id) assert response.status_code == 201 assert response.json['id'] != new_id
def test_views_slabs_post(self, init_db_data): """Test post api endpoint""" # retrieve database informations building_id = str(init_db_data['buildings'][0]) # Get list: no items response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post a new item f_floors = [ str(init_db_data['floors'][0]), str(init_db_data['floors'][1]) ] response = self.post_item(name='Magical slab', floors=f_floors, surface_info={'area': 42}, description='It can disappear...', building_id=building_id) assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'Magical slab' assert response.json['floors'] == f_floors assert response.json['surface_info']['area'] == 42 assert response.json['description'] == 'It can disappear...' assert 'kind' not in response.json assert response.json['building_id'] == building_id assert response.json['windows'] == [] # Get list: 1 found slab_json = self.get_items() assert slab_json.status_code == 200 assert len(slab_json.json) == 1 # Errors: # wrong spatial info (422) response = self.post_item(name='wrong_spatial', surface_info='wrong', floors=[str(init_db_data['floors'][2])], building_id=str( init_db_data['buildings'][3])) assert response.status_code == 422 # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', floors=[str(init_db_data['floors'][0])], surface_info={'area': 0}, building_id=str( init_db_data['buildings'][0])) assert response.status_code == 201 assert response.json['id'] != new_id
def test_views_sensors_post(self, init_db_data): """Test post api endpoint""" # Get sensor list: no Sensors response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 #Post sensor 1 response = self.post_item( name='New sensor', description='Mock sensor #1', static=False, localization={'building_id': str(init_db_data['buildings'][0])} #location={'identifier':building_id, 'type':'building'}) ) assert response.status_code == 201 assert response.json['name'] == 'New sensor' assert response.json['description'] == 'Mock sensor #1' assert response.json['static'] is False assert response.json['localization']['building_id'] == \ str(init_db_data['buildings'][0]) #Post sensor 2 response = self.post_item( name='New sensor', description='Mock sensor #2', localization={'building_id': str(init_db_data['buildings'][0])}) assert response.status_code == 201 assert response.json['name'] == 'New sensor' assert response.json['description'] == 'Mock sensor #2' assert response.json['static'] is True assert response.json['localization']['building_id'] == \ str(init_db_data['buildings'][0]) #Get sensor list: four sensor found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 2 # Errors: # no system, no localization (422) #TOFIX capture exception and send back HHTP error # response = self.post_item(name='New sensor', description='Mock sensor #3') # assert response.status_code == 422 #Remarks: #id is 'read only' new_id = str(uuid_gen()) response = self.post_item( id=new_id, name='id_is_read_only', system_kind='electrical_communication_appliance_network', localization={'building_id': str(init_db_data['buildings'][0])}) assert response.status_code == 201 assert response.json['id'] != new_id
def test_views_sites_post(self): """Test post api endpoint""" # Get list: no items response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post a new item response = self.post_item(name='New site', geographic_info={ 'latitude': 6, 'longitude': 66 }) assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'New site' assert response.json['geographic_info']['latitude'] == 6 assert response.json['geographic_info']['longitude'] == 66 assert 'description' not in response.json # Get list: 1 item found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 1 # Errors: # missing name (422) response = self.post_item(geographic_info={ 'latitude': 6, 'longitude': 66 }) assert response.status_code == 422 # missing geographical data (422) response = self.post_item(name='missing_geo_data') assert response.status_code == 422 # wrong geographical data (422) response = self.post_item(name='test_error', geographic_info='wrong') assert response.status_code == 422 # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', geographic_info={ 'latitude': 6, 'longitude': 66 }) assert response.status_code == 201 assert response.json['id'] != new_id
def test_database_mock_find(self): """Test get_by_id function""" # find building assert len(self.db_items) > 0 item = next(iter(self.db_items.values())) item_found = self.db.get_by_id(self.db_item_class, item.id) assert item_found == item assert item_found.id == item.id assert item_found.name == item.name # not found error with pytest.raises(ItemNotFoundError): self.db.get_by_id(self.db_item_class, str(uuid_gen()))
def test_views_ifc_import(self, init_db_data): """Test import api endpoint""" # retrieve database informations ifc_file_id = next(iter(init_db_data['ifc_files'])) # Import file response = self.post_subitem(item_id=str(ifc_file_id), subitem_params={'import': None}) assert response.status_code == 201 assert response.json['success'] # Errors: # file id not found (404) response = self.post_subitem(item_id=str(uuid_gen()), subitem_params={'import': None}) assert response.status_code == 404
def test_filestorage_manager_entry_extract( self, tmpdir, ifc_multi_zip_file_data): """Tests on file storage manager extract function.""" fs_mgr = FileStorageMgr(str(tmpdir)) file_id = uuid_gen() file_name, data_stream = ifc_multi_zip_file_data # save the entry fs_entry = fs_mgr.add(file_id, file_name, data_stream) assert not fs_entry.is_empty() assert fs_entry.is_compressed() # archive is not extracted yet assert len(fs_entry.extracted_file_paths) == 0 # extract archive extracted_file_paths = fs_entry.extract() assert len(extracted_file_paths) == 2 for extracted_file_path in extracted_file_paths: assert extracted_file_path.parent == ( tmpdir / str(file_id) / fs_entry.EXTRACTED_DIR) # archive is now extracted assert len(fs_entry.extracted_file_paths) == 2 # extract archive again extracted_file_paths = fs_entry.extract() assert len(extracted_file_paths) == 2 for extracted_file_path in extracted_file_paths: assert extracted_file_path.parent == ( tmpdir / str(file_id) / fs_entry.EXTRACTED_DIR) # list entry files entry_file_paths = fs_entry.list_file_paths() assert len(entry_file_paths) == 1 assert entry_file_paths[0] == fs_entry.file_path entry_file_paths = fs_entry.list_file_paths(include_extracted=True) assert len(entry_file_paths) == 3 # delete fs_entry.delete() # file storage entry has been removed with pytest.raises(FileNotFoundError): fs_mgr.get(file_id)
def test_database_mock_get_one(self): """Test get_one function""" # find one item assert len(self.db_items) > 0 item = next(iter(self.db_items.values())) item_found = self.db.get_one(self.db_item_class, sieve={'id': item.id}) assert item_found == item assert item_found.id == item.id assert item_found.name == item.name # not found error # id do not exist with pytest.raises(ItemNotFoundError): self.db.get_one(self.db_item_class, sieve={'id': uuid_gen()}) # multiple area result with pytest.raises(ItemNotFoundError): self.db.get_one(self.db_item_class, sieve={'area': 666})
def test_views_facades_get_by_id(self, init_db_data): """Test get_by_id api endpoint""" # retrieve database informations facade_id = str(init_db_data['facades'][0]) # Get by its ID response = self.get_item_by_id(item_id=facade_id) assert response.status_code == 200 etag_value = response.headers.get('etag', None) # Get by its ID with etag: not modified (304) response = self.get_item_by_id(item_id=facade_id, headers={'If-None-Match': etag_value}) assert response.status_code == 304 # Errors: # not found (404) response = self.get_item_by_id(item_id=str(uuid_gen())) assert response.status_code == 404
def test_filestorage_manager_entry(self, tmpdir, ifc_file_data_stream): """Tests on file storage manager functions.""" fs_mgr = FileStorageMgr(str(tmpdir)) file_id = uuid_gen() file_name, data_stream = ifc_file_data_stream # `file_id` entry does not exists yet with pytest.raises(FileNotFoundError): fs_mgr.get(file_id) # create an entry fs_entry = fs_mgr.add(file_id, file_name, data_stream) assert isinstance(fs_entry, FileStorageEntry) assert fs_entry.file_id == file_id assert fs_entry.file_name == file_name assert fs_entry.base_dir_path == tmpdir assert fs_entry.entry_dir_path == tmpdir / str(file_id) assert fs_entry.file_path == tmpdir / str(file_id) / file_name assert not fs_entry.is_empty() assert not fs_entry.is_compressed() # create the entry again with pytest.raises(FileExistsError): fs_mgr.add(file_id, file_name, data_stream) # get the entry fs_entry = fs_mgr.get(file_id) assert fs_entry.file_name == file_name # delete the entry # `fs_entry.delete()` is also available fs_mgr.delete(file_id) # file storage entry has been removed with pytest.raises(FileNotFoundError): fs_mgr.get(file_id)
def __init__(self, cereal, quantity, expiration_date=None): super().__init__(id=uuid_gen()) self.cereal = cereal self.quantity = quantity self.expiration_date = expiration_date
def test_views_facades_post(self, init_db_data): """Test post api endpoint""" # retrieve database informations building_id = str(init_db_data['buildings'][0]) # Get list: no items response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post a new item f_spaces = [ str(init_db_data['spaces'][0]), str(init_db_data['spaces'][1]), str(init_db_data['spaces'][3]) ] response = self.post_item(name='Magical facade', distance_unit='metric', spaces=f_spaces, surface_info={'area': 42}, windows_wall_ratio=0.14, description='It can disappear...', orientation='East', building_id=building_id) assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'Magical facade' assert response.json['spaces'] == f_spaces assert response.json['surface_info']['area'] == 42 assert response.json['orientation'] == 'East' assert response.json['windows_wall_ratio'] == 0.14 assert response.json['description'] == 'It can disappear...' assert response.json['building_id'] == building_id # Get list: 1 found facade_json = self.get_items() assert facade_json.status_code == 200 assert len(facade_json.json) == 1 # Errors: # wrong spatial info (422) response = self.post_item(name='wrong_spatial', surface_info='wrong', windows_wall_ratio=0, spaces=[str(init_db_data['spaces'][2])], building_id=str( init_db_data['buildings'][3])) assert response.status_code == 422 # wrong windows_wall_ratio (422) response = self.post_item(name='wrong_windows_wall_ratio', spaces=[str(uuid_gen())], surface_info={'area': 0}, orientation='North', windows_wall_ratio=42, building_id=str( init_db_data['buildings'][3])) assert response.status_code == 422 # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', spaces=[str(init_db_data['spaces'][0])], surface_info={'area': 0}, orientation='North', windows_wall_ratio=0, building_id=str( init_db_data['buildings'][0])) assert response.status_code == 201 assert response.json['id'] != new_id
def test_views_spaces_post(self, init_db_data): """Test post api endpoint""" # retrieve database informations floor_id = str(init_db_data['floors'][0]) # Get list: no items response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post a new item response = self.post_item(name='Bryan\'s kitchen', kind='Kitchen', floor_id=floor_id, occupancy={ 'nb_permanents': 6, 'nb_max': 66 }, spatial_info={ 'area': 10, 'max_height': 2 }, description='Maybe there is a dog...') assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'Bryan\'s kitchen' assert response.json['kind'] == 'Kitchen' assert response.json['occupancy']['nb_permanents'] == 6 assert response.json['occupancy']['nb_max'] == 66 assert response.json['spatial_info']['area'] == 10 assert response.json['spatial_info']['max_height'] == 2 assert response.json['floor_id'] == floor_id # Get list: 1 space found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 1 # Errors: # wrong kind (422) response = self.post_item( name='wrong_kind_choice', kind='wrong', floor_id=floor_id, #occupancy={'nb_permanents': 6, 'nb_max': 66}, spatial_info={ 'area': 10, 'max_height': 2 }) assert response.status_code == 422 # missing kind (201) response = self.post_item(name='missing_kind', floor_id=floor_id, occupancy={ 'nb_permanents': 6, 'nb_max': 66 }, spatial_info={ 'area': 10, 'max_height': 2 }) assert response.status_code == 201 # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', kind='Bathroom', occupancy={ 'nb_permanents': 6, 'nb_max': 66 }, spatial_info={ 'area': 10, 'max_height': 2 }, floor_id=floor_id) assert response.status_code == 201 assert response.json['id'] != new_id
def test_views_floors_post(self, init_db_data): """Test post api endpoint""" # retrieve database informations building_id = str(init_db_data['buildings'][0]) # Get list: no items response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post a new item response = self.post_item(name='A floor', kind='Floor', level=69, spatial_info={ 'area': 42, 'max_height': 2.4 }, description='A top level floor !', building_id=building_id) assert response.status_code == 201 assert response.json['id'] is not None assert response.json['name'] == 'A floor' assert response.json['kind'] == 'Floor' assert response.json['level'] == 69 assert response.json['spatial_info']['area'] == 42 assert response.json['spatial_info']['max_height'] == 2.4 assert response.json['description'] == 'A top level floor !' assert response.json['building_id'] == building_id # Get list: 1 found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 1 # Errors: # wrong kind (422) response = self.post_item(name='wrong_kind', kind='wrong', level=0, spatial_info={ 'area': 0, 'max_height': 0 }, building_id=building_id) assert response.status_code == 422 assert 'kind' in response.json['errors'] # Remarks: # id is 'read only' new_id = str(uuid_gen()) response = self.post_item(id=new_id, name='id_is_read_only', kind='Floor', level=0, spatial_info={ 'area': 0, 'max_height': 0 }, building_id=building_id) assert response.status_code == 201 assert response.json['id'] != new_id
def test_views_ifc_post(self, ifc_file_data, ifc_file_obj, ifc_zip_file_obj, ifc_multi_zip_file_obj): """Test post api endpoint""" # retrieve ifc file informations file_name, file_content = ifc_file_data zip_file_name, zip_file_obj = ifc_zip_file_obj _, zip_multi_file_obj = ifc_multi_zip_file_obj # Get ifc file list: no items response = self.get_items() assert response.status_code == 200 assert len(response.json) == 0 # Post an ifc file response = self.post_item(description='A description of the file...', file=ifc_file_obj, content_type='multipart/form-data') assert response.status_code == 201 assert response.json['id'] is not None assert response.json['original_file_name'] == file_name assert response.json['file_name'] == secure_filename(file_name) # Get ifc file list: 1 item found response = self.get_items() assert response.status_code == 200 assert len(response.json) == 1 # Post a zipped ifc file response = self.post_item( description='Another description of the file...', file=zip_file_obj, content_type='multipart/form-data') assert response.status_code == 201 assert response.json['id'] is not None assert response.json['original_file_name'] == zip_file_name assert response.json['file_name'] == secure_filename(zip_file_name) # Errors: # missing file (422) response = self.post_item(content_type='multipart/form-data') assert response.status_code == 422 # wrong file format (422) response = self.post_item(file=build_file_obj('wrong_format.pdf', 'just a test'), content_type='multipart/form-data') assert response.status_code == 422 # more than ONE file in zipped archive (500) with pytest.raises(IFCFileBadArchiveError): self.post_item(file=zip_multi_file_obj, content_type='multipart/form-data') # save error (500) # XXX: update this test when database is not mocked anymore with pytest.raises(ItemSaveError): self.post_item(file=build_file_obj('save_error.ifc', file_content), content_type='multipart/form-data') # Remarks: # id is 'read only' new_id = str(uuid_gen()) ifc_file_name = 'id_is_read_only.ifc' response = self.post_item(id=new_id, file=build_file_obj(ifc_file_name, file_content), content_type='multipart/form-data') assert response.status_code == 201 assert response.json['id'] != new_id