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:
Czar Echavez
2023-08-14 08:27:09 +00:00
3 changed files with 37 additions and 7 deletions

View File

@@ -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)

View 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]):
"""

View File

@@ -1,4 +1,5 @@
from primaite.simulator.file_system.file_system import FileSystem
from primaite.simulator.file_system.file_system_file import FileSystemFile
from primaite.simulator.file_system.file_system_folder import FileSystemFolder
@@ -37,7 +38,7 @@ def test_delete_file():
file_system.delete_file(file=file)
assert len(file_system.folders) is 1
assert len(file_system.get_folder_by_id(folder.uuid).files) is 0
assert len(folder.files) is 0
def test_delete_non_existent_file():
@@ -45,16 +46,20 @@ def test_delete_non_existent_file():
file_system = FileSystem()
file = file_system.create_file(file_name="test_file", size=10)
not_added_file = file_system.create_file(file_name="test_file", size=10)
not_added_file = FileSystemFile(name="not_added")
# folder should be created
assert len(file_system.folders) is 1
# should only have 1 file in the file system
folder_id = list(file_system.folders.keys())[0]
folder = file_system.get_folder_by_id(folder_id)
assert len(list(folder.files)) is 1
assert folder.get_file_by_id(file.uuid) is file
# deleting should not change how many files are in folder
file_system.delete_file(file=not_added_file)
assert len(file_system.folders) is 1
assert len(file_system.get_folder_by_id(folder.uuid).files) is 1
assert len(list(folder.files)) is 1
def test_delete_folder():