mirror of
https://github.com/infeeeee/dyn2py
synced 2025-12-16 22:16:18 +01:00
More tests, refactor PythonNode
This commit is contained in:
@@ -4,6 +4,7 @@ import dyn2py
|
||||
INPUT_DIR = "tests/input_files"
|
||||
OUTPUT_DIR = "tests/output_files"
|
||||
|
||||
|
||||
def cleanup_output_dir():
|
||||
output_dir = pathlib.Path(OUTPUT_DIR)
|
||||
if output_dir.exists():
|
||||
@@ -13,9 +14,14 @@ def cleanup_output_dir():
|
||||
output_dir.mkdir()
|
||||
|
||||
|
||||
def extract_single_node_dyn():
|
||||
def extract_single_node_dyn(modify_py: bool = False):
|
||||
"""Extract python from single_node.dyn
|
||||
File will be here: f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py"
|
||||
Modified file will be here: f"{OUTPUT_DIR}/single_node_mod.py"
|
||||
|
||||
Args:
|
||||
modify_py (bool, optional): Also do some changes on the exported file. Defaults to False.
|
||||
|
||||
"""
|
||||
cleanup_output_dir()
|
||||
|
||||
@@ -23,3 +29,12 @@ def extract_single_node_dyn():
|
||||
options = dyn2py.Options(python_folder=OUTPUT_DIR)
|
||||
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||
dyn.extract_python(options)
|
||||
|
||||
if modify_py:
|
||||
# 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)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import unittest
|
||||
import dyn2py
|
||||
import pathlib
|
||||
import shutil
|
||||
import simplejson as json
|
||||
|
||||
from tests.support import *
|
||||
|
||||
@@ -8,13 +10,11 @@ from tests.support import *
|
||||
class TestDynamoFile(unittest.TestCase):
|
||||
|
||||
# Missing methods:
|
||||
# get_related_python_files
|
||||
# update_python_node
|
||||
# write
|
||||
# update_python_node: exception
|
||||
|
||||
def test_init(self):
|
||||
dyn2py.DynamoFile.open_files.clear()
|
||||
|
||||
|
||||
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
|
||||
|
||||
self.assertEqual(dyn.uuid, "3c3b4c05-9716-4e93-9360-ca0637cb5486")
|
||||
@@ -61,8 +61,71 @@ class TestDynamoFile(unittest.TestCase):
|
||||
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"))
|
||||
|
||||
def test_get_related_python_files(self):
|
||||
cleanup_output_dir()
|
||||
|
||||
opt = dyn2py.Options(python_folder=OUTPUT_DIR)
|
||||
dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
|
||||
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||
for dyn in [dyn1, dyn2]:
|
||||
dyn.extract_python(options=opt)
|
||||
|
||||
python_files1 = dyn1.get_related_python_files(options=opt)
|
||||
python_files2 = dyn2.get_related_python_files(options=opt)
|
||||
|
||||
self.assertEqual(len(python_files1), 6)
|
||||
self.assertEqual(len(python_files2), 1)
|
||||
|
||||
no_python_files = dyn1.get_related_python_files()
|
||||
|
||||
self.assertFalse(no_python_files)
|
||||
|
||||
def test_write_same(self):
|
||||
cleanup_output_dir()
|
||||
|
||||
shutil.copy(f"{INPUT_DIR}/python_nodes.dyn",
|
||||
f"{OUTPUT_DIR}/python_nodes.dyn")
|
||||
|
||||
new_dyn = dyn2py.DynamoFile(f"{OUTPUT_DIR}/python_nodes.dyn")
|
||||
new_dyn.modified = True
|
||||
new_dyn.write()
|
||||
|
||||
with open(f"{INPUT_DIR}/python_nodes.dyn", "r", encoding="utf-8") as file1,\
|
||||
open(f"{OUTPUT_DIR}/python_nodes.dyn", "r", encoding="utf-8") as file2:
|
||||
json1 = json.load(file1, use_decimal=True)
|
||||
json2 = json.load(file2, use_decimal=True)
|
||||
|
||||
self.assertEqual(json1, json2)
|
||||
|
||||
def test_update_and_write(self):
|
||||
cleanup_output_dir()
|
||||
|
||||
extract_single_node_dyn(modify_py=True)
|
||||
|
||||
shutil.copy(f"{INPUT_DIR}/single_node.dyn",
|
||||
f"{OUTPUT_DIR}/single_node.dyn")
|
||||
|
||||
py = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
|
||||
node = dyn2py.PythonNode(python_file=py)
|
||||
dyn = dyn2py.DynamoFile(f"{OUTPUT_DIR}/single_node.dyn")
|
||||
dyn.update_python_node(node)
|
||||
|
||||
self.assertTrue(dyn.modified)
|
||||
self.assertTrue(node in dyn.python_nodes)
|
||||
|
||||
# Save the file:
|
||||
dyn.write()
|
||||
dyn2py.DynamoFile.open_files.clear()
|
||||
|
||||
shutil.copy(f"{OUTPUT_DIR}/single_node.dyn",
|
||||
f"{OUTPUT_DIR}/single_node2.dyn")
|
||||
|
||||
dyn2 = dyn2py.DynamoFile(f"{OUTPUT_DIR}/single_node2.dyn")
|
||||
node2 = dyn2.get_python_node_by_id(node_id=node.id)
|
||||
self.assertTrue(node2)
|
||||
self.assertEqual(node.checksum, node2.checksum)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import unittest
|
||||
import dyn2py
|
||||
import hashlib
|
||||
|
||||
from tests.support import *
|
||||
|
||||
|
||||
class TestPythonNode(unittest.TestCase):
|
||||
|
||||
# Test needed:
|
||||
# init exception
|
||||
|
||||
def test_init_from_dyn(self):
|
||||
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||
node_dict = next((n for n in dyn.full_dict["Nodes"]
|
||||
@@ -18,8 +21,7 @@ class TestPythonNode(unittest.TestCase):
|
||||
|
||||
node = dyn2py.PythonNode(
|
||||
node_dict_from_dyn=node_dict,
|
||||
full_nodeviews_dict_from_dyn=node_views,
|
||||
source_dynamo_file=dyn
|
||||
dynamo_file=dyn
|
||||
)
|
||||
|
||||
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
|
||||
@@ -31,26 +33,12 @@ class TestPythonNode(unittest.TestCase):
|
||||
|
||||
def test_init_from_py(self):
|
||||
|
||||
extract_single_node_dyn()
|
||||
|
||||
# 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)
|
||||
extract_single_node_dyn(modify_py=True)
|
||||
|
||||
py = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
|
||||
|
||||
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()
|
||||
)
|
||||
|
||||
node = dyn2py.PythonNode(python_file=py)
|
||||
|
||||
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
|
||||
self.assertEqual(node.engine, "CPython3")
|
||||
self.assertEqual(node.checksum, "8d9091d24788a6fdfa5e1e109298b50e")
|
||||
|
||||
Reference in New Issue
Block a user