Source code for tests.file_system.test_file_upload
from os import mkdir
from os.path import join, isdir
from werkzeug.datastructures.file_storage import FileStorage
from xrf_explorer.server.file_system.helper import set_config, get_config
from xrf_explorer.server.file_system.sources.data_listing import get_data_sources_names, get_data_source_files
RESOURCES_PATH: str = join('tests', 'resources')
[docs]
class TestUploadFileToServer:
CUSTOM_CONFIG_PATH: str = join(RESOURCES_PATH, 'configs', 'upload-file-server-backend.yml')
CUSTOM_CONFIG_PATH_NO_SOURCES: str = join(RESOURCES_PATH, 'configs', 'upload-file-server-backend-no-sources.yml')
UPLOAD_FILE_PATH: str = join(RESOURCES_PATH, 'file_system', 'txt-file.txt')
[docs]
@staticmethod
def get_filestorage_object(path: str):
with open(path, 'rb') as f:
file_storage: FileStorage = FileStorage(f)
return file_storage
[docs]
def test_get_file_names(self):
# setup
data_source_name: str = "test_data_source"
set_config(self.CUSTOM_CONFIG_PATH)
# execute
result: list[str] = get_data_sources_names()
# validate
assert data_source_name in result
[docs]
def test_no_file_names(self):
# setup
set_config(self.CUSTOM_CONFIG_PATH_NO_SOURCES)
config: dict = get_config()
if not isdir(config["uploads-folder"]):
mkdir(config["uploads-folder"])
# execute
result: list[str] = get_data_sources_names()
# validate
assert result == []
[docs]
def test_get_data_source_names_invalid_config(self):
# setup
set_config("fake_config_path")
# execute
result: list[str] = get_data_sources_names()
# validate
assert result == []
[docs]
def test_get_data_source_files(self, caplog):
# setup
set_config(self.CUSTOM_CONFIG_PATH)
# execute
files: list[str] = get_data_source_files('test_data_source')
# validate
assert len(files) == 2
assert 'test_data_cube.txt' in files
assert 'workspace.json' in files
[docs]
def test_get_data_source_files_invalid_config(self):
# setup
set_config("fake_config_path")
# execute
result: list[str] = get_data_source_files('test_data_source')
# validate
assert result == []
[docs]
def test_get_data_source_files_invalid_path(self):
# setup
set_config(self.CUSTOM_CONFIG_PATH)
# execute
result: list[str] = get_data_source_files('invalid_data_source')
# validate
assert result == []