1- import unittest
1+ import json
22import os
3+ import sys
4+ import unittest
35from pyiron_workflow import Workflow , to_function_node
46from python_workflow_definition .pyiron_workflow import load_workflow_json , write_workflow_json
57
@@ -16,6 +18,25 @@ def get_square(x):
1618 return x ** 2
1719"""
1820
21+ echo_function_str = """
22+ def echo(filename):
23+ return filename
24+ """
25+
26+ filename_workflow_str = """
27+ {
28+ "version": "0.1.0",
29+ "nodes": [
30+ {"id": 0, "type": "function", "value": "echo_module.echo"},
31+ {"id": 1, "type": "input", "value": "image.png", "name": "filename"},
32+ {"id": 2, "type": "output", "name": "result"}
33+ ],
34+ "edges": [
35+ {"target": 0, "targetPort": "filename", "source": 1, "sourcePort": null},
36+ {"target": 2, "targetPort": null, "source": 0, "sourcePort": null}
37+ ]
38+ }"""
39+
1940
2041class TestPyironWorkflow (unittest .TestCase ):
2142 def test_pyiron_workflow (self ):
@@ -41,3 +62,58 @@ def test_pyiron_workflow(self):
4162 wf .run ()
4263
4364 self .assertTrue (os .path .exists (workflow_json_filename ))
65+
66+ def test_pyiron_workflow_filename_input (self ):
67+ """A filename string like 'image.png' must be passed through as a plain
68+ string input, not interpreted as a Python module path or a float."""
69+ workflow_json_filename = "pyiron_workflow_filename.json"
70+ with open ("echo_module.py" , "w" ) as f :
71+ f .write (echo_function_str )
72+ sys .modules .pop ("echo_module" , None )
73+
74+ with open (workflow_json_filename , "w" ) as f :
75+ f .write (filename_workflow_str )
76+
77+ with open (workflow_json_filename ) as f :
78+ saved = json .load (f )
79+ input_values = [
80+ n ["value" ]
81+ for n in saved ["nodes" ]
82+ if n ["type" ] == "input"
83+ ]
84+ self .assertIn ("image.png" , input_values )
85+
86+ wf = load_workflow_json (file_name = workflow_json_filename )
87+ wf .run ()
88+ self .assertTrue (os .path .exists (workflow_json_filename ))
89+
90+ def test_pyiron_workflow_filename_input_programmatic (self ):
91+ """Write and round-trip a workflow with a filename input using the
92+ programmatic write_workflow_json / load_workflow_json path."""
93+ workflow_json_filename = "pyiron_workflow_filename_prog.json"
94+ with open ("echo_module.py" , "w" ) as f :
95+ f .write (echo_function_str )
96+ sys .modules .pop ("echo_module" , None )
97+
98+ from echo_module import echo as _echo
99+
100+ echo_node = to_function_node ("echo" , _echo , "echo" )
101+ wf = Workflow ("filename_workflow" )
102+ wf .filename = "image.png"
103+ wf .result = echo_node (filename = wf .filename )
104+ write_workflow_json (graph_as_dict = wf .graph_as_dict , file_name = workflow_json_filename )
105+ self .assertTrue (os .path .exists (workflow_json_filename ))
106+
107+ with open (workflow_json_filename ) as f :
108+ saved = json .load (f )
109+ input_values = [
110+ n ["value" ]
111+ for n in saved ["nodes" ]
112+ if n ["type" ] == "input"
113+ ]
114+ self .assertIn ("image.png" , input_values )
115+
116+ sys .modules .pop ("echo_module" , None )
117+ wf2 = load_workflow_json (file_name = workflow_json_filename )
118+ wf2 .run ()
119+ self .assertTrue (os .path .exists (workflow_json_filename ))
0 commit comments