From a9e29965a7d1be8bac1fc61a54767ffb27b881e5 Mon Sep 17 00:00:00 2001 From: Jacob Wahlman <jacwa448@student.liu.se> Date: Fri, 24 Apr 2020 08:58:48 +0200 Subject: [PATCH] added check for overwrite, added more tests --- b_asic/utils.py | 11 +++++--- test/test_load_save_structure.py | 43 ++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/b_asic/utils.py b/b_asic/utils.py index 7cca1f15..11f690c9 100644 --- a/b_asic/utils.py +++ b/b_asic/utils.py @@ -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: diff --git a/test/test_load_save_structure.py b/test/test_load_save_structure.py index 193753b6..d84d8da0 100644 --- a/test/test_load_save_structure.py +++ b/test/test_load_save_structure.py @@ -1,4 +1,10 @@ -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 -- GitLab