Update Tests

This commit is contained in:
2023-04-25 00:39:24 +02:00
parent e2aecc684f
commit 8909594a98
5 changed files with 86 additions and 30 deletions

View File

@@ -7,6 +7,7 @@
- [x] DynamoFile - [x] DynamoFile
- [x] PythonFile - [x] PythonFile
- [x] PythonNode - [x] PythonNode
- [ ] Options
- [ ] run() - [ ] run()
## CI/CD ## CI/CD

View File

@@ -3,15 +3,17 @@ import dyn2py
INPUT_DIR = "tests/input_files" INPUT_DIR = "tests/input_files"
OUTPUT_DIR = "tests/output_files" OUTPUT_DIR = "tests/output_files"
TEMP_DIR = "tests/temp_files"
def cleanup_output_dir(): def cleanup_dirs():
output_dir = pathlib.Path(OUTPUT_DIR) for p in [OUTPUT_DIR, TEMP_DIR]:
if output_dir.exists(): the_dir = pathlib.Path(p)
for f in output_dir.iterdir(): if the_dir.exists():
for f in the_dir.iterdir():
f.unlink() f.unlink()
else: else:
output_dir.mkdir() the_dir.mkdir()
def extract_single_node_dyn(modify_py: bool = False): def extract_single_node_dyn(modify_py: bool = False):
@@ -23,7 +25,7 @@ def extract_single_node_dyn(modify_py: bool = False):
modify_py (bool, optional): Also do some changes on the exported file. Defaults to False. modify_py (bool, optional): Also do some changes on the exported file. Defaults to False.
""" """
cleanup_output_dir() cleanup_dirs()
# Extract py: # Extract py:
options = dyn2py.Options(python_folder=OUTPUT_DIR) options = dyn2py.Options(python_folder=OUTPUT_DIR)

View File

@@ -36,8 +36,12 @@ class TestCommandLine(unittest.TestCase):
self.assertFalse(p.stderr) self.assertFalse(p.stderr)
dyn_sources = [ dyn_sources = [
{"filename": "python_nodes.dyn", "py_file_count": 6}, {"filename": "python_nodes.dyn", "output_file_count": 6},
{"filename": "single_node.dyn", "py_file_count": 1} {"filename": "single_node.dyn", "output_file_count": 1}
]
py_sources = [
{"filename": "single_node_mod.py"},
] ]
dyn_sources_error = ["dynamo1file.dyn", dyn_sources_error = ["dynamo1file.dyn",
@@ -85,7 +89,7 @@ class TestCommandLine(unittest.TestCase):
test_dicts[0]["filenames"] = [ test_dicts[0]["filenames"] = [
test_dicts[0]["filename"]] test_dicts[0]["filename"]]
if i == 0: if i == 0 and len(source_dict) > 1:
# Create a multi file version on the first file: # Create a multi file version on the first file:
d = {} d = {}
for key in source_dict: for key in source_dict:
@@ -100,7 +104,7 @@ class TestCommandLine(unittest.TestCase):
for test_dict in test_dicts: for test_dict in test_dicts:
if pfolder_option: if pfolder_option:
file_dir = INPUT_DIR file_dir = TEMP_DIR
pfolder_arg = f"{pfolder_option} {OUTPUT_DIR}" pfolder_arg = f"{pfolder_option} {OUTPUT_DIR}"
else: else:
file_dir = OUTPUT_DIR file_dir = OUTPUT_DIR
@@ -122,7 +126,7 @@ class TestCommandLine(unittest.TestCase):
def test_dyn_error(self): def test_dyn_error(self):
for s in self.dyn_sources_error: for s in self.dyn_sources_error:
cleanup_output_dir() cleanup_dirs()
if pathlib.Path(f"{INPUT_DIR}/{s}").exists(): if pathlib.Path(f"{INPUT_DIR}/{s}").exists():
shutil.copy(f"{INPUT_DIR}/{s}", shutil.copy(f"{INPUT_DIR}/{s}",
@@ -140,12 +144,19 @@ class TestCommandLine(unittest.TestCase):
dyn_tests = self.generate_test_args(self.dyn_sources) dyn_tests = self.generate_test_args(self.dyn_sources)
for s in dyn_tests: for s in dyn_tests:
cleanup_output_dir() cleanup_dirs()
# if no pythonfolder, everything should be in output ddir
if not s["pfolder_arg"]: if not s["pfolder_arg"]:
source_dir = OUTPUT_DIR
else:
source_dir = TEMP_DIR
# copy source files:
for filename in s["filenames"]: for filename in s["filenames"]:
shutil.copy(f"{INPUT_DIR}/{filename}", shutil.copy(f"{INPUT_DIR}/{filename}",
f"{OUTPUT_DIR}/{filename}") f"{source_dir}/{filename}")
# Open files normally # Open files normally
file_open = self.run_command( file_open = self.run_command(
@@ -155,7 +166,7 @@ class TestCommandLine(unittest.TestCase):
self.assertFalse( self.assertFalse(
bool(file_open["stderr"]), msg=file_open["stderr"]) bool(file_open["stderr"]), msg=file_open["stderr"])
self.assertEqual( self.assertEqual(
len(file_open["python_file_mtimes"]), s["py_file_count"]) len(file_open["python_file_mtimes"]), s["output_file_count"])
# Test no overwrite # Test no overwrite
file_no_overwrite = self.run_command( file_no_overwrite = self.run_command(
@@ -164,7 +175,7 @@ class TestCommandLine(unittest.TestCase):
# Should give error, because they already exist: # Should give error, because they already exist:
self.assertTrue(bool(file_no_overwrite["stderr"])) self.assertTrue(bool(file_no_overwrite["stderr"]))
self.assertEqual( self.assertEqual(
len(file_no_overwrite["python_file_mtimes"]), s["py_file_count"]) len(file_no_overwrite["python_file_mtimes"]), s["output_file_count"])
# The modify time shouldn't change as they were not overwritten: # The modify time shouldn't change as they were not overwritten:
for p in file_no_overwrite["python_file_mtimes"]: for p in file_no_overwrite["python_file_mtimes"]:
@@ -178,7 +189,7 @@ class TestCommandLine(unittest.TestCase):
# Should not have an error: # Should not have an error:
self.assertFalse(bool(file_force["stderr"])) self.assertFalse(bool(file_force["stderr"]))
self.assertEqual( self.assertEqual(
len(file_force["python_file_mtimes"]), s["py_file_count"]) len(file_force["python_file_mtimes"]), s["output_file_count"])
# Modify time should be higher as they were replaced # Modify time should be higher as they were replaced
for p in file_force["python_file_mtimes"]: for p in file_force["python_file_mtimes"]:
@@ -193,7 +204,8 @@ class TestCommandLine(unittest.TestCase):
self.assertFalse(bool(file_backup["stderr"])) self.assertFalse(bool(file_backup["stderr"]))
self.assertEqual( self.assertEqual(
len(file_backup["python_file_mtimes"]), s["py_file_count"] * 2, len(file_backup["python_file_mtimes"]
), s["output_file_count"] * 2,
msg=f"" msg=f""
) )
@@ -202,11 +214,42 @@ class TestCommandLine(unittest.TestCase):
file_backup["python_file_mtimes"][p] > file_force["python_file_mtimes"][p] file_backup["python_file_mtimes"][p] > file_force["python_file_mtimes"][p]
) )
# def test_py(self):
# py_tests = self.generate_test_args(self.py_sources)
# # TODO add more python files!
# self.assertEqual(len(py_tests), 1)
# for s in py_tests:
# cleanup_dirs()
# extract_single_node_dyn(modify_py=True)
# # if pythonfolder, python should be in the temp folder:
# if s["pfolder_arg"]:
# for filename in s["filenames"]:
# shutil.move(f"{OUTPUT_DIR}/{filename}",
# f"{TEMP_DIR}/{filename}")
# # Open files normally
# file_open = self.run_command(
# [s["pfolder_arg"], s['filepath']])
# # Open without error:
# self.assertFalse(
# bool(file_open["stderr"]), msg=file_open["stderr"])
# self.assertEqual(
# len(file_open["python_file_mtimes"]), s["output_file_count"])
def test_single_dyn_dryrun(self): def test_single_dyn_dryrun(self):
for s in self.dyn_sources: for s in self.dyn_sources:
for arg in ["-n", "--dry-run"]: for arg in ["-n", "--dry-run"]:
cleanup_output_dir() cleanup_dirs()
shutil.copy(f"{INPUT_DIR}/{s['filename']}", shutil.copy(f"{INPUT_DIR}/{s['filename']}",
f"{OUTPUT_DIR}/{s['filename']}") f"{OUTPUT_DIR}/{s['filename']}")

View File

@@ -43,7 +43,7 @@ class TestDynamoFile(unittest.TestCase):
dyn.get_python_node_by_id("wrongid") dyn.get_python_node_by_id("wrongid")
def test_extract_python(self): def test_extract_python(self):
cleanup_output_dir() cleanup_dirs()
dyn2py.PythonFile.open_files.clear() dyn2py.PythonFile.open_files.clear()
opt = dyn2py.Options(python_folder=OUTPUT_DIR) opt = dyn2py.Options(python_folder=OUTPUT_DIR)
@@ -69,7 +69,7 @@ class TestDynamoFile(unittest.TestCase):
dyn2py.DynamoFile.get_open_file_by_uuid("76de5c79-17c5-4c74-9f90-ad99a213d339")) dyn2py.DynamoFile.get_open_file_by_uuid("76de5c79-17c5-4c74-9f90-ad99a213d339"))
def test_get_related_python_files(self): def test_get_related_python_files(self):
cleanup_output_dir() cleanup_dirs()
opt = dyn2py.Options(python_folder=OUTPUT_DIR) opt = dyn2py.Options(python_folder=OUTPUT_DIR)
dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn") dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
@@ -91,7 +91,7 @@ class TestDynamoFile(unittest.TestCase):
self.assertFalse(no_python_files) self.assertFalse(no_python_files)
def test_write_same(self): def test_write_same(self):
cleanup_output_dir() cleanup_dirs()
shutil.copy(f"{INPUT_DIR}/python_nodes.dyn", shutil.copy(f"{INPUT_DIR}/python_nodes.dyn",
f"{OUTPUT_DIR}/python_nodes.dyn") f"{OUTPUT_DIR}/python_nodes.dyn")

View File

@@ -8,8 +8,6 @@ from tests.support import *
class TestFile(unittest.TestCase): class TestFile(unittest.TestCase):
# Write methods should be tested in subclasses!
def test_init(self): def test_init(self):
paths = [ paths = [
f"{INPUT_DIR}/python_nodes.dyn", f"{INPUT_DIR}/python_nodes.dyn",
@@ -71,7 +69,7 @@ class TestFile(unittest.TestCase):
nonexisting_file = dyn2py.File(f"{INPUT_DIR}/new_file.py") nonexisting_file = dyn2py.File(f"{INPUT_DIR}/new_file.py")
# Extract a python file so it is always newer than the others: # Extract a python file so it is always newer than the others:
cleanup_output_dir() cleanup_dirs()
opt = dyn2py.Options(python_folder=OUTPUT_DIR) opt = dyn2py.Options(python_folder=OUTPUT_DIR)
older_file.extract_python(options=opt) # type: ignore older_file.extract_python(options=opt) # type: ignore
for f in dyn2py.PythonFile.open_files: for f in dyn2py.PythonFile.open_files:
@@ -103,3 +101,15 @@ class TestFile(unittest.TestCase):
self.assertEqual(file.is_dynamo_file(), f == "dyn") self.assertEqual(file.is_dynamo_file(), f == "dyn")
self.assertEqual(file.is_python_file(), f == "py") self.assertEqual(file.is_python_file(), f == "py")
def test_write(self):
# new empty file:
empty_filepath = pathlib.Path(f"{OUTPUT_DIR}/empty.txt")
empty_filepath.touch()
empty_file = dyn2py.File(f"{OUTPUT_DIR}/empty.txt")
self.assertTrue(empty_file.exists)
with self.assertRaises(TypeError):
empty_file.write()