mirror of
https://github.com/infeeeee/dyn2py
synced 2025-12-16 22:16:18 +01:00
More tests
This commit is contained in:
6
TODO.md
6
TODO.md
@@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
- [ ] Commandline
|
- [ ] Commandline
|
||||||
- [x] File
|
- [x] File
|
||||||
- [ ] DynamoFile
|
- [x] DynamoFile
|
||||||
- [ ] PythonFile
|
- [x] PythonFile
|
||||||
- [ ] PythonNode
|
- [x] PythonNode
|
||||||
- [ ] run()
|
- [ ] run()
|
||||||
|
|
||||||
## CI/CD
|
## CI/CD
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import hashlib
|
|||||||
import pathlib
|
import pathlib
|
||||||
import textwrap
|
import textwrap
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from pathvalidate import sanitize_filename
|
from pathvalidate import sanitize_filename
|
||||||
@@ -362,7 +363,7 @@ class PythonFile(File):
|
|||||||
header_data: dict
|
header_data: dict
|
||||||
"""Parsed dict from the header of a python file."""
|
"""Parsed dict from the header of a python file."""
|
||||||
text: str
|
text: str
|
||||||
"""Full contents of the file before writing."""
|
"""Full contents of the file."""
|
||||||
|
|
||||||
open_files: set["PythonFile"] = set()
|
open_files: set["PythonFile"] = set()
|
||||||
"""A set of open Python files."""
|
"""A set of open Python files."""
|
||||||
@@ -421,6 +422,7 @@ class PythonFile(File):
|
|||||||
python_node.code
|
python_node.code
|
||||||
])
|
])
|
||||||
|
|
||||||
|
self.code = python_node.code
|
||||||
self.modified = True
|
self.modified = True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -449,6 +451,7 @@ class PythonFile(File):
|
|||||||
with open(self.filepath, mode="r", newline="", encoding="utf-8") as input_py:
|
with open(self.filepath, mode="r", newline="", encoding="utf-8") as input_py:
|
||||||
python_lines = input_py.readlines()
|
python_lines = input_py.readlines()
|
||||||
|
|
||||||
|
self.text = os.linesep.join(python_lines)
|
||||||
self.header_data = {}
|
self.header_data = {}
|
||||||
header_separator_count = 0
|
header_separator_count = 0
|
||||||
code_start_line = 0
|
code_start_line = 0
|
||||||
@@ -490,12 +493,6 @@ class PythonFile(File):
|
|||||||
if not options:
|
if not options:
|
||||||
options = Options()
|
options = Options()
|
||||||
|
|
||||||
# Check if it was already opened:
|
|
||||||
dynamo_file = DynamoFile.get_open_file_by_uuid(
|
|
||||||
self.header_data["dyn_uuid"])
|
|
||||||
|
|
||||||
# Open if it's the first time:
|
|
||||||
if not dynamo_file:
|
|
||||||
dynamo_file = self.get_source_dynamo_file()
|
dynamo_file = self.get_source_dynamo_file()
|
||||||
|
|
||||||
new_python_node = PythonNode(python_file=self)
|
new_python_node = PythonNode(python_file=self)
|
||||||
@@ -519,19 +516,22 @@ class PythonFile(File):
|
|||||||
"""Get the source Dynamo file of this PythonFile
|
"""Get the source Dynamo file of this PythonFile
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FileNotFoundError: The dynamo file not found
|
|
||||||
DynamoFileException: The uuid of the dynamo file changed
|
DynamoFileException: The uuid of the dynamo file changed
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
DynamoFile: The DynamoFile
|
DynamoFile: The DynamoFile
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Check if it was already opened:
|
||||||
|
dynamo_file = DynamoFile.get_open_file_by_uuid(
|
||||||
|
self.header_data["dyn_uuid"])
|
||||||
|
|
||||||
|
# Open if it's the first time:
|
||||||
|
if not dynamo_file:
|
||||||
|
|
||||||
dynamo_file = DynamoFile(
|
dynamo_file = DynamoFile(
|
||||||
pathlib.Path(self.header_data["dyn_path"]))
|
pathlib.Path(self.header_data["dyn_path"]))
|
||||||
|
|
||||||
if not dynamo_file.exists:
|
|
||||||
raise FileNotFoundError(
|
|
||||||
f"Dynamo graph not found: {dynamo_file.filepath}")
|
|
||||||
|
|
||||||
# Check if uuid is ok:
|
# Check if uuid is ok:
|
||||||
if not dynamo_file.uuid == self.header_data["dyn_uuid"]:
|
if not dynamo_file.uuid == self.header_data["dyn_uuid"]:
|
||||||
raise DynamoFileException(f"Dynamo graph uuid changed!")
|
raise DynamoFileException(f"Dynamo graph uuid changed!")
|
||||||
|
|||||||
@@ -3,15 +3,13 @@ import dyn2py
|
|||||||
import pathlib
|
import pathlib
|
||||||
import shutil
|
import shutil
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
from dyn2py.files import DynamoFile
|
||||||
|
|
||||||
from tests.support import *
|
from tests.support import *
|
||||||
|
|
||||||
|
|
||||||
class TestDynamoFile(unittest.TestCase):
|
class TestDynamoFile(unittest.TestCase):
|
||||||
|
|
||||||
# Missing methods:
|
|
||||||
# update_python_node: exception
|
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
dyn2py.DynamoFile.open_files.clear()
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
|
|
||||||
@@ -19,7 +17,7 @@ class TestDynamoFile(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(dyn.uuid, "3c3b4c05-9716-4e93-9360-ca0637cb5486")
|
self.assertEqual(dyn.uuid, "3c3b4c05-9716-4e93-9360-ca0637cb5486")
|
||||||
self.assertEqual(dyn.name, "python_nodes")
|
self.assertEqual(dyn.name, "python_nodes")
|
||||||
self.assertTrue(dyn in dyn.open_files)
|
self.assertTrue(dyn in DynamoFile.open_files)
|
||||||
|
|
||||||
# Dynamo 1 file:
|
# Dynamo 1 file:
|
||||||
with self.assertRaises(dyn2py.DynamoFileException):
|
with self.assertRaises(dyn2py.DynamoFileException):
|
||||||
@@ -39,7 +37,7 @@ class TestDynamoFile(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(len(dyn.python_nodes), 6)
|
self.assertEqual(len(dyn.python_nodes), 6)
|
||||||
self.assertTrue(py_node)
|
self.assertTrue(py_node)
|
||||||
self.assertTrue(py_node in dyn.python_nodes)
|
self.assertIn(py_node, dyn.python_nodes)
|
||||||
self.assertEqual(py_node.checksum, "1f3d9e6153804fe1ed37571a9cda8e26")
|
self.assertEqual(py_node.checksum, "1f3d9e6153804fe1ed37571a9cda8e26")
|
||||||
|
|
||||||
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
|
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
|
||||||
@@ -103,29 +101,36 @@ class TestDynamoFile(unittest.TestCase):
|
|||||||
self.assertEqual(json1, json2)
|
self.assertEqual(json1, json2)
|
||||||
|
|
||||||
def test_update_and_write(self):
|
def test_update_and_write(self):
|
||||||
cleanup_output_dir()
|
|
||||||
|
|
||||||
extract_single_node_dyn(modify_py=True)
|
extract_single_node_dyn(modify_py=True)
|
||||||
|
|
||||||
|
# Create a copy to update it:
|
||||||
shutil.copy(f"{INPUT_DIR}/single_node.dyn",
|
shutil.copy(f"{INPUT_DIR}/single_node.dyn",
|
||||||
f"{OUTPUT_DIR}/single_node.dyn")
|
f"{OUTPUT_DIR}/single_node.dyn")
|
||||||
|
|
||||||
|
# Read back the modified py, and update:
|
||||||
py = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
|
py = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
|
||||||
node = dyn2py.PythonNode(python_file=py)
|
node1 = dyn2py.PythonNode(python_file=py)
|
||||||
dyn = dyn2py.DynamoFile(f"{OUTPUT_DIR}/single_node.dyn")
|
dyn1 = dyn2py.DynamoFile(f"{OUTPUT_DIR}/single_node.dyn")
|
||||||
dyn.update_python_node(node)
|
dyn1.update_python_node(node1)
|
||||||
|
|
||||||
self.assertTrue(dyn.modified)
|
self.assertTrue(dyn1.modified)
|
||||||
self.assertTrue(node in dyn.python_nodes)
|
self.assertIn(node1, dyn1.python_nodes)
|
||||||
|
|
||||||
# Save the file:
|
# Save the file:
|
||||||
dyn.write()
|
dyn1.write()
|
||||||
dyn2py.DynamoFile.open_files.clear()
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
|
|
||||||
shutil.copy(f"{OUTPUT_DIR}/single_node.dyn",
|
shutil.copy(f"{OUTPUT_DIR}/single_node.dyn",
|
||||||
f"{OUTPUT_DIR}/single_node2.dyn")
|
f"{OUTPUT_DIR}/single_node2.dyn")
|
||||||
|
|
||||||
|
# Check if the node in the copy:
|
||||||
dyn2 = dyn2py.DynamoFile(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)
|
node2 = dyn2.get_python_node_by_id(node_id=node1.id)
|
||||||
|
|
||||||
self.assertTrue(node2)
|
self.assertTrue(node2)
|
||||||
self.assertEqual(node.checksum, node2.checksum)
|
self.assertEqual(node1.checksum, node2.checksum)
|
||||||
|
|
||||||
|
with self.assertRaises(dyn2py.PythonNodeNotFoundException):
|
||||||
|
node2.id = "wrong_id"
|
||||||
|
dyn2.update_python_node(node2)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class TestFile(unittest.TestCase):
|
|||||||
self.assertEqual(the_file.extension, ".dyn")
|
self.assertEqual(the_file.extension, ".dyn")
|
||||||
self.assertFalse(the_file.modified)
|
self.assertFalse(the_file.modified)
|
||||||
|
|
||||||
self.assertEqual(the_file.__class__, dyn2py.DynamoFile)
|
self.assertIs(type(the_file), dyn2py.DynamoFile)
|
||||||
|
|
||||||
def test_init_newfile(self):
|
def test_init_newfile(self):
|
||||||
paths = [
|
paths = [
|
||||||
|
|||||||
@@ -1,19 +1,106 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
import dyn2py
|
||||||
|
import shutil
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
from tests.support import *
|
from tests.support import *
|
||||||
|
|
||||||
|
|
||||||
class TestPythonFile(unittest.TestCase):
|
class TestPythonFile(unittest.TestCase):
|
||||||
pass
|
|
||||||
|
|
||||||
# Missing variables:
|
def test_init(self):
|
||||||
# code
|
extract_single_node_dyn()
|
||||||
# header_data
|
|
||||||
# text
|
|
||||||
# open_files
|
|
||||||
|
|
||||||
# Missing methods:
|
py1 = dyn2py.PythonFile(
|
||||||
# generate_text()
|
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||||
# update_dynamo()
|
|
||||||
# get_source_dynamo_file()
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
# read()
|
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||||
# write()
|
node = list(dyn.python_nodes)[0]
|
||||||
|
py2 = dyn2py.PythonFile(filepath=node.filepath,
|
||||||
|
dynamo_file=dyn, python_node=node)
|
||||||
|
|
||||||
|
for py in [py1, py2]:
|
||||||
|
|
||||||
|
self.assertEqual(len(py.code.split("\n")), 17)
|
||||||
|
self.assertEqual(len(py.text.split("\r\n")), 31)
|
||||||
|
self.assertIs(type(py.header_data), dict)
|
||||||
|
self.assertTrue(py in dyn2py.PythonFile.open_files)
|
||||||
|
|
||||||
|
def test_update_dynamo(self):
|
||||||
|
extract_single_node_dyn(modify_py=True)
|
||||||
|
|
||||||
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
|
dyn2py.PythonFile.open_files.clear()
|
||||||
|
|
||||||
|
py1 = dyn2py.PythonFile(
|
||||||
|
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||||
|
|
||||||
|
dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||||
|
|
||||||
|
# This shouldn't modify, as this file was not updated:
|
||||||
|
py1.update_dynamo()
|
||||||
|
self.assertFalse(dyn1.modified)
|
||||||
|
|
||||||
|
# The modified files should update:
|
||||||
|
py2 = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
|
||||||
|
py2.update_dynamo()
|
||||||
|
self.assertTrue(dyn1.modified)
|
||||||
|
|
||||||
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
|
|
||||||
|
# Make sure that the dyn file is newer:
|
||||||
|
sleep(1)
|
||||||
|
shutil.copy(f"{INPUT_DIR}/single_node.dyn",
|
||||||
|
f"{OUTPUT_DIR}/single_node.dyn")
|
||||||
|
dyn2 = dyn2py.DynamoFile(f"{OUTPUT_DIR}/single_node.dyn")
|
||||||
|
py2.header_data["dyn_path"] = f"{OUTPUT_DIR}/single_node.dyn"
|
||||||
|
self.assertFalse(py2.is_newer(dyn2))
|
||||||
|
|
||||||
|
# This shouldn't modify, as it is newer:
|
||||||
|
py2.update_dynamo()
|
||||||
|
self.assertFalse(dyn2.modified)
|
||||||
|
|
||||||
|
# It should work with force:
|
||||||
|
opt = dyn2py.Options(force=True)
|
||||||
|
py2.update_dynamo(options=opt)
|
||||||
|
self.assertTrue(dyn2.modified)
|
||||||
|
|
||||||
|
def test_get_source_dynamo_file(self):
|
||||||
|
extract_single_node_dyn()
|
||||||
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
|
dyn2py.PythonFile.open_files.clear()
|
||||||
|
|
||||||
|
py1 = dyn2py.PythonFile(
|
||||||
|
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||||
|
|
||||||
|
dyn1 = py1.get_source_dynamo_file()
|
||||||
|
self.assertEqual(len(dyn2py.DynamoFile.open_files), 1)
|
||||||
|
self.assertIn(dyn1, dyn2py.DynamoFile.open_files)
|
||||||
|
|
||||||
|
dyn2 = py1.get_source_dynamo_file()
|
||||||
|
self.assertIs(dyn1, dyn2)
|
||||||
|
|
||||||
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
|
|
||||||
|
with self.assertRaises(dyn2py.DynamoFileException):
|
||||||
|
py1.header_data["dyn_uuid"] = "wrong-uuid"
|
||||||
|
py1.get_source_dynamo_file()
|
||||||
|
|
||||||
|
def test_write(self):
|
||||||
|
extract_single_node_dyn()
|
||||||
|
dyn2py.DynamoFile.open_files.clear()
|
||||||
|
dyn2py.PythonFile.open_files.clear()
|
||||||
|
|
||||||
|
py1 = dyn2py.PythonFile(
|
||||||
|
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")
|
||||||
|
|
||||||
|
dyn1 = py1.get_source_dynamo_file()
|
||||||
|
node = list(dyn1.python_nodes)[0]
|
||||||
|
py2 = dyn2py.PythonFile(
|
||||||
|
node.filepath, dynamo_file=dyn1, python_node=node)
|
||||||
|
self.assertIsNot(py1, py2)
|
||||||
|
self.assertEqual(py1.code, py2.code)
|
||||||
|
for d in py1.header_data:
|
||||||
|
if not d == "dyn2py_extracted":
|
||||||
|
self.assertEqual(py1.header_data[d], py2.header_data[d])
|
||||||
|
|||||||
@@ -6,9 +6,6 @@ from tests.support import *
|
|||||||
|
|
||||||
class TestPythonNode(unittest.TestCase):
|
class TestPythonNode(unittest.TestCase):
|
||||||
|
|
||||||
# Test needed:
|
|
||||||
# init exception
|
|
||||||
|
|
||||||
def test_init_from_dyn(self):
|
def test_init_from_dyn(self):
|
||||||
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||||
node_dict = next((n for n in dyn.full_dict["Nodes"]
|
node_dict = next((n for n in dyn.full_dict["Nodes"]
|
||||||
@@ -17,8 +14,6 @@ class TestPythonNode(unittest.TestCase):
|
|||||||
# Found a node:
|
# Found a node:
|
||||||
self.assertTrue(node_dict)
|
self.assertTrue(node_dict)
|
||||||
|
|
||||||
node_views = dyn.full_dict["View"]["NodeViews"]
|
|
||||||
|
|
||||||
node = dyn2py.PythonNode(
|
node = dyn2py.PythonNode(
|
||||||
node_dict_from_dyn=node_dict,
|
node_dict_from_dyn=node_dict,
|
||||||
dynamo_file=dyn
|
dynamo_file=dyn
|
||||||
@@ -42,3 +37,22 @@ class TestPythonNode(unittest.TestCase):
|
|||||||
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
|
self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0")
|
||||||
self.assertEqual(node.engine, "CPython3")
|
self.assertEqual(node.engine, "CPython3")
|
||||||
self.assertEqual(node.checksum, "8d9091d24788a6fdfa5e1e109298b50e")
|
self.assertEqual(node.checksum, "8d9091d24788a6fdfa5e1e109298b50e")
|
||||||
|
|
||||||
|
def test_init_exception(self):
|
||||||
|
|
||||||
|
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
|
||||||
|
node_dict = next((n for n in dyn.full_dict["Nodes"]
|
||||||
|
if n["NodeType"] == "PythonScriptNode"), {})
|
||||||
|
|
||||||
|
extract_single_node_dyn(modify_py=True)
|
||||||
|
|
||||||
|
py = dyn2py.PythonFile(f"{OUTPUT_DIR}/single_node_mod.py")
|
||||||
|
|
||||||
|
with self.assertRaises(dyn2py.PythonNodeException):
|
||||||
|
node1 = dyn2py.PythonNode(
|
||||||
|
node_dict_from_dyn=node_dict,
|
||||||
|
dynamo_file=dyn,
|
||||||
|
python_file=py
|
||||||
|
)
|
||||||
|
|
||||||
|
node2 = dyn2py.PythonNode()
|
||||||
|
|||||||
Reference in New Issue
Block a user