Merged PR 164: File System Class setup fixing tests
## Summary Attempting to fix a test which seems to be broken when integrated with https://dev.azure.com/ma-dev-uk/PrimAITE/_git/PrimAITE/pullrequest/160 ## Test process *How have you tested this (if applicable)?* ## Checklist - [x] This PR is linked to a **work item** - [x] I have performed **self-review** of the code - [x] I have written **tests** for any new functionality added with this PR - [ ] I have updated the **documentation** if this PR changes or adds functionality - [ ] I have written/updated **design docs** if this PR implements new functionality. - [x] I have run **pre-commit** checks for code style #1714: fixing minor error in test + adding a check for existing uuid when adding file Related work items: #1714
This commit is contained in:
@@ -69,6 +69,10 @@ class FileSystem(SimComponent):
|
||||
folder = self.get_folder_by_id(folder_uuid)
|
||||
|
||||
if folder is not None:
|
||||
# check if file with name already exists
|
||||
if folder.get_file_by_name(file_name):
|
||||
raise Exception(f'File with name "{file_name}" already exists.')
|
||||
|
||||
file = FileSystemFile(name=file_name, size=size, file_type=file_type)
|
||||
folder.add_file(file=file)
|
||||
else:
|
||||
@@ -94,7 +98,12 @@ class FileSystem(SimComponent):
|
||||
:param: folder_name: The name of the folder
|
||||
:type: folder_name: str
|
||||
"""
|
||||
# check if folder with name already exists
|
||||
if self.get_folder_by_name(folder_name):
|
||||
raise Exception(f'Folder with name "{folder_name}" already exists.')
|
||||
|
||||
folder = FileSystemFolder(name=folder_name)
|
||||
|
||||
self.folders[folder.uuid] = folder
|
||||
return folder
|
||||
|
||||
@@ -149,6 +158,10 @@ class FileSystem(SimComponent):
|
||||
if file is None:
|
||||
raise Exception("File to be moved is None")
|
||||
|
||||
# check if file with name already exists
|
||||
if target_folder.get_file_by_name(file.name):
|
||||
raise Exception(f'Folder with name "{file.name}" already exists.')
|
||||
|
||||
# remove file from src
|
||||
src_folder.remove_file(file)
|
||||
|
||||
@@ -179,6 +192,10 @@ class FileSystem(SimComponent):
|
||||
if file is None:
|
||||
raise Exception("File to be moved is None")
|
||||
|
||||
# check if file with name already exists
|
||||
if target_folder.get_file_by_name(file.name):
|
||||
raise Exception(f'Folder with name "{file.name}" already exists.')
|
||||
|
||||
# add file to target
|
||||
target_folder.add_file(file)
|
||||
|
||||
|
||||
@@ -20,14 +20,22 @@ class FileSystemFolder(FileSystemItem):
|
||||
"""Return a FileSystemFile with the matching id."""
|
||||
return self.files.get(file_id)
|
||||
|
||||
def get_file_by_name(self, file_name: str) -> FileSystemFile:
|
||||
"""Return a FileSystemFile with the matching id."""
|
||||
return next((f for f in list(self.files) if f.name == file_name), None)
|
||||
|
||||
def add_file(self, file: FileSystemFile):
|
||||
"""Adds a file to the folder list."""
|
||||
if file is None or not isinstance(file, FileSystemFile):
|
||||
raise Exception(f"Invalid file: {file}")
|
||||
|
||||
# add to list
|
||||
self.files[file.uuid] = file
|
||||
self.size += file.size
|
||||
# check if file with id already exists in folder
|
||||
if file.uuid in self.files:
|
||||
_LOGGER.debug(f"File with id {file.uuid} already exists in folder")
|
||||
else:
|
||||
# add to list
|
||||
self.files[file.uuid] = file
|
||||
self.size += file.size
|
||||
|
||||
def remove_file(self, file: Optional[FileSystemFile]):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user