New tests, small changes on a lot of things

This commit is contained in:
2023-03-16 02:31:28 +01:00
parent d6fa3d25db
commit 9a7eb92eb4
10 changed files with 350 additions and 118 deletions

View File

@@ -28,7 +28,11 @@ def extract_single_node_dyn(modify_py: bool = False):
# Extract py:
options = dyn2py.Options(python_folder=OUTPUT_DIR)
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
dyn.extract_python(options)
pythonfiles = dyn.extract_python(options)
pythonfiles[0].write()
dyn2py.PythonFile.open_files.clear()
dyn2py.DynamoFile.open_files.clear()
if modify_py:
# Open the extracted file and replace a string:
@@ -38,3 +42,6 @@ def extract_single_node_dyn(modify_py: bool = False):
if "asd_string" in line:
line = line.replace("asd_string", "qwe_string")
mod_py.write(line)

View File

@@ -1,15 +1,172 @@
import unittest
import subprocess
import shutil
import pathlib
from tests.support import *
class TestCommandLine(unittest.TestCase):
def test_help(self):
args = ["-h", "--help"]
# Sources to test: normal dry_run force backup filter update python_folder
# - single dyn file ✅ ✅ ✅ ✅ ✅
# - multiple dyn files
# - single python file
# - multiple python files
# - single folder
# - multiple folders
# Options to test:
# dry run
# force
# backup
# filter
# update
# python folder
def test_help_and_version(self):
args = ["-h", "--help", "-v", "--version"]
for arg in args:
p = subprocess.run(f"dyn2py {arg}",
capture_output=True, shell=True)
# No error:
self.assertFalse(p.stderr)
dyn_sources = [
{"filename": "python_nodes.dyn", "py_file_count": 6},
{"filename": "single_node.dyn", "py_file_count": 1}
]
dyn_sources_error = ["dynamo1file.dyn",
"no_python.dyn",
"nonexisting.dyn"]
@staticmethod
def run_command(args: list = []) -> dict:
argstring = " ".join(args)
process = subprocess.run(f"dyn2py -l WARNING {argstring}",
capture_output=True, shell=True)
stderr = process.stderr.decode()
python_files = {p: p.stat().st_mtime for p in pathlib.Path(OUTPUT_DIR).iterdir()
if p.suffix == ".py"}
output = {
"stderr": stderr,
"python_file_mtimes": python_files
}
return output
def test_dyn_error(self):
for s in self.dyn_sources_error:
cleanup_output_dir()
if pathlib.Path(f"{INPUT_DIR}/{s}").exists():
shutil.copy(f"{INPUT_DIR}/{s}",
f"{OUTPUT_DIR}/{s}")
file_open = self.run_command(
[f"{OUTPUT_DIR}/{s}"])
self.assertTrue(bool(file_open["stderr"]))
self.assertEqual(
len(file_open["python_file_mtimes"]), 0)
def test_single_dyn(self):
dyn_tests = []
for source_dict in self.dyn_sources:
for backup_arg in ["-b", "--backup"]:
for force_arg in ["-F", "--force"]:
for pfolder_option in ["", "-p", "--python-folder"]:
test_dict = source_dict.copy()
if pfolder_option:
test_dict["filepath"] = f"{INPUT_DIR}/{source_dict['filename']}"
pfolder_arg = f"{pfolder_option} {OUTPUT_DIR}"
else:
test_dict["filepath"] = f"{OUTPUT_DIR}/{source_dict['filename']}"
pfolder_arg = ""
test_dict.update({
"pfolder_arg": pfolder_arg,
"backup_arg": backup_arg,
"force_arg": force_arg
})
dyn_tests.append(test_dict)
for s in dyn_tests:
cleanup_output_dir()
if not s["pfolder_arg"]:
shutil.copy(f"{INPUT_DIR}/{s['filename']}",
f"{OUTPUT_DIR}/{s['filename']}")
file_open = self.run_command(
[s["pfolder_arg"], s['filepath']])
self.assertFalse(bool(file_open["stderr"]))
self.assertEqual(
len(file_open["python_file_mtimes"]), s["py_file_count"])
# Test no overwrite
file_no_overwrite = self.run_command(
[s["pfolder_arg"], s['filepath']])
self.assertTrue(bool(file_no_overwrite["stderr"]))
self.assertEqual(
len(file_no_overwrite["python_file_mtimes"]), s["py_file_count"])
for p in file_no_overwrite["python_file_mtimes"]:
self.assertEqual(
file_no_overwrite["python_file_mtimes"][p], file_open["python_file_mtimes"][p])
# Test force:
file_force = self.run_command(
[s["pfolder_arg"], s["force_arg"], s['filepath']])
self.assertFalse(bool(file_force["stderr"]))
self.assertEqual(
len(file_force["python_file_mtimes"]), s["py_file_count"])
for p in file_force["python_file_mtimes"]:
self.assertTrue(
file_force["python_file_mtimes"][p] > file_open["python_file_mtimes"][p]
)
# Test backup
file_backup = self.run_command(
[s["pfolder_arg"], s["force_arg"], s["backup_arg"], s['filepath']])
self.assertFalse(bool(file_backup["stderr"]))
self.assertEqual(
len(file_backup["python_file_mtimes"]), s["py_file_count"] * 2)
for p in file_force["python_file_mtimes"]:
self.assertTrue(
file_backup["python_file_mtimes"][p] > file_force["python_file_mtimes"][p]
)
def test_single_dyn_dryrun(self):
for s in self.dyn_sources:
for arg in ["-n", "--dry-run"]:
cleanup_output_dir()
shutil.copy(f"{INPUT_DIR}/{s['filename']}",
f"{OUTPUT_DIR}/{s['filename']}")
file_dryrun = self.run_command(
[arg, f"{OUTPUT_DIR}/{s['filename']}"])
self.assertFalse(bool(file_dryrun["stderr"]))
self.assertFalse(file_dryrun["python_file_mtimes"])

View File

@@ -3,7 +3,6 @@ import dyn2py
import pathlib
import shutil
import simplejson as json
from dyn2py.files import DynamoFile
from tests.support import *
@@ -17,7 +16,7 @@ class TestDynamoFile(unittest.TestCase):
self.assertEqual(dyn.uuid, "3c3b4c05-9716-4e93-9360-ca0637cb5486")
self.assertEqual(dyn.name, "python_nodes")
self.assertTrue(dyn in DynamoFile.open_files)
self.assertTrue(dyn in dyn2py.DynamoFile.open_files)
# Dynamo 1 file:
with self.assertRaises(dyn2py.DynamoFileException):
@@ -45,11 +44,17 @@ class TestDynamoFile(unittest.TestCase):
def test_extract_python(self):
cleanup_output_dir()
dyn2py.PythonFile.open_files.clear()
opt = dyn2py.Options(python_folder=OUTPUT_DIR)
dyn = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn")
dyn.extract_python(options=opt)
self.assertEqual(len(dyn2py.PythonFile.open_files), 6)
for f in dyn2py.PythonFile.open_files:
f.write()
output_dir = pathlib.Path(OUTPUT_DIR)
self.assertEqual(len(list(output_dir.iterdir())), 6)
@@ -72,6 +77,9 @@ class TestDynamoFile(unittest.TestCase):
dyn2 = dyn2py.DynamoFile(f"{INPUT_DIR}/single_node.dyn")
for dyn in [dyn1, dyn2]:
dyn.extract_python(options=opt)
for f in dyn2py.PythonFile.open_files:
f.write()
dyn2py.PythonFile.open_files.clear()
python_files1 = dyn1.get_related_python_files(options=opt)
python_files2 = dyn2.get_related_python_files(options=opt)

View File

@@ -74,6 +74,8 @@ class TestFile(unittest.TestCase):
cleanup_output_dir()
opt = dyn2py.Options(python_folder=OUTPUT_DIR)
older_file.extract_python(options=opt) # type: ignore
for f in dyn2py.PythonFile.open_files:
f.write()
newer_file = dyn2py.File(
f"{OUTPUT_DIR}/single_node_1c5d99792882409e97e132b3e9f814b0.py")