Move read() methods to __init__(), new tests

This commit is contained in:
2023-03-13 19:04:24 +01:00
parent 4acc0e5da2
commit c40ff88bcf
8 changed files with 248 additions and 217 deletions

View File

@@ -9,48 +9,42 @@ class TestDynamoFile(unittest.TestCase):
# Missing methods:
# get_related_python_files
# get_open_file_by_uuid
# update_python_node
# write
def test_read_and_variables(self):
def test_init(self):
dyn2py.DynamoFile.open_files.clear()
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
dyn.read()
self.assertEqual(dyn.uuid, "3c3b4c05-9716-4e93-9360-ca0637cb5486")
self.assertEqual(dyn.name, "python_nodes")
self.assertTrue(dyn in dyn.open_files)
# Dynamo 1 file:
with self.assertRaises(dyn2py.DynamoFileException):
dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/dynamo1file.dyn")
dyn1.read()
# Not existing file:
with self.assertRaises(FileNotFoundError):
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/not_existing.dyn")
dyn2.read()
# No python nodes:
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/no_python.dyn")
def test_get_python_nodes(self):
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
py_nodes = dyn.get_python_nodes()
py_node = dyn.get_python_node_by_id("d7704617c75e4bf1a5c387b7c3f001ea")
self.assertEqual(len(py_nodes), 6)
self.assertEqual(len(dyn.python_nodes), 6)
self.assertTrue(py_node)
self.assertTrue(py_node in py_nodes)
self.assertTrue(py_node in dyn.python_nodes)
self.assertEqual(py_node.checksum, "1f3d9e6153804fe1ed37571a9cda8e26")
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
dyn.get_python_node_by_id("wrongid")
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/no_python.dyn")
# Raise error on file without python nodes:
with self.assertRaises(dyn2py.DynamoFileException):
dyn2.get_python_nodes()
def test_extract_python(self):
cleanup_output_dir()
@@ -60,3 +54,15 @@ class TestDynamoFile(unittest.TestCase):
output_dir = pathlib.Path(OUTPUT_DIR)
self.assertEqual(len(list(output_dir.iterdir())), 6)
def test_get_open_file_by_uuid(self):
dyn2py.DynamoFile.open_files.clear()
dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
self.assertEqual(dyn1,
dyn2py.DynamoFile.get_open_file_by_uuid("3c3b4c05-9716-4e93-9360-ca0637cb5486"))
self.assertEqual(dyn2,
dyn2py.DynamoFile.get_open_file_by_uuid("76de5c79-17c5-4c74-9f90-ad99a213d339"))

View File

@@ -8,6 +8,8 @@ from tests.support import *
class TestFile(unittest.TestCase):
# Write methods should be tested in subclasses!
def test_init(self):
paths = [
f"{INPUT_DIR}/python_nodes.dyn",
@@ -33,23 +35,25 @@ class TestFile(unittest.TestCase):
self.assertEqual(the_file.extension, ".dyn")
self.assertFalse(the_file.modified)
self.assertEqual(the_file.__class__, dyn2py.DynamoFile)
def test_init_newfile(self):
paths = [
f"{INPUT_DIR}/new_file.dyn",
pathlib.Path(f"{INPUT_DIR}/new_file.dyn")
f"{INPUT_DIR}/new_file.py",
pathlib.Path(f"{INPUT_DIR}/new_file.py")
]
if platform.system() == "Windows":
paths.extend([
fr"{INPUT_DIR}\new_file.dyn",
pathlib.Path(fr"{INPUT_DIR}\new_file.dyn")
fr"{INPUT_DIR}\new_file.py",
pathlib.Path(fr"{INPUT_DIR}\new_file.py")
])
for path in paths:
the_file = dyn2py.File(path)
self.assertEqual(the_file.filepath,
pathlib.Path(f"{INPUT_DIR}/new_file.dyn"))
pathlib.Path(f"{INPUT_DIR}/new_file.py"))
self.assertEqual(the_file.basename, "new_file")
self.assertEqual(the_file.dirpath, pathlib.Path(INPUT_DIR))
self.assertEqual(the_file.realpath, pathlib.Path(path).resolve())
@@ -57,17 +61,21 @@ class TestFile(unittest.TestCase):
self.assertEqual(the_file.mtime, 0.0)
self.assertEqual(the_file.mtimeiso, "")
self.assertFalse(the_file.exists)
self.assertEqual(the_file.extension, ".dyn")
self.assertEqual(the_file.extension, ".py")
self.assertFalse(the_file.modified)
def test_newer(self):
# Touch a file, so it will be always newer than the others:
touched_file = pathlib.Path(f"{OUTPUT_DIR}/touched_file.py")
touched_file.touch()
newer_file = dyn2py.File(touched_file)
self.assertEqual(the_file.__class__, dyn2py.PythonFile)
older_file = dyn2py.File(f"{INPUT_DIR}/python_nodes.dyn")
nonexisting_file = dyn2py.File(f"{INPUT_DIR}/new_file.dyn")
def test_newer(self):
older_file = dyn2py.File(f"{INPUT_DIR}/single_node.dyn")
nonexisting_file = dyn2py.File(f"{INPUT_DIR}/new_file.py")
# Extract a python file so it is always newer than the others:
cleanup_output_dir()
opt = dyn2py.Options(python_folder=OUTPUT_DIR)
older_file.extract_python(options=opt) # type: ignore
newer_file = dyn2py.File(
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
self.assertTrue(newer_file.is_newer(older_file))
self.assertTrue(newer_file.is_newer(nonexisting_file))
@@ -78,17 +86,6 @@ class TestFile(unittest.TestCase):
self.assertFalse(nonexisting_file.is_newer(older_file))
self.assertFalse(nonexisting_file.is_newer(newer_file))
def test_write(self):
existing_file = dyn2py.File(f"{INPUT_DIR}/python_nodes.dyn")
nonexisting_file = dyn2py.File(f"{INPUT_DIR}/new_file.dyn")
options = dyn2py.Options()
with self.assertRaises(TypeError):
existing_file.write(options)
with self.assertRaises(TypeError):
nonexisting_file.write(options)
def test_is_file(self):
extract_single_node_dyn()
@@ -101,8 +98,6 @@ class TestFile(unittest.TestCase):
for path, f in paths:
file = dyn2py.File(path)
self.assertEqual(file.is_dynamo_file(), f=="dyn")
self.assertEqual(file.is_python_file(), f=="py")
self.assertEqual(file.is_dynamo_file(), f == "dyn")
self.assertEqual(file.is_python_file(), f == "py")

View File

@@ -8,7 +8,6 @@ class TestPythonNode(unittest.TestCase):
def test_init_from_dyn(self):
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
dyn.read()
node_dict = next((n for n in dyn.full_dict["Nodes"]
if n["NodeType"] == "PythonScriptNode"), {})
@@ -43,8 +42,7 @@ class TestPythonNode(unittest.TestCase):
mod_py.write(line)
py = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
py.read()
node = dyn2py.PythonNode(
node_id=py.header_data["py_id"],
engine=py.header_data["py_engine"],