Fixed unit tests

This commit is contained in:
2025-07-13 18:11:17 +02:00
parent f3deeaefd1
commit ed793995fb
3 changed files with 39 additions and 55 deletions

View File

@@ -66,64 +66,48 @@ class WorkflowPlayer(BaseComponent):
self.global_error = "No connections defined."
return
self._init_state()
self._init_state(ComponentState.NOT_RUN)
components_by_id = {c.id: c for c in self._designer.get_workflow_components()}
try:
sorted_components = self._get_sorted_components()
engine = self._get_engine(sorted_components)
except ValueError as e:
# Handle workflow structure errors (e.g., cycles)
self.has_error = True
self.global_error = f"Workflow configuration error: {e}"
self._datagrid.init_from_dataframe(pd.DataFrame([]))
return
try:
engine = self._get_engine()
except WorkflowsPlayerError as ex:
self.has_error = True
self.global_error = f"Failed to init component '{ex.component_id}': {ex.error}"
if ex.component_id in self.runtime_states:
self.runtime_states[ex.component_id].state = ComponentState.FAILURE
self.runtime_states[ex.component_id].error_message = str(ex.error)
self.global_error = f"Failed to init component '{ex.component_id}': {ex.error}"
return
res = engine.run_to_list()
if engine.has_error:
if engine.has_error and not engine.errors:
self.has_error = True
if not engine.errors:
self.global_error = engine.global_error
else:
# Determine component states by simulating a "stop-on-fail" execution
first_failure_found = False
for component in sorted_components:
runtime_state = self.runtime_states.get(component.id)
if not runtime_state:
continue
if first_failure_found:
# After a failure, all subsequent components are marked as NOT_RUN
runtime_state.state = ComponentState.NOT_RUN
continue
if component.id in engine.errors:
# This is the first component that failed
first_failure_found = True
error = engine.errors[component.id]
runtime_state.state = ComponentState.FAILURE
runtime_state.error_message = str(error)
# As requested, display the component error in the global error area
component_props = components_by_id[component.id].properties
component_name = component_props.get("processor_name", f"ID: {component.id}")
self.global_error = f"Error in component '{component_name}': {str(error)}"
else:
# This component ran successfully
runtime_state.state = ComponentState.SUCCESS
else:
self.has_error = False
self.global_error = engine.global_error
else: # loop through the components and update the runtime states
for component in sorted_components:
runtime_state = self.runtime_states.get(component.id)
if component.id not in engine.errors:
runtime_state.state = ComponentState.SUCCESS
continue
# the component failed
error = engine.errors[component.id]
runtime_state.state = ComponentState.FAILURE
runtime_state.error_message = str(error)
self.global_error = f"Error in component '{error.component_id}': {error.error}" # update global error as well
self.has_error = True
break # the remaining components will remain as NOT_RUN
data = [row.as_dict() for row in res]
df = pd.DataFrame(data)
@@ -194,9 +178,8 @@ class WorkflowPlayer(BaseComponent):
# Return sorted components
return [components_by_id[cid] for cid in sorted_order]
def _get_engine(self):
def _get_engine(self, sorted_components):
# first reorder the component, according to the connection definitions
sorted_components = self._get_sorted_components()
engine = WorkflowEngine()
for component in sorted_components:
try:
@@ -218,10 +201,10 @@ class WorkflowPlayer(BaseComponent):
return engine
def _init_state(self):
def _init_state(self, state: ComponentState = ComponentState.SUCCESS):
self.global_error = None
self.has_error = False
self.runtime_states = {component.id: WorkflowComponentRuntimeState(component.id)
self.runtime_states = {component.id: WorkflowComponentRuntimeState(component.id, state)
for component in self._designer.get_workflow_components()}
@staticmethod

View File

@@ -43,7 +43,7 @@ class WorkflowComponentRuntimeState:
Represents the runtime state of a single workflow component.
"""
id: str
state: ComponentState = ComponentState.NOT_RUN
state: ComponentState = ComponentState.SUCCESS
error_message: str | None = None