Tests, documentation, fixes

This commit is contained in:
2023-03-10 20:45:07 +01:00
parent 62347eaf31
commit c9df6b5408
9 changed files with 547 additions and 34 deletions

63
tests/test_PythonNode.py Normal file
View File

@@ -0,0 +1,63 @@
import unittest
import dyn2py
import hashlib
from tests.support import *
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"), {})
# Found a node:
self.assertTrue(node_dict)
node_views = dyn.full_dict["View"]["NodeViews"]
node = dyn2py.PythonNode(
node_dict_from_dyn=node_dict,
full_nodeviews_dict_from_dyn=node_views,
source_dynamo_file=dyn
)
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
self.assertEqual(node.engine, "CPython3")
self.assertEqual(node.checksum, "ec2c85a11ddbf8375da03f11272d427a")
self.assertEqual(node.name, "Python Script")
self.assertEqual(
node.filename, "single_node_1c5d99792882409e97e132b3e9f814b0.py")
def test_init_from_py(self):
cleanup_output_dir()
# Extract py:
options = dyn2py.Options(python_folder=OUTPUT_DIR)
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
dyn.extract_python(options)
# Open the extracted file and replace a string:
with open(f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py") as orig_py, \
open(f"{OUTPUT_DIR}/single_node_mod.py", "w") as mod_py:
for line in orig_py:
if "asd_string" in line:
line = line.replace("asd_string", "qwe_string")
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"],
code=py.code,
checksum=hashlib.md5(py.code.encode()).hexdigest()
)
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
self.assertEqual(node.engine, "CPython3")
self.assertEqual(node.checksum, "bf0f039ef6f11c3043e0821143801d48")