Skip to content
Snippets Groups Projects
Commit a9e29965 authored by Jacob Wahlman's avatar Jacob Wahlman :ok_hand:
Browse files

added check for overwrite, added more tests

parent 28dd5687
No related branches found
No related tags found
2 merge requests!33Resolve "Load/Save SFG to file",!27Resolve "Load/Save SFG to file"
Pipeline #13971 passed
......@@ -20,15 +20,18 @@ def save_structure(struct: AbstractOperation, _path: Optional[str] = None, _name
Keyword Arguments:
_path: The path (str) to which the structure will be saved.
_name: The name (str) of the file to be saved. Only used if _path is not defined and is not None.
_name: The name (str) of the file to be saved. Only used if _path is None.
"""
try:
_name = _name if _name is not None else f"{struct.type_name}.pickle"
if _path is None:
_name = _name if _name is not None else f"{struct.type_name}.pickle"
_path = path.join(getcwd(), _name)
print(_path)
index = 1
while path.exists(_path):
_path = path.join(getcwd(), f"{struct.type_name}({index}).pickle")
index += 1
with open(_path, "wb") as handle:
dill.dump(struct, handle, protocol=dill.HIGHEST_PROTOCOL)
except Exception as e:
......
from os.path import isfile
"""
B-ASIC test suite for load/save datastructures.
"""
from os import getcwd, path
from random import choice
from string import ascii_lowercase
import pytest
......@@ -9,15 +15,38 @@ class TestSaveStructures:
def test_save_sfg(self, large_operation_tree):
sfg = SFG(outputs=[Output(large_operation_tree)])
path = save_structure(sfg)
assert path is not None
assert isfile(path)
_path = save_structure(sfg)
assert _path is not None
assert path.isfile(_path)
def test_load_sfg(self, large_operation_tree):
sfg = SFG(outputs=[Output(large_operation_tree)])
path = save_structure(sfg)
_sfg = load_structure(path)
_path = save_structure(sfg)
_sfg = load_structure(_path)
assert isinstance(_sfg, SFG)
assert sorted([comp.type_name for comp in _sfg.components]) == sorted([comp.type_name for comp in sfg.components])
assert sfg.evaluate() == _sfg.evaluate()
def test_save_invalid_path(self, large_operation_tree):
sfg = SFG(outputs=[Output(large_operation_tree)])
_folder = "".join(choice(ascii_lowercase) for _ in range(4))
while path.exists(path.join(getcwd(), _folder)):
_folder = "".join(choice(ascii_lowercase) for _ in range(4))
_invalid_path = path.join(getcwd(), _folder, "cool.pickle")
_path = save_structure(sfg, _path=_invalid_path)
assert _path is None
def test_load_invalid_path(self, large_operation_tree):
_folder = "".join(choice(ascii_lowercase) for _ in range(4))
while path.exists(path.join(getcwd(), _folder)):
_folder = "".join(choice(ascii_lowercase) for _ in range(4))
_invalid_path = path.join(getcwd(), _folder, "cool.pickle")
_path = load_structure(_path=_invalid_path)
assert _path is None
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment